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>
50 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_debug.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_message.h>
64 #include <cam/scsi/scsi_pass.h>
67 /* Datastructures internal to the xpt layer */
68 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
71 * Definition of an async handler callback block. These are used to add
72 * SIMs and peripherals to the async callback lists.
75 SLIST_ENTRY(async_node) links;
76 u_int32_t event_enable; /* Async Event enables */
77 void (*callback)(void *arg, u_int32_t code,
78 struct cam_path *path, void *args);
82 SLIST_HEAD(async_list, async_node);
83 SLIST_HEAD(periph_list, cam_periph);
84 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
87 * This is the maximum number of high powered commands (e.g. start unit)
88 * that can be outstanding at a particular time.
90 #ifndef CAM_MAX_HIGHPOWER
91 #define CAM_MAX_HIGHPOWER 4
94 /* number of high powered commands that can go through right now */
95 static int num_highpower = CAM_MAX_HIGHPOWER;
98 * Structure for queueing a device in a run queue.
99 * There is one run queue for allocating new ccbs,
100 * and another for sending ccbs to the controller.
102 struct cam_ed_qinfo {
104 struct cam_ed *device;
108 * The CAM EDT (Existing Device Table) contains the device information for
109 * all devices for all busses in the system. The table contains a
110 * cam_ed structure for each device on the bus.
113 TAILQ_ENTRY(cam_ed) links;
114 struct cam_ed_qinfo alloc_ccb_entry;
115 struct cam_ed_qinfo send_ccb_entry;
116 struct cam_et *target;
119 * Queue of type drivers wanting to do
120 * work on this device.
122 struct cam_ccbq ccbq; /* Queue of pending ccbs */
123 struct async_list asyncs; /* Async callback info for this B/T/L */
124 struct periph_list periphs; /* All attached devices */
125 u_int generation; /* Generation number */
126 struct cam_periph *owner; /* Peripheral driver's ownership tag */
127 struct xpt_quirk_entry *quirk; /* Oddities about this device */
128 /* Storage for the inquiry data */
129 #ifdef CAM_NEW_TRAN_CODE
131 u_int protocol_version;
133 u_int transport_version;
134 #endif /* CAM_NEW_TRAN_CODE */
135 struct scsi_inquiry_data inq_data;
136 u_int8_t inq_flags; /*
137 * Current settings for inquiry flags.
138 * This allows us to override settings
139 * like disconnection and tagged
140 * queuing for a device.
142 u_int8_t queue_flags; /* Queue flags from the control page */
143 u_int8_t serial_num_len;
144 u_int8_t *serial_num;
145 u_int32_t qfrozen_cnt;
147 #define CAM_DEV_UNCONFIGURED 0x01
148 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
149 #define CAM_DEV_REL_ON_COMPLETE 0x04
150 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
151 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
152 #define CAM_DEV_TAG_AFTER_COUNT 0x20
153 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
154 u_int32_t tag_delay_count;
155 #define CAM_TAG_DELAY_COUNT 5
156 u_int32_t tag_saved_openings;
158 struct callout_handle c_handle;
162 * Each target is represented by an ET (Existing Target). These
163 * entries are created when a target is successfully probed with an
164 * identify, and removed when a device fails to respond after a number
165 * of retries, or a bus rescan finds the device missing.
168 TAILQ_HEAD(, cam_ed) ed_entries;
169 TAILQ_ENTRY(cam_et) links;
171 target_id_t target_id;
174 struct timeval last_reset;
178 * Each bus is represented by an EB (Existing Bus). These entries
179 * are created by calls to xpt_bus_register and deleted by calls to
180 * xpt_bus_deregister.
183 TAILQ_HEAD(, cam_et) et_entries;
184 TAILQ_ENTRY(cam_eb) links;
187 struct timeval last_reset;
189 #define CAM_EB_RUNQ_SCHEDULED 0x01
195 struct cam_periph *periph;
197 struct cam_et *target;
198 struct cam_ed *device;
201 struct xpt_quirk_entry {
202 struct scsi_inquiry_pattern inq_pat;
204 #define CAM_QUIRK_NOLUNS 0x01
205 #define CAM_QUIRK_NOSERIAL 0x02
206 #define CAM_QUIRK_HILUNS 0x04
207 #define CAM_QUIRK_NOHILUNS 0x08
211 #define CAM_SCSI2_MAXLUN 8
213 * If we're not quirked to search <= the first 8 luns
214 * and we are either quirked to search above lun 8,
215 * or we're > SCSI-2, we can look for luns above lun 8.
217 #define CAN_SRCH_HI(dv) \
218 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
219 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
220 || SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2))
228 u_int32_t generation;
231 static const char quantum[] = "QUANTUM";
232 static const char sony[] = "SONY";
233 static const char west_digital[] = "WDIGTL";
234 static const char samsung[] = "SAMSUNG";
235 static const char seagate[] = "SEAGATE";
236 static const char microp[] = "MICROP";
238 static struct xpt_quirk_entry xpt_quirk_table[] =
241 /* Reports QUEUE FULL for temporary resource shortages */
242 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
243 /*quirks*/0, /*mintags*/24, /*maxtags*/32
246 /* Reports QUEUE FULL for temporary resource shortages */
247 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
248 /*quirks*/0, /*mintags*/24, /*maxtags*/32
251 /* Reports QUEUE FULL for temporary resource shortages */
252 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
253 /*quirks*/0, /*mintags*/24, /*maxtags*/32
256 /* Broken tagged queuing drive */
257 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
258 /*quirks*/0, /*mintags*/0, /*maxtags*/0
261 /* Broken tagged queuing drive */
262 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
263 /*quirks*/0, /*mintags*/0, /*maxtags*/0
266 /* Broken tagged queuing drive */
267 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
268 /*quirks*/0, /*mintags*/0, /*maxtags*/0
272 * Unfortunately, the Quantum Atlas III has the same
273 * problem as the Atlas II drives above.
274 * Reported by: "Johan Granlund" <johan@granlund.nu>
276 * For future reference, the drive with the problem was:
277 * QUANTUM QM39100TD-SW N1B0
279 * It's possible that Quantum will fix the problem in later
280 * firmware revisions. If that happens, the quirk entry
281 * will need to be made specific to the firmware revisions
285 /* Reports QUEUE FULL for temporary resource shortages */
286 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
287 /*quirks*/0, /*mintags*/24, /*maxtags*/32
291 * 18 Gig Atlas III, same problem as the 9G version.
292 * Reported by: Andre Albsmeier
293 * <andre.albsmeier@mchp.siemens.de>
295 * For future reference, the drive with the problem was:
296 * QUANTUM QM318000TD-S N491
298 /* Reports QUEUE FULL for temporary resource shortages */
299 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
300 /*quirks*/0, /*mintags*/24, /*maxtags*/32
304 * Broken tagged queuing drive
305 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
306 * and: Martin Renters <martin@tdc.on.ca>
308 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
309 /*quirks*/0, /*mintags*/0, /*maxtags*/0
312 * The Seagate Medalist Pro drives have very poor write
313 * performance with anything more than 2 tags.
315 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
316 * Drive: <SEAGATE ST36530N 1444>
318 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
319 * Drive: <SEAGATE ST34520W 1281>
321 * No one has actually reported that the 9G version
322 * (ST39140*) of the Medalist Pro has the same problem, but
323 * we're assuming that it does because the 4G and 6.5G
324 * versions of the drive are broken.
327 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
328 /*quirks*/0, /*mintags*/2, /*maxtags*/2
331 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
332 /*quirks*/0, /*mintags*/2, /*maxtags*/2
335 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
336 /*quirks*/0, /*mintags*/2, /*maxtags*/2
340 * Slow when tagged queueing is enabled. Write performance
341 * steadily drops off with more and more concurrent
342 * transactions. Best sequential write performance with
343 * tagged queueing turned off and write caching turned on.
346 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
347 * Drive: DCAS-34330 w/ "S65A" firmware.
349 * The drive with the problem had the "S65A" firmware
350 * revision, and has also been reported (by Stephen J.
351 * Roznowski <sjr@home.net>) for a drive with the "S61A"
354 * Although no one has reported problems with the 2 gig
355 * version of the DCAS drive, the assumption is that it
356 * has the same problems as the 4 gig version. Therefore
357 * this quirk entries disables tagged queueing for all
360 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
361 /*quirks*/0, /*mintags*/0, /*maxtags*/0
364 /* Broken tagged queuing drive */
365 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
366 /*quirks*/0, /*mintags*/0, /*maxtags*/0
369 /* Broken tagged queuing drive */
370 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
371 /*quirks*/0, /*mintags*/0, /*maxtags*/0
375 * Broken tagged queuing drive.
377 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
380 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
381 /*quirks*/0, /*mintags*/0, /*maxtags*/0
385 * Slow when tagged queueing is enabled. (1.5MB/sec versus
387 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
388 * Best performance with these drives is achieved with
389 * tagged queueing turned off, and write caching turned on.
391 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
392 /*quirks*/0, /*mintags*/0, /*maxtags*/0
396 * Slow when tagged queueing is enabled. (1.5MB/sec versus
398 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
399 * Best performance with these drives is achieved with
400 * tagged queueing turned off, and write caching turned on.
402 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
403 /*quirks*/0, /*mintags*/0, /*maxtags*/0
407 * Doesn't handle queue full condition correctly,
408 * so we need to limit maxtags to what the device
409 * can handle instead of determining this automatically.
411 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
412 /*quirks*/0, /*mintags*/2, /*maxtags*/32
415 /* Really only one LUN */
416 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
417 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
420 /* I can't believe we need a quirk for DPT volumes. */
421 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
422 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
423 /*mintags*/0, /*maxtags*/255
427 * Many Sony CDROM drives don't like multi-LUN probing.
429 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
430 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
434 * This drive doesn't like multiple LUN probing.
435 * Submitted by: Parag Patel <parag@cgt.com>
437 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
438 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
441 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
442 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
446 * The 8200 doesn't like multi-lun probing, and probably
447 * don't like serial number requests either.
450 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
453 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
457 * Let's try the same as above, but for a drive that says
458 * it's an IPL-6860 but is actually an EXB 8200.
461 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
464 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
468 * These Hitachi drives don't like multi-lun probing.
469 * The PR submitter has a DK319H, but says that the Linux
470 * kernel has a similar work-around for the DK312 and DK314,
471 * so all DK31* drives are quirked here.
473 * Submitted by: Paul Haddad <paul@pth.com>
475 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
476 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
480 * The Hitachi CJ series with J8A8 firmware apparantly has
481 * problems with tagged commands.
483 * Reported by: amagai@nue.org
485 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
486 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
490 * These are the large storage arrays.
491 * Submitted by: William Carrel <william.carrel@infospace.com>
493 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
494 CAM_QUIRK_HILUNS, 2, 1024
498 * This old revision of the TDC3600 is also SCSI-1, and
499 * hangs upon serial number probing.
502 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
505 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
509 * Maxtor Personal Storage 3000XT (Firewire)
510 * hangs upon serial number probing.
513 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
516 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
520 * Would repond to all LUNs if asked for.
523 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
526 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
530 * Would repond to all LUNs if asked for.
533 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
536 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
539 /* Submitted by: Matthew Dodd <winter@jurai.net> */
540 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
541 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
544 /* Submitted by: Matthew Dodd <winter@jurai.net> */
545 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
546 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
549 /* TeraSolutions special settings for TRC-22 RAID */
550 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
551 /*quirks*/0, /*mintags*/55, /*maxtags*/255
554 /* Veritas Storage Appliance */
555 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
556 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
560 * Would respond to all LUNs. Device type and removable
561 * flag are jumper-selectable.
563 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
566 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
569 /* EasyRAID E5A aka. areca ARC-6010 */
570 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
571 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
574 /* Default tagged queuing parameters for all devices */
576 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
577 /*vendor*/"*", /*product*/"*", /*revision*/"*"
579 /*quirks*/0, /*mintags*/2, /*maxtags*/255
583 static const int xpt_quirk_table_size =
584 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
588 DM_RET_FLAG_MASK = 0x0f,
591 DM_RET_DESCEND = 0x20,
593 DM_RET_ACTION_MASK = 0xf0
601 } xpt_traverse_depth;
603 struct xpt_traverse_config {
604 xpt_traverse_depth depth;
609 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
610 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
611 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
612 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
613 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
615 /* Transport layer configuration information */
616 static struct xpt_softc xsoftc;
618 /* Queues for our software interrupt handler */
619 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
620 static cam_isrq_t cam_bioq;
621 static struct mtx cam_bioq_lock;
623 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
624 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
625 static u_int xpt_max_ccbs; /*
626 * Maximum size of ccb pool. Modified as
627 * devices are added/removed or have their
628 * opening counts changed.
630 static u_int xpt_ccb_count; /* Current count of allocated ccbs */
632 struct cam_periph *xpt_periph;
634 static periph_init_t xpt_periph_init;
636 static periph_init_t probe_periph_init;
638 static struct periph_driver xpt_driver =
640 xpt_periph_init, "xpt",
641 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
644 static struct periph_driver probe_driver =
646 probe_periph_init, "probe",
647 TAILQ_HEAD_INITIALIZER(probe_driver.units)
650 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
651 PERIPHDRIVER_DECLARE(probe, probe_driver);
654 static d_open_t xptopen;
655 static d_close_t xptclose;
656 static d_ioctl_t xptioctl;
658 static struct cdevsw xpt_cdevsw = {
659 .d_version = D_VERSION,
660 .d_flags = D_NEEDGIANT,
667 static struct intr_config_hook *xpt_config_hook;
669 /* Registered busses */
670 static TAILQ_HEAD(,cam_eb) xpt_busses;
671 static u_int bus_generation;
673 /* Storage for debugging datastructures */
675 struct cam_path *cam_dpath;
676 u_int32_t cam_dflags;
677 u_int32_t cam_debug_delay;
680 /* Pointers to software interrupt handlers */
681 static void *cambio_ih;
683 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
684 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
688 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
689 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
690 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
692 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
693 || defined(CAM_DEBUG_LUN)
695 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
696 || !defined(CAM_DEBUG_LUN)
697 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
699 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
700 #else /* !CAMDEBUG */
701 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
702 #endif /* CAMDEBUG */
703 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
705 /* Our boot-time initialization hook */
706 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
708 static moduledata_t cam_moduledata = {
710 cam_module_event_handler,
714 static void xpt_init(void *);
716 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
717 MODULE_VERSION(cam, 1);
720 static cam_status xpt_compile_path(struct cam_path *new_path,
721 struct cam_periph *perph,
723 target_id_t target_id,
726 static void xpt_release_path(struct cam_path *path);
728 static void xpt_async_bcast(struct async_list *async_head,
729 u_int32_t async_code,
730 struct cam_path *path,
732 static void xpt_dev_async(u_int32_t async_code,
734 struct cam_et *target,
735 struct cam_ed *device,
737 static path_id_t xptnextfreepathid(void);
738 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
739 static union ccb *xpt_get_ccb(struct cam_ed *device);
740 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
741 u_int32_t new_priority);
742 static void xpt_run_dev_allocq(struct cam_eb *bus);
743 static void xpt_run_dev_sendq(struct cam_eb *bus);
744 static timeout_t xpt_release_devq_timeout;
745 static timeout_t xpt_release_simq_timeout;
746 static void xpt_release_bus(struct cam_eb *bus);
747 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
749 static struct cam_et*
750 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
751 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
752 static struct cam_ed*
753 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
755 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
756 struct cam_ed *device);
757 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
758 static struct cam_eb*
759 xpt_find_bus(path_id_t path_id);
760 static struct cam_et*
761 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
762 static struct cam_ed*
763 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
764 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
765 static void xpt_scan_lun(struct cam_periph *periph,
766 struct cam_path *path, cam_flags flags,
768 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
769 static xpt_busfunc_t xptconfigbuscountfunc;
770 static xpt_busfunc_t xptconfigfunc;
771 static void xpt_config(void *arg);
772 static xpt_devicefunc_t xptpassannouncefunc;
773 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
774 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
775 static void xptpoll(struct cam_sim *sim);
776 static void camisr(void *);
778 static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
779 static void xptasync(struct cam_periph *periph,
780 u_int32_t code, cam_path *path);
782 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
783 u_int num_patterns, struct cam_eb *bus);
784 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
786 struct cam_ed *device);
787 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
789 struct cam_periph *periph);
790 static xpt_busfunc_t xptedtbusfunc;
791 static xpt_targetfunc_t xptedttargetfunc;
792 static xpt_devicefunc_t xptedtdevicefunc;
793 static xpt_periphfunc_t xptedtperiphfunc;
794 static xpt_pdrvfunc_t xptplistpdrvfunc;
795 static xpt_periphfunc_t xptplistperiphfunc;
796 static int xptedtmatch(struct ccb_dev_match *cdm);
797 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
798 static int xptbustraverse(struct cam_eb *start_bus,
799 xpt_busfunc_t *tr_func, void *arg);
800 static int xpttargettraverse(struct cam_eb *bus,
801 struct cam_et *start_target,
802 xpt_targetfunc_t *tr_func, void *arg);
803 static int xptdevicetraverse(struct cam_et *target,
804 struct cam_ed *start_device,
805 xpt_devicefunc_t *tr_func, void *arg);
806 static int xptperiphtraverse(struct cam_ed *device,
807 struct cam_periph *start_periph,
808 xpt_periphfunc_t *tr_func, void *arg);
809 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
810 xpt_pdrvfunc_t *tr_func, void *arg);
811 static int xptpdperiphtraverse(struct periph_driver **pdrv,
812 struct cam_periph *start_periph,
813 xpt_periphfunc_t *tr_func,
815 static xpt_busfunc_t xptdefbusfunc;
816 static xpt_targetfunc_t xptdeftargetfunc;
817 static xpt_devicefunc_t xptdefdevicefunc;
818 static xpt_periphfunc_t xptdefperiphfunc;
819 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
821 static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
824 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
827 static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
830 static xpt_devicefunc_t xptsetasyncfunc;
831 static xpt_busfunc_t xptsetasyncbusfunc;
832 static cam_status xptregister(struct cam_periph *periph,
834 static cam_status proberegister(struct cam_periph *periph,
836 static void probeschedule(struct cam_periph *probe_periph);
837 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
838 static void proberequestdefaultnegotiation(struct cam_periph *periph);
839 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
840 static void probecleanup(struct cam_periph *periph);
841 static void xpt_find_quirk(struct cam_ed *device);
842 #ifdef CAM_NEW_TRAN_CODE
843 static void xpt_devise_transport(struct cam_path *path);
844 #endif /* CAM_NEW_TRAN_CODE */
845 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
846 struct cam_ed *device,
848 static void xpt_toggle_tags(struct cam_path *path);
849 static void xpt_start_tags(struct cam_path *path);
850 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
852 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
854 static __inline int periph_is_queued(struct cam_periph *periph);
855 static __inline int device_is_alloc_queued(struct cam_ed *device);
856 static __inline int device_is_send_queued(struct cam_ed *device);
857 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
860 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
864 if (dev->ccbq.devq_openings > 0) {
865 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
866 cam_ccbq_resize(&dev->ccbq,
867 dev->ccbq.dev_openings
868 + dev->ccbq.dev_active);
869 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
872 * The priority of a device waiting for CCB resources
873 * is that of the the highest priority peripheral driver
876 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
877 &dev->alloc_ccb_entry.pinfo,
878 CAMQ_GET_HEAD(&dev->drvq)->priority);
887 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
891 if (dev->ccbq.dev_openings > 0) {
893 * The priority of a device waiting for controller
894 * resources is that of the the highest priority CCB
898 xpt_schedule_dev(&bus->sim->devq->send_queue,
899 &dev->send_ccb_entry.pinfo,
900 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
908 periph_is_queued(struct cam_periph *periph)
910 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
914 device_is_alloc_queued(struct cam_ed *device)
916 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
920 device_is_send_queued(struct cam_ed *device)
922 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
926 dev_allocq_is_runnable(struct cam_devq *devq)
930 * Have space to do more work.
931 * Allowed to do work.
933 return ((devq->alloc_queue.qfrozen_cnt == 0)
934 && (devq->alloc_queue.entries > 0)
935 && (devq->alloc_openings > 0));
941 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
951 xptdone(struct cam_periph *periph, union ccb *done_ccb)
953 /* Caller will release the CCB */
954 wakeup(&done_ccb->ccb_h.cbfcnp);
958 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
962 unit = minor(dev) & 0xff;
965 * Only allow read-write access.
967 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
971 * We don't allow nonblocking access.
973 if ((flags & O_NONBLOCK) != 0) {
974 printf("xpt%d: can't do nonblocking access\n", unit);
979 * We only have one transport layer right now. If someone accesses
980 * us via something other than minor number 1, point out their
984 printf("xptopen: got invalid xpt unit %d\n", unit);
988 /* Mark ourselves open */
989 xsoftc.flags |= XPT_FLAG_OPEN;
995 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
999 unit = minor(dev) & 0xff;
1002 * We only have one transport layer right now. If someone accesses
1003 * us via something other than minor number 1, point out their
1007 printf("xptclose: got invalid xpt unit %d\n", unit);
1011 /* Mark ourselves closed */
1012 xsoftc.flags &= ~XPT_FLAG_OPEN;
1018 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1023 unit = minor(dev) & 0xff;
1026 * We only have one transport layer right now. If someone accesses
1027 * us via something other than minor number 1, point out their
1031 printf("xptioctl: got invalid xpt unit %d\n", unit);
1037 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1038 * to accept CCB types that don't quite make sense to send through a
1039 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1042 case CAMIOCOMMAND: {
1046 inccb = (union ccb *)addr;
1048 switch(inccb->ccb_h.func_code) {
1051 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1052 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1061 ccb = xpt_alloc_ccb();
1064 * Create a path using the bus, target, and lun the
1067 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1068 inccb->ccb_h.path_id,
1069 inccb->ccb_h.target_id,
1070 inccb->ccb_h.target_lun) !=
1076 /* Ensure all of our fields are correct */
1077 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1078 inccb->ccb_h.pinfo.priority);
1079 xpt_merge_ccb(ccb, inccb);
1080 ccb->ccb_h.cbfcnp = xptdone;
1081 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1082 bcopy(ccb, inccb, sizeof(union ccb));
1083 xpt_free_path(ccb->ccb_h.path);
1091 * This is an immediate CCB, so it's okay to
1092 * allocate it on the stack.
1096 * Create a path using the bus, target, and lun the
1099 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1100 inccb->ccb_h.path_id,
1101 inccb->ccb_h.target_id,
1102 inccb->ccb_h.target_lun) !=
1107 /* Ensure all of our fields are correct */
1108 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1109 inccb->ccb_h.pinfo.priority);
1110 xpt_merge_ccb(&ccb, inccb);
1111 ccb.ccb_h.cbfcnp = xptdone;
1113 bcopy(&ccb, inccb, sizeof(union ccb));
1114 xpt_free_path(ccb.ccb_h.path);
1118 case XPT_DEV_MATCH: {
1119 struct cam_periph_map_info mapinfo;
1120 struct cam_path *old_path;
1123 * We can't deal with physical addresses for this
1124 * type of transaction.
1126 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1132 * Save this in case the caller had it set to
1133 * something in particular.
1135 old_path = inccb->ccb_h.path;
1138 * We really don't need a path for the matching
1139 * code. The path is needed because of the
1140 * debugging statements in xpt_action(). They
1141 * assume that the CCB has a valid path.
1143 inccb->ccb_h.path = xpt_periph->path;
1145 bzero(&mapinfo, sizeof(mapinfo));
1148 * Map the pattern and match buffers into kernel
1149 * virtual address space.
1151 error = cam_periph_mapmem(inccb, &mapinfo);
1154 inccb->ccb_h.path = old_path;
1159 * This is an immediate CCB, we can send it on directly.
1164 * Map the buffers back into user space.
1166 cam_periph_unmapmem(inccb, &mapinfo);
1168 inccb->ccb_h.path = old_path;
1180 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1181 * with the periphal driver name and unit name filled in. The other
1182 * fields don't really matter as input. The passthrough driver name
1183 * ("pass"), and unit number are passed back in the ccb. The current
1184 * device generation number, and the index into the device peripheral
1185 * driver list, and the status are also passed back. Note that
1186 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1187 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1188 * (or rather should be) impossible for the device peripheral driver
1189 * list to change since we look at the whole thing in one pass, and
1190 * we do it with splcam protection.
1193 case CAMGETPASSTHRU: {
1195 struct cam_periph *periph;
1196 struct periph_driver **p_drv;
1199 u_int cur_generation;
1200 int base_periph_found;
1204 ccb = (union ccb *)addr;
1205 unit = ccb->cgdl.unit_number;
1206 name = ccb->cgdl.periph_name;
1208 * Every 100 devices, we want to drop our spl protection to
1209 * give the software interrupt handler a chance to run.
1210 * Most systems won't run into this check, but this should
1211 * avoid starvation in the software interrupt handler in
1216 ccb = (union ccb *)addr;
1218 base_periph_found = 0;
1221 * Sanity check -- make sure we don't get a null peripheral
1224 if (*ccb->cgdl.periph_name == '\0') {
1229 /* Keep the list from changing while we traverse it */
1232 cur_generation = xsoftc.generation;
1234 /* first find our driver in the list of drivers */
1235 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1236 if (strcmp((*p_drv)->driver_name, name) == 0)
1239 if (*p_drv == NULL) {
1241 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1242 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1243 *ccb->cgdl.periph_name = '\0';
1244 ccb->cgdl.unit_number = 0;
1250 * Run through every peripheral instance of this driver
1251 * and check to see whether it matches the unit passed
1252 * in by the user. If it does, get out of the loops and
1253 * find the passthrough driver associated with that
1254 * peripheral driver.
1256 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1257 periph = TAILQ_NEXT(periph, unit_links)) {
1259 if (periph->unit_number == unit) {
1261 } else if (--splbreaknum == 0) {
1265 if (cur_generation != xsoftc.generation)
1270 * If we found the peripheral driver that the user passed
1271 * in, go through all of the peripheral drivers for that
1272 * particular device and look for a passthrough driver.
1274 if (periph != NULL) {
1275 struct cam_ed *device;
1278 base_periph_found = 1;
1279 device = periph->path->device;
1280 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1282 periph = SLIST_NEXT(periph, periph_links), i++) {
1284 * Check to see whether we have a
1285 * passthrough device or not.
1287 if (strcmp(periph->periph_name, "pass") == 0) {
1289 * Fill in the getdevlist fields.
1291 strcpy(ccb->cgdl.periph_name,
1292 periph->periph_name);
1293 ccb->cgdl.unit_number =
1294 periph->unit_number;
1295 if (SLIST_NEXT(periph, periph_links))
1297 CAM_GDEVLIST_MORE_DEVS;
1300 CAM_GDEVLIST_LAST_DEVICE;
1301 ccb->cgdl.generation =
1303 ccb->cgdl.index = i;
1305 * Fill in some CCB header fields
1306 * that the user may want.
1308 ccb->ccb_h.path_id =
1309 periph->path->bus->path_id;
1310 ccb->ccb_h.target_id =
1311 periph->path->target->target_id;
1312 ccb->ccb_h.target_lun =
1313 periph->path->device->lun_id;
1314 ccb->ccb_h.status = CAM_REQ_CMP;
1321 * If the periph is null here, one of two things has
1322 * happened. The first possibility is that we couldn't
1323 * find the unit number of the particular peripheral driver
1324 * that the user is asking about. e.g. the user asks for
1325 * the passthrough driver for "da11". We find the list of
1326 * "da" peripherals all right, but there is no unit 11.
1327 * The other possibility is that we went through the list
1328 * of peripheral drivers attached to the device structure,
1329 * but didn't find one with the name "pass". Either way,
1330 * we return ENOENT, since we couldn't find something.
1332 if (periph == NULL) {
1333 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1334 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1335 *ccb->cgdl.periph_name = '\0';
1336 ccb->cgdl.unit_number = 0;
1339 * It is unfortunate that this is even necessary,
1340 * but there are many, many clueless users out there.
1341 * If this is true, the user is looking for the
1342 * passthrough driver, but doesn't have one in his
1345 if (base_periph_found == 1) {
1346 printf("xptioctl: pass driver is not in the "
1348 printf("xptioctl: put \"device pass0\" in "
1349 "your kernel config file\n");
1364 cam_module_event_handler(module_t mod, int what, void *arg)
1366 if (what == MOD_LOAD) {
1368 } else if (what == MOD_UNLOAD) {
1377 /* Functions accessed by the peripheral drivers */
1382 struct cam_sim *xpt_sim;
1383 struct cam_path *path;
1384 struct cam_devq *devq;
1387 TAILQ_INIT(&xpt_busses);
1388 TAILQ_INIT(&cam_bioq);
1389 SLIST_INIT(&ccb_freeq);
1390 STAILQ_INIT(&highpowerq);
1392 mtx_init(&cam_bioq_lock, "CAM BIOQ lock", NULL, MTX_DEF);
1395 * The xpt layer is, itself, the equivelent of a SIM.
1396 * Allow 16 ccbs in the ccb pool for it. This should
1397 * give decent parallelism when we probe busses and
1398 * perform other XPT functions.
1400 devq = cam_simq_alloc(16);
1401 xpt_sim = cam_sim_alloc(xptaction,
1406 /*max_dev_transactions*/0,
1407 /*max_tagged_dev_transactions*/0,
1411 xpt_bus_register(xpt_sim, /*bus #*/0);
1414 * Looking at the XPT from the SIM layer, the XPT is
1415 * the equivelent of a peripheral driver. Allocate
1416 * a peripheral driver entry for us.
1418 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1419 CAM_TARGET_WILDCARD,
1420 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1421 printf("xpt_init: xpt_create_path failed with status %#x,"
1422 " failing attach\n", status);
1426 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1427 path, NULL, 0, NULL);
1428 xpt_free_path(path);
1430 xpt_sim->softc = xpt_periph;
1433 * Register a callback for when interrupts are enabled.
1436 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1437 M_TEMP, M_NOWAIT | M_ZERO);
1438 if (xpt_config_hook == NULL) {
1439 printf("xpt_init: Cannot malloc config hook "
1440 "- failing attach\n");
1444 xpt_config_hook->ich_func = xpt_config;
1445 if (config_intrhook_establish(xpt_config_hook) != 0) {
1446 free (xpt_config_hook, M_TEMP);
1447 printf("xpt_init: config_intrhook_establish failed "
1448 "- failing attach\n");
1451 /* Install our software interrupt handlers */
1452 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1456 xptregister(struct cam_periph *periph, void *arg)
1458 if (periph == NULL) {
1459 printf("xptregister: periph was NULL!!\n");
1460 return(CAM_REQ_CMP_ERR);
1463 periph->softc = NULL;
1465 xpt_periph = periph;
1467 return(CAM_REQ_CMP);
1471 xpt_add_periph(struct cam_periph *periph)
1473 struct cam_ed *device;
1475 struct periph_list *periph_head;
1479 device = periph->path->device;
1481 periph_head = &device->periphs;
1483 status = CAM_REQ_CMP;
1485 if (device != NULL) {
1489 * Make room for this peripheral
1490 * so it will fit in the queue
1491 * when it's scheduled to run
1494 status = camq_resize(&device->drvq,
1495 device->drvq.array_size + 1);
1497 device->generation++;
1499 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1504 xsoftc.generation++;
1510 xpt_remove_periph(struct cam_periph *periph)
1512 struct cam_ed *device;
1516 device = periph->path->device;
1518 if (device != NULL) {
1520 struct periph_list *periph_head;
1522 periph_head = &device->periphs;
1524 /* Release the slot for this peripheral */
1526 camq_resize(&device->drvq, device->drvq.array_size - 1);
1528 device->generation++;
1530 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1535 xsoftc.generation++;
1539 #ifdef CAM_NEW_TRAN_CODE
1542 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1544 struct ccb_pathinq cpi;
1545 struct ccb_trans_settings cts;
1546 struct cam_path *path;
1554 path = periph->path;
1556 * To ensure that this is printed in one piece,
1557 * mask out CAM interrupts.
1560 printf("%s%d at %s%d bus %d target %d lun %d\n",
1561 periph->periph_name, periph->unit_number,
1562 path->bus->sim->sim_name,
1563 path->bus->sim->unit_number,
1564 path->bus->sim->bus_id,
1565 path->target->target_id,
1566 path->device->lun_id);
1567 printf("%s%d: ", periph->periph_name, periph->unit_number);
1568 scsi_print_inquiry(&path->device->inq_data);
1569 if (bootverbose && path->device->serial_num_len > 0) {
1570 /* Don't wrap the screen - print only the first 60 chars */
1571 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1572 periph->unit_number, path->device->serial_num);
1574 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1575 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1576 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1577 xpt_action((union ccb*)&cts);
1579 /* Ask the SIM for its base transfer speed */
1580 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1581 cpi.ccb_h.func_code = XPT_PATH_INQ;
1582 xpt_action((union ccb *)&cpi);
1584 speed = cpi.base_transfer_speed;
1586 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1587 struct ccb_trans_settings_spi *spi;
1589 spi = &cts.xport_specific.spi;
1590 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1591 && spi->sync_offset != 0) {
1592 freq = scsi_calc_syncsrate(spi->sync_period);
1596 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1597 speed *= (0x01 << spi->bus_width);
1600 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1601 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1602 if (fc->valid & CTS_FC_VALID_SPEED) {
1603 speed = fc->bitrate;
1609 printf("%s%d: %d.%03dMB/s transfers",
1610 periph->periph_name, periph->unit_number,
1613 printf("%s%d: %dKB/s transfers", periph->periph_name,
1614 periph->unit_number, speed);
1615 /* Report additional information about SPI connections */
1616 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1617 struct ccb_trans_settings_spi *spi;
1619 spi = &cts.xport_specific.spi;
1621 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1623 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1627 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1628 && spi->bus_width > 0) {
1634 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1635 } else if (freq != 0) {
1639 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1640 struct ccb_trans_settings_fc *fc;
1642 fc = &cts.xport_specific.fc;
1643 if (fc->valid & CTS_FC_VALID_WWNN)
1644 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1645 if (fc->valid & CTS_FC_VALID_WWPN)
1646 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1647 if (fc->valid & CTS_FC_VALID_PORT)
1648 printf(" PortID 0x%x", fc->port);
1651 if (path->device->inq_flags & SID_CmdQue
1652 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1653 printf("\n%s%d: Tagged Queueing Enabled",
1654 periph->periph_name, periph->unit_number);
1659 * We only want to print the caller's announce string if they've
1662 if (announce_string != NULL)
1663 printf("%s%d: %s\n", periph->periph_name,
1664 periph->unit_number, announce_string);
1667 #else /* CAM_NEW_TRAN_CODE */
1669 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1673 struct cam_path *path;
1674 struct ccb_trans_settings cts;
1678 path = periph->path;
1680 * To ensure that this is printed in one piece,
1681 * mask out CAM interrupts.
1684 printf("%s%d at %s%d bus %d target %d lun %d\n",
1685 periph->periph_name, periph->unit_number,
1686 path->bus->sim->sim_name,
1687 path->bus->sim->unit_number,
1688 path->bus->sim->bus_id,
1689 path->target->target_id,
1690 path->device->lun_id);
1691 printf("%s%d: ", periph->periph_name, periph->unit_number);
1692 scsi_print_inquiry(&path->device->inq_data);
1694 && (path->device->serial_num_len > 0)) {
1695 /* Don't wrap the screen - print only the first 60 chars */
1696 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1697 periph->unit_number, path->device->serial_num);
1699 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1700 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1701 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1702 xpt_action((union ccb*)&cts);
1703 if (cts.ccb_h.status == CAM_REQ_CMP) {
1707 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1708 && cts.sync_offset != 0) {
1709 freq = scsi_calc_syncsrate(cts.sync_period);
1712 struct ccb_pathinq cpi;
1714 /* Ask the SIM for its base transfer speed */
1715 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1716 cpi.ccb_h.func_code = XPT_PATH_INQ;
1717 xpt_action((union ccb *)&cpi);
1719 speed = cpi.base_transfer_speed;
1722 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1723 speed *= (0x01 << cts.bus_width);
1726 printf("%s%d: %d.%03dMB/s transfers",
1727 periph->periph_name, periph->unit_number,
1730 printf("%s%d: %dKB/s transfers", periph->periph_name,
1731 periph->unit_number, speed);
1732 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1733 && cts.sync_offset != 0) {
1734 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1735 freq % 1000, cts.sync_offset);
1737 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1738 && cts.bus_width > 0) {
1739 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1740 && cts.sync_offset != 0) {
1745 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1746 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1747 && cts.sync_offset != 0) {
1751 if (path->device->inq_flags & SID_CmdQue
1752 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1753 printf(", Tagged Queueing Enabled");
1757 } else if (path->device->inq_flags & SID_CmdQue
1758 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1759 printf("%s%d: Tagged Queueing Enabled\n",
1760 periph->periph_name, periph->unit_number);
1764 * We only want to print the caller's announce string if they've
1767 if (announce_string != NULL)
1768 printf("%s%d: %s\n", periph->periph_name,
1769 periph->unit_number, announce_string);
1773 #endif /* CAM_NEW_TRAN_CODE */
1775 static dev_match_ret
1776 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1779 dev_match_ret retval;
1782 retval = DM_RET_NONE;
1785 * If we aren't given something to match against, that's an error.
1788 return(DM_RET_ERROR);
1791 * If there are no match entries, then this bus matches no
1794 if ((patterns == NULL) || (num_patterns == 0))
1795 return(DM_RET_DESCEND | DM_RET_COPY);
1797 for (i = 0; i < num_patterns; i++) {
1798 struct bus_match_pattern *cur_pattern;
1801 * If the pattern in question isn't for a bus node, we
1802 * aren't interested. However, we do indicate to the
1803 * calling routine that we should continue descending the
1804 * tree, since the user wants to match against lower-level
1807 if (patterns[i].type != DEV_MATCH_BUS) {
1808 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1809 retval |= DM_RET_DESCEND;
1813 cur_pattern = &patterns[i].pattern.bus_pattern;
1816 * If they want to match any bus node, we give them any
1819 if (cur_pattern->flags == BUS_MATCH_ANY) {
1820 /* set the copy flag */
1821 retval |= DM_RET_COPY;
1824 * If we've already decided on an action, go ahead
1827 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1832 * Not sure why someone would do this...
1834 if (cur_pattern->flags == BUS_MATCH_NONE)
1837 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1838 && (cur_pattern->path_id != bus->path_id))
1841 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1842 && (cur_pattern->bus_id != bus->sim->bus_id))
1845 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1846 && (cur_pattern->unit_number != bus->sim->unit_number))
1849 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1850 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1855 * If we get to this point, the user definitely wants
1856 * information on this bus. So tell the caller to copy the
1859 retval |= DM_RET_COPY;
1862 * If the return action has been set to descend, then we
1863 * know that we've already seen a non-bus matching
1864 * expression, therefore we need to further descend the tree.
1865 * This won't change by continuing around the loop, so we
1866 * go ahead and return. If we haven't seen a non-bus
1867 * matching expression, we keep going around the loop until
1868 * we exhaust the matching expressions. We'll set the stop
1869 * flag once we fall out of the loop.
1871 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1876 * If the return action hasn't been set to descend yet, that means
1877 * we haven't seen anything other than bus matching patterns. So
1878 * tell the caller to stop descending the tree -- the user doesn't
1879 * want to match against lower level tree elements.
1881 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1882 retval |= DM_RET_STOP;
1887 static dev_match_ret
1888 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1889 struct cam_ed *device)
1891 dev_match_ret retval;
1894 retval = DM_RET_NONE;
1897 * If we aren't given something to match against, that's an error.
1900 return(DM_RET_ERROR);
1903 * If there are no match entries, then this device matches no
1906 if ((patterns == NULL) || (num_patterns == 0))
1907 return(DM_RET_DESCEND | DM_RET_COPY);
1909 for (i = 0; i < num_patterns; i++) {
1910 struct device_match_pattern *cur_pattern;
1913 * If the pattern in question isn't for a device node, we
1914 * aren't interested.
1916 if (patterns[i].type != DEV_MATCH_DEVICE) {
1917 if ((patterns[i].type == DEV_MATCH_PERIPH)
1918 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1919 retval |= DM_RET_DESCEND;
1923 cur_pattern = &patterns[i].pattern.device_pattern;
1926 * If they want to match any device node, we give them any
1929 if (cur_pattern->flags == DEV_MATCH_ANY) {
1930 /* set the copy flag */
1931 retval |= DM_RET_COPY;
1935 * If we've already decided on an action, go ahead
1938 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1943 * Not sure why someone would do this...
1945 if (cur_pattern->flags == DEV_MATCH_NONE)
1948 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1949 && (cur_pattern->path_id != device->target->bus->path_id))
1952 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1953 && (cur_pattern->target_id != device->target->target_id))
1956 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1957 && (cur_pattern->target_lun != device->lun_id))
1960 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1961 && (cam_quirkmatch((caddr_t)&device->inq_data,
1962 (caddr_t)&cur_pattern->inq_pat,
1963 1, sizeof(cur_pattern->inq_pat),
1964 scsi_static_inquiry_match) == NULL))
1968 * If we get to this point, the user definitely wants
1969 * information on this device. So tell the caller to copy
1972 retval |= DM_RET_COPY;
1975 * If the return action has been set to descend, then we
1976 * know that we've already seen a peripheral matching
1977 * expression, therefore we need to further descend the tree.
1978 * This won't change by continuing around the loop, so we
1979 * go ahead and return. If we haven't seen a peripheral
1980 * matching expression, we keep going around the loop until
1981 * we exhaust the matching expressions. We'll set the stop
1982 * flag once we fall out of the loop.
1984 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1989 * If the return action hasn't been set to descend yet, that means
1990 * we haven't seen any peripheral matching patterns. So tell the
1991 * caller to stop descending the tree -- the user doesn't want to
1992 * match against lower level tree elements.
1994 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1995 retval |= DM_RET_STOP;
2001 * Match a single peripheral against any number of match patterns.
2003 static dev_match_ret
2004 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
2005 struct cam_periph *periph)
2007 dev_match_ret retval;
2011 * If we aren't given something to match against, that's an error.
2014 return(DM_RET_ERROR);
2017 * If there are no match entries, then this peripheral matches no
2020 if ((patterns == NULL) || (num_patterns == 0))
2021 return(DM_RET_STOP | DM_RET_COPY);
2024 * There aren't any nodes below a peripheral node, so there's no
2025 * reason to descend the tree any further.
2027 retval = DM_RET_STOP;
2029 for (i = 0; i < num_patterns; i++) {
2030 struct periph_match_pattern *cur_pattern;
2033 * If the pattern in question isn't for a peripheral, we
2034 * aren't interested.
2036 if (patterns[i].type != DEV_MATCH_PERIPH)
2039 cur_pattern = &patterns[i].pattern.periph_pattern;
2042 * If they want to match on anything, then we will do so.
2044 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2045 /* set the copy flag */
2046 retval |= DM_RET_COPY;
2049 * We've already set the return action to stop,
2050 * since there are no nodes below peripherals in
2057 * Not sure why someone would do this...
2059 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2062 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2063 && (cur_pattern->path_id != periph->path->bus->path_id))
2067 * For the target and lun id's, we have to make sure the
2068 * target and lun pointers aren't NULL. The xpt peripheral
2069 * has a wildcard target and device.
2071 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2072 && ((periph->path->target == NULL)
2073 ||(cur_pattern->target_id != periph->path->target->target_id)))
2076 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2077 && ((periph->path->device == NULL)
2078 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2081 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2082 && (cur_pattern->unit_number != periph->unit_number))
2085 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2086 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2091 * If we get to this point, the user definitely wants
2092 * information on this peripheral. So tell the caller to
2093 * copy the data out.
2095 retval |= DM_RET_COPY;
2098 * The return action has already been set to stop, since
2099 * peripherals don't have any nodes below them in the EDT.
2105 * If we get to this point, the peripheral that was passed in
2106 * doesn't match any of the patterns.
2112 xptedtbusfunc(struct cam_eb *bus, void *arg)
2114 struct ccb_dev_match *cdm;
2115 dev_match_ret retval;
2117 cdm = (struct ccb_dev_match *)arg;
2120 * If our position is for something deeper in the tree, that means
2121 * that we've already seen this node. So, we keep going down.
2123 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2124 && (cdm->pos.cookie.bus == bus)
2125 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2126 && (cdm->pos.cookie.target != NULL))
2127 retval = DM_RET_DESCEND;
2129 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2132 * If we got an error, bail out of the search.
2134 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2135 cdm->status = CAM_DEV_MATCH_ERROR;
2140 * If the copy flag is set, copy this bus out.
2142 if (retval & DM_RET_COPY) {
2145 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2146 sizeof(struct dev_match_result));
2149 * If we don't have enough space to put in another
2150 * match result, save our position and tell the
2151 * user there are more devices to check.
2153 if (spaceleft < sizeof(struct dev_match_result)) {
2154 bzero(&cdm->pos, sizeof(cdm->pos));
2155 cdm->pos.position_type =
2156 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2158 cdm->pos.cookie.bus = bus;
2159 cdm->pos.generations[CAM_BUS_GENERATION]=
2161 cdm->status = CAM_DEV_MATCH_MORE;
2164 j = cdm->num_matches;
2166 cdm->matches[j].type = DEV_MATCH_BUS;
2167 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2168 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2169 cdm->matches[j].result.bus_result.unit_number =
2170 bus->sim->unit_number;
2171 strncpy(cdm->matches[j].result.bus_result.dev_name,
2172 bus->sim->sim_name, DEV_IDLEN);
2176 * If the user is only interested in busses, there's no
2177 * reason to descend to the next level in the tree.
2179 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2183 * If there is a target generation recorded, check it to
2184 * make sure the target list hasn't changed.
2186 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2187 && (bus == cdm->pos.cookie.bus)
2188 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2189 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2190 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2192 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2196 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2197 && (cdm->pos.cookie.bus == bus)
2198 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2199 && (cdm->pos.cookie.target != NULL))
2200 return(xpttargettraverse(bus,
2201 (struct cam_et *)cdm->pos.cookie.target,
2202 xptedttargetfunc, arg));
2204 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2208 xptedttargetfunc(struct cam_et *target, void *arg)
2210 struct ccb_dev_match *cdm;
2212 cdm = (struct ccb_dev_match *)arg;
2215 * If there is a device list generation recorded, check it to
2216 * make sure the device list hasn't changed.
2218 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2219 && (cdm->pos.cookie.bus == target->bus)
2220 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2221 && (cdm->pos.cookie.target == target)
2222 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2223 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2224 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2225 target->generation)) {
2226 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2230 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2231 && (cdm->pos.cookie.bus == target->bus)
2232 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2233 && (cdm->pos.cookie.target == target)
2234 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2235 && (cdm->pos.cookie.device != NULL))
2236 return(xptdevicetraverse(target,
2237 (struct cam_ed *)cdm->pos.cookie.device,
2238 xptedtdevicefunc, arg));
2240 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2244 xptedtdevicefunc(struct cam_ed *device, void *arg)
2247 struct ccb_dev_match *cdm;
2248 dev_match_ret retval;
2250 cdm = (struct ccb_dev_match *)arg;
2253 * If our position is for something deeper in the tree, that means
2254 * that we've already seen this node. So, we keep going down.
2256 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2257 && (cdm->pos.cookie.device == device)
2258 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2259 && (cdm->pos.cookie.periph != NULL))
2260 retval = DM_RET_DESCEND;
2262 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2265 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2266 cdm->status = CAM_DEV_MATCH_ERROR;
2271 * If the copy flag is set, copy this device out.
2273 if (retval & DM_RET_COPY) {
2276 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2277 sizeof(struct dev_match_result));
2280 * If we don't have enough space to put in another
2281 * match result, save our position and tell the
2282 * user there are more devices to check.
2284 if (spaceleft < sizeof(struct dev_match_result)) {
2285 bzero(&cdm->pos, sizeof(cdm->pos));
2286 cdm->pos.position_type =
2287 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2288 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2290 cdm->pos.cookie.bus = device->target->bus;
2291 cdm->pos.generations[CAM_BUS_GENERATION]=
2293 cdm->pos.cookie.target = device->target;
2294 cdm->pos.generations[CAM_TARGET_GENERATION] =
2295 device->target->bus->generation;
2296 cdm->pos.cookie.device = device;
2297 cdm->pos.generations[CAM_DEV_GENERATION] =
2298 device->target->generation;
2299 cdm->status = CAM_DEV_MATCH_MORE;
2302 j = cdm->num_matches;
2304 cdm->matches[j].type = DEV_MATCH_DEVICE;
2305 cdm->matches[j].result.device_result.path_id =
2306 device->target->bus->path_id;
2307 cdm->matches[j].result.device_result.target_id =
2308 device->target->target_id;
2309 cdm->matches[j].result.device_result.target_lun =
2311 bcopy(&device->inq_data,
2312 &cdm->matches[j].result.device_result.inq_data,
2313 sizeof(struct scsi_inquiry_data));
2315 /* Let the user know whether this device is unconfigured */
2316 if (device->flags & CAM_DEV_UNCONFIGURED)
2317 cdm->matches[j].result.device_result.flags =
2318 DEV_RESULT_UNCONFIGURED;
2320 cdm->matches[j].result.device_result.flags =
2325 * If the user isn't interested in peripherals, don't descend
2326 * the tree any further.
2328 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2332 * If there is a peripheral list generation recorded, make sure
2333 * it hasn't changed.
2335 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2336 && (device->target->bus == cdm->pos.cookie.bus)
2337 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2338 && (device->target == cdm->pos.cookie.target)
2339 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2340 && (device == cdm->pos.cookie.device)
2341 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2342 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2343 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2344 device->generation)){
2345 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2349 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2350 && (cdm->pos.cookie.bus == device->target->bus)
2351 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2352 && (cdm->pos.cookie.target == device->target)
2353 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2354 && (cdm->pos.cookie.device == device)
2355 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2356 && (cdm->pos.cookie.periph != NULL))
2357 return(xptperiphtraverse(device,
2358 (struct cam_periph *)cdm->pos.cookie.periph,
2359 xptedtperiphfunc, arg));
2361 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2365 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2367 struct ccb_dev_match *cdm;
2368 dev_match_ret retval;
2370 cdm = (struct ccb_dev_match *)arg;
2372 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2374 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2375 cdm->status = CAM_DEV_MATCH_ERROR;
2380 * If the copy flag is set, copy this peripheral out.
2382 if (retval & DM_RET_COPY) {
2385 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2386 sizeof(struct dev_match_result));
2389 * If we don't have enough space to put in another
2390 * match result, save our position and tell the
2391 * user there are more devices to check.
2393 if (spaceleft < sizeof(struct dev_match_result)) {
2394 bzero(&cdm->pos, sizeof(cdm->pos));
2395 cdm->pos.position_type =
2396 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2397 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2400 cdm->pos.cookie.bus = periph->path->bus;
2401 cdm->pos.generations[CAM_BUS_GENERATION]=
2403 cdm->pos.cookie.target = periph->path->target;
2404 cdm->pos.generations[CAM_TARGET_GENERATION] =
2405 periph->path->bus->generation;
2406 cdm->pos.cookie.device = periph->path->device;
2407 cdm->pos.generations[CAM_DEV_GENERATION] =
2408 periph->path->target->generation;
2409 cdm->pos.cookie.periph = periph;
2410 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2411 periph->path->device->generation;
2412 cdm->status = CAM_DEV_MATCH_MORE;
2416 j = cdm->num_matches;
2418 cdm->matches[j].type = DEV_MATCH_PERIPH;
2419 cdm->matches[j].result.periph_result.path_id =
2420 periph->path->bus->path_id;
2421 cdm->matches[j].result.periph_result.target_id =
2422 periph->path->target->target_id;
2423 cdm->matches[j].result.periph_result.target_lun =
2424 periph->path->device->lun_id;
2425 cdm->matches[j].result.periph_result.unit_number =
2426 periph->unit_number;
2427 strncpy(cdm->matches[j].result.periph_result.periph_name,
2428 periph->periph_name, DEV_IDLEN);
2435 xptedtmatch(struct ccb_dev_match *cdm)
2439 cdm->num_matches = 0;
2442 * Check the bus list generation. If it has changed, the user
2443 * needs to reset everything and start over.
2445 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2446 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2447 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2448 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2452 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2453 && (cdm->pos.cookie.bus != NULL))
2454 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2455 xptedtbusfunc, cdm);
2457 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2460 * If we get back 0, that means that we had to stop before fully
2461 * traversing the EDT. It also means that one of the subroutines
2462 * has set the status field to the proper value. If we get back 1,
2463 * we've fully traversed the EDT and copied out any matching entries.
2466 cdm->status = CAM_DEV_MATCH_LAST;
2472 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2474 struct ccb_dev_match *cdm;
2476 cdm = (struct ccb_dev_match *)arg;
2478 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2479 && (cdm->pos.cookie.pdrv == pdrv)
2480 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2481 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2482 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2483 (*pdrv)->generation)) {
2484 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2488 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2489 && (cdm->pos.cookie.pdrv == pdrv)
2490 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2491 && (cdm->pos.cookie.periph != NULL))
2492 return(xptpdperiphtraverse(pdrv,
2493 (struct cam_periph *)cdm->pos.cookie.periph,
2494 xptplistperiphfunc, arg));
2496 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2500 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2502 struct ccb_dev_match *cdm;
2503 dev_match_ret retval;
2505 cdm = (struct ccb_dev_match *)arg;
2507 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2509 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2510 cdm->status = CAM_DEV_MATCH_ERROR;
2515 * If the copy flag is set, copy this peripheral out.
2517 if (retval & DM_RET_COPY) {
2520 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2521 sizeof(struct dev_match_result));
2524 * If we don't have enough space to put in another
2525 * match result, save our position and tell the
2526 * user there are more devices to check.
2528 if (spaceleft < sizeof(struct dev_match_result)) {
2529 struct periph_driver **pdrv;
2532 bzero(&cdm->pos, sizeof(cdm->pos));
2533 cdm->pos.position_type =
2534 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2538 * This may look a bit non-sensical, but it is
2539 * actually quite logical. There are very few
2540 * peripheral drivers, and bloating every peripheral
2541 * structure with a pointer back to its parent
2542 * peripheral driver linker set entry would cost
2543 * more in the long run than doing this quick lookup.
2545 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2546 if (strcmp((*pdrv)->driver_name,
2547 periph->periph_name) == 0)
2551 if (*pdrv == NULL) {
2552 cdm->status = CAM_DEV_MATCH_ERROR;
2556 cdm->pos.cookie.pdrv = pdrv;
2558 * The periph generation slot does double duty, as
2559 * does the periph pointer slot. They are used for
2560 * both edt and pdrv lookups and positioning.
2562 cdm->pos.cookie.periph = periph;
2563 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2564 (*pdrv)->generation;
2565 cdm->status = CAM_DEV_MATCH_MORE;
2569 j = cdm->num_matches;
2571 cdm->matches[j].type = DEV_MATCH_PERIPH;
2572 cdm->matches[j].result.periph_result.path_id =
2573 periph->path->bus->path_id;
2576 * The transport layer peripheral doesn't have a target or
2579 if (periph->path->target)
2580 cdm->matches[j].result.periph_result.target_id =
2581 periph->path->target->target_id;
2583 cdm->matches[j].result.periph_result.target_id = -1;
2585 if (periph->path->device)
2586 cdm->matches[j].result.periph_result.target_lun =
2587 periph->path->device->lun_id;
2589 cdm->matches[j].result.periph_result.target_lun = -1;
2591 cdm->matches[j].result.periph_result.unit_number =
2592 periph->unit_number;
2593 strncpy(cdm->matches[j].result.periph_result.periph_name,
2594 periph->periph_name, DEV_IDLEN);
2601 xptperiphlistmatch(struct ccb_dev_match *cdm)
2605 cdm->num_matches = 0;
2608 * At this point in the edt traversal function, we check the bus
2609 * list generation to make sure that no busses have been added or
2610 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2611 * For the peripheral driver list traversal function, however, we
2612 * don't have to worry about new peripheral driver types coming or
2613 * going; they're in a linker set, and therefore can't change
2614 * without a recompile.
2617 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2618 && (cdm->pos.cookie.pdrv != NULL))
2619 ret = xptpdrvtraverse(
2620 (struct periph_driver **)cdm->pos.cookie.pdrv,
2621 xptplistpdrvfunc, cdm);
2623 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2626 * If we get back 0, that means that we had to stop before fully
2627 * traversing the peripheral driver tree. It also means that one of
2628 * the subroutines has set the status field to the proper value. If
2629 * we get back 1, we've fully traversed the EDT and copied out any
2633 cdm->status = CAM_DEV_MATCH_LAST;
2639 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2641 struct cam_eb *bus, *next_bus;
2646 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2649 next_bus = TAILQ_NEXT(bus, links);
2651 retval = tr_func(bus, arg);
2660 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2661 xpt_targetfunc_t *tr_func, void *arg)
2663 struct cam_et *target, *next_target;
2667 for (target = (start_target ? start_target :
2668 TAILQ_FIRST(&bus->et_entries));
2669 target != NULL; target = next_target) {
2671 next_target = TAILQ_NEXT(target, links);
2673 retval = tr_func(target, arg);
2683 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2684 xpt_devicefunc_t *tr_func, void *arg)
2686 struct cam_ed *device, *next_device;
2690 for (device = (start_device ? start_device :
2691 TAILQ_FIRST(&target->ed_entries));
2693 device = next_device) {
2695 next_device = TAILQ_NEXT(device, links);
2697 retval = tr_func(device, arg);
2707 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2708 xpt_periphfunc_t *tr_func, void *arg)
2710 struct cam_periph *periph, *next_periph;
2715 for (periph = (start_periph ? start_periph :
2716 SLIST_FIRST(&device->periphs));
2718 periph = next_periph) {
2720 next_periph = SLIST_NEXT(periph, periph_links);
2722 retval = tr_func(periph, arg);
2731 xptpdrvtraverse(struct periph_driver **start_pdrv,
2732 xpt_pdrvfunc_t *tr_func, void *arg)
2734 struct periph_driver **pdrv;
2740 * We don't traverse the peripheral driver list like we do the
2741 * other lists, because it is a linker set, and therefore cannot be
2742 * changed during runtime. If the peripheral driver list is ever
2743 * re-done to be something other than a linker set (i.e. it can
2744 * change while the system is running), the list traversal should
2745 * be modified to work like the other traversal functions.
2747 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2748 *pdrv != NULL; pdrv++) {
2749 retval = tr_func(pdrv, arg);
2759 xptpdperiphtraverse(struct periph_driver **pdrv,
2760 struct cam_periph *start_periph,
2761 xpt_periphfunc_t *tr_func, void *arg)
2763 struct cam_periph *periph, *next_periph;
2768 for (periph = (start_periph ? start_periph :
2769 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2770 periph = next_periph) {
2772 next_periph = TAILQ_NEXT(periph, unit_links);
2774 retval = tr_func(periph, arg);
2782 xptdefbusfunc(struct cam_eb *bus, void *arg)
2784 struct xpt_traverse_config *tr_config;
2786 tr_config = (struct xpt_traverse_config *)arg;
2788 if (tr_config->depth == XPT_DEPTH_BUS) {
2789 xpt_busfunc_t *tr_func;
2791 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2793 return(tr_func(bus, tr_config->tr_arg));
2795 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2799 xptdeftargetfunc(struct cam_et *target, void *arg)
2801 struct xpt_traverse_config *tr_config;
2803 tr_config = (struct xpt_traverse_config *)arg;
2805 if (tr_config->depth == XPT_DEPTH_TARGET) {
2806 xpt_targetfunc_t *tr_func;
2808 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2810 return(tr_func(target, tr_config->tr_arg));
2812 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2816 xptdefdevicefunc(struct cam_ed *device, void *arg)
2818 struct xpt_traverse_config *tr_config;
2820 tr_config = (struct xpt_traverse_config *)arg;
2822 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2823 xpt_devicefunc_t *tr_func;
2825 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2827 return(tr_func(device, tr_config->tr_arg));
2829 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2833 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2835 struct xpt_traverse_config *tr_config;
2836 xpt_periphfunc_t *tr_func;
2838 tr_config = (struct xpt_traverse_config *)arg;
2840 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2843 * Unlike the other default functions, we don't check for depth
2844 * here. The peripheral driver level is the last level in the EDT,
2845 * so if we're here, we should execute the function in question.
2847 return(tr_func(periph, tr_config->tr_arg));
2851 * Execute the given function for every bus in the EDT.
2854 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2856 struct xpt_traverse_config tr_config;
2858 tr_config.depth = XPT_DEPTH_BUS;
2859 tr_config.tr_func = tr_func;
2860 tr_config.tr_arg = arg;
2862 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2867 * Execute the given function for every target in the EDT.
2870 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2872 struct xpt_traverse_config tr_config;
2874 tr_config.depth = XPT_DEPTH_TARGET;
2875 tr_config.tr_func = tr_func;
2876 tr_config.tr_arg = arg;
2878 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2880 #endif /* notusedyet */
2883 * Execute the given function for every device in the EDT.
2886 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2888 struct xpt_traverse_config tr_config;
2890 tr_config.depth = XPT_DEPTH_DEVICE;
2891 tr_config.tr_func = tr_func;
2892 tr_config.tr_arg = arg;
2894 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2899 * Execute the given function for every peripheral in the EDT.
2902 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2904 struct xpt_traverse_config tr_config;
2906 tr_config.depth = XPT_DEPTH_PERIPH;
2907 tr_config.tr_func = tr_func;
2908 tr_config.tr_arg = arg;
2910 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2912 #endif /* notusedyet */
2915 xptsetasyncfunc(struct cam_ed *device, void *arg)
2917 struct cam_path path;
2918 struct ccb_getdev cgd;
2919 struct async_node *cur_entry;
2921 cur_entry = (struct async_node *)arg;
2924 * Don't report unconfigured devices (Wildcard devs,
2925 * devices only for target mode, device instances
2926 * that have been invalidated but are waiting for
2927 * their last reference count to be released).
2929 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2932 xpt_compile_path(&path,
2934 device->target->bus->path_id,
2935 device->target->target_id,
2937 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2938 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2939 xpt_action((union ccb *)&cgd);
2940 cur_entry->callback(cur_entry->callback_arg,
2943 xpt_release_path(&path);
2949 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2951 struct cam_path path;
2952 struct ccb_pathinq cpi;
2953 struct async_node *cur_entry;
2955 cur_entry = (struct async_node *)arg;
2957 xpt_compile_path(&path, /*periph*/NULL,
2959 CAM_TARGET_WILDCARD,
2961 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2962 cpi.ccb_h.func_code = XPT_PATH_INQ;
2963 xpt_action((union ccb *)&cpi);
2964 cur_entry->callback(cur_entry->callback_arg,
2967 xpt_release_path(&path);
2973 xpt_action(union ccb *start_ccb)
2979 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2981 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2983 iopl = splsoftcam();
2984 switch (start_ccb->ccb_h.func_code) {
2987 #ifdef CAM_NEW_TRAN_CODE
2988 struct cam_ed *device;
2989 #endif /* CAM_NEW_TRAN_CODE */
2991 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2992 struct cam_path *path;
2994 path = start_ccb->ccb_h.path;
2998 * For the sake of compatibility with SCSI-1
2999 * devices that may not understand the identify
3000 * message, we include lun information in the
3001 * second byte of all commands. SCSI-1 specifies
3002 * that luns are a 3 bit value and reserves only 3
3003 * bits for lun information in the CDB. Later
3004 * revisions of the SCSI spec allow for more than 8
3005 * luns, but have deprecated lun information in the
3006 * CDB. So, if the lun won't fit, we must omit.
3008 * Also be aware that during initial probing for devices,
3009 * the inquiry information is unknown but initialized to 0.
3010 * This means that this code will be exercised while probing
3011 * devices with an ANSI revision greater than 2.
3013 #ifdef CAM_NEW_TRAN_CODE
3014 device = start_ccb->ccb_h.path->device;
3015 if (device->protocol_version <= SCSI_REV_2
3016 #else /* CAM_NEW_TRAN_CODE */
3017 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
3018 #endif /* CAM_NEW_TRAN_CODE */
3019 && start_ccb->ccb_h.target_lun < 8
3020 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
3022 start_ccb->csio.cdb_io.cdb_bytes[1] |=
3023 start_ccb->ccb_h.target_lun << 5;
3025 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
3026 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
3027 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
3028 &path->device->inq_data),
3029 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
3030 cdb_str, sizeof(cdb_str))));
3034 case XPT_CONT_TARGET_IO:
3035 start_ccb->csio.sense_resid = 0;
3036 start_ccb->csio.resid = 0;
3041 struct cam_path *path;
3045 path = start_ccb->ccb_h.path;
3048 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3049 if (path->device->qfrozen_cnt == 0)
3050 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3055 xpt_run_dev_sendq(path->bus);
3058 case XPT_SET_TRAN_SETTINGS:
3060 xpt_set_transfer_settings(&start_ccb->cts,
3061 start_ccb->ccb_h.path->device,
3062 /*async_update*/FALSE);
3065 case XPT_CALC_GEOMETRY:
3067 struct cam_sim *sim;
3069 /* Filter out garbage */
3070 if (start_ccb->ccg.block_size == 0
3071 || start_ccb->ccg.volume_size == 0) {
3072 start_ccb->ccg.cylinders = 0;
3073 start_ccb->ccg.heads = 0;
3074 start_ccb->ccg.secs_per_track = 0;
3075 start_ccb->ccb_h.status = CAM_REQ_CMP;
3080 * In a PC-98 system, geometry translation depens on
3081 * the "real" device geometry obtained from mode page 4.
3082 * SCSI geometry translation is performed in the
3083 * initialization routine of the SCSI BIOS and the result
3084 * stored in host memory. If the translation is available
3085 * in host memory, use it. If not, rely on the default
3086 * translation the device driver performs.
3088 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3089 start_ccb->ccb_h.status = CAM_REQ_CMP;
3093 sim = start_ccb->ccb_h.path->bus->sim;
3094 (*(sim->sim_action))(sim, start_ccb);
3099 union ccb* abort_ccb;
3102 abort_ccb = start_ccb->cab.abort_ccb;
3103 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3105 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3106 struct cam_ccbq *ccbq;
3108 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3109 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3110 abort_ccb->ccb_h.status =
3111 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3112 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3114 xpt_done(abort_ccb);
3116 start_ccb->ccb_h.status = CAM_REQ_CMP;
3119 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3120 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3122 * We've caught this ccb en route to
3123 * the SIM. Flag it for abort and the
3124 * SIM will do so just before starting
3125 * real work on the CCB.
3127 abort_ccb->ccb_h.status =
3128 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3129 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3130 start_ccb->ccb_h.status = CAM_REQ_CMP;
3134 if (XPT_FC_IS_QUEUED(abort_ccb)
3135 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3137 * It's already completed but waiting
3138 * for our SWI to get to it.
3140 start_ccb->ccb_h.status = CAM_UA_ABORT;
3144 * If we weren't able to take care of the abort request
3145 * in the XPT, pass the request down to the SIM for processing.
3149 case XPT_ACCEPT_TARGET_IO:
3151 case XPT_IMMED_NOTIFY:
3152 case XPT_NOTIFY_ACK:
3153 case XPT_GET_TRAN_SETTINGS:
3156 struct cam_sim *sim;
3158 sim = start_ccb->ccb_h.path->bus->sim;
3159 (*(sim->sim_action))(sim, start_ccb);
3164 struct cam_sim *sim;
3166 sim = start_ccb->ccb_h.path->bus->sim;
3167 (*(sim->sim_action))(sim, start_ccb);
3170 case XPT_PATH_STATS:
3171 start_ccb->cpis.last_reset =
3172 start_ccb->ccb_h.path->bus->last_reset;
3173 start_ccb->ccb_h.status = CAM_REQ_CMP;
3180 dev = start_ccb->ccb_h.path->device;
3182 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3183 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3185 struct ccb_getdev *cgd;
3189 cgd = &start_ccb->cgd;
3190 bus = cgd->ccb_h.path->bus;
3191 tar = cgd->ccb_h.path->target;
3192 cgd->inq_data = dev->inq_data;
3193 cgd->ccb_h.status = CAM_REQ_CMP;
3194 cgd->serial_num_len = dev->serial_num_len;
3195 if ((dev->serial_num_len > 0)
3196 && (dev->serial_num != NULL))
3197 bcopy(dev->serial_num, cgd->serial_num,
3198 dev->serial_num_len);
3203 case XPT_GDEV_STATS:
3208 dev = start_ccb->ccb_h.path->device;
3210 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3211 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3213 struct ccb_getdevstats *cgds;
3217 cgds = &start_ccb->cgds;
3218 bus = cgds->ccb_h.path->bus;
3219 tar = cgds->ccb_h.path->target;
3220 cgds->dev_openings = dev->ccbq.dev_openings;
3221 cgds->dev_active = dev->ccbq.dev_active;
3222 cgds->devq_openings = dev->ccbq.devq_openings;
3223 cgds->devq_queued = dev->ccbq.queue.entries;
3224 cgds->held = dev->ccbq.held;
3225 cgds->last_reset = tar->last_reset;
3226 cgds->maxtags = dev->quirk->maxtags;
3227 cgds->mintags = dev->quirk->mintags;
3228 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3229 cgds->last_reset = bus->last_reset;
3230 cgds->ccb_h.status = CAM_REQ_CMP;
3237 struct cam_periph *nperiph;
3238 struct periph_list *periph_head;
3239 struct ccb_getdevlist *cgdl;
3242 struct cam_ed *device;
3249 * Don't want anyone mucking with our data.
3252 device = start_ccb->ccb_h.path->device;
3253 periph_head = &device->periphs;
3254 cgdl = &start_ccb->cgdl;
3257 * Check and see if the list has changed since the user
3258 * last requested a list member. If so, tell them that the
3259 * list has changed, and therefore they need to start over
3260 * from the beginning.
3262 if ((cgdl->index != 0) &&
3263 (cgdl->generation != device->generation)) {
3264 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3270 * Traverse the list of peripherals and attempt to find
3271 * the requested peripheral.
3273 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3274 (nperiph != NULL) && (i <= cgdl->index);
3275 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3276 if (i == cgdl->index) {
3277 strncpy(cgdl->periph_name,
3278 nperiph->periph_name,
3280 cgdl->unit_number = nperiph->unit_number;
3285 cgdl->status = CAM_GDEVLIST_ERROR;
3290 if (nperiph == NULL)
3291 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3293 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3296 cgdl->generation = device->generation;
3299 cgdl->ccb_h.status = CAM_REQ_CMP;
3305 dev_pos_type position_type;
3306 struct ccb_dev_match *cdm;
3308 cdm = &start_ccb->cdm;
3311 * Prevent EDT changes while we traverse it.
3315 * There are two ways of getting at information in the EDT.
3316 * The first way is via the primary EDT tree. It starts
3317 * with a list of busses, then a list of targets on a bus,
3318 * then devices/luns on a target, and then peripherals on a
3319 * device/lun. The "other" way is by the peripheral driver
3320 * lists. The peripheral driver lists are organized by
3321 * peripheral driver. (obviously) So it makes sense to
3322 * use the peripheral driver list if the user is looking
3323 * for something like "da1", or all "da" devices. If the
3324 * user is looking for something on a particular bus/target
3325 * or lun, it's generally better to go through the EDT tree.
3328 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3329 position_type = cdm->pos.position_type;
3333 position_type = CAM_DEV_POS_NONE;
3335 for (i = 0; i < cdm->num_patterns; i++) {
3336 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3337 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3338 position_type = CAM_DEV_POS_EDT;
3343 if (cdm->num_patterns == 0)
3344 position_type = CAM_DEV_POS_EDT;
3345 else if (position_type == CAM_DEV_POS_NONE)
3346 position_type = CAM_DEV_POS_PDRV;
3349 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3350 case CAM_DEV_POS_EDT:
3353 case CAM_DEV_POS_PDRV:
3354 xptperiphlistmatch(cdm);
3357 cdm->status = CAM_DEV_MATCH_ERROR;
3363 if (cdm->status == CAM_DEV_MATCH_ERROR)
3364 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3366 start_ccb->ccb_h.status = CAM_REQ_CMP;
3372 struct ccb_setasync *csa;
3373 struct async_node *cur_entry;
3374 struct async_list *async_head;
3378 csa = &start_ccb->csa;
3379 added = csa->event_enable;
3380 async_head = &csa->ccb_h.path->device->asyncs;
3383 * If there is already an entry for us, simply
3387 cur_entry = SLIST_FIRST(async_head);
3388 while (cur_entry != NULL) {
3389 if ((cur_entry->callback_arg == csa->callback_arg)
3390 && (cur_entry->callback == csa->callback))
3392 cur_entry = SLIST_NEXT(cur_entry, links);
3395 if (cur_entry != NULL) {
3397 * If the request has no flags set,
3400 added &= ~cur_entry->event_enable;
3401 if (csa->event_enable == 0) {
3402 SLIST_REMOVE(async_head, cur_entry,
3404 csa->ccb_h.path->device->refcount--;
3405 free(cur_entry, M_CAMXPT);
3407 cur_entry->event_enable = csa->event_enable;
3410 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3412 if (cur_entry == NULL) {
3414 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3417 cur_entry->event_enable = csa->event_enable;
3418 cur_entry->callback_arg = csa->callback_arg;
3419 cur_entry->callback = csa->callback;
3420 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3421 csa->ccb_h.path->device->refcount++;
3424 if ((added & AC_FOUND_DEVICE) != 0) {
3426 * Get this peripheral up to date with all
3427 * the currently existing devices.
3429 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3431 if ((added & AC_PATH_REGISTERED) != 0) {
3433 * Get this peripheral up to date with all
3434 * the currently existing busses.
3436 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3439 start_ccb->ccb_h.status = CAM_REQ_CMP;
3444 struct ccb_relsim *crs;
3448 crs = &start_ccb->crs;
3449 dev = crs->ccb_h.path->device;
3452 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3458 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3460 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3462 /* Don't ever go below one opening */
3463 if (crs->openings > 0) {
3464 xpt_dev_ccbq_resize(crs->ccb_h.path,
3468 xpt_print_path(crs->ccb_h.path);
3469 printf("tagged openings "
3477 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3479 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3482 * Just extend the old timeout and decrement
3483 * the freeze count so that a single timeout
3484 * is sufficient for releasing the queue.
3486 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3487 untimeout(xpt_release_devq_timeout,
3488 dev, dev->c_handle);
3491 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3495 timeout(xpt_release_devq_timeout,
3497 (crs->release_timeout * hz) / 1000);
3499 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3503 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3505 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3507 * Decrement the freeze count so that a single
3508 * completion is still sufficient to unfreeze
3511 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3514 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3515 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3519 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3521 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3522 || (dev->ccbq.dev_active == 0)) {
3524 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3527 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3528 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3533 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3535 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3538 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3539 start_ccb->ccb_h.status = CAM_REQ_CMP;
3543 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3546 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3547 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3555 #ifdef CAM_DEBUG_DELAY
3556 cam_debug_delay = CAM_DEBUG_DELAY;
3558 cam_dflags = start_ccb->cdbg.flags;
3559 if (cam_dpath != NULL) {
3560 xpt_free_path(cam_dpath);
3564 if (cam_dflags != CAM_DEBUG_NONE) {
3565 if (xpt_create_path(&cam_dpath, xpt_periph,
3566 start_ccb->ccb_h.path_id,
3567 start_ccb->ccb_h.target_id,
3568 start_ccb->ccb_h.target_lun) !=
3570 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3571 cam_dflags = CAM_DEBUG_NONE;
3573 start_ccb->ccb_h.status = CAM_REQ_CMP;
3574 xpt_print_path(cam_dpath);
3575 printf("debugging flags now %x\n", cam_dflags);
3579 start_ccb->ccb_h.status = CAM_REQ_CMP;
3582 #else /* !CAMDEBUG */
3583 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3584 #endif /* CAMDEBUG */
3588 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3589 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3590 start_ccb->ccb_h.status = CAM_REQ_CMP;
3597 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3604 xpt_polled_action(union ccb *start_ccb)
3608 struct cam_sim *sim;
3609 struct cam_devq *devq;
3614 timeout = start_ccb->ccb_h.timeout;
3615 sim = start_ccb->ccb_h.path->bus->sim;
3617 dev = start_ccb->ccb_h.path->device;
3622 * Steal an opening so that no other queued requests
3623 * can get it before us while we simulate interrupts.
3625 dev->ccbq.devq_openings--;
3626 dev->ccbq.dev_openings--;
3628 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3629 && (--timeout > 0)) {
3631 (*(sim->sim_poll))(sim);
3635 dev->ccbq.devq_openings++;
3636 dev->ccbq.dev_openings++;
3639 xpt_action(start_ccb);
3640 while(--timeout > 0) {
3641 (*(sim->sim_poll))(sim);
3643 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3650 * XXX Is it worth adding a sim_timeout entry
3651 * point so we can attempt recovery? If
3652 * this is only used for dumps, I don't think
3655 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3658 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3664 * Schedule a peripheral driver to receive a ccb when it's
3665 * target device has space for more transactions.
3668 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3670 struct cam_ed *device;
3676 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3677 device = perph->path->device;
3679 if (periph_is_queued(perph)) {
3680 /* Simply reorder based on new priority */
3681 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3682 (" change priority to %d\n", new_priority));
3683 if (new_priority < perph->pinfo.priority) {
3684 camq_change_priority(&device->drvq,
3690 /* New entry on the queue */
3691 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3692 (" added periph to queue\n"));
3693 perph->pinfo.priority = new_priority;
3694 perph->pinfo.generation = ++device->drvq.generation;
3695 camq_insert(&device->drvq, &perph->pinfo);
3696 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3700 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3701 (" calling xpt_run_devq\n"));
3702 xpt_run_dev_allocq(perph->path->bus);
3708 * Schedule a device to run on a given queue.
3709 * If the device was inserted as a new entry on the queue,
3710 * return 1 meaning the device queue should be run. If we
3711 * were already queued, implying someone else has already
3712 * started the queue, return 0 so the caller doesn't attempt
3713 * to run the queue. Must be run at either splsoftcam
3714 * (or splcam since that encompases splsoftcam).
3717 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3718 u_int32_t new_priority)
3721 u_int32_t old_priority;
3723 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3725 old_priority = pinfo->priority;
3728 * Are we already queued?
3730 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3731 /* Simply reorder based on new priority */
3732 if (new_priority < old_priority) {
3733 camq_change_priority(queue, pinfo->index,
3735 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3736 ("changed priority to %d\n",
3741 /* New entry on the queue */
3742 if (new_priority < old_priority)
3743 pinfo->priority = new_priority;
3745 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3746 ("Inserting onto queue\n"));
3747 pinfo->generation = ++queue->generation;
3748 camq_insert(queue, pinfo);
3755 xpt_run_dev_allocq(struct cam_eb *bus)
3757 struct cam_devq *devq;
3760 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3761 devq = bus->sim->devq;
3763 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3764 (" qfrozen_cnt == 0x%x, entries == %d, "
3765 "openings == %d, active == %d\n",
3766 devq->alloc_queue.qfrozen_cnt,
3767 devq->alloc_queue.entries,
3768 devq->alloc_openings,
3769 devq->alloc_active));
3772 devq->alloc_queue.qfrozen_cnt++;
3773 while ((devq->alloc_queue.entries > 0)
3774 && (devq->alloc_openings > 0)
3775 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3776 struct cam_ed_qinfo *qinfo;
3777 struct cam_ed *device;
3778 union ccb *work_ccb;
3779 struct cam_periph *drv;
3782 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3784 device = qinfo->device;
3786 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3787 ("running device %p\n", device));
3789 drvq = &device->drvq;
3792 if (drvq->entries <= 0) {
3793 panic("xpt_run_dev_allocq: "
3794 "Device on queue without any work to do");
3797 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3798 devq->alloc_openings--;
3799 devq->alloc_active++;
3800 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3802 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3803 drv->pinfo.priority);
3804 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3805 ("calling periph start\n"));
3806 drv->periph_start(drv, work_ccb);
3809 * Malloc failure in alloc_ccb
3812 * XXX add us to a list to be run from free_ccb
3813 * if we don't have any ccbs active on this
3814 * device queue otherwise we may never get run
3820 /* Raise IPL for possible insertion and test at top of loop */
3823 if (drvq->entries > 0) {
3824 /* We have more work. Attempt to reschedule */
3825 xpt_schedule_dev_allocq(bus, device);
3828 devq->alloc_queue.qfrozen_cnt--;
3833 xpt_run_dev_sendq(struct cam_eb *bus)
3835 struct cam_devq *devq;
3838 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3840 devq = bus->sim->devq;
3843 devq->send_queue.qfrozen_cnt++;
3846 while ((devq->send_queue.entries > 0)
3847 && (devq->send_openings > 0)) {
3848 struct cam_ed_qinfo *qinfo;
3849 struct cam_ed *device;
3850 union ccb *work_ccb;
3851 struct cam_sim *sim;
3855 if (devq->send_queue.qfrozen_cnt > 1) {
3860 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3862 device = qinfo->device;
3865 * If the device has been "frozen", don't attempt
3868 if (device->qfrozen_cnt > 0) {
3873 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3874 ("running device %p\n", device));
3876 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3877 if (work_ccb == NULL) {
3878 printf("device on run queue with no ccbs???\n");
3883 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3885 if (num_highpower <= 0) {
3887 * We got a high power command, but we
3888 * don't have any available slots. Freeze
3889 * the device queue until we have a slot
3892 device->qfrozen_cnt++;
3893 STAILQ_INSERT_TAIL(&highpowerq,
3901 * Consume a high power slot while
3907 devq->active_dev = device;
3908 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3910 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3913 devq->send_openings--;
3914 devq->send_active++;
3916 if (device->ccbq.queue.entries > 0)
3917 xpt_schedule_dev_sendq(bus, device);
3919 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3921 * The client wants to freeze the queue
3922 * after this CCB is sent.
3925 device->qfrozen_cnt++;
3931 /* In Target mode, the peripheral driver knows best... */
3932 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3933 if ((device->inq_flags & SID_CmdQue) != 0
3934 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3935 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3938 * Clear this in case of a retried CCB that
3939 * failed due to a rejected tag.
3941 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3945 * Device queues can be shared among multiple sim instances
3946 * that reside on different busses. Use the SIM in the queue
3947 * CCB's path, rather than the one in the bus that was passed
3948 * into this function.
3950 sim = work_ccb->ccb_h.path->bus->sim;
3951 (*(sim->sim_action))(sim, work_ccb);
3954 devq->active_dev = NULL;
3956 /* Raise IPL for possible insertion and test at top of loop */
3961 devq->send_queue.qfrozen_cnt--;
3966 * This function merges stuff from the slave ccb into the master ccb, while
3967 * keeping important fields in the master ccb constant.
3970 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3975 * Pull fields that are valid for peripheral drivers to set
3976 * into the master CCB along with the CCB "payload".
3978 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3979 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3980 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3981 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3982 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3983 sizeof(union ccb) - sizeof(struct ccb_hdr));
3987 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3991 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3992 ccb_h->pinfo.priority = priority;
3994 ccb_h->path_id = path->bus->path_id;
3996 ccb_h->target_id = path->target->target_id;
3998 ccb_h->target_id = CAM_TARGET_WILDCARD;
4000 ccb_h->target_lun = path->device->lun_id;
4001 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
4003 ccb_h->target_lun = CAM_TARGET_WILDCARD;
4005 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4009 /* Path manipulation functions */
4011 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
4012 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4014 struct cam_path *path;
4019 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
4022 status = CAM_RESRC_UNAVAIL;
4025 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
4026 if (status != CAM_REQ_CMP) {
4027 free(path, M_CAMXPT);
4030 *new_path_ptr = path;
4035 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
4036 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4039 struct cam_et *target;
4040 struct cam_ed *device;
4044 status = CAM_REQ_CMP; /* Completed without error */
4045 target = NULL; /* Wildcarded */
4046 device = NULL; /* Wildcarded */
4049 * We will potentially modify the EDT, so block interrupts
4050 * that may attempt to create cam paths.
4053 bus = xpt_find_bus(path_id);
4055 status = CAM_PATH_INVALID;
4057 target = xpt_find_target(bus, target_id);
4058 if (target == NULL) {
4060 struct cam_et *new_target;
4062 new_target = xpt_alloc_target(bus, target_id);
4063 if (new_target == NULL) {
4064 status = CAM_RESRC_UNAVAIL;
4066 target = new_target;
4069 if (target != NULL) {
4070 device = xpt_find_device(target, lun_id);
4071 if (device == NULL) {
4073 struct cam_ed *new_device;
4075 new_device = xpt_alloc_device(bus,
4078 if (new_device == NULL) {
4079 status = CAM_RESRC_UNAVAIL;
4081 device = new_device;
4089 * Only touch the user's data if we are successful.
4091 if (status == CAM_REQ_CMP) {
4092 new_path->periph = perph;
4093 new_path->bus = bus;
4094 new_path->target = target;
4095 new_path->device = device;
4096 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4099 xpt_release_device(bus, target, device);
4101 xpt_release_target(bus, target);
4103 xpt_release_bus(bus);
4109 xpt_release_path(struct cam_path *path)
4111 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4112 if (path->device != NULL) {
4113 xpt_release_device(path->bus, path->target, path->device);
4114 path->device = NULL;
4116 if (path->target != NULL) {
4117 xpt_release_target(path->bus, path->target);
4118 path->target = NULL;
4120 if (path->bus != NULL) {
4121 xpt_release_bus(path->bus);
4127 xpt_free_path(struct cam_path *path)
4131 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4132 xpt_release_path(path);
4133 free(path, M_CAMXPT);
4138 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4139 * in path1, 2 for match with wildcards in path2.
4142 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4148 if (path1->bus != path2->bus) {
4149 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4151 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4156 if (path1->target != path2->target) {
4157 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4160 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4165 if (path1->device != path2->device) {
4166 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4169 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4178 xpt_print_path(struct cam_path *path)
4183 printf("(nopath): ");
4185 if (path->periph != NULL)
4186 printf("(%s%d:", path->periph->periph_name,
4187 path->periph->unit_number);
4189 printf("(noperiph:");
4191 if (path->bus != NULL)
4192 printf("%s%d:%d:", path->bus->sim->sim_name,
4193 path->bus->sim->unit_number,
4194 path->bus->sim->bus_id);
4198 if (path->target != NULL)
4199 printf("%d:", path->target->target_id);
4203 if (path->device != NULL)
4204 printf("%d): ", path->device->lun_id);
4211 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4217 sbuf_new(&sb, str, str_len, 0);
4220 sbuf_printf(&sb, "(nopath): ");
4222 if (path->periph != NULL)
4223 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4224 path->periph->unit_number);
4226 sbuf_printf(&sb, "(noperiph:");
4228 if (path->bus != NULL)
4229 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4230 path->bus->sim->unit_number,
4231 path->bus->sim->bus_id);
4233 sbuf_printf(&sb, "nobus:");
4235 if (path->target != NULL)
4236 sbuf_printf(&sb, "%d:", path->target->target_id);
4238 sbuf_printf(&sb, "X:");
4240 if (path->device != NULL)
4241 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4243 sbuf_printf(&sb, "X): ");
4247 return(sbuf_len(&sb));
4251 xpt_path_path_id(struct cam_path *path)
4255 return(path->bus->path_id);
4259 xpt_path_target_id(struct cam_path *path)
4263 if (path->target != NULL)
4264 return (path->target->target_id);
4266 return (CAM_TARGET_WILDCARD);
4270 xpt_path_lun_id(struct cam_path *path)
4274 if (path->device != NULL)
4275 return (path->device->lun_id);
4277 return (CAM_LUN_WILDCARD);
4281 xpt_path_sim(struct cam_path *path)
4285 return (path->bus->sim);
4289 xpt_path_periph(struct cam_path *path)
4293 return (path->periph);
4297 * Release a CAM control block for the caller. Remit the cost of the structure
4298 * to the device referenced by the path. If the this device had no 'credits'
4299 * and peripheral drivers have registered async callbacks for this notification
4303 xpt_release_ccb(union ccb *free_ccb)
4306 struct cam_path *path;
4307 struct cam_ed *device;
4312 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4313 path = free_ccb->ccb_h.path;
4314 device = path->device;
4317 cam_ccbq_release_opening(&device->ccbq);
4318 if (xpt_ccb_count > xpt_max_ccbs) {
4319 xpt_free_ccb(free_ccb);
4322 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4324 bus->sim->devq->alloc_openings++;
4325 bus->sim->devq->alloc_active--;
4326 /* XXX Turn this into an inline function - xpt_run_device?? */
4327 if ((device_is_alloc_queued(device) == 0)
4328 && (device->drvq.entries > 0)) {
4329 xpt_schedule_dev_allocq(bus, device);
4332 if (dev_allocq_is_runnable(bus->sim->devq))
4333 xpt_run_dev_allocq(bus);
4336 /* Functions accessed by SIM drivers */
4339 * A sim structure, listing the SIM entry points and instance
4340 * identification info is passed to xpt_bus_register to hook the SIM
4341 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4342 * for this new bus and places it in the array of busses and assigns
4343 * it a path_id. The path_id may be influenced by "hard wiring"
4344 * information specified by the user. Once interrupt services are
4345 * availible, the bus will be probed.
4348 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4350 struct cam_eb *new_bus;
4351 struct cam_eb *old_bus;
4352 struct ccb_pathinq cpi;
4358 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4359 M_CAMXPT, M_NOWAIT);
4360 if (new_bus == NULL) {
4361 /* Couldn't satisfy request */
4362 return (CAM_RESRC_UNAVAIL);
4365 if (strcmp(sim->sim_name, "xpt") != 0) {
4368 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4371 TAILQ_INIT(&new_bus->et_entries);
4372 new_bus->path_id = sim->path_id;
4374 timevalclear(&new_bus->last_reset);
4376 new_bus->refcount = 1; /* Held until a bus_deregister event */
4377 new_bus->generation = 0;
4379 old_bus = TAILQ_FIRST(&xpt_busses);
4380 while (old_bus != NULL
4381 && old_bus->path_id < new_bus->path_id)
4382 old_bus = TAILQ_NEXT(old_bus, links);
4383 if (old_bus != NULL)
4384 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4386 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4390 /* Notify interested parties */
4391 if (sim->path_id != CAM_XPT_PATH_ID) {
4392 struct cam_path path;
4394 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4395 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4396 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4397 cpi.ccb_h.func_code = XPT_PATH_INQ;
4398 xpt_action((union ccb *)&cpi);
4399 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4400 xpt_release_path(&path);
4402 return (CAM_SUCCESS);
4406 xpt_bus_deregister(path_id_t pathid)
4408 struct cam_path bus_path;
4413 status = xpt_compile_path(&bus_path, NULL, pathid,
4414 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4415 if (status != CAM_REQ_CMP)
4418 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4419 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4421 /* Release the reference count held while registered. */
4422 xpt_release_bus(bus_path.bus);
4423 xpt_release_path(&bus_path);
4425 return (CAM_REQ_CMP);
4429 xptnextfreepathid(void)
4436 bus = TAILQ_FIRST(&xpt_busses);
4438 /* Find an unoccupied pathid */
4440 && bus->path_id <= pathid) {
4441 if (bus->path_id == pathid)
4443 bus = TAILQ_NEXT(bus, links);
4447 * Ensure that this pathid is not reserved for
4448 * a bus that may be registered in the future.
4450 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4452 /* Start the search over */
4459 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4466 pathid = CAM_XPT_PATH_ID;
4467 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4469 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4470 if (strcmp(dname, "scbus")) {
4471 /* Avoid a bit of foot shooting. */
4474 if (dunit < 0) /* unwired?! */
4476 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4477 if (sim_bus == val) {
4481 } else if (sim_bus == 0) {
4482 /* Unspecified matches bus 0 */
4486 printf("Ambiguous scbus configuration for %s%d "
4487 "bus %d, cannot wire down. The kernel "
4488 "config entry for scbus%d should "
4489 "specify a controller bus.\n"
4490 "Scbus will be assigned dynamically.\n",
4491 sim_name, sim_unit, sim_bus, dunit);
4496 if (pathid == CAM_XPT_PATH_ID)
4497 pathid = xptnextfreepathid();
4502 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4505 struct cam_et *target, *next_target;
4506 struct cam_ed *device, *next_device;
4511 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4514 * Most async events come from a CAM interrupt context. In
4515 * a few cases, the error recovery code at the peripheral layer,
4516 * which may run from our SWI or a process context, may signal
4517 * deferred events with a call to xpt_async. Ensure async
4518 * notifications are serialized by blocking cam interrupts.
4524 if (async_code == AC_BUS_RESET) {
4528 /* Update our notion of when the last reset occurred */
4529 microtime(&bus->last_reset);
4533 for (target = TAILQ_FIRST(&bus->et_entries);
4535 target = next_target) {
4537 next_target = TAILQ_NEXT(target, links);
4539 if (path->target != target
4540 && path->target->target_id != CAM_TARGET_WILDCARD
4541 && target->target_id != CAM_TARGET_WILDCARD)
4544 if (async_code == AC_SENT_BDR) {
4547 /* Update our notion of when the last reset occurred */
4549 microtime(&path->target->last_reset);
4553 for (device = TAILQ_FIRST(&target->ed_entries);
4555 device = next_device) {
4557 next_device = TAILQ_NEXT(device, links);
4559 if (path->device != device
4560 && path->device->lun_id != CAM_LUN_WILDCARD
4561 && device->lun_id != CAM_LUN_WILDCARD)
4564 xpt_dev_async(async_code, bus, target,
4567 xpt_async_bcast(&device->asyncs, async_code,
4573 * If this wasn't a fully wildcarded async, tell all
4574 * clients that want all async events.
4576 if (bus != xpt_periph->path->bus)
4577 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4583 xpt_async_bcast(struct async_list *async_head,
4584 u_int32_t async_code,
4585 struct cam_path *path, void *async_arg)
4587 struct async_node *cur_entry;
4589 cur_entry = SLIST_FIRST(async_head);
4590 while (cur_entry != NULL) {
4591 struct async_node *next_entry;
4593 * Grab the next list entry before we call the current
4594 * entry's callback. This is because the callback function
4595 * can delete its async callback entry.
4597 next_entry = SLIST_NEXT(cur_entry, links);
4598 if ((cur_entry->event_enable & async_code) != 0)
4599 cur_entry->callback(cur_entry->callback_arg,
4602 cur_entry = next_entry;
4607 * Handle any per-device event notifications that require action by the XPT.
4610 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4611 struct cam_ed *device, void *async_arg)
4614 struct cam_path newpath;
4617 * We only need to handle events for real devices.
4619 if (target->target_id == CAM_TARGET_WILDCARD
4620 || device->lun_id == CAM_LUN_WILDCARD)
4624 * We need our own path with wildcards expanded to
4625 * handle certain types of events.
4627 if ((async_code == AC_SENT_BDR)
4628 || (async_code == AC_BUS_RESET)
4629 || (async_code == AC_INQ_CHANGED))
4630 status = xpt_compile_path(&newpath, NULL,
4635 status = CAM_REQ_CMP_ERR;
4637 if (status == CAM_REQ_CMP) {
4640 * Allow transfer negotiation to occur in a
4641 * tag free environment.
4643 if (async_code == AC_SENT_BDR
4644 || async_code == AC_BUS_RESET)
4645 xpt_toggle_tags(&newpath);
4647 if (async_code == AC_INQ_CHANGED) {
4649 * We've sent a start unit command, or
4650 * something similar to a device that
4651 * may have caused its inquiry data to
4652 * change. So we re-scan the device to
4653 * refresh the inquiry data for it.
4655 xpt_scan_lun(newpath.periph, &newpath,
4656 CAM_EXPECT_INQ_CHANGE, NULL);
4658 xpt_release_path(&newpath);
4659 } else if (async_code == AC_LOST_DEVICE) {
4660 device->flags |= CAM_DEV_UNCONFIGURED;
4661 } else if (async_code == AC_TRANSFER_NEG) {
4662 struct ccb_trans_settings *settings;
4664 settings = (struct ccb_trans_settings *)async_arg;
4665 xpt_set_transfer_settings(settings, device,
4666 /*async_update*/TRUE);
4671 xpt_freeze_devq(struct cam_path *path, u_int count)
4674 struct ccb_hdr *ccbh;
4679 path->device->qfrozen_cnt += count;
4682 * Mark the last CCB in the queue as needing
4683 * to be requeued if the driver hasn't
4684 * changed it's state yet. This fixes a race
4685 * where a ccb is just about to be queued to
4686 * a controller driver when it's interrupt routine
4687 * freezes the queue. To completly close the
4688 * hole, controller drives must check to see
4689 * if a ccb's status is still CAM_REQ_INPROG
4690 * under spl protection just before they queue
4691 * the CCB. See ahc_action/ahc_freeze_devq for
4694 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4695 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4696 ccbh->status = CAM_REQUEUE_REQ;
4698 return (path->device->qfrozen_cnt);
4702 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4706 sim->devq->send_queue.qfrozen_cnt += count;
4707 if (sim->devq->active_dev != NULL) {
4708 struct ccb_hdr *ccbh;
4710 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4712 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4713 ccbh->status = CAM_REQUEUE_REQ;
4715 return (sim->devq->send_queue.qfrozen_cnt);
4719 xpt_release_devq_timeout(void *arg)
4721 struct cam_ed *device;
4723 device = (struct cam_ed *)arg;
4725 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4729 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4733 xpt_release_devq_device(path->device, count, run_queue);
4737 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4745 if (dev->qfrozen_cnt > 0) {
4747 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4748 dev->qfrozen_cnt -= count;
4749 if (dev->qfrozen_cnt == 0) {
4752 * No longer need to wait for a successful
4753 * command completion.
4755 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4758 * Remove any timeouts that might be scheduled
4759 * to release this queue.
4761 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4762 untimeout(xpt_release_devq_timeout, dev,
4764 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4768 * Now that we are unfrozen schedule the
4769 * device so any pending transactions are
4772 if ((dev->ccbq.queue.entries > 0)
4773 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4774 && (run_queue != 0)) {
4781 xpt_run_dev_sendq(dev->target->bus);
4786 xpt_release_simq(struct cam_sim *sim, int run_queue)
4793 sendq = &(sim->devq->send_queue);
4795 if (sendq->qfrozen_cnt > 0) {
4797 sendq->qfrozen_cnt--;
4798 if (sendq->qfrozen_cnt == 0) {
4802 * If there is a timeout scheduled to release this
4803 * sim queue, remove it. The queue frozen count is
4806 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4807 untimeout(xpt_release_simq_timeout, sim,
4809 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4811 bus = xpt_find_bus(sim->path_id);
4816 * Now that we are unfrozen run the send queue.
4818 xpt_run_dev_sendq(bus);
4820 xpt_release_bus(bus);
4828 xpt_release_simq_timeout(void *arg)
4830 struct cam_sim *sim;
4832 sim = (struct cam_sim *)arg;
4833 xpt_release_simq(sim, /* run_queue */ TRUE);
4837 xpt_done(union ccb *done_ccb)
4843 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4844 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4846 * Queue up the request for handling by our SWI handler
4847 * any of the "non-immediate" type of ccbs.
4849 switch (done_ccb->ccb_h.path->periph->type) {
4850 case CAM_PERIPH_BIO:
4851 mtx_lock(&cam_bioq_lock);
4852 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4854 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4855 mtx_unlock(&cam_bioq_lock);
4856 swi_sched(cambio_ih, 0);
4859 panic("unknown periph type %d",
4860 done_ccb->ccb_h.path->periph->type);
4873 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_WAITOK);
4878 xpt_alloc_ccb_nowait()
4884 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_NOWAIT);
4889 xpt_free_ccb(union ccb *free_ccb)
4891 free(free_ccb, M_CAMXPT);
4896 /* Private XPT functions */
4899 * Get a CAM control block for the caller. Charge the structure to the device
4900 * referenced by the path. If the this device has no 'credits' then the
4901 * device already has the maximum number of outstanding operations under way
4902 * and we return NULL. If we don't have sufficient resources to allocate more
4903 * ccbs, we also return NULL.
4906 xpt_get_ccb(struct cam_ed *device)
4912 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4913 new_ccb = xpt_alloc_ccb_nowait();
4914 if (new_ccb == NULL) {
4918 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4919 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4923 cam_ccbq_take_opening(&device->ccbq);
4924 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4930 xpt_release_bus(struct cam_eb *bus)
4935 if ((--bus->refcount == 0)
4936 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4937 TAILQ_REMOVE(&xpt_busses, bus, links);
4940 free(bus, M_CAMXPT);
4945 static struct cam_et *
4946 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4948 struct cam_et *target;
4950 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4951 if (target != NULL) {
4952 struct cam_et *cur_target;
4954 TAILQ_INIT(&target->ed_entries);
4956 target->target_id = target_id;
4957 target->refcount = 1;
4958 target->generation = 0;
4959 timevalclear(&target->last_reset);
4961 * Hold a reference to our parent bus so it
4962 * will not go away before we do.
4966 /* Insertion sort into our bus's target list */
4967 cur_target = TAILQ_FIRST(&bus->et_entries);
4968 while (cur_target != NULL && cur_target->target_id < target_id)
4969 cur_target = TAILQ_NEXT(cur_target, links);
4971 if (cur_target != NULL) {
4972 TAILQ_INSERT_BEFORE(cur_target, target, links);
4974 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4982 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4987 if ((--target->refcount == 0)
4988 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4989 TAILQ_REMOVE(&bus->et_entries, target, links);
4992 free(target, M_CAMXPT);
4993 xpt_release_bus(bus);
4998 static struct cam_ed *
4999 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
5001 #ifdef CAM_NEW_TRAN_CODE
5002 struct cam_path path;
5003 #endif /* CAM_NEW_TRAN_CODE */
5004 struct cam_ed *device;
5005 struct cam_devq *devq;
5008 /* Make space for us in the device queue on our bus */
5009 devq = bus->sim->devq;
5010 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
5012 if (status != CAM_REQ_CMP) {
5015 device = (struct cam_ed *)malloc(sizeof(*device),
5016 M_CAMXPT, M_NOWAIT);
5019 if (device != NULL) {
5020 struct cam_ed *cur_device;
5022 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5023 device->alloc_ccb_entry.device = device;
5024 cam_init_pinfo(&device->send_ccb_entry.pinfo);
5025 device->send_ccb_entry.device = device;
5026 device->target = target;
5027 device->lun_id = lun_id;
5028 /* Initialize our queues */
5029 if (camq_init(&device->drvq, 0) != 0) {
5030 free(device, M_CAMXPT);
5033 if (cam_ccbq_init(&device->ccbq,
5034 bus->sim->max_dev_openings) != 0) {
5035 camq_fini(&device->drvq);
5036 free(device, M_CAMXPT);
5039 SLIST_INIT(&device->asyncs);
5040 SLIST_INIT(&device->periphs);
5041 device->generation = 0;
5042 device->owner = NULL;
5044 * Take the default quirk entry until we have inquiry
5045 * data and can determine a better quirk to use.
5047 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5048 bzero(&device->inq_data, sizeof(device->inq_data));
5049 device->inq_flags = 0;
5050 device->queue_flags = 0;
5051 device->serial_num = NULL;
5052 device->serial_num_len = 0;
5053 device->qfrozen_cnt = 0;
5054 device->flags = CAM_DEV_UNCONFIGURED;
5055 device->tag_delay_count = 0;
5056 device->tag_saved_openings = 0;
5057 device->refcount = 1;
5058 callout_handle_init(&device->c_handle);
5061 * Hold a reference to our parent target so it
5062 * will not go away before we do.
5067 * XXX should be limited by number of CCBs this bus can
5070 xpt_max_ccbs += device->ccbq.devq_openings;
5071 /* Insertion sort into our target's device list */
5072 cur_device = TAILQ_FIRST(&target->ed_entries);
5073 while (cur_device != NULL && cur_device->lun_id < lun_id)
5074 cur_device = TAILQ_NEXT(cur_device, links);
5075 if (cur_device != NULL) {
5076 TAILQ_INSERT_BEFORE(cur_device, device, links);
5078 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5080 target->generation++;
5081 #ifdef CAM_NEW_TRAN_CODE
5082 if (lun_id != CAM_LUN_WILDCARD) {
5083 xpt_compile_path(&path,
5088 xpt_devise_transport(&path);
5089 xpt_release_path(&path);
5091 #endif /* CAM_NEW_TRAN_CODE */
5097 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5098 struct cam_ed *device)
5103 if ((--device->refcount == 0)
5104 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5105 struct cam_devq *devq;
5107 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5108 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5109 panic("Removing device while still queued for ccbs");
5111 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5112 untimeout(xpt_release_devq_timeout, device,
5115 TAILQ_REMOVE(&target->ed_entries, device,links);
5116 target->generation++;
5117 xpt_max_ccbs -= device->ccbq.devq_openings;
5118 /* Release our slot in the devq */
5119 devq = bus->sim->devq;
5120 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5122 camq_fini(&device->drvq);
5123 camq_fini(&device->ccbq.queue);
5124 free(device, M_CAMXPT);
5125 xpt_release_target(bus, target);
5131 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5141 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5142 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5143 if (result == CAM_REQ_CMP && (diff < 0)) {
5144 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5146 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5147 || (dev->inq_flags & SID_CmdQue) != 0)
5148 dev->tag_saved_openings = newopenings;
5149 /* Adjust the global limit */
5150 xpt_max_ccbs += diff;
5155 static struct cam_eb *
5156 xpt_find_bus(path_id_t path_id)
5160 for (bus = TAILQ_FIRST(&xpt_busses);
5162 bus = TAILQ_NEXT(bus, links)) {
5163 if (bus->path_id == path_id) {
5171 static struct cam_et *
5172 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5174 struct cam_et *target;
5176 for (target = TAILQ_FIRST(&bus->et_entries);
5178 target = TAILQ_NEXT(target, links)) {
5179 if (target->target_id == target_id) {
5187 static struct cam_ed *
5188 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5190 struct cam_ed *device;
5192 for (device = TAILQ_FIRST(&target->ed_entries);
5194 device = TAILQ_NEXT(device, links)) {
5195 if (device->lun_id == lun_id) {
5204 union ccb *request_ccb;
5205 struct ccb_pathinq *cpi;
5207 } xpt_scan_bus_info;
5210 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5211 * As the scan progresses, xpt_scan_bus is used as the
5212 * callback on completion function.
5215 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5217 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5218 ("xpt_scan_bus\n"));
5219 switch (request_ccb->ccb_h.func_code) {
5222 xpt_scan_bus_info *scan_info;
5223 union ccb *work_ccb;
5224 struct cam_path *path;
5229 /* Find out the characteristics of the bus */
5230 work_ccb = xpt_alloc_ccb();
5231 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5232 request_ccb->ccb_h.pinfo.priority);
5233 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5234 xpt_action(work_ccb);
5235 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5236 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5237 xpt_free_ccb(work_ccb);
5238 xpt_done(request_ccb);
5242 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5244 * Can't scan the bus on an adapter that
5245 * cannot perform the initiator role.
5247 request_ccb->ccb_h.status = CAM_REQ_CMP;
5248 xpt_free_ccb(work_ccb);
5249 xpt_done(request_ccb);
5253 /* Save some state for use while we probe for devices */
5254 scan_info = (xpt_scan_bus_info *)
5255 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5256 scan_info->request_ccb = request_ccb;
5257 scan_info->cpi = &work_ccb->cpi;
5259 /* Cache on our stack so we can work asynchronously */
5260 max_target = scan_info->cpi->max_target;
5261 initiator_id = scan_info->cpi->initiator_id;
5264 * Don't count the initiator if the
5265 * initiator is addressable.
5267 scan_info->pending_count = max_target + 1;
5268 if (initiator_id <= max_target)
5269 scan_info->pending_count--;
5271 for (i = 0; i <= max_target; i++) {
5273 if (i == initiator_id)
5276 status = xpt_create_path(&path, xpt_periph,
5277 request_ccb->ccb_h.path_id,
5279 if (status != CAM_REQ_CMP) {
5280 printf("xpt_scan_bus: xpt_create_path failed"
5281 " with status %#x, bus scan halted\n",
5285 work_ccb = xpt_alloc_ccb();
5286 xpt_setup_ccb(&work_ccb->ccb_h, path,
5287 request_ccb->ccb_h.pinfo.priority);
5288 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5289 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5290 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5291 work_ccb->crcn.flags = request_ccb->crcn.flags;
5292 xpt_action(work_ccb);
5298 xpt_scan_bus_info *scan_info;
5300 target_id_t target_id;
5303 /* Reuse the same CCB to query if a device was really found */
5304 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5305 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5306 request_ccb->ccb_h.pinfo.priority);
5307 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5309 path_id = request_ccb->ccb_h.path_id;
5310 target_id = request_ccb->ccb_h.target_id;
5311 lun_id = request_ccb->ccb_h.target_lun;
5312 xpt_action(request_ccb);
5314 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5315 struct cam_ed *device;
5316 struct cam_et *target;
5320 * If we already probed lun 0 successfully, or
5321 * we have additional configured luns on this
5322 * target that might have "gone away", go onto
5325 target = request_ccb->ccb_h.path->target;
5327 * We may touch devices that we don't
5328 * hold references too, so ensure they
5329 * don't disappear out from under us.
5330 * The target above is referenced by the
5331 * path in the request ccb.
5335 device = TAILQ_FIRST(&target->ed_entries);
5336 if (device != NULL) {
5337 phl = CAN_SRCH_HI(device);
5338 if (device->lun_id == 0)
5339 device = TAILQ_NEXT(device, links);
5342 if ((lun_id != 0) || (device != NULL)) {
5343 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5347 struct cam_ed *device;
5349 device = request_ccb->ccb_h.path->device;
5351 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5352 /* Try the next lun */
5353 if (lun_id < (CAM_SCSI2_MAXLUN-1)
5354 || CAN_SRCH_HI(device))
5359 xpt_free_path(request_ccb->ccb_h.path);
5362 if ((lun_id == request_ccb->ccb_h.target_lun)
5363 || lun_id > scan_info->cpi->max_lun) {
5366 xpt_free_ccb(request_ccb);
5367 scan_info->pending_count--;
5368 if (scan_info->pending_count == 0) {
5369 xpt_free_ccb((union ccb *)scan_info->cpi);
5370 request_ccb = scan_info->request_ccb;
5371 free(scan_info, M_TEMP);
5372 request_ccb->ccb_h.status = CAM_REQ_CMP;
5373 xpt_done(request_ccb);
5376 /* Try the next device */
5377 struct cam_path *path;
5380 status = xpt_create_path(&path, xpt_periph,
5381 path_id, target_id, lun_id);
5382 if (status != CAM_REQ_CMP) {
5383 printf("xpt_scan_bus: xpt_create_path failed "
5384 "with status %#x, halting LUN scan\n",
5386 xpt_free_ccb(request_ccb);
5387 scan_info->pending_count--;
5388 if (scan_info->pending_count == 0) {
5390 (union ccb *)scan_info->cpi);
5391 request_ccb = scan_info->request_ccb;
5392 free(scan_info, M_TEMP);
5393 request_ccb->ccb_h.status = CAM_REQ_CMP;
5394 xpt_done(request_ccb);
5398 xpt_setup_ccb(&request_ccb->ccb_h, path,
5399 request_ccb->ccb_h.pinfo.priority);
5400 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5401 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5402 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5403 request_ccb->crcn.flags =
5404 scan_info->request_ccb->crcn.flags;
5405 xpt_action(request_ccb);
5420 PROBE_TUR_FOR_NEGOTIATION
5424 PROBE_INQUIRY_CKSUM = 0x01,
5425 PROBE_SERIAL_CKSUM = 0x02,
5426 PROBE_NO_ANNOUNCE = 0x04
5430 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5431 probe_action action;
5432 union ccb saved_ccb;
5435 u_int8_t digest[16];
5439 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5440 cam_flags flags, union ccb *request_ccb)
5442 struct ccb_pathinq cpi;
5444 struct cam_path *new_path;
5445 struct cam_periph *old_periph;
5448 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5449 ("xpt_scan_lun\n"));
5451 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5452 cpi.ccb_h.func_code = XPT_PATH_INQ;
5453 xpt_action((union ccb *)&cpi);
5455 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5456 if (request_ccb != NULL) {
5457 request_ccb->ccb_h.status = cpi.ccb_h.status;
5458 xpt_done(request_ccb);
5463 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5465 * Can't scan the bus on an adapter that
5466 * cannot perform the initiator role.
5468 if (request_ccb != NULL) {
5469 request_ccb->ccb_h.status = CAM_REQ_CMP;
5470 xpt_done(request_ccb);
5475 if (request_ccb == NULL) {
5476 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5477 if (request_ccb == NULL) {
5478 xpt_print_path(path);
5479 printf("xpt_scan_lun: can't allocate CCB, can't "
5483 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5484 if (new_path == NULL) {
5485 xpt_print_path(path);
5486 printf("xpt_scan_lun: can't allocate path, can't "
5488 free(request_ccb, M_TEMP);
5491 status = xpt_compile_path(new_path, xpt_periph,
5493 path->target->target_id,
5494 path->device->lun_id);
5496 if (status != CAM_REQ_CMP) {
5497 xpt_print_path(path);
5498 printf("xpt_scan_lun: can't compile path, can't "
5500 free(request_ccb, M_TEMP);
5501 free(new_path, M_TEMP);
5504 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5505 request_ccb->ccb_h.cbfcnp = xptscandone;
5506 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5507 request_ccb->crcn.flags = flags;
5511 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5514 softc = (probe_softc *)old_periph->softc;
5515 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5518 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5519 probestart, "probe",
5521 request_ccb->ccb_h.path, NULL, 0,
5524 if (status != CAM_REQ_CMP) {
5525 xpt_print_path(path);
5526 printf("xpt_scan_lun: cam_alloc_periph returned an "
5527 "error, can't continue probe\n");
5528 request_ccb->ccb_h.status = status;
5529 xpt_done(request_ccb);
5536 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5538 xpt_release_path(done_ccb->ccb_h.path);
5539 free(done_ccb->ccb_h.path, M_TEMP);
5540 free(done_ccb, M_TEMP);
5544 proberegister(struct cam_periph *periph, void *arg)
5546 union ccb *request_ccb; /* CCB representing the probe request */
5549 request_ccb = (union ccb *)arg;
5550 if (periph == NULL) {
5551 printf("proberegister: periph was NULL!!\n");
5552 return(CAM_REQ_CMP_ERR);
5555 if (request_ccb == NULL) {
5556 printf("proberegister: no probe CCB, "
5557 "can't register device\n");
5558 return(CAM_REQ_CMP_ERR);
5561 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5563 if (softc == NULL) {
5564 printf("proberegister: Unable to probe new device. "
5565 "Unable to allocate softc\n");
5566 return(CAM_REQ_CMP_ERR);
5568 TAILQ_INIT(&softc->request_ccbs);
5569 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5572 periph->softc = softc;
5573 cam_periph_acquire(periph);
5575 * Ensure we've waited at least a bus settle
5576 * delay before attempting to probe the device.
5577 * For HBAs that don't do bus resets, this won't make a difference.
5579 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5581 probeschedule(periph);
5582 return(CAM_REQ_CMP);
5586 probeschedule(struct cam_periph *periph)
5588 struct ccb_pathinq cpi;
5592 softc = (probe_softc *)periph->softc;
5593 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5595 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5596 cpi.ccb_h.func_code = XPT_PATH_INQ;
5597 xpt_action((union ccb *)&cpi);
5600 * If a device has gone away and another device, or the same one,
5601 * is back in the same place, it should have a unit attention
5602 * condition pending. It will not report the unit attention in
5603 * response to an inquiry, which may leave invalid transfer
5604 * negotiations in effect. The TUR will reveal the unit attention
5605 * condition. Only send the TUR for lun 0, since some devices
5606 * will get confused by commands other than inquiry to non-existent
5607 * luns. If you think a device has gone away start your scan from
5608 * lun 0. This will insure that any bogus transfer settings are
5611 * If we haven't seen the device before and the controller supports
5612 * some kind of transfer negotiation, negotiate with the first
5613 * sent command if no bus reset was performed at startup. This
5614 * ensures that the device is not confused by transfer negotiation
5615 * settings left over by loader or BIOS action.
5617 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5618 && (ccb->ccb_h.target_lun == 0)) {
5619 softc->action = PROBE_TUR;
5620 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5621 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5622 proberequestdefaultnegotiation(periph);
5623 softc->action = PROBE_INQUIRY;
5625 softc->action = PROBE_INQUIRY;
5628 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5629 softc->flags |= PROBE_NO_ANNOUNCE;
5631 softc->flags &= ~PROBE_NO_ANNOUNCE;
5633 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5637 probestart(struct cam_periph *periph, union ccb *start_ccb)
5639 /* Probe the device that our peripheral driver points to */
5640 struct ccb_scsiio *csio;
5643 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5645 softc = (probe_softc *)periph->softc;
5646 csio = &start_ccb->csio;
5648 switch (softc->action) {
5650 case PROBE_TUR_FOR_NEGOTIATION:
5652 scsi_test_unit_ready(csio,
5661 case PROBE_FULL_INQUIRY:
5664 struct scsi_inquiry_data *inq_buf;
5666 inq_buf = &periph->path->device->inq_data;
5668 * If the device is currently configured, we calculate an
5669 * MD5 checksum of the inquiry data, and if the serial number
5670 * length is greater than 0, add the serial number data
5671 * into the checksum as well. Once the inquiry and the
5672 * serial number check finish, we attempt to figure out
5673 * whether we still have the same device.
5675 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5677 MD5Init(&softc->context);
5678 MD5Update(&softc->context, (unsigned char *)inq_buf,
5679 sizeof(struct scsi_inquiry_data));
5680 softc->flags |= PROBE_INQUIRY_CKSUM;
5681 if (periph->path->device->serial_num_len > 0) {
5682 MD5Update(&softc->context,
5683 periph->path->device->serial_num,
5684 periph->path->device->serial_num_len);
5685 softc->flags |= PROBE_SERIAL_CKSUM;
5687 MD5Final(softc->digest, &softc->context);
5690 if (softc->action == PROBE_INQUIRY)
5691 inquiry_len = SHORT_INQUIRY_LENGTH;
5693 inquiry_len = inq_buf->additional_length
5694 + offsetof(struct scsi_inquiry_data,
5695 additional_length) + 1;
5698 * Some parallel SCSI devices fail to send an
5699 * ignore wide residue message when dealing with
5700 * odd length inquiry requests. Round up to be
5703 inquiry_len = roundup2(inquiry_len, 2);
5709 (u_int8_t *)inq_buf,
5714 /*timeout*/60 * 1000);
5717 case PROBE_MODE_SENSE:
5722 mode_buf_len = sizeof(struct scsi_mode_header_6)
5723 + sizeof(struct scsi_mode_blk_desc)
5724 + sizeof(struct scsi_control_page);
5725 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5726 if (mode_buf != NULL) {
5727 scsi_mode_sense(csio,
5732 SMS_PAGE_CTRL_CURRENT,
5733 SMS_CONTROL_MODE_PAGE,
5740 xpt_print_path(periph->path);
5741 printf("Unable to mode sense control page - malloc failure\n");
5742 softc->action = PROBE_SERIAL_NUM;
5745 case PROBE_SERIAL_NUM:
5747 struct scsi_vpd_unit_serial_number *serial_buf;
5748 struct cam_ed* device;
5751 device = periph->path->device;
5752 device->serial_num = NULL;
5753 device->serial_num_len = 0;
5755 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5756 serial_buf = (struct scsi_vpd_unit_serial_number *)
5757 malloc(sizeof(*serial_buf), M_TEMP,
5760 if (serial_buf != NULL) {
5765 (u_int8_t *)serial_buf,
5766 sizeof(*serial_buf),
5768 SVPD_UNIT_SERIAL_NUMBER,
5770 /*timeout*/60 * 1000);
5774 * We'll have to do without, let our probedone
5775 * routine finish up for us.
5777 start_ccb->csio.data_ptr = NULL;
5778 probedone(periph, start_ccb);
5782 xpt_action(start_ccb);
5786 proberequestdefaultnegotiation(struct cam_periph *periph)
5788 struct ccb_trans_settings cts;
5790 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5791 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5792 #ifdef CAM_NEW_TRAN_CODE
5793 cts.type = CTS_TYPE_USER_SETTINGS;
5794 #else /* CAM_NEW_TRAN_CODE */
5795 cts.flags = CCB_TRANS_USER_SETTINGS;
5796 #endif /* CAM_NEW_TRAN_CODE */
5797 xpt_action((union ccb *)&cts);
5798 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5799 #ifdef CAM_NEW_TRAN_CODE
5800 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5801 #else /* CAM_NEW_TRAN_CODE */
5802 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5803 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5804 #endif /* CAM_NEW_TRAN_CODE */
5805 xpt_action((union ccb *)&cts);
5809 probedone(struct cam_periph *periph, union ccb *done_ccb)
5812 struct cam_path *path;
5815 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5817 softc = (probe_softc *)periph->softc;
5818 path = done_ccb->ccb_h.path;
5819 priority = done_ccb->ccb_h.pinfo.priority;
5821 switch (softc->action) {
5824 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5826 if (cam_periph_error(done_ccb, 0,
5827 SF_NO_PRINT, NULL) == ERESTART)
5829 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5830 /* Don't wedge the queue */
5831 xpt_release_devq(done_ccb->ccb_h.path,
5835 softc->action = PROBE_INQUIRY;
5836 xpt_release_ccb(done_ccb);
5837 xpt_schedule(periph, priority);
5841 case PROBE_FULL_INQUIRY:
5843 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5844 struct scsi_inquiry_data *inq_buf;
5845 u_int8_t periph_qual;
5847 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5848 inq_buf = &path->device->inq_data;
5850 periph_qual = SID_QUAL(inq_buf);
5852 switch(periph_qual) {
5853 case SID_QUAL_LU_CONNECTED:
5858 * We conservatively request only
5859 * SHORT_INQUIRY_LEN bytes of inquiry
5860 * information during our first try
5861 * at sending an INQUIRY. If the device
5862 * has more information to give,
5863 * perform a second request specifying
5864 * the amount of information the device
5865 * is willing to give.
5867 len = inq_buf->additional_length
5868 + offsetof(struct scsi_inquiry_data,
5869 additional_length) + 1;
5870 if (softc->action == PROBE_INQUIRY
5871 && len > SHORT_INQUIRY_LENGTH) {
5872 softc->action = PROBE_FULL_INQUIRY;
5873 xpt_release_ccb(done_ccb);
5874 xpt_schedule(periph, priority);
5878 xpt_find_quirk(path->device);
5880 #ifdef CAM_NEW_TRAN_CODE
5881 xpt_devise_transport(path);
5882 #endif /* CAM_NEW_TRAN_CODE */
5883 if ((inq_buf->flags & SID_CmdQue) != 0)
5884 softc->action = PROBE_MODE_SENSE;
5886 softc->action = PROBE_SERIAL_NUM;
5888 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5890 xpt_release_ccb(done_ccb);
5891 xpt_schedule(periph, priority);
5897 } else if (cam_periph_error(done_ccb, 0,
5898 done_ccb->ccb_h.target_lun > 0
5899 ? SF_RETRY_UA|SF_QUIET_IR
5901 &softc->saved_ccb) == ERESTART) {
5903 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5904 /* Don't wedge the queue */
5905 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5909 * If we get to this point, we got an error status back
5910 * from the inquiry and the error status doesn't require
5911 * automatically retrying the command. Therefore, the
5912 * inquiry failed. If we had inquiry information before
5913 * for this device, but this latest inquiry command failed,
5914 * the device has probably gone away. If this device isn't
5915 * already marked unconfigured, notify the peripheral
5916 * drivers that this device is no more.
5918 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5919 /* Send the async notification. */
5920 xpt_async(AC_LOST_DEVICE, path, NULL);
5922 xpt_release_ccb(done_ccb);
5925 case PROBE_MODE_SENSE:
5927 struct ccb_scsiio *csio;
5928 struct scsi_mode_header_6 *mode_hdr;
5930 csio = &done_ccb->csio;
5931 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5932 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5933 struct scsi_control_page *page;
5936 offset = ((u_int8_t *)&mode_hdr[1])
5937 + mode_hdr->blk_desc_len;
5938 page = (struct scsi_control_page *)offset;
5939 path->device->queue_flags = page->queue_flags;
5940 } else if (cam_periph_error(done_ccb, 0,
5941 SF_RETRY_UA|SF_NO_PRINT,
5942 &softc->saved_ccb) == ERESTART) {
5944 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5945 /* Don't wedge the queue */
5946 xpt_release_devq(done_ccb->ccb_h.path,
5947 /*count*/1, /*run_queue*/TRUE);
5949 xpt_release_ccb(done_ccb);
5950 free(mode_hdr, M_TEMP);
5951 softc->action = PROBE_SERIAL_NUM;
5952 xpt_schedule(periph, priority);
5955 case PROBE_SERIAL_NUM:
5957 struct ccb_scsiio *csio;
5958 struct scsi_vpd_unit_serial_number *serial_buf;
5965 csio = &done_ccb->csio;
5966 priority = done_ccb->ccb_h.pinfo.priority;
5968 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5970 /* Clean up from previous instance of this device */
5971 if (path->device->serial_num != NULL) {
5972 free(path->device->serial_num, M_CAMXPT);
5973 path->device->serial_num = NULL;
5974 path->device->serial_num_len = 0;
5977 if (serial_buf == NULL) {
5979 * Don't process the command as it was never sent
5981 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5982 && (serial_buf->length > 0)) {
5985 path->device->serial_num =
5986 (u_int8_t *)malloc((serial_buf->length + 1),
5987 M_CAMXPT, M_NOWAIT);
5988 if (path->device->serial_num != NULL) {
5989 bcopy(serial_buf->serial_num,
5990 path->device->serial_num,
5991 serial_buf->length);
5992 path->device->serial_num_len =
5994 path->device->serial_num[serial_buf->length]
5997 } else if (cam_periph_error(done_ccb, 0,
5998 SF_RETRY_UA|SF_NO_PRINT,
5999 &softc->saved_ccb) == ERESTART) {
6001 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6002 /* Don't wedge the queue */
6003 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6008 * Let's see if we have seen this device before.
6010 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
6012 u_int8_t digest[16];
6017 (unsigned char *)&path->device->inq_data,
6018 sizeof(struct scsi_inquiry_data));
6021 MD5Update(&context, serial_buf->serial_num,
6022 serial_buf->length);
6024 MD5Final(digest, &context);
6025 if (bcmp(softc->digest, digest, 16) == 0)
6029 * XXX Do we need to do a TUR in order to ensure
6030 * that the device really hasn't changed???
6033 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6034 xpt_async(AC_LOST_DEVICE, path, NULL);
6036 if (serial_buf != NULL)
6037 free(serial_buf, M_TEMP);
6041 * Now that we have all the necessary
6042 * information to safely perform transfer
6043 * negotiations... Controllers don't perform
6044 * any negotiation or tagged queuing until
6045 * after the first XPT_SET_TRAN_SETTINGS ccb is
6046 * received. So, on a new device, just retreive
6047 * the user settings, and set them as the current
6048 * settings to set the device up.
6050 proberequestdefaultnegotiation(periph);
6051 xpt_release_ccb(done_ccb);
6054 * Perform a TUR to allow the controller to
6055 * perform any necessary transfer negotiation.
6057 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6058 xpt_schedule(periph, priority);
6061 xpt_release_ccb(done_ccb);
6064 case PROBE_TUR_FOR_NEGOTIATION:
6065 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6066 /* Don't wedge the queue */
6067 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6071 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6073 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6074 /* Inform the XPT that a new device has been found */
6075 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6076 xpt_action(done_ccb);
6078 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6081 xpt_release_ccb(done_ccb);
6084 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6085 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6086 done_ccb->ccb_h.status = CAM_REQ_CMP;
6088 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6089 cam_periph_invalidate(periph);
6090 cam_periph_release(periph);
6092 probeschedule(periph);
6097 probecleanup(struct cam_periph *periph)
6099 free(periph->softc, M_TEMP);
6103 xpt_find_quirk(struct cam_ed *device)
6107 match = cam_quirkmatch((caddr_t)&device->inq_data,
6108 (caddr_t)xpt_quirk_table,
6109 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6110 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6113 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6115 device->quirk = (struct xpt_quirk_entry *)match;
6118 #ifdef CAM_NEW_TRAN_CODE
6121 xpt_devise_transport(struct cam_path *path)
6123 struct ccb_pathinq cpi;
6124 struct ccb_trans_settings cts;
6125 struct scsi_inquiry_data *inq_buf;
6127 /* Get transport information from the SIM */
6128 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6129 cpi.ccb_h.func_code = XPT_PATH_INQ;
6130 xpt_action((union ccb *)&cpi);
6133 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6134 inq_buf = &path->device->inq_data;
6135 path->device->protocol = PROTO_SCSI;
6136 path->device->protocol_version =
6137 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6138 path->device->transport = cpi.transport;
6139 path->device->transport_version = cpi.transport_version;
6142 * Any device not using SPI3 features should
6143 * be considered SPI2 or lower.
6145 if (inq_buf != NULL) {
6146 if (path->device->transport == XPORT_SPI
6147 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6148 && path->device->transport_version > 2)
6149 path->device->transport_version = 2;
6151 struct cam_ed* otherdev;
6153 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6155 otherdev = TAILQ_NEXT(otherdev, links)) {
6156 if (otherdev != path->device)
6160 if (otherdev != NULL) {
6162 * Initially assume the same versioning as
6163 * prior luns for this target.
6165 path->device->protocol_version =
6166 otherdev->protocol_version;
6167 path->device->transport_version =
6168 otherdev->transport_version;
6170 /* Until we know better, opt for safty */
6171 path->device->protocol_version = 2;
6172 if (path->device->transport == XPORT_SPI)
6173 path->device->transport_version = 2;
6175 path->device->transport_version = 0;
6181 * For a device compliant with SPC-2 we should be able
6182 * to determine the transport version supported by
6183 * scrutinizing the version descriptors in the
6187 /* Tell the controller what we think */
6188 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6189 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6190 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6191 cts.transport = path->device->transport;
6192 cts.transport_version = path->device->transport_version;
6193 cts.protocol = path->device->protocol;
6194 cts.protocol_version = path->device->protocol_version;
6195 cts.proto_specific.valid = 0;
6196 cts.xport_specific.valid = 0;
6197 xpt_action((union ccb *)&cts);
6201 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6204 struct ccb_pathinq cpi;
6205 struct ccb_trans_settings cur_cts;
6206 struct ccb_trans_settings_scsi *scsi;
6207 struct ccb_trans_settings_scsi *cur_scsi;
6208 struct cam_sim *sim;
6209 struct scsi_inquiry_data *inq_data;
6211 if (device == NULL) {
6212 cts->ccb_h.status = CAM_PATH_INVALID;
6213 xpt_done((union ccb *)cts);
6217 if (cts->protocol == PROTO_UNKNOWN
6218 || cts->protocol == PROTO_UNSPECIFIED) {
6219 cts->protocol = device->protocol;
6220 cts->protocol_version = device->protocol_version;
6223 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6224 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6225 cts->protocol_version = device->protocol_version;
6227 if (cts->protocol != device->protocol) {
6228 xpt_print_path(cts->ccb_h.path);
6229 printf("Uninitialized Protocol %x:%x?\n",
6230 cts->protocol, device->protocol);
6231 cts->protocol = device->protocol;
6234 if (cts->protocol_version > device->protocol_version) {
6236 xpt_print_path(cts->ccb_h.path);
6237 printf("Down reving Protocol Version from %d to %d?\n",
6238 cts->protocol_version, device->protocol_version);
6240 cts->protocol_version = device->protocol_version;
6243 if (cts->transport == XPORT_UNKNOWN
6244 || cts->transport == XPORT_UNSPECIFIED) {
6245 cts->transport = device->transport;
6246 cts->transport_version = device->transport_version;
6249 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6250 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6251 cts->transport_version = device->transport_version;
6253 if (cts->transport != device->transport) {
6254 xpt_print_path(cts->ccb_h.path);
6255 printf("Uninitialized Transport %x:%x?\n",
6256 cts->transport, device->transport);
6257 cts->transport = device->transport;
6260 if (cts->transport_version > device->transport_version) {
6262 xpt_print_path(cts->ccb_h.path);
6263 printf("Down reving Transport Version from %d to %d?\n",
6264 cts->transport_version,
6265 device->transport_version);
6267 cts->transport_version = device->transport_version;
6270 sim = cts->ccb_h.path->bus->sim;
6273 * Nothing more of interest to do unless
6274 * this is a device connected via the
6277 if (cts->protocol != PROTO_SCSI) {
6278 if (async_update == FALSE)
6279 (*(sim->sim_action))(sim, (union ccb *)cts);
6283 inq_data = &device->inq_data;
6284 scsi = &cts->proto_specific.scsi;
6285 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6286 cpi.ccb_h.func_code = XPT_PATH_INQ;
6287 xpt_action((union ccb *)&cpi);
6289 /* SCSI specific sanity checking */
6290 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6291 || (inq_data->flags & SID_CmdQue) == 0
6292 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6293 || (device->quirk->mintags == 0)) {
6295 * Can't tag on hardware that doesn't support tags,
6296 * doesn't have it enabled, or has broken tag support.
6298 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6301 if (async_update == FALSE) {
6303 * Perform sanity checking against what the
6304 * controller and device can do.
6306 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6307 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6308 cur_cts.type = cts->type;
6309 xpt_action((union ccb *)&cur_cts);
6311 cur_scsi = &cur_cts.proto_specific.scsi;
6312 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6313 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6314 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6316 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6317 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6320 /* SPI specific sanity checking */
6321 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6323 struct ccb_trans_settings_spi *spi;
6324 struct ccb_trans_settings_spi *cur_spi;
6326 spi = &cts->xport_specific.spi;
6328 cur_spi = &cur_cts.xport_specific.spi;
6330 /* Fill in any gaps in what the user gave us */
6331 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6332 spi->sync_period = cur_spi->sync_period;
6333 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6334 spi->sync_period = 0;
6335 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6336 spi->sync_offset = cur_spi->sync_offset;
6337 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6338 spi->sync_offset = 0;
6339 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6340 spi->ppr_options = cur_spi->ppr_options;
6341 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6342 spi->ppr_options = 0;
6343 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6344 spi->bus_width = cur_spi->bus_width;
6345 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6347 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6348 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6349 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6351 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6352 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6353 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6354 && (inq_data->flags & SID_Sync) == 0
6355 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6356 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6357 || (cur_spi->sync_offset == 0)
6358 || (cur_spi->sync_period == 0)) {
6360 spi->sync_period = 0;
6361 spi->sync_offset = 0;
6364 switch (spi->bus_width) {
6365 case MSG_EXT_WDTR_BUS_32_BIT:
6366 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6367 || (inq_data->flags & SID_WBus32) != 0
6368 || cts->type == CTS_TYPE_USER_SETTINGS)
6369 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6371 /* Fall Through to 16-bit */
6372 case MSG_EXT_WDTR_BUS_16_BIT:
6373 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6374 || (inq_data->flags & SID_WBus16) != 0
6375 || cts->type == CTS_TYPE_USER_SETTINGS)
6376 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6377 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6380 /* Fall Through to 8-bit */
6381 default: /* New bus width?? */
6382 case MSG_EXT_WDTR_BUS_8_BIT:
6383 /* All targets can do this */
6384 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6388 spi3caps = cpi.xport_specific.spi.ppr_options;
6389 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6390 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6391 spi3caps &= inq_data->spi3data;
6393 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6394 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6396 if ((spi3caps & SID_SPI_IUS) == 0)
6397 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6399 if ((spi3caps & SID_SPI_QAS) == 0)
6400 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6402 /* No SPI Transfer settings are allowed unless we are wide */
6403 if (spi->bus_width == 0)
6404 spi->ppr_options = 0;
6406 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6408 * Can't tag queue without disconnection.
6410 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6411 scsi->valid |= CTS_SCSI_VALID_TQ;
6415 * If we are currently performing tagged transactions to
6416 * this device and want to change its negotiation parameters,
6417 * go non-tagged for a bit to give the controller a chance to
6418 * negotiate unhampered by tag messages.
6420 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6421 && (device->inq_flags & SID_CmdQue) != 0
6422 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6423 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6424 CTS_SPI_VALID_SYNC_OFFSET|
6425 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6426 xpt_toggle_tags(cts->ccb_h.path);
6429 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6430 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6434 * If we are transitioning from tags to no-tags or
6435 * vice-versa, we need to carefully freeze and restart
6436 * the queue so that we don't overlap tagged and non-tagged
6437 * commands. We also temporarily stop tags if there is
6438 * a change in transfer negotiation settings to allow
6439 * "tag-less" negotiation.
6441 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6442 || (device->inq_flags & SID_CmdQue) != 0)
6443 device_tagenb = TRUE;
6445 device_tagenb = FALSE;
6447 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6448 && device_tagenb == FALSE)
6449 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6450 && device_tagenb == TRUE)) {
6452 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6454 * Delay change to use tags until after a
6455 * few commands have gone to this device so
6456 * the controller has time to perform transfer
6457 * negotiations without tagged messages getting
6460 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6461 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6463 struct ccb_relsim crs;
6465 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6466 device->inq_flags &= ~SID_CmdQue;
6467 xpt_dev_ccbq_resize(cts->ccb_h.path,
6468 sim->max_dev_openings);
6469 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6470 device->tag_delay_count = 0;
6472 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6474 crs.ccb_h.func_code = XPT_REL_SIMQ;
6475 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6477 = crs.release_timeout
6480 xpt_action((union ccb *)&crs);
6484 if (async_update == FALSE)
6485 (*(sim->sim_action))(sim, (union ccb *)cts);
6488 #else /* CAM_NEW_TRAN_CODE */
6491 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6494 struct cam_sim *sim;
6497 sim = cts->ccb_h.path->bus->sim;
6498 if (async_update == FALSE) {
6499 struct scsi_inquiry_data *inq_data;
6500 struct ccb_pathinq cpi;
6501 struct ccb_trans_settings cur_cts;
6503 if (device == NULL) {
6504 cts->ccb_h.status = CAM_PATH_INVALID;
6505 xpt_done((union ccb *)cts);
6510 * Perform sanity checking against what the
6511 * controller and device can do.
6513 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6514 cpi.ccb_h.func_code = XPT_PATH_INQ;
6515 xpt_action((union ccb *)&cpi);
6516 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6517 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6518 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6519 xpt_action((union ccb *)&cur_cts);
6520 inq_data = &device->inq_data;
6522 /* Fill in any gaps in what the user gave us */
6523 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6524 cts->sync_period = cur_cts.sync_period;
6525 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6526 cts->sync_offset = cur_cts.sync_offset;
6527 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6528 cts->bus_width = cur_cts.bus_width;
6529 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6530 cts->flags &= ~CCB_TRANS_DISC_ENB;
6531 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6533 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6534 cts->flags &= ~CCB_TRANS_TAG_ENB;
6535 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6538 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6539 && (inq_data->flags & SID_Sync) == 0)
6540 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6541 || (cts->sync_offset == 0)
6542 || (cts->sync_period == 0)) {
6544 cts->sync_period = 0;
6545 cts->sync_offset = 0;
6546 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6547 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6548 && cts->sync_period <= 0x9) {
6550 * Don't allow DT transmission rates if the
6551 * device does not support it.
6553 cts->sync_period = 0xa;
6556 switch (cts->bus_width) {
6557 case MSG_EXT_WDTR_BUS_32_BIT:
6558 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6559 || (inq_data->flags & SID_WBus32) != 0)
6560 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6562 /* FALLTHROUGH to 16-bit */
6563 case MSG_EXT_WDTR_BUS_16_BIT:
6564 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6565 || (inq_data->flags & SID_WBus16) != 0)
6566 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6567 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6570 /* FALLTHROUGH to 8-bit */
6571 default: /* New bus width?? */
6572 case MSG_EXT_WDTR_BUS_8_BIT:
6573 /* All targets can do this */
6574 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6578 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6580 * Can't tag queue without disconnection.
6582 cts->flags &= ~CCB_TRANS_TAG_ENB;
6583 cts->valid |= CCB_TRANS_TQ_VALID;
6586 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6587 || (inq_data->flags & SID_CmdQue) == 0
6588 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6589 || (device->quirk->mintags == 0)) {
6591 * Can't tag on hardware that doesn't support,
6592 * doesn't have it enabled, or has broken tag support.
6594 cts->flags &= ~CCB_TRANS_TAG_ENB;
6599 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6603 * If we are transitioning from tags to no-tags or
6604 * vice-versa, we need to carefully freeze and restart
6605 * the queue so that we don't overlap tagged and non-tagged
6606 * commands. We also temporarily stop tags if there is
6607 * a change in transfer negotiation settings to allow
6608 * "tag-less" negotiation.
6610 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6611 || (device->inq_flags & SID_CmdQue) != 0)
6612 device_tagenb = TRUE;
6614 device_tagenb = FALSE;
6616 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6617 && device_tagenb == FALSE)
6618 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6619 && device_tagenb == TRUE)) {
6621 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6623 * Delay change to use tags until after a
6624 * few commands have gone to this device so
6625 * the controller has time to perform transfer
6626 * negotiations without tagged messages getting
6629 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6630 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6632 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6634 device->inq_flags &= ~SID_CmdQue;
6635 xpt_dev_ccbq_resize(cts->ccb_h.path,
6636 sim->max_dev_openings);
6637 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6638 device->tag_delay_count = 0;
6643 if (async_update == FALSE) {
6645 * If we are currently performing tagged transactions to
6646 * this device and want to change its negotiation parameters,
6647 * go non-tagged for a bit to give the controller a chance to
6648 * negotiate unhampered by tag messages.
6650 if ((device->inq_flags & SID_CmdQue) != 0
6651 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6652 CCB_TRANS_SYNC_OFFSET_VALID|
6653 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6654 xpt_toggle_tags(cts->ccb_h.path);
6656 (*(sim->sim_action))(sim, (union ccb *)cts);
6660 struct ccb_relsim crs;
6662 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6664 crs.ccb_h.func_code = XPT_REL_SIMQ;
6665 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6667 = crs.release_timeout
6670 xpt_action((union ccb *)&crs);
6675 #endif /* CAM_NEW_TRAN_CODE */
6678 xpt_toggle_tags(struct cam_path *path)
6683 * Give controllers a chance to renegotiate
6684 * before starting tag operations. We
6685 * "toggle" tagged queuing off then on
6686 * which causes the tag enable command delay
6687 * counter to come into effect.
6690 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6691 || ((dev->inq_flags & SID_CmdQue) != 0
6692 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6693 struct ccb_trans_settings cts;
6695 xpt_setup_ccb(&cts.ccb_h, path, 1);
6696 #ifdef CAM_NEW_TRAN_CODE
6697 cts.protocol = PROTO_SCSI;
6698 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6699 cts.transport = XPORT_UNSPECIFIED;
6700 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6701 cts.proto_specific.scsi.flags = 0;
6702 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6703 #else /* CAM_NEW_TRAN_CODE */
6705 cts.valid = CCB_TRANS_TQ_VALID;
6706 #endif /* CAM_NEW_TRAN_CODE */
6707 xpt_set_transfer_settings(&cts, path->device,
6708 /*async_update*/TRUE);
6709 #ifdef CAM_NEW_TRAN_CODE
6710 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6711 #else /* CAM_NEW_TRAN_CODE */
6712 cts.flags = CCB_TRANS_TAG_ENB;
6713 #endif /* CAM_NEW_TRAN_CODE */
6714 xpt_set_transfer_settings(&cts, path->device,
6715 /*async_update*/TRUE);
6720 xpt_start_tags(struct cam_path *path)
6722 struct ccb_relsim crs;
6723 struct cam_ed *device;
6724 struct cam_sim *sim;
6727 device = path->device;
6728 sim = path->bus->sim;
6729 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6730 xpt_freeze_devq(path, /*count*/1);
6731 device->inq_flags |= SID_CmdQue;
6732 if (device->tag_saved_openings != 0)
6733 newopenings = device->tag_saved_openings;
6735 newopenings = min(device->quirk->maxtags,
6736 sim->max_tagged_dev_openings);
6737 xpt_dev_ccbq_resize(path, newopenings);
6738 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6739 crs.ccb_h.func_code = XPT_REL_SIMQ;
6740 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6742 = crs.release_timeout
6745 xpt_action((union ccb *)&crs);
6748 static int busses_to_config;
6749 static int busses_to_reset;
6752 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6754 if (bus->path_id != CAM_XPT_PATH_ID) {
6755 struct cam_path path;
6756 struct ccb_pathinq cpi;
6760 xpt_compile_path(&path, NULL, bus->path_id,
6761 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6762 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6763 cpi.ccb_h.func_code = XPT_PATH_INQ;
6764 xpt_action((union ccb *)&cpi);
6765 can_negotiate = cpi.hba_inquiry;
6766 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6767 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6770 xpt_release_path(&path);
6777 xptconfigfunc(struct cam_eb *bus, void *arg)
6779 struct cam_path *path;
6780 union ccb *work_ccb;
6782 if (bus->path_id != CAM_XPT_PATH_ID) {
6786 work_ccb = xpt_alloc_ccb();
6787 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6788 CAM_TARGET_WILDCARD,
6789 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6790 printf("xptconfigfunc: xpt_create_path failed with "
6791 "status %#x for bus %d\n", status, bus->path_id);
6792 printf("xptconfigfunc: halting bus configuration\n");
6793 xpt_free_ccb(work_ccb);
6795 xpt_finishconfig(xpt_periph, NULL);
6798 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6799 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6800 xpt_action(work_ccb);
6801 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6802 printf("xptconfigfunc: CPI failed on bus %d "
6803 "with status %d\n", bus->path_id,
6804 work_ccb->ccb_h.status);
6805 xpt_finishconfig(xpt_periph, work_ccb);
6809 can_negotiate = work_ccb->cpi.hba_inquiry;
6810 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6811 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6812 && (can_negotiate != 0)) {
6813 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6814 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6815 work_ccb->ccb_h.cbfcnp = NULL;
6816 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6817 ("Resetting Bus\n"));
6818 xpt_action(work_ccb);
6819 xpt_finishconfig(xpt_periph, work_ccb);
6821 /* Act as though we performed a successful BUS RESET */
6822 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6823 xpt_finishconfig(xpt_periph, work_ccb);
6831 xpt_config(void *arg)
6834 * Now that interrupts are enabled, go find our devices
6838 /* Setup debugging flags and path */
6839 #ifdef CAM_DEBUG_FLAGS
6840 cam_dflags = CAM_DEBUG_FLAGS;
6841 #else /* !CAM_DEBUG_FLAGS */
6842 cam_dflags = CAM_DEBUG_NONE;
6843 #endif /* CAM_DEBUG_FLAGS */
6844 #ifdef CAM_DEBUG_BUS
6845 if (cam_dflags != CAM_DEBUG_NONE) {
6846 if (xpt_create_path(&cam_dpath, xpt_periph,
6847 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6848 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6849 printf("xpt_config: xpt_create_path() failed for debug"
6850 " target %d:%d:%d, debugging disabled\n",
6851 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6852 cam_dflags = CAM_DEBUG_NONE;
6856 #else /* !CAM_DEBUG_BUS */
6858 #endif /* CAM_DEBUG_BUS */
6859 #endif /* CAMDEBUG */
6862 * Scan all installed busses.
6864 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6866 if (busses_to_config == 0) {
6867 /* Call manually because we don't have any busses */
6868 xpt_finishconfig(xpt_periph, NULL);
6870 if (busses_to_reset > 0 && scsi_delay >= 2000) {
6871 printf("Waiting %d seconds for SCSI "
6872 "devices to settle\n", scsi_delay/1000);
6874 xpt_for_all_busses(xptconfigfunc, NULL);
6879 * If the given device only has one peripheral attached to it, and if that
6880 * peripheral is the passthrough driver, announce it. This insures that the
6881 * user sees some sort of announcement for every peripheral in their system.
6884 xptpassannouncefunc(struct cam_ed *device, void *arg)
6886 struct cam_periph *periph;
6889 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6890 periph = SLIST_NEXT(periph, periph_links), i++);
6892 periph = SLIST_FIRST(&device->periphs);
6894 && (strncmp(periph->periph_name, "pass", 4) == 0))
6895 xpt_announce_periph(periph, NULL);
6901 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6903 struct periph_driver **p_drv;
6906 if (done_ccb != NULL) {
6907 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6908 ("xpt_finishconfig\n"));
6909 switch(done_ccb->ccb_h.func_code) {
6911 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6912 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6913 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6914 done_ccb->crcn.flags = 0;
6915 xpt_action(done_ccb);
6921 xpt_free_path(done_ccb->ccb_h.path);
6927 if (busses_to_config == 0) {
6928 /* Register all the peripheral drivers */
6929 /* XXX This will have to change when we have loadable modules */
6930 p_drv = periph_drivers;
6931 for (i = 0; p_drv[i] != NULL; i++) {
6932 (*p_drv[i]->init)();
6936 * Check for devices with no "standard" peripheral driver
6937 * attached. For any devices like that, announce the
6938 * passthrough driver so the user will see something.
6940 xpt_for_all_devices(xptpassannouncefunc, NULL);
6942 /* Release our hook so that the boot can continue. */
6943 config_intrhook_disestablish(xpt_config_hook);
6944 free(xpt_config_hook, M_TEMP);
6945 xpt_config_hook = NULL;
6947 if (done_ccb != NULL)
6948 xpt_free_ccb(done_ccb);
6952 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6954 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6956 switch (work_ccb->ccb_h.func_code) {
6957 /* Common cases first */
6958 case XPT_PATH_INQ: /* Path routing inquiry */
6960 struct ccb_pathinq *cpi;
6962 cpi = &work_ccb->cpi;
6963 cpi->version_num = 1; /* XXX??? */
6964 cpi->hba_inquiry = 0;
6965 cpi->target_sprt = 0;
6967 cpi->hba_eng_cnt = 0;
6968 cpi->max_target = 0;
6970 cpi->initiator_id = 0;
6971 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6972 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6973 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6974 cpi->unit_number = sim->unit_number;
6975 cpi->bus_id = sim->bus_id;
6976 cpi->base_transfer_speed = 0;
6977 #ifdef CAM_NEW_TRAN_CODE
6978 cpi->protocol = PROTO_UNSPECIFIED;
6979 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
6980 cpi->transport = XPORT_UNSPECIFIED;
6981 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
6982 #endif /* CAM_NEW_TRAN_CODE */
6983 cpi->ccb_h.status = CAM_REQ_CMP;
6988 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6995 * The xpt as a "controller" has no interrupt sources, so polling
6999 xptpoll(struct cam_sim *sim)
7004 camisr(void *V_queue)
7006 cam_isrq_t *oqueue = V_queue;
7009 struct ccb_hdr *ccb_h;
7012 * Transfer the ccb_bioq list to a temporary list so we can operate
7013 * on it without needing to lock/unlock on every loop. The concat
7014 * function with re-init the real list for us.
7017 mtx_lock(&cam_bioq_lock);
7019 TAILQ_CONCAT(&queue, oqueue, sim_links.tqe);
7020 mtx_unlock(&cam_bioq_lock);
7022 while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) {
7025 TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe);
7026 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7029 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7034 if (ccb_h->flags & CAM_HIGH_POWER) {
7035 struct highpowerlist *hphead;
7036 union ccb *send_ccb;
7038 hphead = &highpowerq;
7040 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7043 * Increment the count since this command is done.
7048 * Any high powered commands queued up?
7050 if (send_ccb != NULL) {
7052 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7054 xpt_release_devq(send_ccb->ccb_h.path,
7055 /*count*/1, /*runqueue*/TRUE);
7058 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7061 dev = ccb_h->path->device;
7064 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7066 ccb_h->path->bus->sim->devq->send_active--;
7067 ccb_h->path->bus->sim->devq->send_openings++;
7070 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7071 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7072 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7073 && (dev->ccbq.dev_active == 0))) {
7075 xpt_release_devq(ccb_h->path, /*count*/1,
7079 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7080 && (--dev->tag_delay_count == 0))
7081 xpt_start_tags(ccb_h->path);
7083 if ((dev->ccbq.queue.entries > 0)
7084 && (dev->qfrozen_cnt == 0)
7085 && (device_is_send_queued(dev) == 0)) {
7086 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7091 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7092 xpt_release_simq(ccb_h->path->bus->sim,
7094 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7098 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7099 && (ccb_h->status & CAM_DEV_QFRZN)) {
7100 xpt_release_devq(ccb_h->path, /*count*/1,
7102 ccb_h->status &= ~CAM_DEV_QFRZN;
7104 xpt_run_dev_sendq(ccb_h->path->bus);
7107 /* Call the peripheral driver's callback */
7108 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7110 /* Raise IPL for while test */