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
29 * $FreeBSD: src/sys/cam/cam_xpt.c,v 1.80.2.18 2002/12/09 17:31:55 gibbs Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/types.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/fcntl.h>
41 #include <sys/devicestat.h>
42 #include <sys/interrupt.h>
44 #include <sys/taskqueue.h>
46 #include <sys/thread.h>
48 #include <sys/spinlock.h>
50 #include <sys/thread2.h>
51 #include <sys/spinlock2.h>
52 #include <sys/mplock2.h>
54 #include <machine/clock.h>
55 #include <machine/stdarg.h>
59 #include "cam_periph.h"
62 #include "cam_xpt_sim.h"
63 #include "cam_xpt_periph.h"
64 #include "cam_debug.h"
66 #include "scsi/scsi_all.h"
67 #include "scsi/scsi_message.h"
68 #include "scsi/scsi_pass.h"
69 #include <sys/kthread.h>
72 /* Datastructures internal to the xpt layer */
73 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
75 /* Object for defering XPT actions to a taskqueue */
83 * Definition of an async handler callback block. These are used to add
84 * SIMs and peripherals to the async callback lists.
87 SLIST_ENTRY(async_node) links;
88 u_int32_t event_enable; /* Async Event enables */
89 void (*callback)(void *arg, u_int32_t code,
90 struct cam_path *path, void *args);
94 SLIST_HEAD(async_list, async_node);
95 SLIST_HEAD(periph_list, cam_periph);
98 * This is the maximum number of high powered commands (e.g. start unit)
99 * that can be outstanding at a particular time.
101 #ifndef CAM_MAX_HIGHPOWER
102 #define CAM_MAX_HIGHPOWER 4
106 * Structure for queueing a device in a run queue.
107 * There is one run queue for allocating new ccbs,
108 * and another for sending ccbs to the controller.
110 struct cam_ed_qinfo {
112 struct cam_ed *device;
116 * The CAM EDT (Existing Device Table) contains the device information for
117 * all devices for all busses in the system. The table contains a
118 * cam_ed structure for each device on the bus.
121 TAILQ_ENTRY(cam_ed) links;
122 struct cam_ed_qinfo alloc_ccb_entry;
123 struct cam_ed_qinfo send_ccb_entry;
124 struct cam_et *target;
128 * Queue of type drivers wanting to do
129 * work on this device.
131 struct cam_ccbq ccbq; /* Queue of pending ccbs */
132 struct async_list asyncs; /* Async callback info for this B/T/L */
133 struct periph_list periphs; /* All attached devices */
134 u_int generation; /* Generation number */
135 struct cam_periph *owner; /* Peripheral driver's ownership tag */
136 struct xpt_quirk_entry *quirk; /* Oddities about this device */
137 /* Storage for the inquiry data */
139 u_int protocol_version;
141 u_int transport_version;
142 struct scsi_inquiry_data inq_data;
143 u_int8_t inq_flags; /*
144 * Current settings for inquiry flags.
145 * This allows us to override settings
146 * like disconnection and tagged
147 * queuing for a device.
149 u_int8_t queue_flags; /* Queue flags from the control page */
150 u_int8_t serial_num_len;
151 u_int8_t *serial_num;
152 u_int32_t qfrozen_cnt;
154 #define CAM_DEV_UNCONFIGURED 0x01
155 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
156 #define CAM_DEV_REL_ON_COMPLETE 0x04
157 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
158 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
159 #define CAM_DEV_TAG_AFTER_COUNT 0x20
160 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
161 #define CAM_DEV_IN_DV 0x80
162 #define CAM_DEV_DV_HIT_BOTTOM 0x100
163 u_int32_t tag_delay_count;
164 #define CAM_TAG_DELAY_COUNT 5
165 u_int32_t tag_saved_openings;
167 struct callout callout;
171 * Each target is represented by an ET (Existing Target). These
172 * entries are created when a target is successfully probed with an
173 * identify, and removed when a device fails to respond after a number
174 * of retries, or a bus rescan finds the device missing.
177 TAILQ_HEAD(, cam_ed) ed_entries;
178 TAILQ_ENTRY(cam_et) links;
180 target_id_t target_id;
183 struct timeval last_reset; /* uptime of last reset */
187 * Each bus is represented by an EB (Existing Bus). These entries
188 * are created by calls to xpt_bus_register and deleted by calls to
189 * xpt_bus_deregister.
192 TAILQ_HEAD(, cam_et) et_entries;
193 TAILQ_ENTRY(cam_eb) links;
196 struct timeval last_reset; /* uptime of last reset */
198 #define CAM_EB_RUNQ_SCHEDULED 0x01
201 int counted_to_config; /* busses_to_config */
205 struct cam_periph *periph;
207 struct cam_et *target;
208 struct cam_ed *device;
211 struct xpt_quirk_entry {
212 struct scsi_inquiry_pattern inq_pat;
214 #define CAM_QUIRK_NOLUNS 0x01
215 #define CAM_QUIRK_NOSERIAL 0x02
216 #define CAM_QUIRK_HILUNS 0x04
217 #define CAM_QUIRK_NOHILUNS 0x08
222 static int cam_srch_hi = 0;
223 TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi);
224 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
225 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
226 sysctl_cam_search_luns, "I",
227 "allow search above LUN 7 for SCSI3 and greater devices");
229 #define CAM_SCSI2_MAXLUN 8
231 * If we're not quirked to search <= the first 8 luns
232 * and we are either quirked to search above lun 8,
233 * or we're > SCSI-2 and we've enabled hilun searching,
234 * or we're > SCSI-2 and the last lun was a success,
235 * we can look for luns above lun 8.
237 #define CAN_SRCH_HI_SPARSE(dv) \
238 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
239 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
240 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
242 #define CAN_SRCH_HI_DENSE(dv) \
243 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
244 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
245 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
253 u_int32_t xpt_generation;
255 /* number of high powered commands that can go through right now */
256 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
259 /* queue for handling async rescan requests. */
260 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
261 int ccb_scanq_running;
263 /* Registered busses */
264 TAILQ_HEAD(,cam_eb) xpt_busses;
265 u_int bus_generation;
267 struct intr_config_hook *xpt_config_hook;
269 struct lock xpt_topo_lock;
270 struct lock xpt_lock;
273 static const char quantum[] = "QUANTUM";
274 static const char sony[] = "SONY";
275 static const char west_digital[] = "WDIGTL";
276 static const char samsung[] = "SAMSUNG";
277 static const char seagate[] = "SEAGATE";
278 static const char microp[] = "MICROP";
280 static struct xpt_quirk_entry xpt_quirk_table[] =
283 /* Reports QUEUE FULL for temporary resource shortages */
284 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
285 /*quirks*/0, /*mintags*/24, /*maxtags*/32
288 /* Reports QUEUE FULL for temporary resource shortages */
289 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
290 /*quirks*/0, /*mintags*/24, /*maxtags*/32
293 /* Reports QUEUE FULL for temporary resource shortages */
294 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
295 /*quirks*/0, /*mintags*/24, /*maxtags*/32
298 /* Broken tagged queuing drive */
299 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
300 /*quirks*/0, /*mintags*/0, /*maxtags*/0
303 /* Broken tagged queuing drive */
304 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
305 /*quirks*/0, /*mintags*/0, /*maxtags*/0
308 /* Broken tagged queuing drive */
309 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
310 /*quirks*/0, /*mintags*/0, /*maxtags*/0
314 * Unfortunately, the Quantum Atlas III has the same
315 * problem as the Atlas II drives above.
316 * Reported by: "Johan Granlund" <johan@granlund.nu>
318 * For future reference, the drive with the problem was:
319 * QUANTUM QM39100TD-SW N1B0
321 * It's possible that Quantum will fix the problem in later
322 * firmware revisions. If that happens, the quirk entry
323 * will need to be made specific to the firmware revisions
327 /* Reports QUEUE FULL for temporary resource shortages */
328 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
329 /*quirks*/0, /*mintags*/24, /*maxtags*/32
333 * 18 Gig Atlas III, same problem as the 9G version.
334 * Reported by: Andre Albsmeier
335 * <andre.albsmeier@mchp.siemens.de>
337 * For future reference, the drive with the problem was:
338 * QUANTUM QM318000TD-S N491
340 /* Reports QUEUE FULL for temporary resource shortages */
341 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
342 /*quirks*/0, /*mintags*/24, /*maxtags*/32
346 * Broken tagged queuing drive
347 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
348 * and: Martin Renters <martin@tdc.on.ca>
350 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
351 /*quirks*/0, /*mintags*/0, /*maxtags*/0
354 * The Seagate Medalist Pro drives have very poor write
355 * performance with anything more than 2 tags.
357 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
358 * Drive: <SEAGATE ST36530N 1444>
360 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
361 * Drive: <SEAGATE ST34520W 1281>
363 * No one has actually reported that the 9G version
364 * (ST39140*) of the Medalist Pro has the same problem, but
365 * we're assuming that it does because the 4G and 6.5G
366 * versions of the drive are broken.
369 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
370 /*quirks*/0, /*mintags*/2, /*maxtags*/2
373 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
374 /*quirks*/0, /*mintags*/2, /*maxtags*/2
377 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
378 /*quirks*/0, /*mintags*/2, /*maxtags*/2
382 * Slow when tagged queueing is enabled. Write performance
383 * steadily drops off with more and more concurrent
384 * transactions. Best sequential write performance with
385 * tagged queueing turned off and write caching turned on.
388 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
389 * Drive: DCAS-34330 w/ "S65A" firmware.
391 * The drive with the problem had the "S65A" firmware
392 * revision, and has also been reported (by Stephen J.
393 * Roznowski <sjr@home.net>) for a drive with the "S61A"
396 * Although no one has reported problems with the 2 gig
397 * version of the DCAS drive, the assumption is that it
398 * has the same problems as the 4 gig version. Therefore
399 * this quirk entries disables tagged queueing for all
402 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
403 /*quirks*/0, /*mintags*/0, /*maxtags*/0
406 /* Broken tagged queuing drive */
407 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
408 /*quirks*/0, /*mintags*/0, /*maxtags*/0
411 /* Broken tagged queuing drive */
412 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
413 /*quirks*/0, /*mintags*/0, /*maxtags*/0
416 /* This does not support other than LUN 0 */
417 { T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
418 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
422 * Broken tagged queuing drive.
424 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
427 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
428 /*quirks*/0, /*mintags*/0, /*maxtags*/0
432 * Slow when tagged queueing is enabled. (1.5MB/sec versus
434 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
435 * Best performance with these drives is achieved with
436 * tagged queueing turned off, and write caching turned on.
438 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
439 /*quirks*/0, /*mintags*/0, /*maxtags*/0
443 * Slow when tagged queueing is enabled. (1.5MB/sec versus
445 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
446 * Best performance with these drives is achieved with
447 * tagged queueing turned off, and write caching turned on.
449 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
450 /*quirks*/0, /*mintags*/0, /*maxtags*/0
454 * Doesn't handle queue full condition correctly,
455 * so we need to limit maxtags to what the device
456 * can handle instead of determining this automatically.
458 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
459 /*quirks*/0, /*mintags*/2, /*maxtags*/32
462 /* Really only one LUN */
463 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
464 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
467 /* I can't believe we need a quirk for DPT volumes. */
468 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
470 /*mintags*/0, /*maxtags*/255
474 * Many Sony CDROM drives don't like multi-LUN probing.
476 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
477 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
481 * This drive doesn't like multiple LUN probing.
482 * Submitted by: Parag Patel <parag@cgt.com>
484 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
485 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
488 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
489 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
493 * The 8200 doesn't like multi-lun probing, and probably
494 * don't like serial number requests either.
497 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
500 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
504 * Let's try the same as above, but for a drive that says
505 * it's an IPL-6860 but is actually an EXB 8200.
508 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
511 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
515 * These Hitachi drives don't like multi-lun probing.
516 * The PR submitter has a DK319H, but says that the Linux
517 * kernel has a similar work-around for the DK312 and DK314,
518 * so all DK31* drives are quirked here.
520 * Submitted by: Paul Haddad <paul@pth.com>
522 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
523 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
527 * The Hitachi CJ series with J8A8 firmware apparantly has
528 * problems with tagged commands.
530 * Reported by: amagai@nue.org
532 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
533 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
537 * These are the large storage arrays.
538 * Submitted by: William Carrel <william.carrel@infospace.com>
540 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
541 CAM_QUIRK_HILUNS, 2, 1024
545 * This old revision of the TDC3600 is also SCSI-1, and
546 * hangs upon serial number probing.
549 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
552 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
556 * Would repond to all LUNs if asked for.
559 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
562 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
566 * Would repond to all LUNs if asked for.
569 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
572 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
575 /* Submitted by: Matthew Dodd <winter@jurai.net> */
576 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
577 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
580 /* Submitted by: Matthew Dodd <winter@jurai.net> */
581 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
582 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
585 /* TeraSolutions special settings for TRC-22 RAID */
586 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
587 /*quirks*/0, /*mintags*/55, /*maxtags*/255
590 /* Veritas Storage Appliance */
591 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
592 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
596 * Would respond to all LUNs. Device type and removable
597 * flag are jumper-selectable.
599 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
602 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
605 /* EasyRAID E5A aka. areca ARC-6010 */
606 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
607 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
610 { T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
611 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
614 /* Default tagged queuing parameters for all devices */
616 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
617 /*vendor*/"*", /*product*/"*", /*revision*/"*"
619 /*quirks*/0, /*mintags*/2, /*maxtags*/255
623 static const int xpt_quirk_table_size = NELEM(xpt_quirk_table);
627 DM_RET_FLAG_MASK = 0x0f,
630 DM_RET_DESCEND = 0x20,
632 DM_RET_ACTION_MASK = 0xf0
640 } xpt_traverse_depth;
642 struct xpt_traverse_config {
643 xpt_traverse_depth depth;
648 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
649 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
650 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
651 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
652 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
654 /* Transport layer configuration information */
655 static struct xpt_softc xsoftc;
657 /* Queues for our software interrupt handler */
658 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
659 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
660 static cam_simq_t cam_simq;
661 static struct spinlock cam_simq_spin;
663 struct cam_periph *xpt_periph;
665 static periph_init_t xpt_periph_init;
667 static periph_init_t probe_periph_init;
669 static struct periph_driver xpt_driver =
671 xpt_periph_init, "xpt",
672 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
675 static struct periph_driver probe_driver =
677 probe_periph_init, "probe",
678 TAILQ_HEAD_INITIALIZER(probe_driver.units)
681 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
682 PERIPHDRIVER_DECLARE(probe, probe_driver);
684 static d_open_t xptopen;
685 static d_close_t xptclose;
686 static d_ioctl_t xptioctl;
688 static struct dev_ops xpt_ops = {
695 static void dead_sim_action(struct cam_sim *sim, union ccb *ccb);
696 static void dead_sim_poll(struct cam_sim *sim);
698 /* Dummy SIM that is used when the real one has gone. */
699 static struct cam_sim cam_dead_sim;
700 static struct lock cam_dead_lock;
702 /* Storage for debugging datastructures */
704 struct cam_path *cam_dpath;
705 u_int32_t cam_dflags;
706 u_int32_t cam_debug_delay;
709 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
710 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
714 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
715 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
716 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
718 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
719 || defined(CAM_DEBUG_LUN)
721 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
722 || !defined(CAM_DEBUG_LUN)
723 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
725 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
726 #else /* !CAMDEBUG */
727 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
728 #endif /* CAMDEBUG */
729 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
731 /* Our boot-time initialization hook */
732 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
734 static moduledata_t cam_moduledata = {
736 cam_module_event_handler,
740 static int xpt_init(void *);
742 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
743 MODULE_VERSION(cam, 1);
746 static cam_status xpt_compile_path(struct cam_path *new_path,
747 struct cam_periph *perph,
749 target_id_t target_id,
752 static void xpt_release_path(struct cam_path *path);
754 static void xpt_async_bcast(struct async_list *async_head,
755 u_int32_t async_code,
756 struct cam_path *path,
758 static void xpt_dev_async(u_int32_t async_code,
760 struct cam_et *target,
761 struct cam_ed *device,
763 static path_id_t xptnextfreepathid(void);
764 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
765 static union ccb *xpt_get_ccb(struct cam_ed *device);
766 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
767 u_int32_t new_priority);
768 static void xpt_run_dev_allocq(struct cam_eb *bus);
769 static void xpt_run_dev_sendq(struct cam_eb *bus);
770 static timeout_t xpt_release_devq_timeout;
771 static void xpt_release_bus(struct cam_eb *bus);
772 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
774 static struct cam_et*
775 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
776 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
777 static struct cam_ed*
778 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
780 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
781 struct cam_ed *device);
782 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
783 static struct cam_eb*
784 xpt_find_bus(path_id_t path_id);
785 static struct cam_et*
786 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
787 static struct cam_ed*
788 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
789 static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
790 static void xpt_scan_lun(struct cam_periph *periph,
791 struct cam_path *path, cam_flags flags,
793 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
794 static xpt_busfunc_t xptconfigbuscountfunc;
795 static xpt_busfunc_t xptconfigfunc;
796 static void xpt_config(void *arg);
797 static xpt_devicefunc_t xptpassannouncefunc;
798 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
799 static void xpt_uncount_bus (struct cam_eb *bus);
800 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
801 static void xptpoll(struct cam_sim *sim);
802 static inthand2_t swi_cambio;
803 static void camisr(void *);
804 static void camisr_runqueue(struct cam_sim *);
805 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
806 u_int num_patterns, struct cam_eb *bus);
807 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
809 struct cam_ed *device);
810 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
812 struct cam_periph *periph);
813 static xpt_busfunc_t xptedtbusfunc;
814 static xpt_targetfunc_t xptedttargetfunc;
815 static xpt_devicefunc_t xptedtdevicefunc;
816 static xpt_periphfunc_t xptedtperiphfunc;
817 static xpt_pdrvfunc_t xptplistpdrvfunc;
818 static xpt_periphfunc_t xptplistperiphfunc;
819 static int xptedtmatch(struct ccb_dev_match *cdm);
820 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
821 static int xptbustraverse(struct cam_eb *start_bus,
822 xpt_busfunc_t *tr_func, void *arg);
823 static int xpttargettraverse(struct cam_eb *bus,
824 struct cam_et *start_target,
825 xpt_targetfunc_t *tr_func, void *arg);
826 static int xptdevicetraverse(struct cam_et *target,
827 struct cam_ed *start_device,
828 xpt_devicefunc_t *tr_func, void *arg);
829 static int xptperiphtraverse(struct cam_ed *device,
830 struct cam_periph *start_periph,
831 xpt_periphfunc_t *tr_func, void *arg);
832 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
833 xpt_pdrvfunc_t *tr_func, void *arg);
834 static int xptpdperiphtraverse(struct periph_driver **pdrv,
835 struct cam_periph *start_periph,
836 xpt_periphfunc_t *tr_func,
838 static xpt_busfunc_t xptdefbusfunc;
839 static xpt_targetfunc_t xptdeftargetfunc;
840 static xpt_devicefunc_t xptdefdevicefunc;
841 static xpt_periphfunc_t xptdefperiphfunc;
842 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
843 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
845 static xpt_devicefunc_t xptsetasyncfunc;
846 static xpt_busfunc_t xptsetasyncbusfunc;
847 static cam_status xptregister(struct cam_periph *periph,
849 static cam_status proberegister(struct cam_periph *periph,
851 static void probeschedule(struct cam_periph *probe_periph);
852 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
853 static void proberequestdefaultnegotiation(struct cam_periph *periph);
854 static int proberequestbackoff(struct cam_periph *periph,
855 struct cam_ed *device);
856 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
857 static void probecleanup(struct cam_periph *periph);
858 static void xpt_find_quirk(struct cam_ed *device);
859 static void xpt_devise_transport(struct cam_path *path);
860 static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
861 struct cam_ed *device,
863 static void xpt_toggle_tags(struct cam_path *path);
864 static void xpt_start_tags(struct cam_path *path);
865 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
867 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
869 static __inline int periph_is_queued(struct cam_periph *periph);
870 static __inline int device_is_alloc_queued(struct cam_ed *device);
871 static __inline int device_is_send_queued(struct cam_ed *device);
872 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
875 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
879 if (bus->sim->devq && dev->ccbq.devq_openings > 0) {
880 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
881 cam_ccbq_resize(&dev->ccbq,
882 dev->ccbq.dev_openings
883 + dev->ccbq.dev_active);
884 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
887 * The priority of a device waiting for CCB resources
888 * is that of the the highest priority peripheral driver
891 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
892 &dev->alloc_ccb_entry.pinfo,
893 CAMQ_GET_HEAD(&dev->drvq)->priority);
902 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
906 if (bus->sim->devq && dev->ccbq.dev_openings > 0) {
908 * The priority of a device waiting for controller
909 * resources is that of the the highest priority CCB
913 xpt_schedule_dev(&bus->sim->devq->send_queue,
914 &dev->send_ccb_entry.pinfo,
915 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
923 periph_is_queued(struct cam_periph *periph)
925 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
929 device_is_alloc_queued(struct cam_ed *device)
931 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
935 device_is_send_queued(struct cam_ed *device)
937 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
941 dev_allocq_is_runnable(struct cam_devq *devq)
945 * Have space to do more work.
946 * Allowed to do work.
948 return ((devq->alloc_queue.qfrozen_cnt == 0)
949 && (devq->alloc_queue.entries > 0)
950 && (devq->alloc_openings > 0));
954 xpt_periph_init(void)
956 make_dev(&xpt_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
960 probe_periph_init(void)
966 xptdone(struct cam_periph *periph, union ccb *done_ccb)
968 /* Caller will release the CCB */
969 wakeup(&done_ccb->ccb_h.cbfcnp);
973 xptopen(struct dev_open_args *ap)
975 cdev_t dev = ap->a_head.a_dev;
978 * Only allow read-write access.
980 if (((ap->a_oflags & FWRITE) == 0) || ((ap->a_oflags & FREAD) == 0))
984 * We don't allow nonblocking access.
986 if ((ap->a_oflags & O_NONBLOCK) != 0) {
987 kprintf("%s: can't do nonblocking access\n", devtoname(dev));
991 /* Mark ourselves open */
992 lockmgr(&xsoftc.xpt_lock, LK_EXCLUSIVE);
993 xsoftc.flags |= XPT_FLAG_OPEN;
994 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
1000 xptclose(struct dev_close_args *ap)
1003 /* Mark ourselves closed */
1004 lockmgr(&xsoftc.xpt_lock, LK_EXCLUSIVE);
1005 xsoftc.flags &= ~XPT_FLAG_OPEN;
1006 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
1012 * Don't automatically grab the xpt softc lock here even though this is going
1013 * through the xpt device. The xpt device is really just a back door for
1014 * accessing other devices and SIMs, so the right thing to do is to grab
1015 * the appropriate SIM lock once the bus/SIM is located.
1018 xptioctl(struct dev_ioctl_args *ap)
1026 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1027 * to accept CCB types that don't quite make sense to send through a
1028 * passthrough driver.
1030 case CAMIOCOMMAND: {
1035 inccb = (union ccb *)ap->a_data;
1037 bus = xpt_find_bus(inccb->ccb_h.path_id);
1043 switch(inccb->ccb_h.func_code) {
1046 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1047 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1056 ccb = xpt_alloc_ccb();
1058 CAM_SIM_LOCK(bus->sim);
1061 * Create a path using the bus, target, and lun the
1064 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1065 inccb->ccb_h.path_id,
1066 inccb->ccb_h.target_id,
1067 inccb->ccb_h.target_lun) !=
1070 CAM_SIM_UNLOCK(bus->sim);
1074 /* Ensure all of our fields are correct */
1075 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1076 inccb->ccb_h.pinfo.priority);
1077 xpt_merge_ccb(ccb, inccb);
1078 ccb->ccb_h.cbfcnp = xptdone;
1079 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1080 bcopy(ccb, inccb, sizeof(union ccb));
1081 xpt_free_path(ccb->ccb_h.path);
1083 CAM_SIM_UNLOCK(bus->sim);
1090 * This is an immediate CCB, so it's okay to
1091 * allocate it on the stack.
1094 CAM_SIM_LOCK(bus->sim);
1097 * Create a path using the bus, target, and lun the
1100 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1101 inccb->ccb_h.path_id,
1102 inccb->ccb_h.target_id,
1103 inccb->ccb_h.target_lun) !=
1106 CAM_SIM_UNLOCK(bus->sim);
1109 /* Ensure all of our fields are correct */
1110 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1111 inccb->ccb_h.pinfo.priority);
1112 xpt_merge_ccb(&ccb, inccb);
1113 ccb.ccb_h.cbfcnp = xptdone;
1115 CAM_SIM_UNLOCK(bus->sim);
1116 bcopy(&ccb, inccb, sizeof(union ccb));
1117 xpt_free_path(ccb.ccb_h.path);
1121 case XPT_DEV_MATCH: {
1122 struct cam_periph_map_info mapinfo;
1123 struct cam_path *old_path;
1126 * We can't deal with physical addresses for this
1127 * type of transaction.
1129 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1135 * Save this in case the caller had it set to
1136 * something in particular.
1138 old_path = inccb->ccb_h.path;
1141 * We really don't need a path for the matching
1142 * code. The path is needed because of the
1143 * debugging statements in xpt_action(). They
1144 * assume that the CCB has a valid path.
1146 inccb->ccb_h.path = xpt_periph->path;
1148 bzero(&mapinfo, sizeof(mapinfo));
1151 * Map the pattern and match buffers into kernel
1152 * virtual address space.
1154 error = cam_periph_mapmem(inccb, &mapinfo);
1157 inccb->ccb_h.path = old_path;
1162 * This is an immediate CCB, we can send it on directly.
1167 * Map the buffers back into user space.
1169 cam_periph_unmapmem(inccb, &mapinfo);
1171 inccb->ccb_h.path = old_path;
1180 xpt_release_bus(bus);
1184 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1185 * with the periphal driver name and unit name filled in. The other
1186 * fields don't really matter as input. The passthrough driver name
1187 * ("pass"), and unit number are passed back in the ccb. The current
1188 * device generation number, and the index into the device peripheral
1189 * driver list, and the status are also passed back. Note that
1190 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1191 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1192 * (or rather should be) impossible for the device peripheral driver
1193 * list to change since we look at the whole thing in one pass, and
1194 * we do it with lock protection.
1197 case CAMGETPASSTHRU: {
1199 struct cam_periph *periph;
1200 struct periph_driver **p_drv;
1203 u_int cur_generation;
1204 int base_periph_found;
1207 ccb = (union ccb *)ap->a_data;
1208 unit = ccb->cgdl.unit_number;
1209 name = ccb->cgdl.periph_name;
1211 * Every 100 devices, we want to drop our lock protection to
1212 * give the software interrupt handler a chance to run.
1213 * Most systems won't run into this check, but this should
1214 * avoid starvation in the software interrupt handler in
1219 ccb = (union ccb *)ap->a_data;
1221 base_periph_found = 0;
1224 * Sanity check -- make sure we don't get a null peripheral
1227 if (*ccb->cgdl.periph_name == '\0') {
1232 /* Keep the list from changing while we traverse it */
1233 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
1235 cur_generation = xsoftc.xpt_generation;
1237 /* first find our driver in the list of drivers */
1238 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) {
1239 if (strcmp((*p_drv)->driver_name, name) == 0)
1243 if (*p_drv == NULL) {
1244 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
1245 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1246 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1247 *ccb->cgdl.periph_name = '\0';
1248 ccb->cgdl.unit_number = 0;
1254 * Run through every peripheral instance of this driver
1255 * and check to see whether it matches the unit passed
1256 * in by the user. If it does, get out of the loops and
1257 * find the passthrough driver associated with that
1258 * peripheral driver.
1260 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) {
1262 if (periph->unit_number == unit) {
1264 } else if (--splbreaknum == 0) {
1265 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
1266 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
1268 if (cur_generation != xsoftc.xpt_generation)
1273 * If we found the peripheral driver that the user passed
1274 * in, go through all of the peripheral drivers for that
1275 * particular device and look for a passthrough driver.
1277 if (periph != NULL) {
1278 struct cam_ed *device;
1281 base_periph_found = 1;
1282 device = periph->path->device;
1283 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1285 periph = SLIST_NEXT(periph, periph_links), i++) {
1287 * Check to see whether we have a
1288 * passthrough device or not.
1290 if (strcmp(periph->periph_name, "pass") == 0) {
1292 * Fill in the getdevlist fields.
1294 strcpy(ccb->cgdl.periph_name,
1295 periph->periph_name);
1296 ccb->cgdl.unit_number =
1297 periph->unit_number;
1298 if (SLIST_NEXT(periph, periph_links))
1300 CAM_GDEVLIST_MORE_DEVS;
1303 CAM_GDEVLIST_LAST_DEVICE;
1304 ccb->cgdl.generation =
1306 ccb->cgdl.index = i;
1308 * Fill in some CCB header fields
1309 * that the user may want.
1311 ccb->ccb_h.path_id =
1312 periph->path->bus->path_id;
1313 ccb->ccb_h.target_id =
1314 periph->path->target->target_id;
1315 ccb->ccb_h.target_lun =
1316 periph->path->device->lun_id;
1317 ccb->ccb_h.status = CAM_REQ_CMP;
1324 * If the periph is null here, one of two things has
1325 * happened. The first possibility is that we couldn't
1326 * find the unit number of the particular peripheral driver
1327 * that the user is asking about. e.g. the user asks for
1328 * the passthrough driver for "da11". We find the list of
1329 * "da" peripherals all right, but there is no unit 11.
1330 * The other possibility is that we went through the list
1331 * of peripheral drivers attached to the device structure,
1332 * but didn't find one with the name "pass". Either way,
1333 * we return ENOENT, since we couldn't find something.
1335 if (periph == NULL) {
1336 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1337 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1338 *ccb->cgdl.periph_name = '\0';
1339 ccb->cgdl.unit_number = 0;
1342 * It is unfortunate that this is even necessary,
1343 * but there are many, many clueless users out there.
1344 * If this is true, the user is looking for the
1345 * passthrough driver, but doesn't have one in his
1348 if (base_periph_found == 1) {
1349 kprintf("xptioctl: pass driver is not in the "
1351 kprintf("xptioctl: put \"device pass\" in "
1352 "your kernel config file\n");
1355 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
1367 cam_module_event_handler(module_t mod, int what, void *arg)
1373 if ((error = xpt_init(NULL)) != 0)
1386 * Thread to handle asynchronous main-context requests.
1388 * This function is typically used by drivers to perform complex actions
1389 * such as bus scans and engineering requests in a main context instead
1390 * of an interrupt context.
1393 xpt_scanner_thread(void *dummy)
1396 struct cam_sim *sim;
1402 xsoftc.ccb_scanq_running = 1;
1403 while ((ccb = (void *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
1404 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h,
1408 sim = ccb->ccb_h.path->bus->sim;
1411 CAM_SIM_UNLOCK(sim);
1415 xsoftc.ccb_scanq_running = 0;
1416 tsleep_interlock(&xsoftc.ccb_scanq, 0);
1418 tsleep(&xsoftc.ccb_scanq, PINTERLOCKED, "ccb_scanq", 0);
1421 rel_mplock(); /* not reached */
1425 * Issue an asynchronous asction
1428 xpt_action_async(union ccb *ccb)
1431 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
1432 if (xsoftc.ccb_scanq_running == 0) {
1433 xsoftc.ccb_scanq_running = 1;
1434 wakeup(&xsoftc.ccb_scanq);
1440 /* Functions accessed by the peripheral drivers */
1442 xpt_init(void *dummy)
1444 struct cam_sim *xpt_sim;
1445 struct cam_path *path;
1446 struct cam_devq *devq;
1449 TAILQ_INIT(&xsoftc.xpt_busses);
1450 TAILQ_INIT(&cam_simq);
1451 TAILQ_INIT(&xsoftc.ccb_scanq);
1452 STAILQ_INIT(&xsoftc.highpowerq);
1453 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
1455 spin_init(&cam_simq_spin, "cam_simq_spin");
1456 lockinit(&xsoftc.xpt_lock, "XPT lock", 0, LK_CANRECURSE);
1457 lockinit(&xsoftc.xpt_topo_lock, "XPT topology lock", 0, LK_CANRECURSE);
1459 SLIST_INIT(&cam_dead_sim.ccb_freeq);
1460 TAILQ_INIT(&cam_dead_sim.sim_doneq);
1461 spin_init(&cam_dead_sim.sim_spin, "cam_dead_sim");
1462 cam_dead_sim.sim_action = dead_sim_action;
1463 cam_dead_sim.sim_poll = dead_sim_poll;
1464 cam_dead_sim.sim_name = "dead_sim";
1465 cam_dead_sim.lock = &cam_dead_lock;
1466 lockinit(&cam_dead_lock, "XPT dead_sim lock", 0, LK_CANRECURSE);
1467 cam_dead_sim.flags |= CAM_SIM_DEREGISTERED;
1470 * The xpt layer is, itself, the equivelent of a SIM.
1471 * Allow 16 ccbs in the ccb pool for it. This should
1472 * give decent parallelism when we probe busses and
1473 * perform other XPT functions.
1475 devq = cam_simq_alloc(16);
1476 xpt_sim = cam_sim_alloc(xptaction,
1481 /*lock*/&xsoftc.xpt_lock,
1482 /*max_dev_transactions*/0,
1483 /*max_tagged_dev_transactions*/0,
1485 cam_simq_release(devq);
1486 if (xpt_sim == NULL)
1489 xpt_sim->max_ccbs = 16;
1491 lockmgr(&xsoftc.xpt_lock, LK_EXCLUSIVE);
1492 if ((status = xpt_bus_register(xpt_sim, /*bus #*/0)) != CAM_SUCCESS) {
1493 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
1494 kprintf("xpt_init: xpt_bus_register failed with status %#x,"
1495 " failing attach\n", status);
1500 * Looking at the XPT from the SIM layer, the XPT is
1501 * the equivelent of a peripheral driver. Allocate
1502 * a peripheral driver entry for us.
1504 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1505 CAM_TARGET_WILDCARD,
1506 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1507 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
1508 kprintf("xpt_init: xpt_create_path failed with status %#x,"
1509 " failing attach\n", status);
1513 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1514 path, NULL, 0, xpt_sim);
1515 xpt_free_path(path);
1517 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
1520 * Register a callback for when interrupts are enabled.
1522 xsoftc.xpt_config_hook = kmalloc(sizeof(struct intr_config_hook),
1523 M_CAMXPT, M_INTWAIT | M_ZERO);
1524 xsoftc.xpt_config_hook->ich_func = xpt_config;
1525 xsoftc.xpt_config_hook->ich_desc = "xpt";
1526 xsoftc.xpt_config_hook->ich_order = 1000;
1527 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
1528 kfree (xsoftc.xpt_config_hook, M_CAMXPT);
1529 kprintf("xpt_init: config_intrhook_establish failed "
1530 "- failing attach\n");
1533 /* fire up rescan thread */
1534 if (kthread_create(xpt_scanner_thread, NULL, NULL, "xpt_thrd")) {
1535 kprintf("xpt_init: failed to create rescan thread\n");
1537 /* Install our software interrupt handlers */
1538 register_swi_mp(SWI_CAMBIO, swi_cambio, NULL, "swi_cambio", NULL, -1);
1544 xptregister(struct cam_periph *periph, void *arg)
1546 struct cam_sim *xpt_sim;
1548 if (periph == NULL) {
1549 kprintf("xptregister: periph was NULL!!\n");
1550 return(CAM_REQ_CMP_ERR);
1553 xpt_sim = (struct cam_sim *)arg;
1554 xpt_sim->softc = periph;
1555 xpt_periph = periph;
1556 periph->softc = NULL;
1558 return(CAM_REQ_CMP);
1562 xpt_add_periph(struct cam_periph *periph)
1564 struct cam_ed *device;
1566 struct periph_list *periph_head;
1568 sim_lock_assert_owned(periph->sim->lock);
1570 device = periph->path->device;
1572 periph_head = &device->periphs;
1574 status = CAM_REQ_CMP;
1576 if (device != NULL) {
1578 * Make room for this peripheral
1579 * so it will fit in the queue
1580 * when it's scheduled to run
1582 status = camq_resize(&device->drvq,
1583 device->drvq.array_size + 1);
1585 device->generation++;
1587 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1590 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
1591 xsoftc.xpt_generation++;
1592 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
1598 xpt_remove_periph(struct cam_periph *periph)
1600 struct cam_ed *device;
1602 sim_lock_assert_owned(periph->sim->lock);
1604 device = periph->path->device;
1606 if (device != NULL) {
1607 struct periph_list *periph_head;
1609 periph_head = &device->periphs;
1611 /* Release the slot for this peripheral */
1612 camq_resize(&device->drvq, device->drvq.array_size - 1);
1614 device->generation++;
1616 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1619 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
1620 xsoftc.xpt_generation++;
1621 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
1625 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1627 struct ccb_pathinq cpi;
1628 struct ccb_trans_settings cts;
1629 struct cam_path *path;
1634 sim_lock_assert_owned(periph->sim->lock);
1636 path = periph->path;
1638 /* Report basic attachment and inquiry data */
1639 kprintf("%s%d at %s%d bus %d target %d lun %d\n",
1640 periph->periph_name, periph->unit_number,
1641 path->bus->sim->sim_name,
1642 path->bus->sim->unit_number,
1643 path->bus->sim->bus_id,
1644 path->target->target_id,
1645 path->device->lun_id);
1646 kprintf("%s%d: ", periph->periph_name, periph->unit_number);
1647 scsi_print_inquiry(&path->device->inq_data);
1649 /* Report serial number */
1650 if (path->device->serial_num_len > 0) {
1651 /* Don't wrap the screen - print only the first 60 chars */
1652 kprintf("%s%d: Serial Number %.60s\n", periph->periph_name,
1653 periph->unit_number, path->device->serial_num);
1656 /* Acquire and report transfer speed */
1657 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1658 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1659 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1660 xpt_action((union ccb*)&cts);
1661 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1665 /* Ask the SIM for its base transfer speed */
1666 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1667 cpi.ccb_h.func_code = XPT_PATH_INQ;
1668 xpt_action((union ccb *)&cpi);
1670 speed = cpi.base_transfer_speed;
1672 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1673 struct ccb_trans_settings_spi *spi;
1675 spi = &cts.xport_specific.spi;
1676 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1677 && spi->sync_offset != 0) {
1678 freq = scsi_calc_syncsrate(spi->sync_period);
1682 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1683 speed *= (0x01 << spi->bus_width);
1685 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1686 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1687 if (fc->valid & CTS_FC_VALID_SPEED) {
1688 speed = fc->bitrate;
1692 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
1693 struct ccb_trans_settings_sas *sas = &cts.xport_specific.sas;
1694 if (sas->valid & CTS_SAS_VALID_SPEED) {
1695 speed = sas->bitrate;
1701 kprintf("%s%d: %d.%03dMB/s transfers",
1702 periph->periph_name, periph->unit_number,
1705 kprintf("%s%d: %dKB/s transfers", periph->periph_name,
1706 periph->unit_number, speed);
1708 /* Report additional information about SPI connections */
1709 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1710 struct ccb_trans_settings_spi *spi;
1712 spi = &cts.xport_specific.spi;
1714 kprintf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1716 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1720 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1721 && spi->bus_width > 0) {
1727 kprintf("%dbit)", 8 * (0x01 << spi->bus_width));
1728 } else if (freq != 0) {
1732 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1733 struct ccb_trans_settings_fc *fc;
1735 fc = &cts.xport_specific.fc;
1736 if (fc->valid & CTS_FC_VALID_WWNN)
1737 kprintf(" WWNN 0x%llx", (long long) fc->wwnn);
1738 if (fc->valid & CTS_FC_VALID_WWPN)
1739 kprintf(" WWPN 0x%llx", (long long) fc->wwpn);
1740 if (fc->valid & CTS_FC_VALID_PORT)
1741 kprintf(" PortID 0x%x", fc->port);
1744 if (path->device->inq_flags & SID_CmdQue
1745 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1746 kprintf("\n%s%d: Command Queueing Enabled",
1747 periph->periph_name, periph->unit_number);
1752 * We only want to print the caller's announce string if they've
1755 if (announce_string != NULL)
1756 kprintf("%s%d: %s\n", periph->periph_name,
1757 periph->unit_number, announce_string);
1760 static dev_match_ret
1761 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1764 dev_match_ret retval;
1767 retval = DM_RET_NONE;
1770 * If we aren't given something to match against, that's an error.
1773 return(DM_RET_ERROR);
1776 * If there are no match entries, then this bus matches no
1779 if ((patterns == NULL) || (num_patterns == 0))
1780 return(DM_RET_DESCEND | DM_RET_COPY);
1782 for (i = 0; i < num_patterns; i++) {
1783 struct bus_match_pattern *cur_pattern;
1786 * If the pattern in question isn't for a bus node, we
1787 * aren't interested. However, we do indicate to the
1788 * calling routine that we should continue descending the
1789 * tree, since the user wants to match against lower-level
1792 if (patterns[i].type != DEV_MATCH_BUS) {
1793 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1794 retval |= DM_RET_DESCEND;
1798 cur_pattern = &patterns[i].pattern.bus_pattern;
1801 * If they want to match any bus node, we give them any
1804 if (cur_pattern->flags == BUS_MATCH_ANY) {
1805 /* set the copy flag */
1806 retval |= DM_RET_COPY;
1809 * If we've already decided on an action, go ahead
1812 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1817 * Not sure why someone would do this...
1819 if (cur_pattern->flags == BUS_MATCH_NONE)
1822 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1823 && (cur_pattern->path_id != bus->path_id))
1826 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1827 && (cur_pattern->bus_id != bus->sim->bus_id))
1830 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1831 && (cur_pattern->unit_number != bus->sim->unit_number))
1834 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1835 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1840 * If we get to this point, the user definitely wants
1841 * information on this bus. So tell the caller to copy the
1844 retval |= DM_RET_COPY;
1847 * If the return action has been set to descend, then we
1848 * know that we've already seen a non-bus matching
1849 * expression, therefore we need to further descend the tree.
1850 * This won't change by continuing around the loop, so we
1851 * go ahead and return. If we haven't seen a non-bus
1852 * matching expression, we keep going around the loop until
1853 * we exhaust the matching expressions. We'll set the stop
1854 * flag once we fall out of the loop.
1856 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1861 * If the return action hasn't been set to descend yet, that means
1862 * we haven't seen anything other than bus matching patterns. So
1863 * tell the caller to stop descending the tree -- the user doesn't
1864 * want to match against lower level tree elements.
1866 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1867 retval |= DM_RET_STOP;
1872 static dev_match_ret
1873 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1874 struct cam_ed *device)
1876 dev_match_ret retval;
1879 retval = DM_RET_NONE;
1882 * If we aren't given something to match against, that's an error.
1885 return(DM_RET_ERROR);
1888 * If there are no match entries, then this device matches no
1891 if ((patterns == NULL) || (num_patterns == 0))
1892 return(DM_RET_DESCEND | DM_RET_COPY);
1894 for (i = 0; i < num_patterns; i++) {
1895 struct device_match_pattern *cur_pattern;
1898 * If the pattern in question isn't for a device node, we
1899 * aren't interested.
1901 if (patterns[i].type != DEV_MATCH_DEVICE) {
1902 if ((patterns[i].type == DEV_MATCH_PERIPH)
1903 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1904 retval |= DM_RET_DESCEND;
1908 cur_pattern = &patterns[i].pattern.device_pattern;
1911 * If they want to match any device node, we give them any
1914 if (cur_pattern->flags == DEV_MATCH_ANY) {
1915 /* set the copy flag */
1916 retval |= DM_RET_COPY;
1920 * If we've already decided on an action, go ahead
1923 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1928 * Not sure why someone would do this...
1930 if (cur_pattern->flags == DEV_MATCH_NONE)
1933 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1934 && (cur_pattern->path_id != device->target->bus->path_id))
1937 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1938 && (cur_pattern->target_id != device->target->target_id))
1941 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1942 && (cur_pattern->target_lun != device->lun_id))
1945 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1946 && (cam_quirkmatch((caddr_t)&device->inq_data,
1947 (caddr_t)&cur_pattern->inq_pat,
1948 1, sizeof(cur_pattern->inq_pat),
1949 scsi_static_inquiry_match) == NULL))
1953 * If we get to this point, the user definitely wants
1954 * information on this device. So tell the caller to copy
1957 retval |= DM_RET_COPY;
1960 * If the return action has been set to descend, then we
1961 * know that we've already seen a peripheral matching
1962 * expression, therefore we need to further descend the tree.
1963 * This won't change by continuing around the loop, so we
1964 * go ahead and return. If we haven't seen a peripheral
1965 * matching expression, we keep going around the loop until
1966 * we exhaust the matching expressions. We'll set the stop
1967 * flag once we fall out of the loop.
1969 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1974 * If the return action hasn't been set to descend yet, that means
1975 * we haven't seen any peripheral matching patterns. So tell the
1976 * caller to stop descending the tree -- the user doesn't want to
1977 * match against lower level tree elements.
1979 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1980 retval |= DM_RET_STOP;
1986 * Match a single peripheral against any number of match patterns.
1988 static dev_match_ret
1989 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1990 struct cam_periph *periph)
1992 dev_match_ret retval;
1996 * If we aren't given something to match against, that's an error.
1999 return(DM_RET_ERROR);
2002 * If there are no match entries, then this peripheral matches no
2005 if ((patterns == NULL) || (num_patterns == 0))
2006 return(DM_RET_STOP | DM_RET_COPY);
2009 * There aren't any nodes below a peripheral node, so there's no
2010 * reason to descend the tree any further.
2012 retval = DM_RET_STOP;
2014 for (i = 0; i < num_patterns; i++) {
2015 struct periph_match_pattern *cur_pattern;
2018 * If the pattern in question isn't for a peripheral, we
2019 * aren't interested.
2021 if (patterns[i].type != DEV_MATCH_PERIPH)
2024 cur_pattern = &patterns[i].pattern.periph_pattern;
2027 * If they want to match on anything, then we will do so.
2029 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2030 /* set the copy flag */
2031 retval |= DM_RET_COPY;
2034 * We've already set the return action to stop,
2035 * since there are no nodes below peripherals in
2042 * Not sure why someone would do this...
2044 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2047 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2048 && (cur_pattern->path_id != periph->path->bus->path_id))
2052 * For the target and lun id's, we have to make sure the
2053 * target and lun pointers aren't NULL. The xpt peripheral
2054 * has a wildcard target and device.
2056 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2057 && ((periph->path->target == NULL)
2058 ||(cur_pattern->target_id != periph->path->target->target_id)))
2061 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2062 && ((periph->path->device == NULL)
2063 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2066 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2067 && (cur_pattern->unit_number != periph->unit_number))
2070 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2071 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2076 * If we get to this point, the user definitely wants
2077 * information on this peripheral. So tell the caller to
2078 * copy the data out.
2080 retval |= DM_RET_COPY;
2083 * The return action has already been set to stop, since
2084 * peripherals don't have any nodes below them in the EDT.
2090 * If we get to this point, the peripheral that was passed in
2091 * doesn't match any of the patterns.
2097 xptedtbusfunc(struct cam_eb *bus, void *arg)
2099 struct ccb_dev_match *cdm;
2100 dev_match_ret retval;
2102 cdm = (struct ccb_dev_match *)arg;
2105 * If our position is for something deeper in the tree, that means
2106 * that we've already seen this node. So, we keep going down.
2108 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2109 && (cdm->pos.cookie.bus == bus)
2110 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2111 && (cdm->pos.cookie.target != NULL))
2112 retval = DM_RET_DESCEND;
2114 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2117 * If we got an error, bail out of the search.
2119 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2120 cdm->status = CAM_DEV_MATCH_ERROR;
2125 * If the copy flag is set, copy this bus out.
2127 if (retval & DM_RET_COPY) {
2130 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2131 sizeof(struct dev_match_result));
2134 * If we don't have enough space to put in another
2135 * match result, save our position and tell the
2136 * user there are more devices to check.
2138 if (spaceleft < sizeof(struct dev_match_result)) {
2139 bzero(&cdm->pos, sizeof(cdm->pos));
2140 cdm->pos.position_type =
2141 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2143 cdm->pos.cookie.bus = bus;
2144 cdm->pos.generations[CAM_BUS_GENERATION]=
2145 xsoftc.bus_generation;
2146 cdm->status = CAM_DEV_MATCH_MORE;
2149 j = cdm->num_matches;
2151 cdm->matches[j].type = DEV_MATCH_BUS;
2152 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2153 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2154 cdm->matches[j].result.bus_result.unit_number =
2155 bus->sim->unit_number;
2156 strncpy(cdm->matches[j].result.bus_result.dev_name,
2157 bus->sim->sim_name, DEV_IDLEN);
2161 * If the user is only interested in busses, there's no
2162 * reason to descend to the next level in the tree.
2164 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2168 * If there is a target generation recorded, check it to
2169 * make sure the target list hasn't changed.
2171 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2172 && (bus == cdm->pos.cookie.bus)
2173 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2174 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2175 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2177 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2181 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2182 && (cdm->pos.cookie.bus == bus)
2183 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2184 && (cdm->pos.cookie.target != NULL))
2185 return(xpttargettraverse(bus,
2186 (struct cam_et *)cdm->pos.cookie.target,
2187 xptedttargetfunc, arg));
2189 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2193 xptedttargetfunc(struct cam_et *target, void *arg)
2195 struct ccb_dev_match *cdm;
2197 cdm = (struct ccb_dev_match *)arg;
2200 * If there is a device list generation recorded, check it to
2201 * make sure the device list hasn't changed.
2203 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2204 && (cdm->pos.cookie.bus == target->bus)
2205 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2206 && (cdm->pos.cookie.target == target)
2207 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2208 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2209 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2210 target->generation)) {
2211 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2215 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2216 && (cdm->pos.cookie.bus == target->bus)
2217 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2218 && (cdm->pos.cookie.target == target)
2219 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2220 && (cdm->pos.cookie.device != NULL))
2221 return(xptdevicetraverse(target,
2222 (struct cam_ed *)cdm->pos.cookie.device,
2223 xptedtdevicefunc, arg));
2225 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2229 xptedtdevicefunc(struct cam_ed *device, void *arg)
2232 struct ccb_dev_match *cdm;
2233 dev_match_ret retval;
2235 cdm = (struct ccb_dev_match *)arg;
2238 * If our position is for something deeper in the tree, that means
2239 * that we've already seen this node. So, we keep going down.
2241 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2242 && (cdm->pos.cookie.device == device)
2243 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2244 && (cdm->pos.cookie.periph != NULL))
2245 retval = DM_RET_DESCEND;
2247 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2250 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2251 cdm->status = CAM_DEV_MATCH_ERROR;
2256 * If the copy flag is set, copy this device out.
2258 if (retval & DM_RET_COPY) {
2261 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2262 sizeof(struct dev_match_result));
2265 * If we don't have enough space to put in another
2266 * match result, save our position and tell the
2267 * user there are more devices to check.
2269 if (spaceleft < sizeof(struct dev_match_result)) {
2270 bzero(&cdm->pos, sizeof(cdm->pos));
2271 cdm->pos.position_type =
2272 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2273 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2275 cdm->pos.cookie.bus = device->target->bus;
2276 cdm->pos.generations[CAM_BUS_GENERATION]=
2277 xsoftc.bus_generation;
2278 cdm->pos.cookie.target = device->target;
2279 cdm->pos.generations[CAM_TARGET_GENERATION] =
2280 device->target->bus->generation;
2281 cdm->pos.cookie.device = device;
2282 cdm->pos.generations[CAM_DEV_GENERATION] =
2283 device->target->generation;
2284 cdm->status = CAM_DEV_MATCH_MORE;
2287 j = cdm->num_matches;
2289 cdm->matches[j].type = DEV_MATCH_DEVICE;
2290 cdm->matches[j].result.device_result.path_id =
2291 device->target->bus->path_id;
2292 cdm->matches[j].result.device_result.target_id =
2293 device->target->target_id;
2294 cdm->matches[j].result.device_result.target_lun =
2296 bcopy(&device->inq_data,
2297 &cdm->matches[j].result.device_result.inq_data,
2298 sizeof(struct scsi_inquiry_data));
2300 /* Let the user know whether this device is unconfigured */
2301 if (device->flags & CAM_DEV_UNCONFIGURED)
2302 cdm->matches[j].result.device_result.flags =
2303 DEV_RESULT_UNCONFIGURED;
2305 cdm->matches[j].result.device_result.flags =
2310 * If the user isn't interested in peripherals, don't descend
2311 * the tree any further.
2313 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2317 * If there is a peripheral list generation recorded, make sure
2318 * it hasn't changed.
2320 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2321 && (device->target->bus == cdm->pos.cookie.bus)
2322 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2323 && (device->target == cdm->pos.cookie.target)
2324 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2325 && (device == cdm->pos.cookie.device)
2326 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2327 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2328 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2329 device->generation)){
2330 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2334 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2335 && (cdm->pos.cookie.bus == device->target->bus)
2336 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2337 && (cdm->pos.cookie.target == device->target)
2338 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2339 && (cdm->pos.cookie.device == device)
2340 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2341 && (cdm->pos.cookie.periph != NULL))
2342 return(xptperiphtraverse(device,
2343 (struct cam_periph *)cdm->pos.cookie.periph,
2344 xptedtperiphfunc, arg));
2346 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2350 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2352 struct ccb_dev_match *cdm;
2353 dev_match_ret retval;
2355 cdm = (struct ccb_dev_match *)arg;
2357 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2359 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2360 cdm->status = CAM_DEV_MATCH_ERROR;
2365 * If the copy flag is set, copy this peripheral out.
2367 if (retval & DM_RET_COPY) {
2370 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2371 sizeof(struct dev_match_result));
2374 * If we don't have enough space to put in another
2375 * match result, save our position and tell the
2376 * user there are more devices to check.
2378 if (spaceleft < sizeof(struct dev_match_result)) {
2379 bzero(&cdm->pos, sizeof(cdm->pos));
2380 cdm->pos.position_type =
2381 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2382 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2385 cdm->pos.cookie.bus = periph->path->bus;
2386 cdm->pos.generations[CAM_BUS_GENERATION]=
2387 xsoftc.bus_generation;
2388 cdm->pos.cookie.target = periph->path->target;
2389 cdm->pos.generations[CAM_TARGET_GENERATION] =
2390 periph->path->bus->generation;
2391 cdm->pos.cookie.device = periph->path->device;
2392 cdm->pos.generations[CAM_DEV_GENERATION] =
2393 periph->path->target->generation;
2394 cdm->pos.cookie.periph = periph;
2395 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2396 periph->path->device->generation;
2397 cdm->status = CAM_DEV_MATCH_MORE;
2401 j = cdm->num_matches;
2403 cdm->matches[j].type = DEV_MATCH_PERIPH;
2404 cdm->matches[j].result.periph_result.path_id =
2405 periph->path->bus->path_id;
2406 cdm->matches[j].result.periph_result.target_id =
2407 periph->path->target->target_id;
2408 cdm->matches[j].result.periph_result.target_lun =
2409 periph->path->device->lun_id;
2410 cdm->matches[j].result.periph_result.unit_number =
2411 periph->unit_number;
2412 strncpy(cdm->matches[j].result.periph_result.periph_name,
2413 periph->periph_name, DEV_IDLEN);
2420 xptedtmatch(struct ccb_dev_match *cdm)
2424 cdm->num_matches = 0;
2427 * Check the bus list generation. If it has changed, the user
2428 * needs to reset everything and start over.
2430 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2431 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2432 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
2433 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2437 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2438 && (cdm->pos.cookie.bus != NULL))
2439 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2440 xptedtbusfunc, cdm);
2442 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2445 * If we get back 0, that means that we had to stop before fully
2446 * traversing the EDT. It also means that one of the subroutines
2447 * has set the status field to the proper value. If we get back 1,
2448 * we've fully traversed the EDT and copied out any matching entries.
2451 cdm->status = CAM_DEV_MATCH_LAST;
2457 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2459 struct ccb_dev_match *cdm;
2461 cdm = (struct ccb_dev_match *)arg;
2463 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2464 && (cdm->pos.cookie.pdrv == pdrv)
2465 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2466 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2467 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2468 (*pdrv)->generation)) {
2469 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2473 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2474 && (cdm->pos.cookie.pdrv == pdrv)
2475 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2476 && (cdm->pos.cookie.periph != NULL))
2477 return(xptpdperiphtraverse(pdrv,
2478 (struct cam_periph *)cdm->pos.cookie.periph,
2479 xptplistperiphfunc, arg));
2481 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2485 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2487 struct ccb_dev_match *cdm;
2488 dev_match_ret retval;
2490 cdm = (struct ccb_dev_match *)arg;
2492 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2494 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2495 cdm->status = CAM_DEV_MATCH_ERROR;
2500 * If the copy flag is set, copy this peripheral out.
2502 if (retval & DM_RET_COPY) {
2505 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2506 sizeof(struct dev_match_result));
2509 * If we don't have enough space to put in another
2510 * match result, save our position and tell the
2511 * user there are more devices to check.
2513 if (spaceleft < sizeof(struct dev_match_result)) {
2514 struct periph_driver **pdrv;
2517 bzero(&cdm->pos, sizeof(cdm->pos));
2518 cdm->pos.position_type =
2519 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2523 * This may look a bit non-sensical, but it is
2524 * actually quite logical. There are very few
2525 * peripheral drivers, and bloating every peripheral
2526 * structure with a pointer back to its parent
2527 * peripheral driver linker set entry would cost
2528 * more in the long run than doing this quick lookup.
2530 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2531 if (strcmp((*pdrv)->driver_name,
2532 periph->periph_name) == 0)
2536 if (*pdrv == NULL) {
2537 cdm->status = CAM_DEV_MATCH_ERROR;
2541 cdm->pos.cookie.pdrv = pdrv;
2543 * The periph generation slot does double duty, as
2544 * does the periph pointer slot. They are used for
2545 * both edt and pdrv lookups and positioning.
2547 cdm->pos.cookie.periph = periph;
2548 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2549 (*pdrv)->generation;
2550 cdm->status = CAM_DEV_MATCH_MORE;
2554 j = cdm->num_matches;
2556 cdm->matches[j].type = DEV_MATCH_PERIPH;
2557 cdm->matches[j].result.periph_result.path_id =
2558 periph->path->bus->path_id;
2561 * The transport layer peripheral doesn't have a target or
2564 if (periph->path->target)
2565 cdm->matches[j].result.periph_result.target_id =
2566 periph->path->target->target_id;
2568 cdm->matches[j].result.periph_result.target_id = -1;
2570 if (periph->path->device)
2571 cdm->matches[j].result.periph_result.target_lun =
2572 periph->path->device->lun_id;
2574 cdm->matches[j].result.periph_result.target_lun = -1;
2576 cdm->matches[j].result.periph_result.unit_number =
2577 periph->unit_number;
2578 strncpy(cdm->matches[j].result.periph_result.periph_name,
2579 periph->periph_name, DEV_IDLEN);
2586 xptperiphlistmatch(struct ccb_dev_match *cdm)
2590 cdm->num_matches = 0;
2593 * At this point in the edt traversal function, we check the bus
2594 * list generation to make sure that no busses have been added or
2595 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2596 * For the peripheral driver list traversal function, however, we
2597 * don't have to worry about new peripheral driver types coming or
2598 * going; they're in a linker set, and therefore can't change
2599 * without a recompile.
2602 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2603 && (cdm->pos.cookie.pdrv != NULL))
2604 ret = xptpdrvtraverse(
2605 (struct periph_driver **)cdm->pos.cookie.pdrv,
2606 xptplistpdrvfunc, cdm);
2608 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2611 * If we get back 0, that means that we had to stop before fully
2612 * traversing the peripheral driver tree. It also means that one of
2613 * the subroutines has set the status field to the proper value. If
2614 * we get back 1, we've fully traversed the EDT and copied out any
2618 cdm->status = CAM_DEV_MATCH_LAST;
2624 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2626 struct cam_eb *bus, *next_bus;
2631 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
2632 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2635 next_bus = TAILQ_NEXT(bus, links);
2637 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
2638 CAM_SIM_LOCK(bus->sim);
2639 retval = tr_func(bus, arg);
2640 CAM_SIM_UNLOCK(bus->sim);
2643 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
2645 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
2651 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2652 xpt_targetfunc_t *tr_func, void *arg)
2654 struct cam_et *target, *next_target;
2658 for (target = (start_target ? start_target :
2659 TAILQ_FIRST(&bus->et_entries));
2660 target != NULL; target = next_target) {
2662 next_target = TAILQ_NEXT(target, links);
2664 retval = tr_func(target, arg);
2674 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2675 xpt_devicefunc_t *tr_func, void *arg)
2677 struct cam_ed *device, *next_device;
2681 for (device = (start_device ? start_device :
2682 TAILQ_FIRST(&target->ed_entries));
2684 device = next_device) {
2686 next_device = TAILQ_NEXT(device, links);
2688 retval = tr_func(device, arg);
2698 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2699 xpt_periphfunc_t *tr_func, void *arg)
2701 struct cam_periph *periph, *next_periph;
2706 for (periph = (start_periph ? start_periph :
2707 SLIST_FIRST(&device->periphs));
2709 periph = next_periph) {
2711 next_periph = SLIST_NEXT(periph, periph_links);
2713 retval = tr_func(periph, arg);
2722 xptpdrvtraverse(struct periph_driver **start_pdrv,
2723 xpt_pdrvfunc_t *tr_func, void *arg)
2725 struct periph_driver **pdrv;
2731 * We don't traverse the peripheral driver list like we do the
2732 * other lists, because it is a linker set, and therefore cannot be
2733 * changed during runtime. If the peripheral driver list is ever
2734 * re-done to be something other than a linker set (i.e. it can
2735 * change while the system is running), the list traversal should
2736 * be modified to work like the other traversal functions.
2738 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2739 *pdrv != NULL; pdrv++) {
2740 retval = tr_func(pdrv, arg);
2750 xptpdperiphtraverse(struct periph_driver **pdrv,
2751 struct cam_periph *start_periph,
2752 xpt_periphfunc_t *tr_func, void *arg)
2754 struct cam_periph *periph, *next_periph;
2759 for (periph = (start_periph ? start_periph :
2760 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2761 periph = next_periph) {
2763 next_periph = TAILQ_NEXT(periph, unit_links);
2765 retval = tr_func(periph, arg);
2773 xptdefbusfunc(struct cam_eb *bus, void *arg)
2775 struct xpt_traverse_config *tr_config;
2777 tr_config = (struct xpt_traverse_config *)arg;
2779 if (tr_config->depth == XPT_DEPTH_BUS) {
2780 xpt_busfunc_t *tr_func;
2782 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2784 return(tr_func(bus, tr_config->tr_arg));
2786 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2790 xptdeftargetfunc(struct cam_et *target, void *arg)
2792 struct xpt_traverse_config *tr_config;
2794 tr_config = (struct xpt_traverse_config *)arg;
2796 if (tr_config->depth == XPT_DEPTH_TARGET) {
2797 xpt_targetfunc_t *tr_func;
2799 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2801 return(tr_func(target, tr_config->tr_arg));
2803 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2807 xptdefdevicefunc(struct cam_ed *device, void *arg)
2809 struct xpt_traverse_config *tr_config;
2811 tr_config = (struct xpt_traverse_config *)arg;
2813 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2814 xpt_devicefunc_t *tr_func;
2816 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2818 return(tr_func(device, tr_config->tr_arg));
2820 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2824 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2826 struct xpt_traverse_config *tr_config;
2827 xpt_periphfunc_t *tr_func;
2829 tr_config = (struct xpt_traverse_config *)arg;
2831 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2834 * Unlike the other default functions, we don't check for depth
2835 * here. The peripheral driver level is the last level in the EDT,
2836 * so if we're here, we should execute the function in question.
2838 return(tr_func(periph, tr_config->tr_arg));
2842 * Execute the given function for every bus in the EDT.
2845 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2847 struct xpt_traverse_config tr_config;
2849 tr_config.depth = XPT_DEPTH_BUS;
2850 tr_config.tr_func = tr_func;
2851 tr_config.tr_arg = arg;
2853 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2857 * Execute the given function for every device in the EDT.
2860 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2862 struct xpt_traverse_config tr_config;
2864 tr_config.depth = XPT_DEPTH_DEVICE;
2865 tr_config.tr_func = tr_func;
2866 tr_config.tr_arg = arg;
2868 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2872 xptsetasyncfunc(struct cam_ed *device, void *arg)
2874 struct cam_path path;
2875 struct ccb_getdev cgd;
2876 struct async_node *cur_entry;
2878 cur_entry = (struct async_node *)arg;
2881 * Don't report unconfigured devices (Wildcard devs,
2882 * devices only for target mode, device instances
2883 * that have been invalidated but are waiting for
2884 * their last reference count to be released).
2886 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2889 xpt_compile_path(&path,
2891 device->target->bus->path_id,
2892 device->target->target_id,
2894 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2895 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2896 xpt_action((union ccb *)&cgd);
2897 cur_entry->callback(cur_entry->callback_arg,
2900 xpt_release_path(&path);
2906 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2908 struct cam_path path;
2909 struct ccb_pathinq cpi;
2910 struct async_node *cur_entry;
2912 cur_entry = (struct async_node *)arg;
2914 xpt_compile_path(&path, /*periph*/NULL,
2916 CAM_TARGET_WILDCARD,
2918 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2919 cpi.ccb_h.func_code = XPT_PATH_INQ;
2920 xpt_action((union ccb *)&cpi);
2921 cur_entry->callback(cur_entry->callback_arg,
2924 xpt_release_path(&path);
2930 xpt_action_sasync_cb(void *context, int pending)
2932 struct async_node *cur_entry;
2933 struct xpt_task *task;
2936 task = (struct xpt_task *)context;
2937 cur_entry = (struct async_node *)task->data1;
2938 added = task->data2;
2940 if ((added & AC_FOUND_DEVICE) != 0) {
2942 * Get this peripheral up to date with all
2943 * the currently existing devices.
2945 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
2947 if ((added & AC_PATH_REGISTERED) != 0) {
2949 * Get this peripheral up to date with all
2950 * the currently existing busses.
2952 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
2954 kfree(task, M_CAMXPT);
2958 xpt_action(union ccb *start_ccb)
2960 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2962 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2964 switch (start_ccb->ccb_h.func_code) {
2968 struct cam_ed *device;
2970 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2971 struct cam_path *path;
2973 path = start_ccb->ccb_h.path;
2977 * For the sake of compatibility with SCSI-1
2978 * devices that may not understand the identify
2979 * message, we include lun information in the
2980 * second byte of all commands. SCSI-1 specifies
2981 * that luns are a 3 bit value and reserves only 3
2982 * bits for lun information in the CDB. Later
2983 * revisions of the SCSI spec allow for more than 8
2984 * luns, but have deprecated lun information in the
2985 * CDB. So, if the lun won't fit, we must omit.
2987 * Also be aware that during initial probing for devices,
2988 * the inquiry information is unknown but initialized to 0.
2989 * This means that this code will be exercised while probing
2990 * devices with an ANSI revision greater than 2.
2992 device = start_ccb->ccb_h.path->device;
2993 if (device->protocol_version <= SCSI_REV_2
2994 && start_ccb->ccb_h.target_lun < 8
2995 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2997 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2998 start_ccb->ccb_h.target_lun << 5;
3000 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
3001 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
3002 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
3003 &path->device->inq_data),
3004 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
3005 cdb_str, sizeof(cdb_str))));
3009 case XPT_CONT_TARGET_IO:
3010 start_ccb->csio.sense_resid = 0;
3011 start_ccb->csio.resid = 0;
3016 struct cam_path *path;
3017 struct cam_sim *sim;
3020 path = start_ccb->ccb_h.path;
3022 sim = path->bus->sim;
3023 if (sim == &cam_dead_sim) {
3024 /* The SIM has gone; just execute the CCB directly. */
3025 cam_ccbq_send_ccb(&path->device->ccbq, start_ccb);
3026 (*(sim->sim_action))(sim, start_ccb);
3030 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3031 if (path->device->qfrozen_cnt == 0)
3032 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3036 xpt_run_dev_sendq(path->bus);
3039 case XPT_SET_TRAN_SETTINGS:
3041 xpt_set_transfer_settings(&start_ccb->cts,
3042 start_ccb->ccb_h.path->device,
3043 /*async_update*/FALSE);
3046 case XPT_CALC_GEOMETRY:
3048 struct cam_sim *sim;
3050 /* Filter out garbage */
3051 if (start_ccb->ccg.block_size == 0
3052 || start_ccb->ccg.volume_size == 0) {
3053 start_ccb->ccg.cylinders = 0;
3054 start_ccb->ccg.heads = 0;
3055 start_ccb->ccg.secs_per_track = 0;
3056 start_ccb->ccb_h.status = CAM_REQ_CMP;
3059 sim = start_ccb->ccb_h.path->bus->sim;
3060 (*(sim->sim_action))(sim, start_ccb);
3065 union ccb* abort_ccb;
3067 abort_ccb = start_ccb->cab.abort_ccb;
3068 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3070 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3071 struct cam_ccbq *ccbq;
3073 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3074 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3075 abort_ccb->ccb_h.status =
3076 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3077 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3078 xpt_done(abort_ccb);
3079 start_ccb->ccb_h.status = CAM_REQ_CMP;
3082 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3083 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3085 * We've caught this ccb en route to
3086 * the SIM. Flag it for abort and the
3087 * SIM will do so just before starting
3088 * real work on the CCB.
3090 abort_ccb->ccb_h.status =
3091 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3092 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3093 start_ccb->ccb_h.status = CAM_REQ_CMP;
3097 if (XPT_FC_IS_QUEUED(abort_ccb)
3098 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3100 * It's already completed but waiting
3101 * for our SWI to get to it.
3103 start_ccb->ccb_h.status = CAM_UA_ABORT;
3107 * If we weren't able to take care of the abort request
3108 * in the XPT, pass the request down to the SIM for processing.
3112 case XPT_ACCEPT_TARGET_IO:
3114 case XPT_IMMED_NOTIFY:
3115 case XPT_NOTIFY_ACK:
3116 case XPT_GET_TRAN_SETTINGS:
3119 struct cam_sim *sim;
3121 sim = start_ccb->ccb_h.path->bus->sim;
3122 (*(sim->sim_action))(sim, start_ccb);
3127 struct cam_sim *sim;
3129 sim = start_ccb->ccb_h.path->bus->sim;
3130 (*(sim->sim_action))(sim, start_ccb);
3133 case XPT_PATH_STATS:
3134 start_ccb->cpis.last_reset =
3135 start_ccb->ccb_h.path->bus->last_reset;
3136 start_ccb->ccb_h.status = CAM_REQ_CMP;
3142 dev = start_ccb->ccb_h.path->device;
3143 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3144 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3146 struct ccb_getdev *cgd;
3148 cgd = &start_ccb->cgd;
3149 cgd->inq_data = dev->inq_data;
3150 cgd->ccb_h.status = CAM_REQ_CMP;
3151 cgd->serial_num_len = dev->serial_num_len;
3152 if ((dev->serial_num_len > 0)
3153 && (dev->serial_num != NULL))
3154 bcopy(dev->serial_num, cgd->serial_num,
3155 dev->serial_num_len);
3159 case XPT_GDEV_STATS:
3163 dev = start_ccb->ccb_h.path->device;
3164 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3165 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3167 struct ccb_getdevstats *cgds;
3171 cgds = &start_ccb->cgds;
3172 bus = cgds->ccb_h.path->bus;
3173 tar = cgds->ccb_h.path->target;
3174 cgds->dev_openings = dev->ccbq.dev_openings;
3175 cgds->dev_active = dev->ccbq.dev_active;
3176 cgds->devq_openings = dev->ccbq.devq_openings;
3177 cgds->devq_queued = dev->ccbq.queue.entries;
3178 cgds->held = dev->ccbq.held;
3179 cgds->last_reset = tar->last_reset;
3180 cgds->maxtags = dev->quirk->maxtags;
3181 cgds->mintags = dev->quirk->mintags;
3182 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3183 cgds->last_reset = bus->last_reset;
3184 cgds->ccb_h.status = CAM_REQ_CMP;
3190 struct cam_periph *nperiph;
3191 struct periph_list *periph_head;
3192 struct ccb_getdevlist *cgdl;
3194 struct cam_ed *device;
3201 * Don't want anyone mucking with our data.
3203 device = start_ccb->ccb_h.path->device;
3204 periph_head = &device->periphs;
3205 cgdl = &start_ccb->cgdl;
3208 * Check and see if the list has changed since the user
3209 * last requested a list member. If so, tell them that the
3210 * list has changed, and therefore they need to start over
3211 * from the beginning.
3213 if ((cgdl->index != 0) &&
3214 (cgdl->generation != device->generation)) {
3215 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3220 * Traverse the list of peripherals and attempt to find
3221 * the requested peripheral.
3223 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3224 (nperiph != NULL) && (i <= cgdl->index);
3225 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3226 if (i == cgdl->index) {
3227 strncpy(cgdl->periph_name,
3228 nperiph->periph_name,
3230 cgdl->unit_number = nperiph->unit_number;
3235 cgdl->status = CAM_GDEVLIST_ERROR;
3239 if (nperiph == NULL)
3240 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3242 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3245 cgdl->generation = device->generation;
3247 cgdl->ccb_h.status = CAM_REQ_CMP;
3252 dev_pos_type position_type;
3253 struct ccb_dev_match *cdm;
3256 cdm = &start_ccb->cdm;
3259 * There are two ways of getting at information in the EDT.
3260 * The first way is via the primary EDT tree. It starts
3261 * with a list of busses, then a list of targets on a bus,
3262 * then devices/luns on a target, and then peripherals on a
3263 * device/lun. The "other" way is by the peripheral driver
3264 * lists. The peripheral driver lists are organized by
3265 * peripheral driver. (obviously) So it makes sense to
3266 * use the peripheral driver list if the user is looking
3267 * for something like "da1", or all "da" devices. If the
3268 * user is looking for something on a particular bus/target
3269 * or lun, it's generally better to go through the EDT tree.
3272 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3273 position_type = cdm->pos.position_type;
3277 position_type = CAM_DEV_POS_NONE;
3279 for (i = 0; i < cdm->num_patterns; i++) {
3280 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3281 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3282 position_type = CAM_DEV_POS_EDT;
3287 if (cdm->num_patterns == 0)
3288 position_type = CAM_DEV_POS_EDT;
3289 else if (position_type == CAM_DEV_POS_NONE)
3290 position_type = CAM_DEV_POS_PDRV;
3293 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3294 case CAM_DEV_POS_EDT:
3295 ret = xptedtmatch(cdm);
3297 case CAM_DEV_POS_PDRV:
3298 ret = xptperiphlistmatch(cdm);
3301 cdm->status = CAM_DEV_MATCH_ERROR;
3305 if (cdm->status == CAM_DEV_MATCH_ERROR)
3306 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3308 start_ccb->ccb_h.status = CAM_REQ_CMP;
3314 struct ccb_setasync *csa;
3315 struct async_node *cur_entry;
3316 struct async_list *async_head;
3319 csa = &start_ccb->csa;
3320 added = csa->event_enable;
3321 async_head = &csa->ccb_h.path->device->asyncs;
3324 * If there is already an entry for us, simply
3327 cur_entry = SLIST_FIRST(async_head);
3328 while (cur_entry != NULL) {
3329 if ((cur_entry->callback_arg == csa->callback_arg)
3330 && (cur_entry->callback == csa->callback))
3332 cur_entry = SLIST_NEXT(cur_entry, links);
3335 if (cur_entry != NULL) {
3337 * If the request has no flags set,
3340 added &= ~cur_entry->event_enable;
3341 if (csa->event_enable == 0) {
3342 SLIST_REMOVE(async_head, cur_entry,
3344 csa->ccb_h.path->device->refcount--;
3345 kfree(cur_entry, M_CAMXPT);
3347 cur_entry->event_enable = csa->event_enable;
3350 cur_entry = kmalloc(sizeof(*cur_entry), M_CAMXPT,
3352 cur_entry->event_enable = csa->event_enable;
3353 cur_entry->callback_arg = csa->callback_arg;
3354 cur_entry->callback = csa->callback;
3355 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3356 csa->ccb_h.path->device->refcount++;
3360 * Need to decouple this operation via a taskqueue so that
3361 * the locking doesn't become a mess.
3363 if ((added & (AC_FOUND_DEVICE | AC_PATH_REGISTERED)) != 0) {
3364 struct xpt_task *task;
3366 task = kmalloc(sizeof(struct xpt_task), M_CAMXPT,
3369 TASK_INIT(&task->task, 0, xpt_action_sasync_cb, task);
3370 task->data1 = cur_entry;
3371 task->data2 = added;
3372 taskqueue_enqueue(taskqueue_thread[mycpuid],
3376 start_ccb->ccb_h.status = CAM_REQ_CMP;
3381 struct ccb_relsim *crs;
3384 crs = &start_ccb->crs;
3385 dev = crs->ccb_h.path->device;
3388 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3392 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3394 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
3395 /* Don't ever go below one opening */
3396 if (crs->openings > 0) {
3397 xpt_dev_ccbq_resize(crs->ccb_h.path,
3401 xpt_print(crs->ccb_h.path,
3402 "tagged openings now %d\n",
3409 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3411 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3414 * Just extend the old timeout and decrement
3415 * the freeze count so that a single timeout
3416 * is sufficient for releasing the queue.
3418 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3419 callout_stop(&dev->callout);
3422 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3425 callout_reset(&dev->callout,
3426 (crs->release_timeout * hz) / 1000,
3427 xpt_release_devq_timeout, dev);
3429 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3433 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3435 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3437 * Decrement the freeze count so that a single
3438 * completion is still sufficient to unfreeze
3441 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3444 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3445 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3449 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3451 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3452 || (dev->ccbq.dev_active == 0)) {
3454 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3457 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3458 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3462 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3464 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3467 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3468 start_ccb->ccb_h.status = CAM_REQ_CMP;
3472 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3475 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3476 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3481 #ifdef CAM_DEBUG_DELAY
3482 cam_debug_delay = CAM_DEBUG_DELAY;
3484 cam_dflags = start_ccb->cdbg.flags;
3485 if (cam_dpath != NULL) {
3486 xpt_free_path(cam_dpath);
3490 if (cam_dflags != CAM_DEBUG_NONE) {
3491 if (xpt_create_path(&cam_dpath, xpt_periph,
3492 start_ccb->ccb_h.path_id,
3493 start_ccb->ccb_h.target_id,
3494 start_ccb->ccb_h.target_lun) !=
3496 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3497 cam_dflags = CAM_DEBUG_NONE;
3499 start_ccb->ccb_h.status = CAM_REQ_CMP;
3500 xpt_print(cam_dpath, "debugging flags now %x\n",
3505 start_ccb->ccb_h.status = CAM_REQ_CMP;
3507 #else /* !CAMDEBUG */
3508 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3509 #endif /* CAMDEBUG */
3513 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3514 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3515 start_ccb->ccb_h.status = CAM_REQ_CMP;
3522 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3528 xpt_polled_action(union ccb *start_ccb)
3531 struct cam_sim *sim;
3532 struct cam_devq *devq;
3535 timeout = start_ccb->ccb_h.timeout;
3536 sim = start_ccb->ccb_h.path->bus->sim;
3538 dev = start_ccb->ccb_h.path->device;
3540 sim_lock_assert_owned(sim->lock);
3543 * Steal an opening so that no other queued requests
3544 * can get it before us while we simulate interrupts.
3546 dev->ccbq.devq_openings--;
3547 dev->ccbq.dev_openings--;
3549 while(((devq && devq->send_openings <= 0) || dev->ccbq.dev_openings < 0)
3550 && (--timeout > 0)) {
3552 (*(sim->sim_poll))(sim);
3553 camisr_runqueue(sim);
3556 dev->ccbq.devq_openings++;
3557 dev->ccbq.dev_openings++;
3560 xpt_action(start_ccb);
3561 while(--timeout > 0) {
3562 (*(sim->sim_poll))(sim);
3563 camisr_runqueue(sim);
3564 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3571 * XXX Is it worth adding a sim_timeout entry
3572 * point so we can attempt recovery? If
3573 * this is only used for dumps, I don't think
3576 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3579 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3584 * Schedule a peripheral driver to receive a ccb when it's
3585 * target device has space for more transactions.
3588 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3590 struct cam_ed *device;
3591 union ccb *work_ccb;
3594 sim_lock_assert_owned(perph->sim->lock);
3596 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3597 device = perph->path->device;
3598 if (periph_is_queued(perph)) {
3599 /* Simply reorder based on new priority */
3600 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3601 (" change priority to %d\n", new_priority));
3602 if (new_priority < perph->pinfo.priority) {
3603 camq_change_priority(&device->drvq,
3608 } else if (perph->path->bus->sim == &cam_dead_sim) {
3609 /* The SIM is gone so just call periph_start directly. */
3610 work_ccb = xpt_get_ccb(perph->path->device);
3611 if (work_ccb == NULL)
3613 xpt_setup_ccb(&work_ccb->ccb_h, perph->path, new_priority);
3614 perph->pinfo.priority = new_priority;
3615 perph->periph_start(perph, work_ccb);
3618 /* New entry on the queue */
3619 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3620 (" added periph to queue\n"));
3621 perph->pinfo.priority = new_priority;
3622 perph->pinfo.generation = ++device->drvq.generation;
3623 camq_insert(&device->drvq, &perph->pinfo);
3624 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3627 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3628 (" calling xpt_run_devq\n"));
3629 xpt_run_dev_allocq(perph->path->bus);
3635 * Schedule a device to run on a given queue.
3636 * If the device was inserted as a new entry on the queue,
3637 * return 1 meaning the device queue should be run. If we
3638 * were already queued, implying someone else has already
3639 * started the queue, return 0 so the caller doesn't attempt
3643 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3644 u_int32_t new_priority)
3647 u_int32_t old_priority;
3649 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3651 old_priority = pinfo->priority;
3654 * Are we already queued?
3656 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3657 /* Simply reorder based on new priority */
3658 if (new_priority < old_priority) {
3659 camq_change_priority(queue, pinfo->index,
3661 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3662 ("changed priority to %d\n",
3667 /* New entry on the queue */
3668 if (new_priority < old_priority)
3669 pinfo->priority = new_priority;
3671 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3672 ("Inserting onto queue\n"));
3673 pinfo->generation = ++queue->generation;
3674 camq_insert(queue, pinfo);
3681 xpt_run_dev_allocq(struct cam_eb *bus)
3683 struct cam_devq *devq;
3685 if ((devq = bus->sim->devq) == NULL) {
3686 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq: NULL devq\n"));
3689 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3691 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3692 (" qfrozen_cnt == 0x%x, entries == %d, "
3693 "openings == %d, active == %d\n",
3694 devq->alloc_queue.qfrozen_cnt,
3695 devq->alloc_queue.entries,
3696 devq->alloc_openings,
3697 devq->alloc_active));
3699 devq->alloc_queue.qfrozen_cnt++;
3700 while ((devq->alloc_queue.entries > 0)
3701 && (devq->alloc_openings > 0)
3702 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3703 struct cam_ed_qinfo *qinfo;
3704 struct cam_ed *device;
3705 union ccb *work_ccb;
3706 struct cam_periph *drv;
3709 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3711 device = qinfo->device;
3713 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3714 ("running device %p\n", device));
3716 drvq = &device->drvq;
3719 if (drvq->entries <= 0) {
3720 panic("xpt_run_dev_allocq: "
3721 "Device on queue without any work to do");
3724 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3725 devq->alloc_openings--;
3726 devq->alloc_active++;
3727 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3728 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3729 drv->pinfo.priority);
3730 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3731 ("calling periph start\n"));
3732 drv->periph_start(drv, work_ccb);
3735 * Malloc failure in alloc_ccb
3738 * XXX add us to a list to be run from free_ccb
3739 * if we don't have any ccbs active on this
3740 * device queue otherwise we may never get run
3746 if (drvq->entries > 0) {
3747 /* We have more work. Attempt to reschedule */
3748 xpt_schedule_dev_allocq(bus, device);
3751 devq->alloc_queue.qfrozen_cnt--;
3755 xpt_run_dev_sendq(struct cam_eb *bus)
3757 struct cam_devq *devq;
3759 if ((devq = bus->sim->devq) == NULL) {
3760 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq: NULL devq\n"));
3763 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3765 devq->send_queue.qfrozen_cnt++;
3766 while ((devq->send_queue.entries > 0)
3767 && (devq->send_openings > 0)) {
3768 struct cam_ed_qinfo *qinfo;
3769 struct cam_ed *device;
3770 union ccb *work_ccb;
3771 struct cam_sim *sim;
3773 if (devq->send_queue.qfrozen_cnt > 1) {
3777 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3779 device = qinfo->device;
3782 * If the device has been "frozen", don't attempt
3785 if (device->qfrozen_cnt > 0) {
3789 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3790 ("running device %p\n", device));
3792 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3793 if (work_ccb == NULL) {
3794 kprintf("device on run queue with no ccbs???\n");
3798 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3800 lockmgr(&xsoftc.xpt_lock, LK_EXCLUSIVE);
3801 if (xsoftc.num_highpower <= 0) {
3803 * We got a high power command, but we
3804 * don't have any available slots. Freeze
3805 * the device queue until we have a slot
3808 device->qfrozen_cnt++;
3809 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3813 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
3817 * Consume a high power slot while
3820 xsoftc.num_highpower--;
3822 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
3824 devq->active_dev = device;
3825 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3827 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3829 devq->send_openings--;
3830 devq->send_active++;
3832 if (device->ccbq.queue.entries > 0)
3833 xpt_schedule_dev_sendq(bus, device);
3835 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3837 * The client wants to freeze the queue
3838 * after this CCB is sent.
3840 device->qfrozen_cnt++;
3843 /* In Target mode, the peripheral driver knows best... */
3844 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3845 if ((device->inq_flags & SID_CmdQue) != 0
3846 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3847 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3850 * Clear this in case of a retried CCB that
3851 * failed due to a rejected tag.
3853 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3857 * Device queues can be shared among multiple sim instances
3858 * that reside on different busses. Use the SIM in the queue
3859 * CCB's path, rather than the one in the bus that was passed
3860 * into this function.
3862 sim = work_ccb->ccb_h.path->bus->sim;
3863 (*(sim->sim_action))(sim, work_ccb);
3865 devq->active_dev = NULL;
3867 devq->send_queue.qfrozen_cnt--;
3871 * This function merges stuff from the slave ccb into the master ccb, while
3872 * keeping important fields in the master ccb constant.
3875 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3878 * Pull fields that are valid for peripheral drivers to set
3879 * into the master CCB along with the CCB "payload".
3881 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3882 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3883 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3884 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3885 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3886 sizeof(union ccb) - sizeof(struct ccb_hdr));
3890 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3892 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3893 callout_init(&ccb_h->timeout_ch);
3894 ccb_h->pinfo.priority = priority;
3896 ccb_h->path_id = path->bus->path_id;
3898 ccb_h->target_id = path->target->target_id;
3900 ccb_h->target_id = CAM_TARGET_WILDCARD;
3902 ccb_h->target_lun = path->device->lun_id;
3903 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3905 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3907 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3911 /* Path manipulation functions */
3913 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3914 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3916 struct cam_path *path;
3919 path = kmalloc(sizeof(*path), M_CAMXPT, M_INTWAIT);
3920 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3921 if (status != CAM_REQ_CMP) {
3922 kfree(path, M_CAMXPT);
3925 *new_path_ptr = path;
3930 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3931 struct cam_periph *periph, path_id_t path_id,
3932 target_id_t target_id, lun_id_t lun_id)
3934 struct cam_path *path;
3935 struct cam_eb *bus = NULL;
3937 int need_unlock = 0;
3939 path = (struct cam_path *)kmalloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3941 if (path_id != CAM_BUS_WILDCARD) {
3942 bus = xpt_find_bus(path_id);
3945 CAM_SIM_LOCK(bus->sim);
3948 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3950 CAM_SIM_UNLOCK(bus->sim);
3951 if (status != CAM_REQ_CMP) {
3952 kfree(path, M_CAMXPT);
3955 *new_path_ptr = path;
3960 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3961 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3964 struct cam_et *target;
3965 struct cam_ed *device;
3968 status = CAM_REQ_CMP; /* Completed without error */
3969 target = NULL; /* Wildcarded */
3970 device = NULL; /* Wildcarded */
3973 * We will potentially modify the EDT, so block interrupts
3974 * that may attempt to create cam paths.
3976 bus = xpt_find_bus(path_id);
3978 status = CAM_PATH_INVALID;
3980 target = xpt_find_target(bus, target_id);
3981 if (target == NULL) {
3983 struct cam_et *new_target;
3985 new_target = xpt_alloc_target(bus, target_id);
3986 if (new_target == NULL) {
3987 status = CAM_RESRC_UNAVAIL;
3989 target = new_target;
3992 if (target != NULL) {
3993 device = xpt_find_device(target, lun_id);
3994 if (device == NULL) {
3996 struct cam_ed *new_device;
3998 new_device = xpt_alloc_device(bus,
4001 if (new_device == NULL) {
4002 status = CAM_RESRC_UNAVAIL;
4004 device = new_device;
4011 * Only touch the user's data if we are successful.
4013 if (status == CAM_REQ_CMP) {
4014 new_path->periph = perph;
4015 new_path->bus = bus;
4016 new_path->target = target;
4017 new_path->device = device;
4018 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4021 xpt_release_device(bus, target, device);
4023 xpt_release_target(bus, target);
4025 xpt_release_bus(bus);
4031 xpt_release_path(struct cam_path *path)
4033 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4034 if (path->device != NULL) {
4035 xpt_release_device(path->bus, path->target, path->device);
4036 path->device = NULL;
4038 if (path->target != NULL) {
4039 xpt_release_target(path->bus, path->target);
4040 path->target = NULL;
4042 if (path->bus != NULL) {
4043 xpt_release_bus(path->bus);
4049 xpt_free_path(struct cam_path *path)
4051 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4052 xpt_release_path(path);
4053 kfree(path, M_CAMXPT);
4058 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4059 * in path1, 2 for match with wildcards in path2.
4062 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4066 if (path1->bus != path2->bus) {
4067 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4069 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4074 if (path1->target != path2->target) {
4075 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4078 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4083 if (path1->device != path2->device) {
4084 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4087 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4096 xpt_print_path(struct cam_path *path)
4100 kprintf("(nopath): ");
4102 if (path->periph != NULL)
4103 kprintf("(%s%d:", path->periph->periph_name,
4104 path->periph->unit_number);
4106 kprintf("(noperiph:");
4108 if (path->bus != NULL)
4109 kprintf("%s%d:%d:", path->bus->sim->sim_name,
4110 path->bus->sim->unit_number,
4111 path->bus->sim->bus_id);
4115 if (path->target != NULL)
4116 kprintf("%d:", path->target->target_id);
4120 if (path->device != NULL)
4121 kprintf("%d): ", path->device->lun_id);
4128 xpt_print(struct cam_path *path, const char *fmt, ...)
4131 xpt_print_path(path);
4132 __va_start(ap, fmt);
4138 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4142 sim_lock_assert_owned(path->bus->sim->lock);
4144 sbuf_new(&sb, str, str_len, 0);
4147 sbuf_printf(&sb, "(nopath): ");
4149 if (path->periph != NULL)
4150 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4151 path->periph->unit_number);
4153 sbuf_printf(&sb, "(noperiph:");
4155 if (path->bus != NULL)
4156 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4157 path->bus->sim->unit_number,
4158 path->bus->sim->bus_id);
4160 sbuf_printf(&sb, "nobus:");
4162 if (path->target != NULL)
4163 sbuf_printf(&sb, "%d:", path->target->target_id);
4165 sbuf_printf(&sb, "X:");
4167 if (path->device != NULL)
4168 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4170 sbuf_printf(&sb, "X): ");
4174 return(sbuf_len(&sb));
4178 xpt_path_path_id(struct cam_path *path)
4180 sim_lock_assert_owned(path->bus->sim->lock);
4182 return(path->bus->path_id);
4186 xpt_path_target_id(struct cam_path *path)
4188 sim_lock_assert_owned(path->bus->sim->lock);
4190 if (path->target != NULL)
4191 return (path->target->target_id);
4193 return (CAM_TARGET_WILDCARD);
4197 xpt_path_lun_id(struct cam_path *path)
4199 sim_lock_assert_owned(path->bus->sim->lock);
4201 if (path->device != NULL)
4202 return (path->device->lun_id);
4204 return (CAM_LUN_WILDCARD);
4208 xpt_path_sim(struct cam_path *path)
4210 return (path->bus->sim);
4214 xpt_path_periph(struct cam_path *path)
4216 sim_lock_assert_owned(path->bus->sim->lock);
4218 return (path->periph);
4222 xpt_path_serialno(struct cam_path *path)
4224 return (path->device->serial_num);
4228 * Release a CAM control block for the caller. Remit the cost of the structure
4229 * to the device referenced by the path. If the this device had no 'credits'
4230 * and peripheral drivers have registered async callbacks for this notification
4234 xpt_release_ccb(union ccb *free_ccb)
4236 struct cam_path *path;
4237 struct cam_ed *device;
4239 struct cam_sim *sim;
4241 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4242 path = free_ccb->ccb_h.path;
4243 device = path->device;
4247 sim_lock_assert_owned(sim->lock);
4249 cam_ccbq_release_opening(&device->ccbq);
4250 if (sim->ccb_count > sim->max_ccbs) {
4251 xpt_free_ccb(free_ccb);
4253 } else if (sim == &cam_dead_sim) {
4254 xpt_free_ccb(free_ccb);
4256 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
4259 if (sim->devq == NULL) {
4262 sim->devq->alloc_openings++;
4263 sim->devq->alloc_active--;
4264 /* XXX Turn this into an inline function - xpt_run_device?? */
4265 if ((device_is_alloc_queued(device) == 0)
4266 && (device->drvq.entries > 0)) {
4267 xpt_schedule_dev_allocq(bus, device);
4269 if (dev_allocq_is_runnable(sim->devq))
4270 xpt_run_dev_allocq(bus);
4273 /* Functions accessed by SIM drivers */
4276 * A sim structure, listing the SIM entry points and instance
4277 * identification info is passed to xpt_bus_register to hook the SIM
4278 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4279 * for this new bus and places it in the array of busses and assigns
4280 * it a path_id. The path_id may be influenced by "hard wiring"
4281 * information specified by the user. Once interrupt services are
4282 * availible, the bus will be probed.
4285 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4287 struct cam_eb *new_bus;
4288 struct cam_eb *old_bus;
4289 struct ccb_pathinq cpi;
4291 sim_lock_assert_owned(sim->lock);
4294 new_bus = kmalloc(sizeof(*new_bus), M_CAMXPT, M_INTWAIT);
4296 if (strcmp(sim->sim_name, "xpt") != 0) {
4298 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4301 TAILQ_INIT(&new_bus->et_entries);
4302 new_bus->path_id = sim->path_id;
4305 timevalclear(&new_bus->last_reset);
4307 new_bus->refcount = 1; /* Held until a bus_deregister event */
4308 new_bus->generation = 0;
4309 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
4310 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4311 while (old_bus != NULL
4312 && old_bus->path_id < new_bus->path_id)
4313 old_bus = TAILQ_NEXT(old_bus, links);
4314 if (old_bus != NULL)
4315 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4317 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
4318 xsoftc.bus_generation++;
4319 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
4321 /* Notify interested parties */
4322 if (sim->path_id != CAM_XPT_PATH_ID) {
4323 struct cam_path path;
4325 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4326 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4327 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4328 cpi.ccb_h.func_code = XPT_PATH_INQ;
4329 xpt_action((union ccb *)&cpi);
4330 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4331 xpt_release_path(&path);
4333 return (CAM_SUCCESS);
4337 * Deregister a bus. We must clean out all transactions pending on the bus.
4338 * This routine is typically called prior to cam_sim_free() (e.g. see
4339 * dev/usbmisc/umass/umass.c)
4342 xpt_bus_deregister(path_id_t pathid)
4344 struct cam_path bus_path;
4345 struct cam_et *target;
4346 struct cam_ed *device;
4347 struct cam_ed_qinfo *qinfo;
4348 struct cam_devq *devq;
4349 struct cam_periph *periph;
4350 struct cam_sim *ccbsim;
4351 union ccb *work_ccb;
4355 status = xpt_compile_path(&bus_path, NULL, pathid,
4356 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4357 if (status != CAM_REQ_CMP)
4361 * This should clear out all pending requests and timeouts, but
4362 * the ccb's may be queued to a software interrupt.
4364 * XXX AC_LOST_DEVICE does not precisely abort the pending requests,
4365 * and it really ought to.
4367 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4368 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4371 * Mark the SIM as having been deregistered. This prevents
4372 * certain operations from re-queueing to it, stops new devices
4373 * from being added, etc.
4375 devq = bus_path.bus->sim->devq;
4376 ccbsim = bus_path.bus->sim;
4377 ccbsim->flags |= CAM_SIM_DEREGISTERED;
4381 * Execute any pending operations now.
4383 while ((qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
4384 CAMQ_HEAD)) != NULL ||
4385 (qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
4386 CAMQ_HEAD)) != NULL) {
4388 device = qinfo->device;
4389 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
4390 if (work_ccb != NULL) {
4391 devq->active_dev = device;
4392 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
4393 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
4394 (*(ccbsim->sim_action))(ccbsim, work_ccb);
4397 periph = (struct cam_periph *)camq_remove(&device->drvq,
4400 xpt_schedule(periph, periph->pinfo.priority);
4401 } while (work_ccb != NULL || periph != NULL);
4405 * Make sure all completed CCBs are processed.
4407 while (!TAILQ_EMPTY(&ccbsim->sim_doneq)) {
4408 camisr_runqueue(ccbsim);
4412 * Check for requeues, reissues asyncs if necessary
4414 if (CAMQ_GET_HEAD(&devq->send_queue))
4415 kprintf("camq: devq send_queue still in use (%d entries)\n",
4416 devq->send_queue.entries);
4417 if (CAMQ_GET_HEAD(&devq->alloc_queue))
4418 kprintf("camq: devq alloc_queue still in use (%d entries)\n",
4419 devq->alloc_queue.entries);
4420 if (CAMQ_GET_HEAD(&devq->send_queue) ||
4421 CAMQ_GET_HEAD(&devq->alloc_queue)) {
4422 if (++retries < 5) {
4423 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4424 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4430 * Retarget the bus and all cached sim pointers to dead_sim.
4432 * Various CAM subsystems may be holding on to targets, devices,
4433 * and/or peripherals and may attempt to use the sim pointer cached
4434 * in some of these structures during close.
4436 bus_path.bus->sim = &cam_dead_sim;
4437 TAILQ_FOREACH(target, &bus_path.bus->et_entries, links) {
4438 TAILQ_FOREACH(device, &target->ed_entries, links) {
4439 device->sim = &cam_dead_sim;
4440 SLIST_FOREACH(periph, &device->periphs, periph_links) {
4441 periph->sim = &cam_dead_sim;
4447 * Repeat the async's for the benefit of any new devices, such as
4448 * might be created from completed probes. Any new device
4449 * ops will run on dead_sim.
4451 * XXX There are probably races :-(
4453 CAM_SIM_LOCK(&cam_dead_sim);
4454 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4455 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4456 CAM_SIM_UNLOCK(&cam_dead_sim);
4458 /* Release the reference count held while registered. */
4459 xpt_release_bus(bus_path.bus);
4460 xpt_release_path(&bus_path);
4462 /* Release the ref we got when the bus was registered */
4463 cam_sim_release(ccbsim, 0);
4465 return (CAM_REQ_CMP);
4469 xptnextfreepathid(void)
4476 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
4477 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4479 /* Find an unoccupied pathid */
4480 while (bus != NULL && bus->path_id <= pathid) {
4481 if (bus->path_id == pathid)
4483 bus = TAILQ_NEXT(bus, links);
4485 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
4488 * Ensure that this pathid is not reserved for
4489 * a bus that may be registered in the future.
4491 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4493 /* Start the search over */
4494 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
4501 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4507 pathid = CAM_XPT_PATH_ID;
4508 ksnprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4510 while ((i = resource_query_string(i, "at", buf)) != -1) {
4511 if (strcmp(resource_query_name(i), "scbus")) {
4512 /* Avoid a bit of foot shooting. */
4515 dunit = resource_query_unit(i);
4516 if (dunit < 0) /* unwired?! */
4518 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4519 if (sim_bus == val) {
4523 } else if (sim_bus == 0) {
4524 /* Unspecified matches bus 0 */
4528 kprintf("Ambiguous scbus configuration for %s%d "
4529 "bus %d, cannot wire down. The kernel "
4530 "config entry for scbus%d should "
4531 "specify a controller bus.\n"
4532 "Scbus will be assigned dynamically.\n",
4533 sim_name, sim_unit, sim_bus, dunit);
4538 if (pathid == CAM_XPT_PATH_ID)
4539 pathid = xptnextfreepathid();
4544 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4547 struct cam_et *target, *next_target;
4548 struct cam_ed *device, *next_device;
4550 sim_lock_assert_owned(path->bus->sim->lock);
4552 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4555 * Most async events come from a CAM interrupt context. In
4556 * a few cases, the error recovery code at the peripheral layer,
4557 * which may run from our SWI or a process context, may signal
4558 * deferred events with a call to xpt_async.
4563 if (async_code == AC_BUS_RESET) {
4564 /* Update our notion of when the last reset occurred */
4565 microuptime(&bus->last_reset);
4568 for (target = TAILQ_FIRST(&bus->et_entries);
4570 target = next_target) {
4572 next_target = TAILQ_NEXT(target, links);
4574 if (path->target != target
4575 && path->target->target_id != CAM_TARGET_WILDCARD
4576 && target->target_id != CAM_TARGET_WILDCARD)
4579 if (async_code == AC_SENT_BDR) {
4580 /* Update our notion of when the last reset occurred */
4581 microuptime(&path->target->last_reset);
4584 for (device = TAILQ_FIRST(&target->ed_entries);
4586 device = next_device) {
4588 next_device = TAILQ_NEXT(device, links);
4590 if (path->device != device
4591 && path->device->lun_id != CAM_LUN_WILDCARD
4592 && device->lun_id != CAM_LUN_WILDCARD)
4595 xpt_dev_async(async_code, bus, target,
4598 xpt_async_bcast(&device->asyncs, async_code,
4604 * If this wasn't a fully wildcarded async, tell all
4605 * clients that want all async events.
4607 if (bus != xpt_periph->path->bus)
4608 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4613 xpt_async_bcast(struct async_list *async_head,
4614 u_int32_t async_code,
4615 struct cam_path *path, void *async_arg)
4617 struct async_node *cur_entry;
4619 cur_entry = SLIST_FIRST(async_head);
4620 while (cur_entry != NULL) {
4621 struct async_node *next_entry;
4623 * Grab the next list entry before we call the current
4624 * entry's callback. This is because the callback function
4625 * can delete its async callback entry.
4627 next_entry = SLIST_NEXT(cur_entry, links);
4628 if ((cur_entry->event_enable & async_code) != 0)
4629 cur_entry->callback(cur_entry->callback_arg,
4632 cur_entry = next_entry;
4637 * Handle any per-device event notifications that require action by the XPT.
4640 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4641 struct cam_ed *device, void *async_arg)
4644 struct cam_path newpath;
4647 * We only need to handle events for real devices.
4649 if (target->target_id == CAM_TARGET_WILDCARD
4650 || device->lun_id == CAM_LUN_WILDCARD)
4654 * We need our own path with wildcards expanded to
4655 * handle certain types of events.
4657 if ((async_code == AC_SENT_BDR)
4658 || (async_code == AC_BUS_RESET)
4659 || (async_code == AC_INQ_CHANGED))
4660 status = xpt_compile_path(&newpath, NULL,
4665 status = CAM_REQ_CMP_ERR;
4667 if (status == CAM_REQ_CMP) {
4670 * Allow transfer negotiation to occur in a
4671 * tag free environment.
4673 if (async_code == AC_SENT_BDR
4674 || async_code == AC_BUS_RESET)
4675 xpt_toggle_tags(&newpath);
4677 if (async_code == AC_INQ_CHANGED) {
4679 * We've sent a start unit command, or
4680 * something similar to a device that
4681 * may have caused its inquiry data to
4682 * change. So we re-scan the device to
4683 * refresh the inquiry data for it.
4685 xpt_scan_lun(newpath.periph, &newpath,
4686 CAM_EXPECT_INQ_CHANGE, NULL);
4688 xpt_release_path(&newpath);
4689 } else if (async_code == AC_LOST_DEVICE) {
4691 * When we lose a device the device may be about to detach
4692 * the sim, we have to clear out all pending timeouts and
4693 * requests before that happens.
4695 * This typically happens most often with USB/UMASS devices.
4697 * XXX it would be nice if we could abort the requests
4698 * pertaining to the device.
4700 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4701 if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
4702 device->flags |= CAM_DEV_UNCONFIGURED;
4703 xpt_release_device(bus, target, device);
4705 } else if (async_code == AC_TRANSFER_NEG) {
4706 struct ccb_trans_settings *settings;
4708 settings = (struct ccb_trans_settings *)async_arg;
4709 xpt_set_transfer_settings(settings, device,
4710 /*async_update*/TRUE);
4715 xpt_freeze_devq(struct cam_path *path, u_int count)
4717 struct ccb_hdr *ccbh;
4719 sim_lock_assert_owned(path->bus->sim->lock);
4721 path->device->qfrozen_cnt += count;
4724 * Mark the last CCB in the queue as needing
4725 * to be requeued if the driver hasn't
4726 * changed it's state yet. This fixes a race
4727 * where a ccb is just about to be queued to
4728 * a controller driver when it's interrupt routine
4729 * freezes the queue. To completly close the
4730 * hole, controller drives must check to see
4731 * if a ccb's status is still CAM_REQ_INPROG
4732 * just before they queue
4733 * the CCB. See ahc_action/ahc_freeze_devq for
4736 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4737 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4738 ccbh->status = CAM_REQUEUE_REQ;
4739 return (path->device->qfrozen_cnt);
4743 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4745 sim_lock_assert_owned(sim->lock);
4747 if (sim->devq == NULL)
4749 sim->devq->send_queue.qfrozen_cnt += count;
4750 if (sim->devq->active_dev != NULL) {
4751 struct ccb_hdr *ccbh;
4753 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4755 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4756 ccbh->status = CAM_REQUEUE_REQ;
4758 return (sim->devq->send_queue.qfrozen_cnt);
4762 * Release the device queue after a timeout has expired, typically used to
4763 * introduce a delay before retrying after an I/O error or other problem.
4766 xpt_release_devq_timeout(void *arg)
4768 struct cam_ed *device;
4770 device = (struct cam_ed *)arg;
4771 CAM_SIM_LOCK(device->sim);
4772 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4773 CAM_SIM_UNLOCK(device->sim);
4777 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4779 sim_lock_assert_owned(path->bus->sim->lock);
4781 xpt_release_devq_device(path->device, count, run_queue);
4785 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4791 if (dev->qfrozen_cnt > 0) {
4793 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4794 dev->qfrozen_cnt -= count;
4795 if (dev->qfrozen_cnt == 0) {
4798 * No longer need to wait for a successful
4799 * command completion.
4801 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4804 * Remove any timeouts that might be scheduled
4805 * to release this queue.
4807 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4808 callout_stop(&dev->callout);
4809 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4813 * Now that we are unfrozen schedule the
4814 * device so any pending transactions are
4817 if ((dev->ccbq.queue.entries > 0)
4818 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4819 && (run_queue != 0)) {
4825 xpt_run_dev_sendq(dev->target->bus);
4829 xpt_release_simq(struct cam_sim *sim, int run_queue)
4833 sim_lock_assert_owned(sim->lock);
4835 if (sim->devq == NULL)
4838 sendq = &(sim->devq->send_queue);
4839 if (sendq->qfrozen_cnt > 0) {
4840 sendq->qfrozen_cnt--;
4841 if (sendq->qfrozen_cnt == 0) {
4845 * If there is a timeout scheduled to release this
4846 * sim queue, remove it. The queue frozen count is
4849 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4850 callout_stop(&sim->callout);
4851 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4853 bus = xpt_find_bus(sim->path_id);
4857 * Now that we are unfrozen run the send queue.
4859 xpt_run_dev_sendq(bus);
4861 xpt_release_bus(bus);
4867 xpt_done(union ccb *done_ccb)
4869 struct cam_sim *sim;
4871 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4872 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4874 * Queue up the request for handling by our SWI handler
4875 * any of the "non-immediate" type of ccbs.
4877 sim = done_ccb->ccb_h.path->bus->sim;
4878 switch (done_ccb->ccb_h.path->periph->type) {
4879 case CAM_PERIPH_BIO:
4880 spin_lock(&sim->sim_spin);
4881 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4883 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4884 spin_unlock(&sim->sim_spin);
4885 if ((sim->flags & CAM_SIM_ON_DONEQ) == 0) {
4886 spin_lock(&cam_simq_spin);
4887 if ((sim->flags & CAM_SIM_ON_DONEQ) == 0) {
4888 TAILQ_INSERT_TAIL(&cam_simq, sim,
4890 sim->flags |= CAM_SIM_ON_DONEQ;
4892 spin_unlock(&cam_simq_spin);
4894 if ((done_ccb->ccb_h.flags & CAM_POLLED) == 0)
4898 panic("unknown periph type %d",
4899 done_ccb->ccb_h.path->periph->type);
4909 new_ccb = kmalloc(sizeof(*new_ccb), M_CAMXPT, M_INTWAIT | M_ZERO);
4914 xpt_free_ccb(union ccb *free_ccb)
4916 kfree(free_ccb, M_CAMXPT);
4921 /* Private XPT functions */
4924 * Get a CAM control block for the caller. Charge the structure to the device
4925 * referenced by the path. If the this device has no 'credits' then the
4926 * device already has the maximum number of outstanding operations under way
4927 * and we return NULL. If we don't have sufficient resources to allocate more
4928 * ccbs, we also return NULL.
4931 xpt_get_ccb(struct cam_ed *device)
4934 struct cam_sim *sim;
4937 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4938 new_ccb = xpt_alloc_ccb();
4939 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4940 callout_init(&new_ccb->ccb_h.timeout_ch);
4941 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4945 cam_ccbq_take_opening(&device->ccbq);
4946 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4951 xpt_release_bus(struct cam_eb *bus)
4954 if ((--bus->refcount == 0)
4955 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4956 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
4957 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4958 xsoftc.bus_generation++;
4959 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
4960 kfree(bus, M_CAMXPT);
4964 static struct cam_et *
4965 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4967 struct cam_et *target;
4968 struct cam_et *cur_target;
4970 target = kmalloc(sizeof(*target), M_CAMXPT, M_INTWAIT);
4972 TAILQ_INIT(&target->ed_entries);
4974 target->target_id = target_id;
4975 target->refcount = 1;
4976 target->generation = 0;
4977 timevalclear(&target->last_reset);
4979 * Hold a reference to our parent bus so it
4980 * will not go away before we do.
4984 /* Insertion sort into our bus's target list */
4985 cur_target = TAILQ_FIRST(&bus->et_entries);
4986 while (cur_target != NULL && cur_target->target_id < target_id)
4987 cur_target = TAILQ_NEXT(cur_target, links);
4989 if (cur_target != NULL) {
4990 TAILQ_INSERT_BEFORE(cur_target, target, links);
4992 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4999 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
5001 if (target->refcount == 1) {
5002 KKASSERT(TAILQ_FIRST(&target->ed_entries) == NULL);
5003 TAILQ_REMOVE(&bus->et_entries, target, links);
5005 xpt_release_bus(bus);
5006 KKASSERT(target->refcount == 1);
5007 kfree(target, M_CAMXPT);
5013 static struct cam_ed *
5014 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
5016 struct cam_path path;
5017 struct cam_ed *device;
5018 struct cam_devq *devq;
5022 * Disallow new devices while trying to deregister a sim
5024 if (bus->sim->flags & CAM_SIM_DEREGISTERED)
5028 * Make space for us in the device queue on our bus
5030 devq = bus->sim->devq;
5033 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
5035 if (status != CAM_REQ_CMP) {
5038 device = kmalloc(sizeof(*device), M_CAMXPT, M_INTWAIT);
5041 if (device != NULL) {
5042 struct cam_ed *cur_device;
5044 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5045 device->alloc_ccb_entry.device = device;
5046 cam_init_pinfo(&device->send_ccb_entry.pinfo);
5047 device->send_ccb_entry.device = device;
5048 device->target = target;
5049 device->lun_id = lun_id;
5050 device->sim = bus->sim;
5051 /* Initialize our queues */
5052 if (camq_init(&device->drvq, 0) != 0) {
5053 kfree(device, M_CAMXPT);
5056 if (cam_ccbq_init(&device->ccbq,
5057 bus->sim->max_dev_openings) != 0) {
5058 camq_fini(&device->drvq);
5059 kfree(device, M_CAMXPT);
5062 SLIST_INIT(&device->asyncs);
5063 SLIST_INIT(&device->periphs);
5064 device->generation = 0;
5065 device->owner = NULL;
5067 * Take the default quirk entry until we have inquiry
5068 * data and can determine a better quirk to use.
5070 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5071 bzero(&device->inq_data, sizeof(device->inq_data));
5072 device->inq_flags = 0;
5073 device->queue_flags = 0;
5074 device->serial_num = NULL;
5075 device->serial_num_len = 0;
5076 device->qfrozen_cnt = 0;
5077 device->flags = CAM_DEV_UNCONFIGURED;
5078 device->tag_delay_count = 0;
5079 device->tag_saved_openings = 0;
5080 device->refcount = 1;
5081 callout_init(&device->callout);
5084 * Hold a reference to our parent target so it
5085 * will not go away before we do.
5090 * XXX should be limited by number of CCBs this bus can
5093 bus->sim->max_ccbs += device->ccbq.devq_openings;
5094 /* Insertion sort into our target's device list */
5095 cur_device = TAILQ_FIRST(&target->ed_entries);
5096 while (cur_device != NULL && cur_device->lun_id < lun_id)
5097 cur_device = TAILQ_NEXT(cur_device, links);
5098 if (cur_device != NULL) {
5099 TAILQ_INSERT_BEFORE(cur_device, device, links);
5101 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5103 target->generation++;
5104 if (lun_id != CAM_LUN_WILDCARD) {
5105 xpt_compile_path(&path,
5110 xpt_devise_transport(&path);
5111 xpt_release_path(&path);
5118 xpt_reference_device(struct cam_ed *device)
5124 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5125 struct cam_ed *device)
5127 struct cam_devq *devq;
5129 if (device->refcount == 1) {
5130 KKASSERT(device->flags & CAM_DEV_UNCONFIGURED);
5132 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5133 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5134 panic("Removing device while still queued for ccbs");
5136 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
5137 device->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
5138 callout_stop(&device->callout);
5141 TAILQ_REMOVE(&target->ed_entries, device,links);
5142 target->generation++;
5143 bus->sim->max_ccbs -= device->ccbq.devq_openings;
5144 if ((devq = bus->sim->devq) != NULL) {
5145 /* Release our slot in the devq */
5146 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5148 camq_fini(&device->drvq);
5149 camq_fini(&device->ccbq.queue);
5150 xpt_release_target(bus, target);
5151 KKASSERT(device->refcount == 1);
5152 kfree(device, M_CAMXPT);
5159 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5167 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5168 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5169 if (result == CAM_REQ_CMP && (diff < 0)) {
5170 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5172 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5173 || (dev->inq_flags & SID_CmdQue) != 0)
5174 dev->tag_saved_openings = newopenings;
5175 /* Adjust the global limit */
5176 dev->sim->max_ccbs += diff;
5180 static struct cam_eb *
5181 xpt_find_bus(path_id_t path_id)
5185 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
5186 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
5187 if (bus->path_id == path_id) {
5192 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
5196 static struct cam_et *
5197 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5199 struct cam_et *target;
5201 TAILQ_FOREACH(target, &bus->et_entries, links) {
5202 if (target->target_id == target_id) {
5210 static struct cam_ed *
5211 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5213 struct cam_ed *device;
5215 TAILQ_FOREACH(device, &target->ed_entries, links) {
5216 if (device->lun_id == lun_id) {
5225 union ccb *request_ccb;
5226 struct ccb_pathinq *cpi;
5228 } xpt_scan_bus_info;
5231 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5232 * As the scan progresses, xpt_scan_bus is used as the
5233 * callback on completion function.
5236 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5238 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5239 ("xpt_scan_bus\n"));
5240 switch (request_ccb->ccb_h.func_code) {
5243 xpt_scan_bus_info *scan_info;
5244 union ccb *work_ccb;
5245 struct cam_path *path;
5250 /* Find out the characteristics of the bus */
5251 work_ccb = xpt_alloc_ccb();
5252 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5253 request_ccb->ccb_h.pinfo.priority);
5254 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5255 xpt_action(work_ccb);
5256 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5257 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5258 xpt_free_ccb(work_ccb);
5259 xpt_done(request_ccb);
5263 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5265 * Can't scan the bus on an adapter that
5266 * cannot perform the initiator role.
5268 request_ccb->ccb_h.status = CAM_REQ_CMP;
5269 xpt_free_ccb(work_ccb);
5270 xpt_done(request_ccb);
5274 /* Save some state for use while we probe for devices */
5275 scan_info = (xpt_scan_bus_info *)
5276 kmalloc(sizeof(xpt_scan_bus_info), M_CAMXPT, M_INTWAIT);
5277 scan_info->request_ccb = request_ccb;
5278 scan_info->cpi = &work_ccb->cpi;
5280 /* Cache on our stack so we can work asynchronously */
5281 max_target = scan_info->cpi->max_target;
5282 initiator_id = scan_info->cpi->initiator_id;
5286 * We can scan all targets in parallel, or do it sequentially.
5288 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5290 scan_info->counter = 0;
5292 scan_info->counter = scan_info->cpi->max_target + 1;
5293 if (scan_info->cpi->initiator_id < scan_info->counter) {
5294 scan_info->counter--;
5298 for (i = 0; i <= max_target; i++) {
5300 if (i == initiator_id)
5303 status = xpt_create_path(&path, xpt_periph,
5304 request_ccb->ccb_h.path_id,
5306 if (status != CAM_REQ_CMP) {
5307 kprintf("xpt_scan_bus: xpt_create_path failed"
5308 " with status %#x, bus scan halted\n",
5310 kfree(scan_info, M_CAMXPT);
5311 request_ccb->ccb_h.status = status;
5312 xpt_free_ccb(work_ccb);
5313 xpt_done(request_ccb);
5316 work_ccb = xpt_alloc_ccb();
5317 xpt_setup_ccb(&work_ccb->ccb_h, path,
5318 request_ccb->ccb_h.pinfo.priority);
5319 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5320 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5321 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5322 work_ccb->crcn.flags = request_ccb->crcn.flags;
5323 xpt_action(work_ccb);
5330 struct cam_path *path;
5331 xpt_scan_bus_info *scan_info;
5333 target_id_t target_id;
5336 /* Reuse the same CCB to query if a device was really found */
5337 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5338 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5339 request_ccb->ccb_h.pinfo.priority);
5340 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5342 path_id = request_ccb->ccb_h.path_id;
5343 target_id = request_ccb->ccb_h.target_id;
5344 lun_id = request_ccb->ccb_h.target_lun;
5345 xpt_action(request_ccb);
5347 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5348 struct cam_ed *device;
5349 struct cam_et *target;
5353 * If we already probed lun 0 successfully, or
5354 * we have additional configured luns on this
5355 * target that might have "gone away", go onto
5358 target = request_ccb->ccb_h.path->target;
5360 * We may touch devices that we don't
5361 * hold references too, so ensure they
5362 * don't disappear out from under us.
5363 * The target above is referenced by the
5364 * path in the request ccb.
5367 device = TAILQ_FIRST(&target->ed_entries);
5368 if (device != NULL) {
5369 phl = CAN_SRCH_HI_SPARSE(device);
5370 if (device->lun_id == 0)
5371 device = TAILQ_NEXT(device, links);
5373 if ((lun_id != 0) || (device != NULL)) {
5374 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5378 struct cam_ed *device;
5380 device = request_ccb->ccb_h.path->device;
5382 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5383 /* Try the next lun */
5384 if (lun_id < (CAM_SCSI2_MAXLUN-1)
5385 || CAN_SRCH_HI_DENSE(device))
5391 * Free the current request path- we're done with it.
5393 xpt_free_path(request_ccb->ccb_h.path);
5396 * Check to see if we scan any further luns.
5398 if (lun_id == request_ccb->ccb_h.target_lun
5399 || lun_id > scan_info->cpi->max_lun) {
5404 if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
5405 scan_info->counter++;
5406 if (scan_info->counter ==
5407 scan_info->cpi->initiator_id) {
5408 scan_info->counter++;
5410 if (scan_info->counter >=
5411 scan_info->cpi->max_target+1) {
5415 scan_info->counter--;
5416 if (scan_info->counter == 0) {
5421 xpt_free_ccb(request_ccb);
5422 xpt_free_ccb((union ccb *)scan_info->cpi);
5423 request_ccb = scan_info->request_ccb;
5424 kfree(scan_info, M_CAMXPT);
5425 request_ccb->ccb_h.status = CAM_REQ_CMP;
5426 xpt_done(request_ccb);
5430 if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
5433 status = xpt_create_path(&path, xpt_periph,
5434 scan_info->request_ccb->ccb_h.path_id,
5435 scan_info->counter, 0);
5436 if (status != CAM_REQ_CMP) {
5437 kprintf("xpt_scan_bus: xpt_create_path failed"
5438 " with status %#x, bus scan halted\n",
5440 xpt_free_ccb(request_ccb);
5441 xpt_free_ccb((union ccb *)scan_info->cpi);
5442 request_ccb = scan_info->request_ccb;
5443 kfree(scan_info, M_CAMXPT);
5444 request_ccb->ccb_h.status = status;
5445 xpt_done(request_ccb);
5448 xpt_setup_ccb(&request_ccb->ccb_h, path,
5449 request_ccb->ccb_h.pinfo.priority);
5450 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5451 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5452 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5453 request_ccb->crcn.flags =
5454 scan_info->request_ccb->crcn.flags;
5456 status = xpt_create_path(&path, xpt_periph,
5457 path_id, target_id, lun_id);
5458 if (status != CAM_REQ_CMP) {
5459 kprintf("xpt_scan_bus: xpt_create_path failed "
5460 "with status %#x, halting LUN scan\n",
5464 xpt_setup_ccb(&request_ccb->ccb_h, path,
5465 request_ccb->ccb_h.pinfo.priority);
5466 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5467 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5468 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5469 request_ccb->crcn.flags =
5470 scan_info->request_ccb->crcn.flags;
5472 xpt_action(request_ccb);
5482 PROBE_INQUIRY, /* this counts as DV0 for Basic Domain Validation */
5487 PROBE_TUR_FOR_NEGOTIATION,
5488 PROBE_INQUIRY_BASIC_DV1,
5489 PROBE_INQUIRY_BASIC_DV2,
5494 static char *probe_action_text[] = {
5497 "PROBE_FULL_INQUIRY",
5499 "PROBE_SERIAL_NUM_0",
5500 "PROBE_SERIAL_NUM_1",
5501 "PROBE_TUR_FOR_NEGOTIATION",
5502 "PROBE_INQUIRY_BASIC_DV1",
5503 "PROBE_INQUIRY_BASIC_DV2",
5508 #define PROBE_SET_ACTION(softc, newaction) \
5511 text = probe_action_text; \
5512 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO, \
5513 ("Probe %s to %s\n", text[(softc)->action], \
5514 text[(newaction)])); \
5515 (softc)->action = (newaction); \
5519 PROBE_INQUIRY_CKSUM = 0x01,
5520 PROBE_SERIAL_CKSUM = 0x02,
5521 PROBE_NO_ANNOUNCE = 0x04
5525 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5526 probe_action action;
5527 union ccb saved_ccb;
5530 u_int8_t digest[16];
5531 struct cam_periph *periph;
5535 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5536 cam_flags flags, union ccb *request_ccb)
5538 struct ccb_pathinq cpi;
5540 struct cam_path *new_path;
5541 struct cam_periph *old_periph;
5543 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5544 ("xpt_scan_lun\n"));
5546 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5547 cpi.ccb_h.func_code = XPT_PATH_INQ;
5548 xpt_action((union ccb *)&cpi);
5550 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5551 if (request_ccb != NULL) {
5552 request_ccb->ccb_h.status = cpi.ccb_h.status;
5553 xpt_done(request_ccb);
5558 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5560 * Can't scan the bus on an adapter that
5561 * cannot perform the initiator role.
5563 if (request_ccb != NULL) {
5564 request_ccb->ccb_h.status = CAM_REQ_CMP;
5565 xpt_done(request_ccb);
5570 if (request_ccb == NULL) {
5571 request_ccb = kmalloc(sizeof(union ccb), M_CAMXPT, M_INTWAIT);
5572 new_path = kmalloc(sizeof(*new_path), M_CAMXPT, M_INTWAIT);
5573 status = xpt_compile_path(new_path, xpt_periph,
5575 path->target->target_id,
5576 path->device->lun_id);
5578 if (status != CAM_REQ_CMP) {
5579 xpt_print(path, "xpt_scan_lun: can't compile path, "
5580 "can't continue\n");
5581 kfree(request_ccb, M_CAMXPT);
5582 kfree(new_path, M_CAMXPT);
5585 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5586 request_ccb->ccb_h.cbfcnp = xptscandone;
5587 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5588 request_ccb->crcn.flags = flags;
5591 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5594 softc = (probe_softc *)old_periph->softc;
5595 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5598 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5599 probestart, "probe",
5601 request_ccb->ccb_h.path, NULL, 0,
5604 if (status != CAM_REQ_CMP) {
5605 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
5606 "returned an error, can't continue probe\n");
5607 request_ccb->ccb_h.status = status;
5608 xpt_done(request_ccb);
5614 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5616 xpt_release_path(done_ccb->ccb_h.path);
5617 kfree(done_ccb->ccb_h.path, M_CAMXPT);
5618 kfree(done_ccb, M_CAMXPT);
5622 proberegister(struct cam_periph *periph, void *arg)
5624 union ccb *request_ccb; /* CCB representing the probe request */
5628 request_ccb = (union ccb *)arg;
5629 if (periph == NULL) {
5630 kprintf("proberegister: periph was NULL!!\n");
5631 return(CAM_REQ_CMP_ERR);
5634 if (request_ccb == NULL) {
5635 kprintf("proberegister: no probe CCB, "
5636 "can't register device\n");
5637 return(CAM_REQ_CMP_ERR);
5640 softc = kmalloc(sizeof(*softc), M_CAMXPT, M_INTWAIT | M_ZERO);
5641 TAILQ_INIT(&softc->request_ccbs);
5642 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5645 periph->softc = softc;
5646 softc->periph = periph;
5647 softc->action = PROBE_INVALID;
5648 status = cam_periph_acquire(periph);
5649 if (status != CAM_REQ_CMP) {
5655 * Ensure we've waited at least a bus settle
5656 * delay before attempting to probe the device.
5657 * For HBAs that don't do bus resets, this won't make a difference.
5659 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5661 probeschedule(periph);
5662 return(CAM_REQ_CMP);
5666 probeschedule(struct cam_periph *periph)
5668 struct ccb_pathinq cpi;
5672 softc = (probe_softc *)periph->softc;
5673 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5675 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5676 cpi.ccb_h.func_code = XPT_PATH_INQ;
5677 xpt_action((union ccb *)&cpi);
5680 * If a device has gone away and another device, or the same one,
5681 * is back in the same place, it should have a unit attention
5682 * condition pending. It will not report the unit attention in
5683 * response to an inquiry, which may leave invalid transfer
5684 * negotiations in effect. The TUR will reveal the unit attention
5685 * condition. Only send the TUR for lun 0, since some devices
5686 * will get confused by commands other than inquiry to non-existent
5687 * luns. If you think a device has gone away start your scan from
5688 * lun 0. This will insure that any bogus transfer settings are
5691 * If we haven't seen the device before and the controller supports
5692 * some kind of transfer negotiation, negotiate with the first
5693 * sent command if no bus reset was performed at startup. This
5694 * ensures that the device is not confused by transfer negotiation
5695 * settings left over by loader or BIOS action.
5697 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5698 && (ccb->ccb_h.target_lun == 0)) {
5699 PROBE_SET_ACTION(softc, PROBE_TUR);
5700 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5701 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5702 proberequestdefaultnegotiation(periph);
5703 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
5705 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
5708 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5709 softc->flags |= PROBE_NO_ANNOUNCE;
5711 softc->flags &= ~PROBE_NO_ANNOUNCE;
5713 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5717 probestart(struct cam_periph *periph, union ccb *start_ccb)
5719 /* Probe the device that our peripheral driver points to */
5720 struct ccb_scsiio *csio;
5723 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5725 softc = (probe_softc *)periph->softc;
5726 csio = &start_ccb->csio;
5728 switch (softc->action) {
5730 case PROBE_TUR_FOR_NEGOTIATION:
5733 scsi_test_unit_ready(csio,
5742 case PROBE_FULL_INQUIRY:
5743 case PROBE_INQUIRY_BASIC_DV1:
5744 case PROBE_INQUIRY_BASIC_DV2:
5747 struct scsi_inquiry_data *inq_buf;
5749 inq_buf = &periph->path->device->inq_data;
5752 * If the device is currently configured, we calculate an
5753 * MD5 checksum of the inquiry data, and if the serial number
5754 * length is greater than 0, add the serial number data
5755 * into the checksum as well. Once the inquiry and the
5756 * serial number check finish, we attempt to figure out
5757 * whether we still have the same device.
5759 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5761 MD5Init(&softc->context);
5762 MD5Update(&softc->context, (unsigned char *)inq_buf,
5763 sizeof(struct scsi_inquiry_data));
5764 softc->flags |= PROBE_INQUIRY_CKSUM;
5765 if (periph->path->device->serial_num_len > 0) {
5766 MD5Update(&softc->context,
5767 periph->path->device->serial_num,
5768 periph->path->device->serial_num_len);
5769 softc->flags |= PROBE_SERIAL_CKSUM;
5771 MD5Final(softc->digest, &softc->context);
5774 if (softc->action == PROBE_INQUIRY)
5775 inquiry_len = SHORT_INQUIRY_LENGTH;
5777 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
5780 * Some parallel SCSI devices fail to send an
5781 * ignore wide residue message when dealing with
5782 * odd length inquiry requests. Round up to be
5785 inquiry_len = roundup2(inquiry_len, 2);
5787 if (softc->action == PROBE_INQUIRY_BASIC_DV1
5788 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
5789 inq_buf = kmalloc(inquiry_len, M_CAMXPT, M_INTWAIT);
5795 (u_int8_t *)inq_buf,
5800 /*timeout*/60 * 1000);
5803 case PROBE_MODE_SENSE:
5808 mode_buf_len = sizeof(struct scsi_mode_header_6)
5809 + sizeof(struct scsi_mode_blk_desc)
5810 + sizeof(struct scsi_control_page);
5811 mode_buf = kmalloc(mode_buf_len, M_CAMXPT, M_INTWAIT);
5812 scsi_mode_sense(csio,
5817 SMS_PAGE_CTRL_CURRENT,
5818 SMS_CONTROL_MODE_PAGE,
5825 case PROBE_SERIAL_NUM_0:
5827 struct scsi_vpd_supported_page_list *vpd_list = NULL;
5828 struct cam_ed *device;
5830 device = periph->path->device;
5831 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0) {
5832 vpd_list = kmalloc(sizeof(*vpd_list), M_CAMXPT,
5833 M_INTWAIT | M_ZERO);
5836 if (vpd_list != NULL) {
5841 (u_int8_t *)vpd_list,
5844 SVPD_SUPPORTED_PAGE_LIST,
5846 /*timeout*/60 * 1000);
5850 * We'll have to do without, let our probedone
5851 * routine finish up for us.
5853 start_ccb->csio.data_ptr = NULL;
5854 probedone(periph, start_ccb);
5857 case PROBE_SERIAL_NUM_1:
5859 struct scsi_vpd_unit_serial_number *serial_buf;
5860 struct cam_ed* device;
5863 device = periph->path->device;
5864 device->serial_num = NULL;
5865 device->serial_num_len = 0;
5867 serial_buf = (struct scsi_vpd_unit_serial_number *)
5868 kmalloc(sizeof(*serial_buf), M_CAMXPT,
5869 M_INTWAIT | M_ZERO);
5874 (u_int8_t *)serial_buf,
5875 sizeof(*serial_buf),
5877 SVPD_UNIT_SERIAL_NUMBER,
5879 /*timeout*/60 * 1000);
5883 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
5884 ("probestart: invalid action state\n"));
5888 xpt_action(start_ccb);
5892 proberequestdefaultnegotiation(struct cam_periph *periph)
5894 struct ccb_trans_settings cts;
5896 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5897 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5898 cts.type = CTS_TYPE_USER_SETTINGS;
5899 xpt_action((union ccb *)&cts);
5900 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5903 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5904 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5905 xpt_action((union ccb *)&cts);
5909 * Backoff Negotiation Code- only pertinent for SPI devices.
5912 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
5914 struct ccb_trans_settings cts;
5915 struct ccb_trans_settings_spi *spi;
5917 memset(&cts, 0, sizeof (cts));
5918 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5919 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5920 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5921 xpt_action((union ccb *)&cts);
5922 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5924 xpt_print(periph->path,
5925 "failed to get current device settings\n");
5929 if (cts.transport != XPORT_SPI) {
5931 xpt_print(periph->path, "not SPI transport\n");
5935 spi = &cts.xport_specific.spi;
5938 * We cannot renegotiate sync rate if we don't have one.
5940 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
5942 xpt_print(periph->path, "no sync rate known\n");
5948 * We'll assert that we don't have to touch PPR options- the
5949 * SIM will see what we do with period and offset and adjust
5950 * the PPR options as appropriate.
5954 * A sync rate with unknown or zero offset is nonsensical.
5955 * A sync period of zero means Async.
5957 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
5958 || spi->sync_offset == 0 || spi->sync_period == 0) {
5960 xpt_print(periph->path, "no sync rate available\n");
5965 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
5966 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
5967 ("hit async: giving up on DV\n"));
5973 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
5974 * We don't try to remember 'last' settings to see if the SIM actually
5975 * gets into the speed we want to set. We check on the SIM telling
5976 * us that a requested speed is bad, but otherwise don't try and
5977 * check the speed due to the asynchronous and handshake nature
5980 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
5983 if (spi->sync_period >= 0xf) {
5984 spi->sync_period = 0;
5985 spi->sync_offset = 0;
5986 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
5987 ("setting to async for DV\n"));
5989 * Once we hit async, we don't want to try
5990 * any more settings.
5992 device->flags |= CAM_DEV_DV_HIT_BOTTOM;
5993 } else if (bootverbose) {
5994 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
5995 ("DV: period 0x%x\n", spi->sync_period));
5996 kprintf("setting period to 0x%x\n", spi->sync_period);
5998 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5999 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6000 xpt_action((union ccb *)&cts);
6001 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6004 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6005 ("DV: failed to set period 0x%x\n", spi->sync_period));
6006 if (spi->sync_period == 0) {
6014 probedone(struct cam_periph *periph, union ccb *done_ccb)
6017 struct cam_path *path;
6020 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
6022 softc = (probe_softc *)periph->softc;
6023 path = done_ccb->ccb_h.path;
6024 priority = done_ccb->ccb_h.pinfo.priority;
6026 switch (softc->action) {
6029 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6031 if (cam_periph_error(done_ccb, 0,
6032 SF_NO_PRINT, NULL) == ERESTART)
6034 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
6035 /* Don't wedge the queue */
6036 xpt_release_devq(done_ccb->ccb_h.path,
6040 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
6041 xpt_release_ccb(done_ccb);
6042 xpt_schedule(periph, priority);
6046 case PROBE_FULL_INQUIRY:
6048 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6049 struct scsi_inquiry_data *inq_buf;
6050 u_int8_t periph_qual;
6052 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
6053 inq_buf = &path->device->inq_data;
6055 periph_qual = SID_QUAL(inq_buf);
6057 switch(periph_qual) {
6058 case SID_QUAL_LU_CONNECTED:
6063 * We conservatively request only
6064 * SHORT_INQUIRY_LEN bytes of inquiry
6065 * information during our first try
6066 * at sending an INQUIRY. If the device
6067 * has more information to give,
6068 * perform a second request specifying
6069 * the amount of information the device
6070 * is willing to give.
6072 len = inq_buf->additional_length
6073 + offsetof(struct scsi_inquiry_data,
6074 additional_length) + 1;
6075 if (softc->action == PROBE_INQUIRY
6076 && len > SHORT_INQUIRY_LENGTH) {
6077 PROBE_SET_ACTION(softc,
6078 PROBE_FULL_INQUIRY);
6079 xpt_release_ccb(done_ccb);
6080 xpt_schedule(periph, priority);
6084 xpt_find_quirk(path->device);
6086 xpt_devise_transport(path);
6087 if (INQ_DATA_TQ_ENABLED(inq_buf))
6088 PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
6090 PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_0);
6092 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6093 xpt_reference_device(path->device);
6095 xpt_release_ccb(done_ccb);
6096 xpt_schedule(periph, priority);
6102 } else if (cam_periph_error(done_ccb, 0,
6103 done_ccb->ccb_h.target_lun > 0
6104 ? SF_RETRY_UA|SF_QUIET_IR|SF_NO_PRINT
6105 : SF_RETRY_UA|SF_NO_PRINT,
6106 &softc->saved_ccb) == ERESTART) {
6108 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6109 /* Don't wedge the queue */
6110 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6114 * If we get to this point, we got an error status back
6115 * from the inquiry and the error status doesn't require
6116 * automatically retrying the command. Therefore, the
6117 * inquiry failed. If we had inquiry information before
6118 * for this device, but this latest inquiry command failed,
6119 * the device has probably gone away. If this device isn't
6120 * already marked unconfigured, notify the peripheral
6121 * drivers that this device is no more.
6123 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
6124 /* Send the async notification. */
6125 xpt_async(AC_LOST_DEVICE, path, NULL);
6128 xpt_release_ccb(done_ccb);
6131 case PROBE_MODE_SENSE:
6133 struct ccb_scsiio *csio;
6134 struct scsi_mode_header_6 *mode_hdr;
6136 csio = &done_ccb->csio;
6137 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
6138 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
6139 struct scsi_control_page *page;
6142 offset = ((u_int8_t *)&mode_hdr[1])
6143 + mode_hdr->blk_desc_len;
6144 page = (struct scsi_control_page *)offset;
6145 path->device->queue_flags = page->queue_flags;
6146 } else if (cam_periph_error(done_ccb, 0,
6147 SF_RETRY_UA|SF_NO_PRINT,
6148 &softc->saved_ccb) == ERESTART) {
6150 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6151 /* Don't wedge the queue */
6152 xpt_release_devq(done_ccb->ccb_h.path,
6153 /*count*/1, /*run_queue*/TRUE);
6155 xpt_release_ccb(done_ccb);
6156 kfree(mode_hdr, M_CAMXPT);
6157 PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_0);
6158 xpt_schedule(periph, priority);
6161 case PROBE_SERIAL_NUM_0:
6163 struct ccb_scsiio *csio;
6164 struct scsi_vpd_supported_page_list *page_list;
6165 int length, serialnum_supported, i;
6167 serialnum_supported = 0;
6168 csio = &done_ccb->csio;
6170 (struct scsi_vpd_supported_page_list *)csio->data_ptr;
6172 if (page_list == NULL) {
6174 * Don't process the command as it was never sent
6176 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
6177 && (page_list->length > 0)) {
6178 length = min(page_list->length,
6179 SVPD_SUPPORTED_PAGES_SIZE);
6180 for (i = 0; i < length; i++) {
6181 if (page_list->list[i] ==
6182 SVPD_UNIT_SERIAL_NUMBER) {
6183 serialnum_supported = 1;
6187 } else if (cam_periph_error(done_ccb, 0,
6188 SF_RETRY_UA|SF_NO_PRINT,
6189 &softc->saved_ccb) == ERESTART) {
6191 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6192 /* Don't wedge the queue */
6193 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6197 if (page_list != NULL)
6198 kfree(page_list, M_DEVBUF);
6200 if (serialnum_supported) {
6201 xpt_release_ccb(done_ccb);
6202 PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM_1);
6203 xpt_schedule(periph, priority);
6207 csio->data_ptr = NULL;
6211 case PROBE_SERIAL_NUM_1:
6213 struct ccb_scsiio *csio;
6214 struct scsi_vpd_unit_serial_number *serial_buf;
6221 csio = &done_ccb->csio;
6222 priority = done_ccb->ccb_h.pinfo.priority;
6224 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
6226 /* Clean up from previous instance of this device */
6227 if (path->device->serial_num != NULL) {
6228 kfree(path->device->serial_num, M_CAMXPT);
6229 path->device->serial_num = NULL;
6230 path->device->serial_num_len = 0;
6233 if (serial_buf == NULL) {
6235 * Don't process the command as it was never sent
6237 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
6238 && (serial_buf->length > 0)) {
6241 path->device->serial_num =
6242 kmalloc((serial_buf->length + 1),
6243 M_CAMXPT, M_INTWAIT);
6244 bcopy(serial_buf->serial_num,
6245 path->device->serial_num,
6246 serial_buf->length);
6247 path->device->serial_num_len = serial_buf->length;
6248 path->device->serial_num[serial_buf->length] = '\0';
6249 } else if (cam_periph_error(done_ccb, 0,
6250 SF_RETRY_UA|SF_NO_PRINT,
6251 &softc->saved_ccb) == ERESTART) {
6253 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6254 /* Don't wedge the queue */
6255 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6260 * Let's see if we have seen this device before.
6262 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
6264 u_int8_t digest[16];
6269 (unsigned char *)&path->device->inq_data,
6270 sizeof(struct scsi_inquiry_data));
6273 MD5Update(&context, serial_buf->serial_num,
6274 serial_buf->length);
6276 MD5Final(digest, &context);
6277 if (bcmp(softc->digest, digest, 16) == 0)
6281 * XXX Do we need to do a TUR in order to ensure
6282 * that the device really hasn't changed???
6285 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6286 xpt_async(AC_LOST_DEVICE, path, NULL);
6288 if (serial_buf != NULL)
6289 kfree(serial_buf, M_CAMXPT);
6293 * Now that we have all the necessary
6294 * information to safely perform transfer
6295 * negotiations... Controllers don't perform
6296 * any negotiation or tagged queuing until
6297 * after the first XPT_SET_TRAN_SETTINGS ccb is
6298 * received. So, on a new device, just retrieve
6299 * the user settings, and set them as the current
6300 * settings to set the device up.
6302 proberequestdefaultnegotiation(periph);
6303 xpt_release_ccb(done_ccb);
6306 * Perform a TUR to allow the controller to
6307 * perform any necessary transfer negotiation.
6309 PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
6310 xpt_schedule(periph, priority);
6313 xpt_release_ccb(done_ccb);
6316 case PROBE_TUR_FOR_NEGOTIATION:
6318 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6319 /* Don't wedge the queue */
6320 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6324 xpt_reference_device(path->device);
6326 * Do Domain Validation for lun 0 on devices that claim
6327 * to support Synchronous Transfer modes.
6329 if (softc->action == PROBE_TUR_FOR_NEGOTIATION
6330 && done_ccb->ccb_h.target_lun == 0
6331 && (path->device->inq_data.flags & SID_Sync) != 0
6332 && (path->device->flags & CAM_DEV_IN_DV) == 0) {
6333 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6334 ("Begin Domain Validation\n"));
6335 path->device->flags |= CAM_DEV_IN_DV;
6336 xpt_release_ccb(done_ccb);
6337 PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
6338 xpt_schedule(periph, priority);
6341 if (softc->action == PROBE_DV_EXIT) {
6342 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6343 ("Leave Domain Validation\n"));
6345 path->device->flags &=
6346 ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
6347 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6348 /* Inform the XPT that a new device has been found */
6349 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6350 xpt_action(done_ccb);
6351 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6354 xpt_release_ccb(done_ccb);
6356 case PROBE_INQUIRY_BASIC_DV1:
6357 case PROBE_INQUIRY_BASIC_DV2:
6359 struct scsi_inquiry_data *nbuf;
6360 struct ccb_scsiio *csio;
6362 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6363 /* Don't wedge the queue */
6364 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6367 csio = &done_ccb->csio;
6368 nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
6369 if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
6371 "inquiry data fails comparison at DV%d step\n",
6372 softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
6373 if (proberequestbackoff(periph, path->device)) {
6374 path->device->flags &= ~CAM_DEV_IN_DV;
6375 PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
6378 PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
6380 kfree(nbuf, M_CAMXPT);
6381 xpt_release_ccb(done_ccb);
6382 xpt_schedule(periph, priority);
6385 kfree(nbuf, M_CAMXPT);
6386 if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
6387 PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
6388 xpt_release_ccb(done_ccb);
6389 xpt_schedule(periph, priority);
6392 if (softc->action == PROBE_DV_EXIT) {
6393 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
6394 ("Leave Domain Validation Successfully\n"));
6396 path->device->flags &=
6397 ~(CAM_DEV_UNCONFIGURED|CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
6398 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6399 /* Inform the XPT that a new device has been found */
6400 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6401 xpt_action(done_ccb);
6402 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6405 xpt_release_ccb(done_ccb);
6409 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
6410 ("probedone: invalid action state\n"));
6414 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6415 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6416 done_ccb->ccb_h.status = CAM_REQ_CMP;
6418 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6419 cam_periph_invalidate(periph);
6420 cam_periph_release(periph);
6422 probeschedule(periph);
6427 probecleanup(struct cam_periph *periph)
6429 kfree(periph->softc, M_CAMXPT);
6433 xpt_find_quirk(struct cam_ed *device)
6437 match = cam_quirkmatch((caddr_t)&device->inq_data,
6438 (caddr_t)xpt_quirk_table,
6439 NELEM(xpt_quirk_table),
6440 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6443 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6445 device->quirk = (struct xpt_quirk_entry *)match;
6449 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
6453 lbool = cam_srch_hi;
6454 error = sysctl_handle_int(oidp, &lbool, 0, req);
6455 if (error != 0 || req->newptr == NULL)
6457 if (lbool == 0 || lbool == 1) {
6458 cam_srch_hi = lbool;
6466 xpt_devise_transport(struct cam_path *path)
6468 struct ccb_pathinq cpi;
6469 struct ccb_trans_settings cts;
6470 struct scsi_inquiry_data *inq_buf;
6472 /* Get transport information from the SIM */
6473 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6474 cpi.ccb_h.func_code = XPT_PATH_INQ;
6475 xpt_action((union ccb *)&cpi);
6478 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6479 inq_buf = &path->device->inq_data;
6480 path->device->protocol = PROTO_SCSI;
6481 path->device->protocol_version =
6482 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6483 path->device->transport = cpi.transport;
6484 path->device->transport_version = cpi.transport_version;
6487 * Any device not using SPI3 features should
6488 * be considered SPI2 or lower.
6490 if (inq_buf != NULL) {
6491 if (path->device->transport == XPORT_SPI
6492 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6493 && path->device->transport_version > 2)
6494 path->device->transport_version = 2;
6496 struct cam_ed* otherdev;
6498 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6500 otherdev = TAILQ_NEXT(otherdev, links)) {
6501 if (otherdev != path->device)
6505 if (otherdev != NULL) {
6507 * Initially assume the same versioning as
6508 * prior luns for this target.
6510 path->device->protocol_version =
6511 otherdev->protocol_version;
6512 path->device->transport_version =
6513 otherdev->transport_version;
6515 /* Until we know better, opt for safty */
6516 path->device->protocol_version = 2;
6517 if (path->device->transport == XPORT_SPI)
6518 path->device->transport_version = 2;
6520 path->device->transport_version = 0;
6526 * For a device compliant with SPC-2 we should be able
6527 * to determine the transport version supported by
6528 * scrutinizing the version descriptors in the
6532 /* Tell the controller what we think */
6533 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6534 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6535 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6536 cts.transport = path->device->transport;
6537 cts.transport_version = path->device->transport_version;
6538 cts.protocol = path->device->protocol;
6539 cts.protocol_version = path->device->protocol_version;
6540 cts.proto_specific.valid = 0;
6541 cts.xport_specific.valid = 0;
6542 xpt_action((union ccb *)&cts);
6546 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6549 struct ccb_pathinq cpi;
6550 struct ccb_trans_settings cur_cts;
6551 struct ccb_trans_settings_scsi *scsi;
6552 struct ccb_trans_settings_scsi *cur_scsi;
6553 struct cam_sim *sim;
6554 struct scsi_inquiry_data *inq_data;
6556 if (device == NULL) {
6557 cts->ccb_h.status = CAM_PATH_INVALID;
6558 xpt_done((union ccb *)cts);
6562 if (cts->protocol == PROTO_UNKNOWN
6563 || cts->protocol == PROTO_UNSPECIFIED) {
6564 cts->protocol = device->protocol;
6565 cts->protocol_version = device->protocol_version;
6568 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6569 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6570 cts->protocol_version = device->protocol_version;
6572 if (cts->protocol != device->protocol) {
6573 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
6574 cts->protocol, device->protocol);
6575 cts->protocol = device->protocol;
6578 if (cts->protocol_version > device->protocol_version) {
6580 xpt_print(cts->ccb_h.path, "Down reving Protocol "
6581 "Version from %d to %d?\n", cts->protocol_version,
6582 device->protocol_version);
6584 cts->protocol_version = device->protocol_version;
6587 if (cts->transport == XPORT_UNKNOWN
6588 || cts->transport == XPORT_UNSPECIFIED) {
6589 cts->transport = device->transport;
6590 cts->transport_version = device->transport_version;
6593 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6594 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6595 cts->transport_version = device->transport_version;
6597 if (cts->transport != device->transport) {
6598 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
6599 cts->transport, device->transport);
6600 cts->transport = device->transport;
6603 if (cts->transport_version > device->transport_version) {
6605 xpt_print(cts->ccb_h.path, "Down reving Transport "
6606 "Version from %d to %d?\n", cts->transport_version,
6607 device->transport_version);
6609 cts->transport_version = device->transport_version;
6612 sim = cts->ccb_h.path->bus->sim;
6615 * Nothing more of interest to do unless
6616 * this is a device connected via the
6619 if (cts->protocol != PROTO_SCSI) {
6620 if (async_update == FALSE)
6621 (*(sim->sim_action))(sim, (union ccb *)cts);
6625 inq_data = &device->inq_data;
6626 scsi = &cts->proto_specific.scsi;
6627 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6628 cpi.ccb_h.func_code = XPT_PATH_INQ;
6629 xpt_action((union ccb *)&cpi);
6631 /* SCSI specific sanity checking */
6632 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6633 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
6634 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6635 || (device->quirk->mintags == 0)) {
6637 * Can't tag on hardware that doesn't support tags,
6638 * doesn't have it enabled, or has broken tag support.
6640 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6643 if (async_update == FALSE) {
6645 * Perform sanity checking against what the
6646 * controller and device can do.
6648 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6649 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6650 cur_cts.type = cts->type;
6651 xpt_action((union ccb *)&cur_cts);
6652 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6655 cur_scsi = &cur_cts.proto_specific.scsi;
6656 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6657 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6658 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6660 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6661 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6664 /* SPI specific sanity checking */
6665 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6667 struct ccb_trans_settings_spi *spi;
6668 struct ccb_trans_settings_spi *cur_spi;
6670 spi = &cts->xport_specific.spi;
6672 cur_spi = &cur_cts.xport_specific.spi;
6674 /* Fill in any gaps in what the user gave us */
6675 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6676 spi->sync_period = cur_spi->sync_period;
6677 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6678 spi->sync_period = 0;
6679 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6680 spi->sync_offset = cur_spi->sync_offset;
6681 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6682 spi->sync_offset = 0;
6683 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6684 spi->ppr_options = cur_spi->ppr_options;
6685 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6686 spi->ppr_options = 0;
6687 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6688 spi->bus_width = cur_spi->bus_width;
6689 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6691 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6692 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6693 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6695 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6696 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6697 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6698 && (inq_data->flags & SID_Sync) == 0
6699 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6700 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
6702 spi->sync_period = 0;
6703 spi->sync_offset = 0;
6706 switch (spi->bus_width) {
6707 case MSG_EXT_WDTR_BUS_32_BIT:
6708 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6709 || (inq_data->flags & SID_WBus32) != 0
6710 || cts->type == CTS_TYPE_USER_SETTINGS)
6711 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6713 /* Fall Through to 16-bit */
6714 case MSG_EXT_WDTR_BUS_16_BIT:
6715 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6716 || (inq_data->flags & SID_WBus16) != 0
6717 || cts->type == CTS_TYPE_USER_SETTINGS)
6718 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6719 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6722 /* Fall Through to 8-bit */
6723 default: /* New bus width?? */
6724 case MSG_EXT_WDTR_BUS_8_BIT:
6725 /* All targets can do this */
6726 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6730 spi3caps = cpi.xport_specific.spi.ppr_options;
6731 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6732 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6733 spi3caps &= inq_data->spi3data;
6735 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6736 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6738 if ((spi3caps & SID_SPI_IUS) == 0)
6739 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6741 if ((spi3caps & SID_SPI_QAS) == 0)
6742 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6744 /* No SPI Transfer settings are allowed unless we are wide */
6745 if (spi->bus_width == 0)
6746 spi->ppr_options = 0;
6748 if ((spi->valid & CTS_SPI_VALID_DISC)
6749 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
6751 * Can't tag queue without disconnection.
6753 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6754 scsi->valid |= CTS_SCSI_VALID_TQ;
6758 * If we are currently performing tagged transactions to
6759 * this device and want to change its negotiation parameters,
6760 * go non-tagged for a bit to give the controller a chance to
6761 * negotiate unhampered by tag messages.
6763 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6764 && (device->inq_flags & SID_CmdQue) != 0
6765 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6766 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6767 CTS_SPI_VALID_SYNC_OFFSET|
6768 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6769 xpt_toggle_tags(cts->ccb_h.path);
6772 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6773 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6777 * If we are transitioning from tags to no-tags or
6778 * vice-versa, we need to carefully freeze and restart
6779 * the queue so that we don't overlap tagged and non-tagged
6780 * commands. We also temporarily stop tags if there is
6781 * a change in transfer negotiation settings to allow
6782 * "tag-less" negotiation.
6784 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6785 || (device->inq_flags & SID_CmdQue) != 0)
6786 device_tagenb = TRUE;
6788 device_tagenb = FALSE;
6790 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6791 && device_tagenb == FALSE)
6792 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6793 && device_tagenb == TRUE)) {
6795 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6797 * Delay change to use tags until after a
6798 * few commands have gone to this device so
6799 * the controller has time to perform transfer
6800 * negotiations without tagged messages getting
6803 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6804 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6806 struct ccb_relsim crs;
6808 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6809 device->inq_flags &= ~SID_CmdQue;
6810 xpt_dev_ccbq_resize(cts->ccb_h.path,
6811 sim->max_dev_openings);
6812 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6813 device->tag_delay_count = 0;
6815 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6817 crs.ccb_h.func_code = XPT_REL_SIMQ;
6818 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6820 = crs.release_timeout
6823 xpt_action((union ccb *)&crs);
6827 if (async_update == FALSE)
6828 (*(sim->sim_action))(sim, (union ccb *)cts);
6832 xpt_toggle_tags(struct cam_path *path)
6837 * Give controllers a chance to renegotiate
6838 * before starting tag operations. We
6839 * "toggle" tagged queuing off then on
6840 * which causes the tag enable command delay
6841 * counter to come into effect.
6844 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6845 || ((dev->inq_flags & SID_CmdQue) != 0
6846 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6847 struct ccb_trans_settings cts;
6849 xpt_setup_ccb(&cts.ccb_h, path, 1);
6850 cts.protocol = PROTO_SCSI;
6851 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6852 cts.transport = XPORT_UNSPECIFIED;
6853 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6854 cts.proto_specific.scsi.flags = 0;
6855 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6856 xpt_set_transfer_settings(&cts, path->device,
6857 /*async_update*/TRUE);
6858 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6859 xpt_set_transfer_settings(&cts, path->device,
6860 /*async_update*/TRUE);
6865 xpt_start_tags(struct cam_path *path)
6867 struct ccb_relsim crs;
6868 struct cam_ed *device;
6869 struct cam_sim *sim;
6872 device = path->device;
6873 sim = path->bus->sim;
6874 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6875 xpt_freeze_devq(path, /*count*/1);
6876 device->inq_flags |= SID_CmdQue;
6877 if (device->tag_saved_openings != 0)
6878 newopenings = device->tag_saved_openings;
6880 newopenings = min(device->quirk->maxtags,
6881 sim->max_tagged_dev_openings);
6882 xpt_dev_ccbq_resize(path, newopenings);
6883 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6884 crs.ccb_h.func_code = XPT_REL_SIMQ;
6885 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6887 = crs.release_timeout
6890 xpt_action((union ccb *)&crs);
6893 static int busses_to_config;
6894 static int busses_to_reset;
6897 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6899 sim_lock_assert_owned(bus->sim->lock);
6901 if (bus->counted_to_config == 0 && bus->path_id != CAM_XPT_PATH_ID) {
6902 struct cam_path path;
6903 struct ccb_pathinq cpi;
6907 kprintf("CAM: Configuring bus:");
6911 bus->sim->unit_number);
6913 kprintf(" (unknown)\n");
6916 atomic_add_int(&busses_to_config, 1);
6917 bus->counted_to_config = 1;
6918 xpt_compile_path(&path, NULL, bus->path_id,
6919 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6920 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6921 cpi.ccb_h.func_code = XPT_PATH_INQ;
6922 xpt_action((union ccb *)&cpi);
6923 can_negotiate = cpi.hba_inquiry;
6924 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6925 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0 && can_negotiate)
6927 xpt_release_path(&path);
6929 if (bus->counted_to_config == 0 && bus->path_id == CAM_XPT_PATH_ID) {
6930 /* this is our dummy periph/bus */
6931 atomic_add_int(&busses_to_config, 1);
6932 bus->counted_to_config = 1;
6939 xptconfigfunc(struct cam_eb *bus, void *arg)
6941 struct cam_path *path;
6942 union ccb *work_ccb;
6944 sim_lock_assert_owned(bus->sim->lock);
6946 if (bus->path_id != CAM_XPT_PATH_ID) {
6950 work_ccb = xpt_alloc_ccb();
6951 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6952 CAM_TARGET_WILDCARD,
6953 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6954 kprintf("xptconfigfunc: xpt_create_path failed with "
6955 "status %#x for bus %d\n", status, bus->path_id);
6956 kprintf("xptconfigfunc: halting bus configuration\n");
6957 xpt_free_ccb(work_ccb);
6958 xpt_uncount_bus(bus);
6961 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6962 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6963 xpt_action(work_ccb);
6964 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6965 kprintf("xptconfigfunc: CPI failed on bus %d "
6966 "with status %d\n", bus->path_id,
6967 work_ccb->ccb_h.status);
6968 xpt_finishconfig(xpt_periph, work_ccb);
6972 can_negotiate = work_ccb->cpi.hba_inquiry;
6973 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6974 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6975 && (can_negotiate != 0)) {
6976 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6977 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6978 work_ccb->ccb_h.cbfcnp = NULL;
6979 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6980 ("Resetting Bus\n"));
6981 xpt_action(work_ccb);
6982 xpt_finishconfig(xpt_periph, work_ccb);
6984 /* Act as though we performed a successful BUS RESET */
6985 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6986 xpt_finishconfig(xpt_periph, work_ccb);
6989 xpt_uncount_bus(bus);
6996 * Now that interrupts are enabled, go find our devices.
6998 * This hook function is called once by run_interrupt_driven_config_hooks().
6999 * XPT is expected to disestablish its hook when done.
7002 xpt_config(void *arg)
7006 /* Setup debugging flags and path */
7007 #ifdef CAM_DEBUG_FLAGS
7008 cam_dflags = CAM_DEBUG_FLAGS;
7009 #else /* !CAM_DEBUG_FLAGS */
7010 cam_dflags = CAM_DEBUG_NONE;
7011 #endif /* CAM_DEBUG_FLAGS */
7012 #ifdef CAM_DEBUG_BUS
7013 if (cam_dflags != CAM_DEBUG_NONE) {
7015 * Locking is specifically omitted here. No SIMs have
7016 * registered yet, so xpt_create_path will only be searching
7017 * empty lists of targets and devices.
7019 if (xpt_create_path(&cam_dpath, xpt_periph,
7020 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
7021 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
7022 kprintf("xpt_config: xpt_create_path() failed for debug"
7023 " target %d:%d:%d, debugging disabled\n",
7024 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
7025 cam_dflags = CAM_DEBUG_NONE;
7030 #else /* !CAM_DEBUG_BUS */
7032 #endif /* CAM_DEBUG_BUS */
7033 #endif /* CAMDEBUG */
7036 * Scan all installed busses. This will also add a count
7037 * for our dummy placeholder (xpt_periph).
7039 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
7041 kprintf("CAM: Configuring %d busses\n", busses_to_config - 1);
7042 if (busses_to_reset > 0 && scsi_delay >= 2000) {
7043 kprintf("Waiting %d seconds for SCSI "
7044 "devices to settle\n",
7047 xpt_for_all_busses(xptconfigfunc, NULL);
7051 * If the given device only has one peripheral attached to it, and if that
7052 * peripheral is the passthrough driver, announce it. This insures that the
7053 * user sees some sort of announcement for every peripheral in their system.
7056 xptpassannouncefunc(struct cam_ed *device, void *arg)
7058 struct cam_periph *periph;
7061 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
7062 periph = SLIST_NEXT(periph, periph_links), i++);
7064 periph = SLIST_FIRST(&device->periphs);
7066 && (strncmp(periph->periph_name, "pass", 4) == 0))
7067 xpt_announce_periph(periph, NULL);
7073 xpt_finishconfig_task(void *context, int pending)
7075 struct periph_driver **p_drv;
7078 kprintf("CAM: finished configuring all busses\n");
7080 if (busses_to_config == 0) {
7081 /* Register all the peripheral drivers */
7082 /* XXX This will have to change when we have loadable modules */
7083 p_drv = periph_drivers;
7084 for (i = 0; p_drv[i] != NULL; i++) {
7085 (*p_drv[i]->init)();
7089 * Check for devices with no "standard" peripheral driver
7090 * attached. For any devices like that, announce the
7091 * passthrough driver so the user will see something.
7093 xpt_for_all_devices(xptpassannouncefunc, NULL);
7095 /* Release our hook so that the boot can continue. */
7096 config_intrhook_disestablish(xsoftc.xpt_config_hook);
7097 kfree(xsoftc.xpt_config_hook, M_CAMXPT);
7098 xsoftc.xpt_config_hook = NULL;
7100 kfree(context, M_CAMXPT);
7104 xpt_uncount_bus (struct cam_eb *bus)
7106 struct xpt_task *task;
7108 if (bus->counted_to_config) {
7109 bus->counted_to_config = 0;
7110 if (atomic_fetchadd_int(&busses_to_config, -1) == 1) {
7111 task = kmalloc(sizeof(struct xpt_task), M_CAMXPT,
7112 M_INTWAIT | M_ZERO);
7113 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
7114 taskqueue_enqueue(taskqueue_thread[mycpuid],
7121 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
7123 struct cam_path *path;
7125 path = done_ccb->ccb_h.path;
7126 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_finishconfig\n"));
7128 switch(done_ccb->ccb_h.func_code) {
7130 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
7131 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
7132 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
7133 done_ccb->crcn.flags = 0;
7134 xpt_action(done_ccb);
7141 kprintf("CAM: Finished configuring bus:");
7142 if (path->bus->sim) {
7144 path->bus->sim->sim_name,
7145 path->bus->sim->unit_number);
7147 kprintf(" (unknown)\n");
7150 xpt_uncount_bus(path->bus);
7151 xpt_free_path(path);
7152 xpt_free_ccb(done_ccb);
7158 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
7159 struct cam_path *path)
7161 struct ccb_setasync csa;
7166 lockmgr(&xsoftc.xpt_lock, LK_EXCLUSIVE);
7167 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
7168 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
7169 if (status != CAM_REQ_CMP) {
7170 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
7176 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
7177 csa.ccb_h.func_code = XPT_SASYNC_CB;
7178 csa.event_enable = event;
7179 csa.callback = cbfunc;
7180 csa.callback_arg = cbarg;
7181 xpt_action((union ccb *)&csa);
7182 status = csa.ccb_h.status;
7184 xpt_free_path(path);
7185 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
7191 xptaction(struct cam_sim *sim, union ccb *work_ccb)
7193 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
7195 switch (work_ccb->ccb_h.func_code) {
7196 /* Common cases first */
7197 case XPT_PATH_INQ: /* Path routing inquiry */
7199 struct ccb_pathinq *cpi;
7201 cpi = &work_ccb->cpi;
7202 cpi->version_num = 1; /* XXX??? */
7203 cpi->hba_inquiry = 0;
7204 cpi->target_sprt = 0;
7206 cpi->hba_eng_cnt = 0;
7207 cpi->max_target = 0;
7209 cpi->initiator_id = 0;
7210 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
7211 strncpy(cpi->hba_vid, "", HBA_IDLEN);
7212 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
7213 cpi->unit_number = sim->unit_number;
7214 cpi->bus_id = sim->bus_id;
7215 cpi->base_transfer_speed = 0;
7216 cpi->protocol = PROTO_UNSPECIFIED;
7217 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
7218 cpi->transport = XPORT_UNSPECIFIED;
7219 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
7220 cpi->ccb_h.status = CAM_REQ_CMP;
7225 work_ccb->ccb_h.status = CAM_REQ_INVALID;
7232 * The xpt as a "controller" has no interrupt sources, so polling
7236 xptpoll(struct cam_sim *sim)
7241 xpt_lock_buses(void)
7243 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
7247 xpt_unlock_buses(void)
7249 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
7254 * Should only be called by the machine interrupt dispatch routines,
7255 * so put these prototypes here instead of in the header.
7259 swi_cambio(void *arg, void *frame)
7268 struct cam_sim *sim;
7270 spin_lock(&cam_simq_spin);
7272 TAILQ_CONCAT(&queue, &cam_simq, links);
7273 spin_unlock(&cam_simq_spin);
7275 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
7276 TAILQ_REMOVE(&queue, sim, links);
7278 sim->flags &= ~CAM_SIM_ON_DONEQ;
7279 camisr_runqueue(sim);
7280 CAM_SIM_UNLOCK(sim);
7285 camisr_runqueue(struct cam_sim *sim)
7287 struct ccb_hdr *ccb_h;
7290 spin_lock(&sim->sim_spin);
7291 while ((ccb_h = TAILQ_FIRST(&sim->sim_doneq)) != NULL) {
7292 TAILQ_REMOVE(&sim->sim_doneq, ccb_h, sim_links.tqe);
7293 spin_unlock(&sim->sim_spin);
7294 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7296 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7301 if (ccb_h->flags & CAM_HIGH_POWER) {
7302 struct highpowerlist *hphead;
7303 union ccb *send_ccb;
7305 lockmgr(&xsoftc.xpt_lock, LK_EXCLUSIVE);
7306 hphead = &xsoftc.highpowerq;
7308 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7311 * Increment the count since this command is done.
7313 xsoftc.num_highpower++;
7316 * Any high powered commands queued up?
7318 if (send_ccb != NULL) {
7319 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7320 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
7322 xpt_release_devq(send_ccb->ccb_h.path,
7323 /*count*/1, /*runqueue*/TRUE);
7325 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
7328 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7331 dev = ccb_h->path->device;
7333 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7336 * devq may be NULL if this is cam_dead_sim
7338 if (ccb_h->path->bus->sim->devq) {
7339 ccb_h->path->bus->sim->devq->send_active--;
7340 ccb_h->path->bus->sim->devq->send_openings++;
7343 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7344 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7345 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7346 && (dev->ccbq.dev_active == 0))) {
7348 xpt_release_devq(ccb_h->path, /*count*/1,
7352 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7353 && (--dev->tag_delay_count == 0))
7354 xpt_start_tags(ccb_h->path);
7356 if ((dev->ccbq.queue.entries > 0)
7357 && (dev->qfrozen_cnt == 0)
7358 && (device_is_send_queued(dev) == 0)) {
7359 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7364 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7365 xpt_release_simq(ccb_h->path->bus->sim,
7367 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7371 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7372 && (ccb_h->status & CAM_DEV_QFRZN)) {
7373 xpt_release_devq(ccb_h->path, /*count*/1,
7375 ccb_h->status &= ~CAM_DEV_QFRZN;
7377 xpt_run_dev_sendq(ccb_h->path->bus);
7380 /* Call the peripheral driver's callback */
7381 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7382 spin_lock(&sim->sim_spin);
7384 spin_unlock(&sim->sim_spin);
7388 * The dead_sim isn't completely hooked into CAM, we have to make sure
7389 * the doneq is cleared after calling xpt_done() so cam_periph_ccbwait()
7393 dead_sim_action(struct cam_sim *sim, union ccb *ccb)
7396 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
7398 camisr_runqueue(sim);
7402 dead_sim_poll(struct cam_sim *sim)