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 $
30 * $DragonFly: src/sys/bus/cam/cam_xpt.c,v 1.66 2008/06/29 19:15:34 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/fcntl.h>
42 #include <sys/devicestat.h>
43 #include <sys/interrupt.h>
45 #include <sys/taskqueue.h>
47 #include <sys/thread.h>
49 #include <sys/spinlock.h>
50 #include <sys/thread2.h>
51 #include <sys/spinlock2.h>
53 #include <machine/clock.h>
54 #include <machine/stdarg.h>
58 #include "cam_periph.h"
61 #include "cam_xpt_sim.h"
62 #include "cam_xpt_periph.h"
63 #include "cam_debug.h"
65 #include "scsi/scsi_all.h"
66 #include "scsi/scsi_message.h"
67 #include "scsi/scsi_pass.h"
68 #include <sys/kthread.h>
71 /* Datastructures internal to the xpt layer */
72 MALLOC_DEFINE(M_CAMXPT
, "CAM XPT", "CAM XPT buffers");
74 /* Object for defering XPT actions to a taskqueue */
82 * Definition of an async handler callback block. These are used to add
83 * SIMs and peripherals to the async callback lists.
86 SLIST_ENTRY(async_node
) links
;
87 u_int32_t event_enable
; /* Async Event enables */
88 void (*callback
)(void *arg
, u_int32_t code
,
89 struct cam_path
*path
, void *args
);
93 SLIST_HEAD(async_list
, async_node
);
94 SLIST_HEAD(periph_list
, cam_periph
);
97 * This is the maximum number of high powered commands (e.g. start unit)
98 * that can be outstanding at a particular time.
100 #ifndef CAM_MAX_HIGHPOWER
101 #define CAM_MAX_HIGHPOWER 4
105 * Structure for queueing a device in a run queue.
106 * There is one run queue for allocating new ccbs,
107 * and another for sending ccbs to the controller.
109 struct cam_ed_qinfo
{
111 struct cam_ed
*device
;
115 * The CAM EDT (Existing Device Table) contains the device information for
116 * all devices for all busses in the system. The table contains a
117 * cam_ed structure for each device on the bus.
120 TAILQ_ENTRY(cam_ed
) links
;
121 struct cam_ed_qinfo alloc_ccb_entry
;
122 struct cam_ed_qinfo send_ccb_entry
;
123 struct cam_et
*target
;
127 * Queue of type drivers wanting to do
128 * work on this device.
130 struct cam_ccbq ccbq
; /* Queue of pending ccbs */
131 struct async_list asyncs
; /* Async callback info for this B/T/L */
132 struct periph_list periphs
; /* All attached devices */
133 u_int generation
; /* Generation number */
134 struct cam_periph
*owner
; /* Peripheral driver's ownership tag */
135 struct xpt_quirk_entry
*quirk
; /* Oddities about this device */
136 /* Storage for the inquiry data */
138 u_int protocol_version
;
140 u_int transport_version
;
141 struct scsi_inquiry_data inq_data
;
142 u_int8_t inq_flags
; /*
143 * Current settings for inquiry flags.
144 * This allows us to override settings
145 * like disconnection and tagged
146 * queuing for a device.
148 u_int8_t queue_flags
; /* Queue flags from the control page */
149 u_int8_t serial_num_len
;
150 u_int8_t
*serial_num
;
151 u_int32_t qfrozen_cnt
;
153 #define CAM_DEV_UNCONFIGURED 0x01
154 #define CAM_DEV_REL_TIMEOUT_PENDING 0x02
155 #define CAM_DEV_REL_ON_COMPLETE 0x04
156 #define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
157 #define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
158 #define CAM_DEV_TAG_AFTER_COUNT 0x20
159 #define CAM_DEV_INQUIRY_DATA_VALID 0x40
160 #define CAM_DEV_IN_DV 0x80
161 #define CAM_DEV_DV_HIT_BOTTOM 0x100
162 u_int32_t tag_delay_count
;
163 #define CAM_TAG_DELAY_COUNT 5
164 u_int32_t tag_saved_openings
;
166 struct callout callout
;
170 * Each target is represented by an ET (Existing Target). These
171 * entries are created when a target is successfully probed with an
172 * identify, and removed when a device fails to respond after a number
173 * of retries, or a bus rescan finds the device missing.
176 TAILQ_HEAD(, cam_ed
) ed_entries
;
177 TAILQ_ENTRY(cam_et
) links
;
179 target_id_t target_id
;
182 struct timeval last_reset
; /* uptime of last reset */
186 * Each bus is represented by an EB (Existing Bus). These entries
187 * are created by calls to xpt_bus_register and deleted by calls to
188 * xpt_bus_deregister.
191 TAILQ_HEAD(, cam_et
) et_entries
;
192 TAILQ_ENTRY(cam_eb
) links
;
195 struct timeval last_reset
; /* uptime of last reset */
197 #define CAM_EB_RUNQ_SCHEDULED 0x01
203 struct cam_periph
*periph
;
205 struct cam_et
*target
;
206 struct cam_ed
*device
;
209 struct xpt_quirk_entry
{
210 struct scsi_inquiry_pattern inq_pat
;
212 #define CAM_QUIRK_NOLUNS 0x01
213 #define CAM_QUIRK_NOSERIAL 0x02
214 #define CAM_QUIRK_HILUNS 0x04
215 #define CAM_QUIRK_NOHILUNS 0x08
220 static int cam_srch_hi
= 0;
221 TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi
);
222 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS
);
223 SYSCTL_PROC(_kern_cam
, OID_AUTO
, cam_srch_hi
, CTLTYPE_INT
|CTLFLAG_RW
, 0, 0,
224 sysctl_cam_search_luns
, "I",
225 "allow search above LUN 7 for SCSI3 and greater devices");
227 #define CAM_SCSI2_MAXLUN 8
229 * If we're not quirked to search <= the first 8 luns
230 * and we are either quirked to search above lun 8,
231 * or we're > SCSI-2 and we've enabled hilun searching,
232 * or we're > SCSI-2 and the last lun was a success,
233 * we can look for luns above lun 8.
235 #define CAN_SRCH_HI_SPARSE(dv) \
236 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
237 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
238 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
240 #define CAN_SRCH_HI_DENSE(dv) \
241 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
242 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
243 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
251 u_int32_t xpt_generation
;
253 /* number of high powered commands that can go through right now */
254 STAILQ_HEAD(highpowerlist
, ccb_hdr
) highpowerq
;
257 /* queue for handling async rescan requests. */
258 TAILQ_HEAD(, ccb_hdr
) ccb_scanq
;
260 /* Registered busses */
261 TAILQ_HEAD(,cam_eb
) xpt_busses
;
262 u_int bus_generation
;
264 struct intr_config_hook
*xpt_config_hook
;
266 struct lock xpt_topo_lock
;
267 struct lock xpt_lock
;
270 static const char quantum
[] = "QUANTUM";
271 static const char sony
[] = "SONY";
272 static const char west_digital
[] = "WDIGTL";
273 static const char samsung
[] = "SAMSUNG";
274 static const char seagate
[] = "SEAGATE";
275 static const char microp
[] = "MICROP";
277 static struct xpt_quirk_entry xpt_quirk_table
[] =
280 /* Reports QUEUE FULL for temporary resource shortages */
281 { T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "XP39100*", "*" },
282 /*quirks*/0, /*mintags*/24, /*maxtags*/32
285 /* Reports QUEUE FULL for temporary resource shortages */
286 { T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "XP34550*", "*" },
287 /*quirks*/0, /*mintags*/24, /*maxtags*/32
290 /* Reports QUEUE FULL for temporary resource shortages */
291 { T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "XP32275*", "*" },
292 /*quirks*/0, /*mintags*/24, /*maxtags*/32
295 /* Broken tagged queuing drive */
296 { T_DIRECT
, SIP_MEDIA_FIXED
, microp
, "4421-07*", "*" },
297 /*quirks*/0, /*mintags*/0, /*maxtags*/0
300 /* Broken tagged queuing drive */
301 { T_DIRECT
, SIP_MEDIA_FIXED
, "HP", "C372*", "*" },
302 /*quirks*/0, /*mintags*/0, /*maxtags*/0
305 /* Broken tagged queuing drive */
306 { T_DIRECT
, SIP_MEDIA_FIXED
, microp
, "3391*", "x43h" },
307 /*quirks*/0, /*mintags*/0, /*maxtags*/0
311 * Unfortunately, the Quantum Atlas III has the same
312 * problem as the Atlas II drives above.
313 * Reported by: "Johan Granlund" <johan@granlund.nu>
315 * For future reference, the drive with the problem was:
316 * QUANTUM QM39100TD-SW N1B0
318 * It's possible that Quantum will fix the problem in later
319 * firmware revisions. If that happens, the quirk entry
320 * will need to be made specific to the firmware revisions
324 /* Reports QUEUE FULL for temporary resource shortages */
325 { T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "QM39100*", "*" },
326 /*quirks*/0, /*mintags*/24, /*maxtags*/32
330 * 18 Gig Atlas III, same problem as the 9G version.
331 * Reported by: Andre Albsmeier
332 * <andre.albsmeier@mchp.siemens.de>
334 * For future reference, the drive with the problem was:
335 * QUANTUM QM318000TD-S N491
337 /* Reports QUEUE FULL for temporary resource shortages */
338 { T_DIRECT
, SIP_MEDIA_FIXED
, quantum
, "QM318000*", "*" },
339 /*quirks*/0, /*mintags*/24, /*maxtags*/32
343 * Broken tagged queuing drive
344 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
345 * and: Martin Renters <martin@tdc.on.ca>
347 { T_DIRECT
, SIP_MEDIA_FIXED
, seagate
, "ST410800*", "71*" },
348 /*quirks*/0, /*mintags*/0, /*maxtags*/0
351 * The Seagate Medalist Pro drives have very poor write
352 * performance with anything more than 2 tags.
354 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
355 * Drive: <SEAGATE ST36530N 1444>
357 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
358 * Drive: <SEAGATE ST34520W 1281>
360 * No one has actually reported that the 9G version
361 * (ST39140*) of the Medalist Pro has the same problem, but
362 * we're assuming that it does because the 4G and 6.5G
363 * versions of the drive are broken.
366 { T_DIRECT
, SIP_MEDIA_FIXED
, seagate
, "ST34520*", "*"},
367 /*quirks*/0, /*mintags*/2, /*maxtags*/2
370 { T_DIRECT
, SIP_MEDIA_FIXED
, seagate
, "ST36530*", "*"},
371 /*quirks*/0, /*mintags*/2, /*maxtags*/2
374 { T_DIRECT
, SIP_MEDIA_FIXED
, seagate
, "ST39140*", "*"},
375 /*quirks*/0, /*mintags*/2, /*maxtags*/2
379 * Slow when tagged queueing is enabled. Write performance
380 * steadily drops off with more and more concurrent
381 * transactions. Best sequential write performance with
382 * tagged queueing turned off and write caching turned on.
385 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
386 * Drive: DCAS-34330 w/ "S65A" firmware.
388 * The drive with the problem had the "S65A" firmware
389 * revision, and has also been reported (by Stephen J.
390 * Roznowski <sjr@home.net>) for a drive with the "S61A"
393 * Although no one has reported problems with the 2 gig
394 * version of the DCAS drive, the assumption is that it
395 * has the same problems as the 4 gig version. Therefore
396 * this quirk entries disables tagged queueing for all
399 { T_DIRECT
, SIP_MEDIA_FIXED
, "IBM", "DCAS*", "*" },
400 /*quirks*/0, /*mintags*/0, /*maxtags*/0
403 /* Broken tagged queuing drive */
404 { T_DIRECT
, SIP_MEDIA_REMOVABLE
, "iomega", "jaz*", "*" },
405 /*quirks*/0, /*mintags*/0, /*maxtags*/0
408 /* Broken tagged queuing drive */
409 { T_DIRECT
, SIP_MEDIA_FIXED
, "CONNER", "CFP2107*", "*" },
410 /*quirks*/0, /*mintags*/0, /*maxtags*/0
413 /* This does not support other than LUN 0 */
414 { T_DIRECT
, SIP_MEDIA_FIXED
, "VMware*", "*", "*" },
415 CAM_QUIRK_NOLUNS
, /*mintags*/2, /*maxtags*/255
419 * Broken tagged queuing drive.
421 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
424 { T_DIRECT
, SIP_MEDIA_FIXED
, samsung
, "WN34324U*", "*" },
425 /*quirks*/0, /*mintags*/0, /*maxtags*/0
429 * Slow when tagged queueing is enabled. (1.5MB/sec versus
431 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
432 * Best performance with these drives is achieved with
433 * tagged queueing turned off, and write caching turned on.
435 { T_DIRECT
, SIP_MEDIA_FIXED
, west_digital
, "WDE*", "*" },
436 /*quirks*/0, /*mintags*/0, /*maxtags*/0
440 * Slow when tagged queueing is enabled. (1.5MB/sec versus
442 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
443 * Best performance with these drives is achieved with
444 * tagged queueing turned off, and write caching turned on.
446 { T_DIRECT
, SIP_MEDIA_FIXED
, west_digital
, "ENTERPRISE", "*" },
447 /*quirks*/0, /*mintags*/0, /*maxtags*/0
451 * Doesn't handle queue full condition correctly,
452 * so we need to limit maxtags to what the device
453 * can handle instead of determining this automatically.
455 { T_DIRECT
, SIP_MEDIA_FIXED
, samsung
, "WN321010S*", "*" },
456 /*quirks*/0, /*mintags*/2, /*maxtags*/32
459 /* Really only one LUN */
460 { T_ENCLOSURE
, SIP_MEDIA_FIXED
, "SUN", "SENA", "*" },
461 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
464 /* I can't believe we need a quirk for DPT volumes. */
465 { T_ANY
, SIP_MEDIA_FIXED
|SIP_MEDIA_REMOVABLE
, "DPT", "*", "*" },
467 /*mintags*/0, /*maxtags*/255
471 * Many Sony CDROM drives don't like multi-LUN probing.
473 { T_CDROM
, SIP_MEDIA_REMOVABLE
, sony
, "CD-ROM CDU*", "*" },
474 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
478 * This drive doesn't like multiple LUN probing.
479 * Submitted by: Parag Patel <parag@cgt.com>
481 { T_WORM
, SIP_MEDIA_REMOVABLE
, sony
, "CD-R CDU9*", "*" },
482 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
485 { T_WORM
, SIP_MEDIA_REMOVABLE
, "YAMAHA", "CDR100*", "*" },
486 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
490 * The 8200 doesn't like multi-lun probing, and probably
491 * don't like serial number requests either.
494 T_SEQUENTIAL
, SIP_MEDIA_REMOVABLE
, "EXABYTE",
497 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
501 * Let's try the same as above, but for a drive that says
502 * it's an IPL-6860 but is actually an EXB 8200.
505 T_SEQUENTIAL
, SIP_MEDIA_REMOVABLE
, "EXABYTE",
508 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
512 * These Hitachi drives don't like multi-lun probing.
513 * The PR submitter has a DK319H, but says that the Linux
514 * kernel has a similar work-around for the DK312 and DK314,
515 * so all DK31* drives are quirked here.
517 * Submitted by: Paul Haddad <paul@pth.com>
519 { T_DIRECT
, SIP_MEDIA_FIXED
, "HITACHI", "DK31*", "*" },
520 CAM_QUIRK_NOLUNS
, /*mintags*/2, /*maxtags*/255
524 * The Hitachi CJ series with J8A8 firmware apparantly has
525 * problems with tagged commands.
527 * Reported by: amagai@nue.org
529 { T_DIRECT
, SIP_MEDIA_FIXED
, "HITACHI", "DK32CJ*", "J8A8" },
530 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
534 * These are the large storage arrays.
535 * Submitted by: William Carrel <william.carrel@infospace.com>
537 { T_DIRECT
, SIP_MEDIA_FIXED
, "HITACHI", "OPEN*", "*" },
538 CAM_QUIRK_HILUNS
, 2, 1024
542 * This old revision of the TDC3600 is also SCSI-1, and
543 * hangs upon serial number probing.
546 T_SEQUENTIAL
, SIP_MEDIA_REMOVABLE
, "TANDBERG",
549 CAM_QUIRK_NOSERIAL
, /*mintags*/0, /*maxtags*/0
553 * Would repond to all LUNs if asked for.
556 T_SEQUENTIAL
, SIP_MEDIA_REMOVABLE
, "CALIPER",
559 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
563 * Would repond to all LUNs if asked for.
566 T_SEQUENTIAL
, SIP_MEDIA_REMOVABLE
, "KENNEDY",
569 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
572 /* Submitted by: Matthew Dodd <winter@jurai.net> */
573 { T_PROCESSOR
, SIP_MEDIA_FIXED
, "Cabletrn", "EA41*", "*" },
574 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
577 /* Submitted by: Matthew Dodd <winter@jurai.net> */
578 { T_PROCESSOR
, SIP_MEDIA_FIXED
, "CABLETRN", "EA41*", "*" },
579 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
582 /* TeraSolutions special settings for TRC-22 RAID */
583 { T_DIRECT
, SIP_MEDIA_FIXED
, "TERASOLU", "TRC-22", "*" },
584 /*quirks*/0, /*mintags*/55, /*maxtags*/255
587 /* Veritas Storage Appliance */
588 { T_DIRECT
, SIP_MEDIA_FIXED
, "VERITAS", "*", "*" },
589 CAM_QUIRK_HILUNS
, /*mintags*/2, /*maxtags*/1024
593 * Would respond to all LUNs. Device type and removable
594 * flag are jumper-selectable.
596 { T_ANY
, SIP_MEDIA_REMOVABLE
|SIP_MEDIA_FIXED
, "MaxOptix",
599 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
602 /* EasyRAID E5A aka. areca ARC-6010 */
603 { T_DIRECT
, SIP_MEDIA_FIXED
, "easyRAID", "*", "*" },
604 CAM_QUIRK_NOHILUNS
, /*mintags*/2, /*maxtags*/255
607 { T_ENCLOSURE
, SIP_MEDIA_FIXED
, "DP", "BACKPLANE", "*" },
608 CAM_QUIRK_NOLUNS
, /*mintags*/0, /*maxtags*/0
611 /* Default tagged queuing parameters for all devices */
613 T_ANY
, SIP_MEDIA_REMOVABLE
|SIP_MEDIA_FIXED
,
614 /*vendor*/"*", /*product*/"*", /*revision*/"*"
616 /*quirks*/0, /*mintags*/2, /*maxtags*/255
620 static const int xpt_quirk_table_size
=
621 sizeof(xpt_quirk_table
) / sizeof(*xpt_quirk_table
);
625 DM_RET_FLAG_MASK
= 0x0f,
628 DM_RET_DESCEND
= 0x20,
630 DM_RET_ACTION_MASK
= 0xf0
638 } xpt_traverse_depth
;
640 struct xpt_traverse_config
{
641 xpt_traverse_depth depth
;
646 typedef int xpt_busfunc_t (struct cam_eb
*bus
, void *arg
);
647 typedef int xpt_targetfunc_t (struct cam_et
*target
, void *arg
);
648 typedef int xpt_devicefunc_t (struct cam_ed
*device
, void *arg
);
649 typedef int xpt_periphfunc_t (struct cam_periph
*periph
, void *arg
);
650 typedef int xpt_pdrvfunc_t (struct periph_driver
**pdrv
, void *arg
);
652 /* Transport layer configuration information */
653 static struct xpt_softc xsoftc
;
655 /* Queues for our software interrupt handler */
656 typedef TAILQ_HEAD(cam_isrq
, ccb_hdr
) cam_isrq_t
;
657 typedef TAILQ_HEAD(cam_simq
, cam_sim
) cam_simq_t
;
658 static cam_simq_t cam_simq
;
659 static struct spinlock cam_simq_spin
;
661 struct cam_periph
*xpt_periph
;
663 static periph_init_t xpt_periph_init
;
665 static periph_init_t probe_periph_init
;
667 static struct periph_driver xpt_driver
=
669 xpt_periph_init
, "xpt",
670 TAILQ_HEAD_INITIALIZER(xpt_driver
.units
)
673 static struct periph_driver probe_driver
=
675 probe_periph_init
, "probe",
676 TAILQ_HEAD_INITIALIZER(probe_driver
.units
)
679 PERIPHDRIVER_DECLARE(xpt
, xpt_driver
);
680 PERIPHDRIVER_DECLARE(probe
, probe_driver
);
682 #define XPT_CDEV_MAJOR 104
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
= {
689 { "xpt", XPT_CDEV_MAJOR
, 0 },
695 static void dead_sim_action(struct cam_sim
*sim
, union ccb
*ccb
);
696 static void dead_sim_poll(struct cam_sim
*sim
);
698 /* Dummy SIM that is used when the real one has gone. */
699 static struct cam_sim cam_dead_sim
= {
700 .sim_action
= dead_sim_action
,
701 .sim_poll
= dead_sim_poll
,
702 .sim_name
= "dead_sim",
705 #define SIM_DEAD(sim) ((sim) == &cam_dead_sim)
707 /* Storage for debugging datastructures */
709 struct cam_path
*cam_dpath
;
710 u_int32_t cam_dflags
;
711 u_int32_t cam_debug_delay
;
714 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
715 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
719 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
720 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
721 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
723 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
724 || defined(CAM_DEBUG_LUN)
726 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
727 || !defined(CAM_DEBUG_LUN)
728 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
730 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
731 #else /* !CAMDEBUG */
732 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
733 #endif /* CAMDEBUG */
734 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
736 /* Our boot-time initialization hook */
737 static int cam_module_event_handler(module_t
, int /*modeventtype_t*/, void *);
739 static moduledata_t cam_moduledata
= {
741 cam_module_event_handler
,
745 static int xpt_init(void *);
747 DECLARE_MODULE(cam
, cam_moduledata
, SI_SUB_CONFIGURE
, SI_ORDER_SECOND
);
748 MODULE_VERSION(cam
, 1);
751 static cam_status
xpt_compile_path(struct cam_path
*new_path
,
752 struct cam_periph
*perph
,
754 target_id_t target_id
,
757 static void xpt_release_path(struct cam_path
*path
);
759 static void xpt_async_bcast(struct async_list
*async_head
,
760 u_int32_t async_code
,
761 struct cam_path
*path
,
763 static void xpt_dev_async(u_int32_t async_code
,
765 struct cam_et
*target
,
766 struct cam_ed
*device
,
768 static path_id_t
xptnextfreepathid(void);
769 static path_id_t
xptpathid(const char *sim_name
, int sim_unit
, int sim_bus
);
770 static union ccb
*xpt_get_ccb(struct cam_ed
*device
);
771 static int xpt_schedule_dev(struct camq
*queue
, cam_pinfo
*dev_pinfo
,
772 u_int32_t new_priority
);
773 static void xpt_run_dev_allocq(struct cam_eb
*bus
);
774 static void xpt_run_dev_sendq(struct cam_eb
*bus
);
775 static timeout_t xpt_release_devq_timeout
;
776 static void xpt_release_bus(struct cam_eb
*bus
);
777 static void xpt_release_devq_device(struct cam_ed
*dev
, u_int count
,
779 static struct cam_et
*
780 xpt_alloc_target(struct cam_eb
*bus
, target_id_t target_id
);
781 static void xpt_release_target(struct cam_eb
*bus
, struct cam_et
*target
);
782 static struct cam_ed
*
783 xpt_alloc_device(struct cam_eb
*bus
, struct cam_et
*target
,
785 static void xpt_release_device(struct cam_eb
*bus
, struct cam_et
*target
,
786 struct cam_ed
*device
);
787 static u_int32_t
xpt_dev_ccbq_resize(struct cam_path
*path
, int newopenings
);
788 static struct cam_eb
*
789 xpt_find_bus(path_id_t path_id
);
790 static struct cam_et
*
791 xpt_find_target(struct cam_eb
*bus
, target_id_t target_id
);
792 static struct cam_ed
*
793 xpt_find_device(struct cam_et
*target
, lun_id_t lun_id
);
794 static void xpt_scan_bus(struct cam_periph
*periph
, union ccb
*ccb
);
795 static void xpt_scan_lun(struct cam_periph
*periph
,
796 struct cam_path
*path
, cam_flags flags
,
798 static void xptscandone(struct cam_periph
*periph
, union ccb
*done_ccb
);
799 static xpt_busfunc_t xptconfigbuscountfunc
;
800 static xpt_busfunc_t xptconfigfunc
;
801 static void xpt_config(void *arg
);
802 static xpt_devicefunc_t xptpassannouncefunc
;
803 static void xpt_finishconfig(struct cam_periph
*periph
, union ccb
*ccb
);
804 static void xptaction(struct cam_sim
*sim
, union ccb
*work_ccb
);
805 static void xptpoll(struct cam_sim
*sim
);
806 static inthand2_t swi_cambio
;
807 static void camisr(void *);
808 static void camisr_runqueue(struct cam_sim
*);
809 static dev_match_ret
xptbusmatch(struct dev_match_pattern
*patterns
,
810 u_int num_patterns
, struct cam_eb
*bus
);
811 static dev_match_ret
xptdevicematch(struct dev_match_pattern
*patterns
,
813 struct cam_ed
*device
);
814 static dev_match_ret
xptperiphmatch(struct dev_match_pattern
*patterns
,
816 struct cam_periph
*periph
);
817 static xpt_busfunc_t xptedtbusfunc
;
818 static xpt_targetfunc_t xptedttargetfunc
;
819 static xpt_devicefunc_t xptedtdevicefunc
;
820 static xpt_periphfunc_t xptedtperiphfunc
;
821 static xpt_pdrvfunc_t xptplistpdrvfunc
;
822 static xpt_periphfunc_t xptplistperiphfunc
;
823 static int xptedtmatch(struct ccb_dev_match
*cdm
);
824 static int xptperiphlistmatch(struct ccb_dev_match
*cdm
);
825 static int xptbustraverse(struct cam_eb
*start_bus
,
826 xpt_busfunc_t
*tr_func
, void *arg
);
827 static int xpttargettraverse(struct cam_eb
*bus
,
828 struct cam_et
*start_target
,
829 xpt_targetfunc_t
*tr_func
, void *arg
);
830 static int xptdevicetraverse(struct cam_et
*target
,
831 struct cam_ed
*start_device
,
832 xpt_devicefunc_t
*tr_func
, void *arg
);
833 static int xptperiphtraverse(struct cam_ed
*device
,
834 struct cam_periph
*start_periph
,
835 xpt_periphfunc_t
*tr_func
, void *arg
);
836 static int xptpdrvtraverse(struct periph_driver
**start_pdrv
,
837 xpt_pdrvfunc_t
*tr_func
, void *arg
);
838 static int xptpdperiphtraverse(struct periph_driver
**pdrv
,
839 struct cam_periph
*start_periph
,
840 xpt_periphfunc_t
*tr_func
,
842 static xpt_busfunc_t xptdefbusfunc
;
843 static xpt_targetfunc_t xptdeftargetfunc
;
844 static xpt_devicefunc_t xptdefdevicefunc
;
845 static xpt_periphfunc_t xptdefperiphfunc
;
846 static int xpt_for_all_busses(xpt_busfunc_t
*tr_func
, void *arg
);
847 static int xpt_for_all_devices(xpt_devicefunc_t
*tr_func
,
849 static xpt_devicefunc_t xptsetasyncfunc
;
850 static xpt_busfunc_t xptsetasyncbusfunc
;
851 static cam_status
xptregister(struct cam_periph
*periph
,
853 static cam_status
proberegister(struct cam_periph
*periph
,
855 static void probeschedule(struct cam_periph
*probe_periph
);
856 static void probestart(struct cam_periph
*periph
, union ccb
*start_ccb
);
857 static void proberequestdefaultnegotiation(struct cam_periph
*periph
);
858 static int proberequestbackoff(struct cam_periph
*periph
,
859 struct cam_ed
*device
);
860 static void probedone(struct cam_periph
*periph
, union ccb
*done_ccb
);
861 static void probecleanup(struct cam_periph
*periph
);
862 static void xpt_find_quirk(struct cam_ed
*device
);
863 static void xpt_devise_transport(struct cam_path
*path
);
864 static void xpt_set_transfer_settings(struct ccb_trans_settings
*cts
,
865 struct cam_ed
*device
,
867 static void xpt_toggle_tags(struct cam_path
*path
);
868 static void xpt_start_tags(struct cam_path
*path
);
869 static __inline
int xpt_schedule_dev_allocq(struct cam_eb
*bus
,
871 static __inline
int xpt_schedule_dev_sendq(struct cam_eb
*bus
,
873 static __inline
int periph_is_queued(struct cam_periph
*periph
);
874 static __inline
int device_is_alloc_queued(struct cam_ed
*device
);
875 static __inline
int device_is_send_queued(struct cam_ed
*device
);
876 static __inline
int dev_allocq_is_runnable(struct cam_devq
*devq
);
879 xpt_schedule_dev_allocq(struct cam_eb
*bus
, struct cam_ed
*dev
)
883 if (bus
->sim
->devq
&& dev
->ccbq
.devq_openings
> 0) {
884 if ((dev
->flags
& CAM_DEV_RESIZE_QUEUE_NEEDED
) != 0) {
885 cam_ccbq_resize(&dev
->ccbq
,
886 dev
->ccbq
.dev_openings
887 + dev
->ccbq
.dev_active
);
888 dev
->flags
&= ~CAM_DEV_RESIZE_QUEUE_NEEDED
;
891 * The priority of a device waiting for CCB resources
892 * is that of the the highest priority peripheral driver
895 retval
= xpt_schedule_dev(&bus
->sim
->devq
->alloc_queue
,
896 &dev
->alloc_ccb_entry
.pinfo
,
897 CAMQ_GET_HEAD(&dev
->drvq
)->priority
);
906 xpt_schedule_dev_sendq(struct cam_eb
*bus
, struct cam_ed
*dev
)
910 if (bus
->sim
->devq
&& dev
->ccbq
.dev_openings
> 0) {
912 * The priority of a device waiting for controller
913 * resources is that of the the highest priority CCB
917 xpt_schedule_dev(&bus
->sim
->devq
->send_queue
,
918 &dev
->send_ccb_entry
.pinfo
,
919 CAMQ_GET_HEAD(&dev
->ccbq
.queue
)->priority
);
927 periph_is_queued(struct cam_periph
*periph
)
929 return (periph
->pinfo
.index
!= CAM_UNQUEUED_INDEX
);
933 device_is_alloc_queued(struct cam_ed
*device
)
935 return (device
->alloc_ccb_entry
.pinfo
.index
!= CAM_UNQUEUED_INDEX
);
939 device_is_send_queued(struct cam_ed
*device
)
941 return (device
->send_ccb_entry
.pinfo
.index
!= CAM_UNQUEUED_INDEX
);
945 dev_allocq_is_runnable(struct cam_devq
*devq
)
949 * Have space to do more work.
950 * Allowed to do work.
952 return ((devq
->alloc_queue
.qfrozen_cnt
== 0)
953 && (devq
->alloc_queue
.entries
> 0)
954 && (devq
->alloc_openings
> 0));
958 xpt_periph_init(void)
960 dev_ops_add(&xpt_ops
, 0, 0);
961 make_dev(&xpt_ops
, 0, UID_ROOT
, GID_OPERATOR
, 0600, "xpt0");
965 probe_periph_init(void)
971 xptdone(struct cam_periph
*periph
, union ccb
*done_ccb
)
973 /* Caller will release the CCB */
974 wakeup(&done_ccb
->ccb_h
.cbfcnp
);
978 xptopen(struct dev_open_args
*ap
)
980 cdev_t dev
= ap
->a_head
.a_dev
;
983 * Only allow read-write access.
985 if (((ap
->a_oflags
& FWRITE
) == 0) || ((ap
->a_oflags
& FREAD
) == 0))
989 * We don't allow nonblocking access.
991 if ((ap
->a_oflags
& O_NONBLOCK
) != 0) {
992 kprintf("%s: can't do nonblocking access\n", devtoname(dev
));
996 /* Mark ourselves open */
997 lockmgr(&xsoftc
.xpt_lock
, LK_EXCLUSIVE
);
998 xsoftc
.flags
|= XPT_FLAG_OPEN
;
999 lockmgr(&xsoftc
.xpt_lock
, LK_RELEASE
);
1005 xptclose(struct dev_close_args
*ap
)
1008 /* Mark ourselves closed */
1009 lockmgr(&xsoftc
.xpt_lock
, LK_EXCLUSIVE
);
1010 xsoftc
.flags
&= ~XPT_FLAG_OPEN
;
1011 lockmgr(&xsoftc
.xpt_lock
, LK_RELEASE
);
1017 * Don't automatically grab the xpt softc lock here even though this is going
1018 * through the xpt device. The xpt device is really just a back door for
1019 * accessing other devices and SIMs, so the right thing to do is to grab
1020 * the appropriate SIM lock once the bus/SIM is located.
1023 xptioctl(struct dev_ioctl_args
*ap
)
1031 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1032 * to accept CCB types that don't quite make sense to send through a
1033 * passthrough driver.
1035 case CAMIOCOMMAND
: {
1040 inccb
= (union ccb
*)ap
->a_data
;
1042 bus
= xpt_find_bus(inccb
->ccb_h
.path_id
);
1048 switch(inccb
->ccb_h
.func_code
) {
1051 if ((inccb
->ccb_h
.target_id
!= CAM_TARGET_WILDCARD
)
1052 || (inccb
->ccb_h
.target_lun
!= CAM_LUN_WILDCARD
)) {
1061 ccb
= xpt_alloc_ccb();
1063 CAM_SIM_LOCK(bus
->sim
);
1066 * Create a path using the bus, target, and lun the
1069 if (xpt_create_path(&ccb
->ccb_h
.path
, xpt_periph
,
1070 inccb
->ccb_h
.path_id
,
1071 inccb
->ccb_h
.target_id
,
1072 inccb
->ccb_h
.target_lun
) !=
1075 CAM_SIM_UNLOCK(bus
->sim
);
1079 /* Ensure all of our fields are correct */
1080 xpt_setup_ccb(&ccb
->ccb_h
, ccb
->ccb_h
.path
,
1081 inccb
->ccb_h
.pinfo
.priority
);
1082 xpt_merge_ccb(ccb
, inccb
);
1083 ccb
->ccb_h
.cbfcnp
= xptdone
;
1084 cam_periph_runccb(ccb
, NULL
, 0, 0, NULL
);
1085 bcopy(ccb
, inccb
, sizeof(union ccb
));
1086 xpt_free_path(ccb
->ccb_h
.path
);
1088 CAM_SIM_UNLOCK(bus
->sim
);
1095 * This is an immediate CCB, so it's okay to
1096 * allocate it on the stack.
1099 CAM_SIM_LOCK(bus
->sim
);
1102 * Create a path using the bus, target, and lun the
1105 if (xpt_create_path(&ccb
.ccb_h
.path
, xpt_periph
,
1106 inccb
->ccb_h
.path_id
,
1107 inccb
->ccb_h
.target_id
,
1108 inccb
->ccb_h
.target_lun
) !=
1111 CAM_SIM_UNLOCK(bus
->sim
);
1114 /* Ensure all of our fields are correct */
1115 xpt_setup_ccb(&ccb
.ccb_h
, ccb
.ccb_h
.path
,
1116 inccb
->ccb_h
.pinfo
.priority
);
1117 xpt_merge_ccb(&ccb
, inccb
);
1118 ccb
.ccb_h
.cbfcnp
= xptdone
;
1120 CAM_SIM_UNLOCK(bus
->sim
);
1121 bcopy(&ccb
, inccb
, sizeof(union ccb
));
1122 xpt_free_path(ccb
.ccb_h
.path
);
1126 case XPT_DEV_MATCH
: {
1127 struct cam_periph_map_info mapinfo
;
1128 struct cam_path
*old_path
;
1131 * We can't deal with physical addresses for this
1132 * type of transaction.
1134 if (inccb
->ccb_h
.flags
& CAM_DATA_PHYS
) {
1140 * Save this in case the caller had it set to
1141 * something in particular.
1143 old_path
= inccb
->ccb_h
.path
;
1146 * We really don't need a path for the matching
1147 * code. The path is needed because of the
1148 * debugging statements in xpt_action(). They
1149 * assume that the CCB has a valid path.
1151 inccb
->ccb_h
.path
= xpt_periph
->path
;
1153 bzero(&mapinfo
, sizeof(mapinfo
));
1156 * Map the pattern and match buffers into kernel
1157 * virtual address space.
1159 error
= cam_periph_mapmem(inccb
, &mapinfo
);
1162 inccb
->ccb_h
.path
= old_path
;
1167 * This is an immediate CCB, we can send it on directly.
1172 * Map the buffers back into user space.
1174 cam_periph_unmapmem(inccb
, &mapinfo
);
1176 inccb
->ccb_h
.path
= old_path
;
1185 xpt_release_bus(bus
);
1189 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1190 * with the periphal driver name and unit name filled in. The other
1191 * fields don't really matter as input. The passthrough driver name
1192 * ("pass"), and unit number are passed back in the ccb. The current
1193 * device generation number, and the index into the device peripheral
1194 * driver list, and the status are also passed back. Note that
1195 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1196 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1197 * (or rather should be) impossible for the device peripheral driver
1198 * list to change since we look at the whole thing in one pass, and
1199 * we do it with lock protection.
1202 case CAMGETPASSTHRU
: {
1204 struct cam_periph
*periph
;
1205 struct periph_driver
**p_drv
;
1208 u_int cur_generation
;
1209 int base_periph_found
;
1212 ccb
= (union ccb
*)ap
->a_data
;
1213 unit
= ccb
->cgdl
.unit_number
;
1214 name
= ccb
->cgdl
.periph_name
;
1216 * Every 100 devices, we want to drop our lock protection to
1217 * give the software interrupt handler a chance to run.
1218 * Most systems won't run into this check, but this should
1219 * avoid starvation in the software interrupt handler in
1224 ccb
= (union ccb
*)ap
->a_data
;
1226 base_periph_found
= 0;
1229 * Sanity check -- make sure we don't get a null peripheral
1232 if (*ccb
->cgdl
.periph_name
== '\0') {
1237 /* Keep the list from changing while we traverse it */
1238 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
1240 cur_generation
= xsoftc
.xpt_generation
;
1242 /* first find our driver in the list of drivers */
1243 for (p_drv
= periph_drivers
; *p_drv
!= NULL
; p_drv
++) {
1244 if (strcmp((*p_drv
)->driver_name
, name
) == 0)
1248 if (*p_drv
== NULL
) {
1249 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
1250 ccb
->ccb_h
.status
= CAM_REQ_CMP_ERR
;
1251 ccb
->cgdl
.status
= CAM_GDEVLIST_ERROR
;
1252 *ccb
->cgdl
.periph_name
= '\0';
1253 ccb
->cgdl
.unit_number
= 0;
1259 * Run through every peripheral instance of this driver
1260 * and check to see whether it matches the unit passed
1261 * in by the user. If it does, get out of the loops and
1262 * find the passthrough driver associated with that
1263 * peripheral driver.
1265 TAILQ_FOREACH(periph
, &(*p_drv
)->units
, unit_links
) {
1267 if (periph
->unit_number
== unit
) {
1269 } else if (--splbreaknum
== 0) {
1270 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
1271 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
1273 if (cur_generation
!= xsoftc
.xpt_generation
)
1278 * If we found the peripheral driver that the user passed
1279 * in, go through all of the peripheral drivers for that
1280 * particular device and look for a passthrough driver.
1282 if (periph
!= NULL
) {
1283 struct cam_ed
*device
;
1286 base_periph_found
= 1;
1287 device
= periph
->path
->device
;
1288 for (i
= 0, periph
= SLIST_FIRST(&device
->periphs
);
1290 periph
= SLIST_NEXT(periph
, periph_links
), i
++) {
1292 * Check to see whether we have a
1293 * passthrough device or not.
1295 if (strcmp(periph
->periph_name
, "pass") == 0) {
1297 * Fill in the getdevlist fields.
1299 strcpy(ccb
->cgdl
.periph_name
,
1300 periph
->periph_name
);
1301 ccb
->cgdl
.unit_number
=
1302 periph
->unit_number
;
1303 if (SLIST_NEXT(periph
, periph_links
))
1305 CAM_GDEVLIST_MORE_DEVS
;
1308 CAM_GDEVLIST_LAST_DEVICE
;
1309 ccb
->cgdl
.generation
=
1311 ccb
->cgdl
.index
= i
;
1313 * Fill in some CCB header fields
1314 * that the user may want.
1316 ccb
->ccb_h
.path_id
=
1317 periph
->path
->bus
->path_id
;
1318 ccb
->ccb_h
.target_id
=
1319 periph
->path
->target
->target_id
;
1320 ccb
->ccb_h
.target_lun
=
1321 periph
->path
->device
->lun_id
;
1322 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
1329 * If the periph is null here, one of two things has
1330 * happened. The first possibility is that we couldn't
1331 * find the unit number of the particular peripheral driver
1332 * that the user is asking about. e.g. the user asks for
1333 * the passthrough driver for "da11". We find the list of
1334 * "da" peripherals all right, but there is no unit 11.
1335 * The other possibility is that we went through the list
1336 * of peripheral drivers attached to the device structure,
1337 * but didn't find one with the name "pass". Either way,
1338 * we return ENOENT, since we couldn't find something.
1340 if (periph
== NULL
) {
1341 ccb
->ccb_h
.status
= CAM_REQ_CMP_ERR
;
1342 ccb
->cgdl
.status
= CAM_GDEVLIST_ERROR
;
1343 *ccb
->cgdl
.periph_name
= '\0';
1344 ccb
->cgdl
.unit_number
= 0;
1347 * It is unfortunate that this is even necessary,
1348 * but there are many, many clueless users out there.
1349 * If this is true, the user is looking for the
1350 * passthrough driver, but doesn't have one in his
1353 if (base_periph_found
== 1) {
1354 kprintf("xptioctl: pass driver is not in the "
1356 kprintf("xptioctl: put \"device pass\" in "
1357 "your kernel config file\n");
1360 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
1372 cam_module_event_handler(module_t mod
, int what
, void *arg
)
1378 if ((error
= xpt_init(NULL
)) != 0)
1390 /* thread to handle bus rescans */
1392 xpt_scanner_thread(void *dummy
)
1396 struct cam_sim
*sim
;
1400 * Wait for a rescan request to come in. When it does, splice
1401 * it onto a queue from local storage so that the xpt lock
1402 * doesn't need to be held while the requests are being
1406 tsleep_interlock(&xsoftc
.ccb_scanq
);
1408 tsleep(&xsoftc
.ccb_scanq
, 0, "ccb_scanq", 0);
1412 TAILQ_CONCAT(&queue
, &xsoftc
.ccb_scanq
, sim_links
.tqe
);
1415 while ((ccb
= (union ccb
*)TAILQ_FIRST(&queue
)) != NULL
) {
1416 TAILQ_REMOVE(&queue
, &ccb
->ccb_h
, sim_links
.tqe
);
1418 sim
= ccb
->ccb_h
.path
->bus
->sim
;
1421 ccb
->ccb_h
.func_code
= XPT_SCAN_BUS
;
1422 ccb
->ccb_h
.cbfcnp
= xptdone
;
1423 xpt_setup_ccb(&ccb
->ccb_h
, ccb
->ccb_h
.path
, 5);
1424 cam_periph_runccb(ccb
, NULL
, 0, 0, NULL
);
1425 xpt_free_path(ccb
->ccb_h
.path
);
1427 CAM_SIM_UNLOCK(sim
);
1433 xpt_rescan(union ccb
*ccb
)
1435 struct ccb_hdr
*hdr
;
1438 * Don't make duplicate entries for the same paths.
1441 TAILQ_FOREACH(hdr
, &xsoftc
.ccb_scanq
, sim_links
.tqe
) {
1442 if (xpt_path_comp(hdr
->path
, ccb
->ccb_h
.path
) == 0) {
1444 xpt_print(ccb
->ccb_h
.path
, "rescan already queued\n");
1445 xpt_free_path(ccb
->ccb_h
.path
);
1450 TAILQ_INSERT_TAIL(&xsoftc
.ccb_scanq
, &ccb
->ccb_h
, sim_links
.tqe
);
1451 wakeup(&xsoftc
.ccb_scanq
);
1456 /* Functions accessed by the peripheral drivers */
1458 xpt_init(void *dummy
)
1460 struct cam_sim
*xpt_sim
;
1461 struct cam_path
*path
;
1462 struct cam_devq
*devq
;
1465 TAILQ_INIT(&xsoftc
.xpt_busses
);
1466 TAILQ_INIT(&cam_simq
);
1467 TAILQ_INIT(&xsoftc
.ccb_scanq
);
1468 STAILQ_INIT(&xsoftc
.highpowerq
);
1469 xsoftc
.num_highpower
= CAM_MAX_HIGHPOWER
;
1471 spin_init(&cam_simq_spin
);
1472 lockinit(&xsoftc
.xpt_lock
, "XPT lock", 0, LK_CANRECURSE
);
1473 lockinit(&xsoftc
.xpt_topo_lock
, "XPT topology lock", 0, LK_CANRECURSE
);
1476 * The xpt layer is, itself, the equivelent of a SIM.
1477 * Allow 16 ccbs in the ccb pool for it. This should
1478 * give decent parallelism when we probe busses and
1479 * perform other XPT functions.
1481 devq
= cam_simq_alloc(16);
1482 xpt_sim
= cam_sim_alloc(xptaction
,
1487 /*lock*/&xsoftc
.xpt_lock
,
1488 /*max_dev_transactions*/0,
1489 /*max_tagged_dev_transactions*/0,
1491 cam_simq_release(devq
);
1492 if (xpt_sim
== NULL
)
1495 xpt_sim
->max_ccbs
= 16;
1497 lockmgr(&xsoftc
.xpt_lock
, LK_EXCLUSIVE
);
1498 if ((status
= xpt_bus_register(xpt_sim
, /*bus #*/0)) != CAM_SUCCESS
) {
1499 kprintf("xpt_init: xpt_bus_register failed with status %#x,"
1500 " failing attach\n", status
);
1505 * Looking at the XPT from the SIM layer, the XPT is
1506 * the equivelent of a peripheral driver. Allocate
1507 * a peripheral driver entry for us.
1509 if ((status
= xpt_create_path(&path
, NULL
, CAM_XPT_PATH_ID
,
1510 CAM_TARGET_WILDCARD
,
1511 CAM_LUN_WILDCARD
)) != CAM_REQ_CMP
) {
1512 kprintf("xpt_init: xpt_create_path failed with status %#x,"
1513 " failing attach\n", status
);
1517 cam_periph_alloc(xptregister
, NULL
, NULL
, NULL
, "xpt", CAM_PERIPH_BIO
,
1518 path
, NULL
, 0, xpt_sim
);
1519 xpt_free_path(path
);
1521 lockmgr(&xsoftc
.xpt_lock
, LK_RELEASE
);
1524 * Register a callback for when interrupts are enabled.
1526 xsoftc
.xpt_config_hook
= kmalloc(sizeof(struct intr_config_hook
),
1527 M_CAMXPT
, M_INTWAIT
| M_ZERO
);
1528 xsoftc
.xpt_config_hook
->ich_func
= xpt_config
;
1529 xsoftc
.xpt_config_hook
->ich_desc
= "xpt";
1530 xsoftc
.xpt_config_hook
->ich_order
= 1000;
1531 if (config_intrhook_establish(xsoftc
.xpt_config_hook
) != 0) {
1532 kfree (xsoftc
.xpt_config_hook
, M_CAMXPT
);
1533 kprintf("xpt_init: config_intrhook_establish failed "
1534 "- failing attach\n");
1537 /* fire up rescan thread */
1538 if (kthread_create(xpt_scanner_thread
, NULL
, NULL
, "xpt_thrd")) {
1539 kprintf("xpt_init: failed to create rescan thread\n");
1541 /* Install our software interrupt handlers */
1542 register_swi(SWI_CAMBIO
, swi_cambio
, NULL
, "swi_cambio", NULL
);
1548 xptregister(struct cam_periph
*periph
, void *arg
)
1550 struct cam_sim
*xpt_sim
;
1552 if (periph
== NULL
) {
1553 kprintf("xptregister: periph was NULL!!\n");
1554 return(CAM_REQ_CMP_ERR
);
1557 xpt_sim
= (struct cam_sim
*)arg
;
1558 xpt_sim
->softc
= periph
;
1559 xpt_periph
= periph
;
1560 periph
->softc
= NULL
;
1562 return(CAM_REQ_CMP
);
1566 xpt_add_periph(struct cam_periph
*periph
)
1568 struct cam_ed
*device
;
1570 struct periph_list
*periph_head
;
1572 sim_lock_assert_owned(periph
->sim
->lock
);
1574 device
= periph
->path
->device
;
1576 periph_head
= &device
->periphs
;
1578 status
= CAM_REQ_CMP
;
1580 if (device
!= NULL
) {
1582 * Make room for this peripheral
1583 * so it will fit in the queue
1584 * when it's scheduled to run
1586 status
= camq_resize(&device
->drvq
,
1587 device
->drvq
.array_size
+ 1);
1589 device
->generation
++;
1591 SLIST_INSERT_HEAD(periph_head
, periph
, periph_links
);
1594 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
1595 xsoftc
.xpt_generation
++;
1596 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
1602 xpt_remove_periph(struct cam_periph
*periph
)
1604 struct cam_ed
*device
;
1606 sim_lock_assert_owned(periph
->sim
->lock
);
1608 device
= periph
->path
->device
;
1610 if (device
!= NULL
) {
1611 struct periph_list
*periph_head
;
1613 periph_head
= &device
->periphs
;
1615 /* Release the slot for this peripheral */
1616 camq_resize(&device
->drvq
, device
->drvq
.array_size
- 1);
1618 device
->generation
++;
1620 SLIST_REMOVE(periph_head
, periph
, cam_periph
, periph_links
);
1623 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
1624 xsoftc
.xpt_generation
++;
1625 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
1629 xpt_announce_periph(struct cam_periph
*periph
, char *announce_string
)
1631 struct ccb_pathinq cpi
;
1632 struct ccb_trans_settings cts
;
1633 struct cam_path
*path
;
1638 sim_lock_assert_owned(periph
->sim
->lock
);
1640 path
= periph
->path
;
1642 * To ensure that this is printed in one piece,
1643 * mask out CAM interrupts.
1645 kprintf("%s%d at %s%d bus %d target %d lun %d\n",
1646 periph
->periph_name
, periph
->unit_number
,
1647 path
->bus
->sim
->sim_name
,
1648 path
->bus
->sim
->unit_number
,
1649 path
->bus
->sim
->bus_id
,
1650 path
->target
->target_id
,
1651 path
->device
->lun_id
);
1652 kprintf("%s%d: ", periph
->periph_name
, periph
->unit_number
);
1653 scsi_print_inquiry(&path
->device
->inq_data
);
1654 if (bootverbose
&& path
->device
->serial_num_len
> 0) {
1655 /* Don't wrap the screen - print only the first 60 chars */
1656 kprintf("%s%d: Serial Number %.60s\n", periph
->periph_name
,
1657 periph
->unit_number
, path
->device
->serial_num
);
1659 xpt_setup_ccb(&cts
.ccb_h
, path
, /*priority*/1);
1660 cts
.ccb_h
.func_code
= XPT_GET_TRAN_SETTINGS
;
1661 cts
.type
= CTS_TYPE_CURRENT_SETTINGS
;
1662 xpt_action((union ccb
*)&cts
);
1663 if ((cts
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
1667 /* Ask the SIM for its base transfer speed */
1668 xpt_setup_ccb(&cpi
.ccb_h
, path
, /*priority*/1);
1669 cpi
.ccb_h
.func_code
= XPT_PATH_INQ
;
1670 xpt_action((union ccb
*)&cpi
);
1672 speed
= cpi
.base_transfer_speed
;
1674 if (cts
.ccb_h
.status
== CAM_REQ_CMP
&& cts
.transport
== XPORT_SPI
) {
1675 struct ccb_trans_settings_spi
*spi
;
1677 spi
= &cts
.xport_specific
.spi
;
1678 if ((spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) != 0
1679 && spi
->sync_offset
!= 0) {
1680 freq
= scsi_calc_syncsrate(spi
->sync_period
);
1684 if ((spi
->valid
& CTS_SPI_VALID_BUS_WIDTH
) != 0)
1685 speed
*= (0x01 << spi
->bus_width
);
1687 if (cts
.ccb_h
.status
== CAM_REQ_CMP
&& cts
.transport
== XPORT_FC
) {
1688 struct ccb_trans_settings_fc
*fc
= &cts
.xport_specific
.fc
;
1689 if (fc
->valid
& CTS_FC_VALID_SPEED
) {
1690 speed
= fc
->bitrate
;
1694 if (cts
.ccb_h
.status
== CAM_REQ_CMP
&& cts
.transport
== XPORT_SAS
) {
1695 struct ccb_trans_settings_sas
*sas
= &cts
.xport_specific
.sas
;
1696 if (sas
->valid
& CTS_SAS_VALID_SPEED
) {
1697 speed
= sas
->bitrate
;
1703 kprintf("%s%d: %d.%03dMB/s transfers",
1704 periph
->periph_name
, periph
->unit_number
,
1707 kprintf("%s%d: %dKB/s transfers", periph
->periph_name
,
1708 periph
->unit_number
, speed
);
1709 /* Report additional information about SPI connections */
1710 if (cts
.ccb_h
.status
== CAM_REQ_CMP
&& cts
.transport
== XPORT_SPI
) {
1711 struct ccb_trans_settings_spi
*spi
;
1713 spi
= &cts
.xport_specific
.spi
;
1715 kprintf(" (%d.%03dMHz%s, offset %d", freq
/ 1000,
1717 (spi
->ppr_options
& MSG_EXT_PPR_DT_REQ
) != 0
1721 if ((spi
->valid
& CTS_SPI_VALID_BUS_WIDTH
) != 0
1722 && spi
->bus_width
> 0) {
1728 kprintf("%dbit)", 8 * (0x01 << spi
->bus_width
));
1729 } else if (freq
!= 0) {
1733 if (cts
.ccb_h
.status
== CAM_REQ_CMP
&& cts
.transport
== XPORT_FC
) {
1734 struct ccb_trans_settings_fc
*fc
;
1736 fc
= &cts
.xport_specific
.fc
;
1737 if (fc
->valid
& CTS_FC_VALID_WWNN
)
1738 kprintf(" WWNN 0x%llx", (long long) fc
->wwnn
);
1739 if (fc
->valid
& CTS_FC_VALID_WWPN
)
1740 kprintf(" WWPN 0x%llx", (long long) fc
->wwpn
);
1741 if (fc
->valid
& CTS_FC_VALID_PORT
)
1742 kprintf(" PortID 0x%x", fc
->port
);
1745 if (path
->device
->inq_flags
& SID_CmdQue
1746 || path
->device
->flags
& CAM_DEV_TAG_AFTER_COUNT
) {
1747 kprintf("\n%s%d: Command Queueing Enabled",
1748 periph
->periph_name
, periph
->unit_number
);
1753 * We only want to print the caller's announce string if they've
1756 if (announce_string
!= NULL
)
1757 kprintf("%s%d: %s\n", periph
->periph_name
,
1758 periph
->unit_number
, announce_string
);
1761 static dev_match_ret
1762 xptbusmatch(struct dev_match_pattern
*patterns
, u_int num_patterns
,
1765 dev_match_ret retval
;
1768 retval
= DM_RET_NONE
;
1771 * If we aren't given something to match against, that's an error.
1774 return(DM_RET_ERROR
);
1777 * If there are no match entries, then this bus matches no
1780 if ((patterns
== NULL
) || (num_patterns
== 0))
1781 return(DM_RET_DESCEND
| DM_RET_COPY
);
1783 for (i
= 0; i
< num_patterns
; i
++) {
1784 struct bus_match_pattern
*cur_pattern
;
1787 * If the pattern in question isn't for a bus node, we
1788 * aren't interested. However, we do indicate to the
1789 * calling routine that we should continue descending the
1790 * tree, since the user wants to match against lower-level
1793 if (patterns
[i
].type
!= DEV_MATCH_BUS
) {
1794 if ((retval
& DM_RET_ACTION_MASK
) == DM_RET_NONE
)
1795 retval
|= DM_RET_DESCEND
;
1799 cur_pattern
= &patterns
[i
].pattern
.bus_pattern
;
1802 * If they want to match any bus node, we give them any
1805 if (cur_pattern
->flags
== BUS_MATCH_ANY
) {
1806 /* set the copy flag */
1807 retval
|= DM_RET_COPY
;
1810 * If we've already decided on an action, go ahead
1813 if ((retval
& DM_RET_ACTION_MASK
) != DM_RET_NONE
)
1818 * Not sure why someone would do this...
1820 if (cur_pattern
->flags
== BUS_MATCH_NONE
)
1823 if (((cur_pattern
->flags
& BUS_MATCH_PATH
) != 0)
1824 && (cur_pattern
->path_id
!= bus
->path_id
))
1827 if (((cur_pattern
->flags
& BUS_MATCH_BUS_ID
) != 0)
1828 && (cur_pattern
->bus_id
!= bus
->sim
->bus_id
))
1831 if (((cur_pattern
->flags
& BUS_MATCH_UNIT
) != 0)
1832 && (cur_pattern
->unit_number
!= bus
->sim
->unit_number
))
1835 if (((cur_pattern
->flags
& BUS_MATCH_NAME
) != 0)
1836 && (strncmp(cur_pattern
->dev_name
, bus
->sim
->sim_name
,
1841 * If we get to this point, the user definitely wants
1842 * information on this bus. So tell the caller to copy the
1845 retval
|= DM_RET_COPY
;
1848 * If the return action has been set to descend, then we
1849 * know that we've already seen a non-bus matching
1850 * expression, therefore we need to further descend the tree.
1851 * This won't change by continuing around the loop, so we
1852 * go ahead and return. If we haven't seen a non-bus
1853 * matching expression, we keep going around the loop until
1854 * we exhaust the matching expressions. We'll set the stop
1855 * flag once we fall out of the loop.
1857 if ((retval
& DM_RET_ACTION_MASK
) == DM_RET_DESCEND
)
1862 * If the return action hasn't been set to descend yet, that means
1863 * we haven't seen anything other than bus matching patterns. So
1864 * tell the caller to stop descending the tree -- the user doesn't
1865 * want to match against lower level tree elements.
1867 if ((retval
& DM_RET_ACTION_MASK
) == DM_RET_NONE
)
1868 retval
|= DM_RET_STOP
;
1873 static dev_match_ret
1874 xptdevicematch(struct dev_match_pattern
*patterns
, u_int num_patterns
,
1875 struct cam_ed
*device
)
1877 dev_match_ret retval
;
1880 retval
= DM_RET_NONE
;
1883 * If we aren't given something to match against, that's an error.
1886 return(DM_RET_ERROR
);
1889 * If there are no match entries, then this device matches no
1892 if ((patterns
== NULL
) || (num_patterns
== 0))
1893 return(DM_RET_DESCEND
| DM_RET_COPY
);
1895 for (i
= 0; i
< num_patterns
; i
++) {
1896 struct device_match_pattern
*cur_pattern
;
1899 * If the pattern in question isn't for a device node, we
1900 * aren't interested.
1902 if (patterns
[i
].type
!= DEV_MATCH_DEVICE
) {
1903 if ((patterns
[i
].type
== DEV_MATCH_PERIPH
)
1904 && ((retval
& DM_RET_ACTION_MASK
) == DM_RET_NONE
))
1905 retval
|= DM_RET_DESCEND
;
1909 cur_pattern
= &patterns
[i
].pattern
.device_pattern
;
1912 * If they want to match any device node, we give them any
1915 if (cur_pattern
->flags
== DEV_MATCH_ANY
) {
1916 /* set the copy flag */
1917 retval
|= DM_RET_COPY
;
1921 * If we've already decided on an action, go ahead
1924 if ((retval
& DM_RET_ACTION_MASK
) != DM_RET_NONE
)
1929 * Not sure why someone would do this...
1931 if (cur_pattern
->flags
== DEV_MATCH_NONE
)
1934 if (((cur_pattern
->flags
& DEV_MATCH_PATH
) != 0)
1935 && (cur_pattern
->path_id
!= device
->target
->bus
->path_id
))
1938 if (((cur_pattern
->flags
& DEV_MATCH_TARGET
) != 0)
1939 && (cur_pattern
->target_id
!= device
->target
->target_id
))
1942 if (((cur_pattern
->flags
& DEV_MATCH_LUN
) != 0)
1943 && (cur_pattern
->target_lun
!= device
->lun_id
))
1946 if (((cur_pattern
->flags
& DEV_MATCH_INQUIRY
) != 0)
1947 && (cam_quirkmatch((caddr_t
)&device
->inq_data
,
1948 (caddr_t
)&cur_pattern
->inq_pat
,
1949 1, sizeof(cur_pattern
->inq_pat
),
1950 scsi_static_inquiry_match
) == NULL
))
1954 * If we get to this point, the user definitely wants
1955 * information on this device. So tell the caller to copy
1958 retval
|= DM_RET_COPY
;
1961 * If the return action has been set to descend, then we
1962 * know that we've already seen a peripheral matching
1963 * expression, therefore we need to further descend the tree.
1964 * This won't change by continuing around the loop, so we
1965 * go ahead and return. If we haven't seen a peripheral
1966 * matching expression, we keep going around the loop until
1967 * we exhaust the matching expressions. We'll set the stop
1968 * flag once we fall out of the loop.
1970 if ((retval
& DM_RET_ACTION_MASK
) == DM_RET_DESCEND
)
1975 * If the return action hasn't been set to descend yet, that means
1976 * we haven't seen any peripheral matching patterns. So tell the
1977 * caller to stop descending the tree -- the user doesn't want to
1978 * match against lower level tree elements.
1980 if ((retval
& DM_RET_ACTION_MASK
) == DM_RET_NONE
)
1981 retval
|= DM_RET_STOP
;
1987 * Match a single peripheral against any number of match patterns.
1989 static dev_match_ret
1990 xptperiphmatch(struct dev_match_pattern
*patterns
, u_int num_patterns
,
1991 struct cam_periph
*periph
)
1993 dev_match_ret retval
;
1997 * If we aren't given something to match against, that's an error.
2000 return(DM_RET_ERROR
);
2003 * If there are no match entries, then this peripheral matches no
2006 if ((patterns
== NULL
) || (num_patterns
== 0))
2007 return(DM_RET_STOP
| DM_RET_COPY
);
2010 * There aren't any nodes below a peripheral node, so there's no
2011 * reason to descend the tree any further.
2013 retval
= DM_RET_STOP
;
2015 for (i
= 0; i
< num_patterns
; i
++) {
2016 struct periph_match_pattern
*cur_pattern
;
2019 * If the pattern in question isn't for a peripheral, we
2020 * aren't interested.
2022 if (patterns
[i
].type
!= DEV_MATCH_PERIPH
)
2025 cur_pattern
= &patterns
[i
].pattern
.periph_pattern
;
2028 * If they want to match on anything, then we will do so.
2030 if (cur_pattern
->flags
== PERIPH_MATCH_ANY
) {
2031 /* set the copy flag */
2032 retval
|= DM_RET_COPY
;
2035 * We've already set the return action to stop,
2036 * since there are no nodes below peripherals in
2043 * Not sure why someone would do this...
2045 if (cur_pattern
->flags
== PERIPH_MATCH_NONE
)
2048 if (((cur_pattern
->flags
& PERIPH_MATCH_PATH
) != 0)
2049 && (cur_pattern
->path_id
!= periph
->path
->bus
->path_id
))
2053 * For the target and lun id's, we have to make sure the
2054 * target and lun pointers aren't NULL. The xpt peripheral
2055 * has a wildcard target and device.
2057 if (((cur_pattern
->flags
& PERIPH_MATCH_TARGET
) != 0)
2058 && ((periph
->path
->target
== NULL
)
2059 ||(cur_pattern
->target_id
!= periph
->path
->target
->target_id
)))
2062 if (((cur_pattern
->flags
& PERIPH_MATCH_LUN
) != 0)
2063 && ((periph
->path
->device
== NULL
)
2064 || (cur_pattern
->target_lun
!= periph
->path
->device
->lun_id
)))
2067 if (((cur_pattern
->flags
& PERIPH_MATCH_UNIT
) != 0)
2068 && (cur_pattern
->unit_number
!= periph
->unit_number
))
2071 if (((cur_pattern
->flags
& PERIPH_MATCH_NAME
) != 0)
2072 && (strncmp(cur_pattern
->periph_name
, periph
->periph_name
,
2077 * If we get to this point, the user definitely wants
2078 * information on this peripheral. So tell the caller to
2079 * copy the data out.
2081 retval
|= DM_RET_COPY
;
2084 * The return action has already been set to stop, since
2085 * peripherals don't have any nodes below them in the EDT.
2091 * If we get to this point, the peripheral that was passed in
2092 * doesn't match any of the patterns.
2098 xptedtbusfunc(struct cam_eb
*bus
, void *arg
)
2100 struct ccb_dev_match
*cdm
;
2101 dev_match_ret retval
;
2103 cdm
= (struct ccb_dev_match
*)arg
;
2106 * If our position is for something deeper in the tree, that means
2107 * that we've already seen this node. So, we keep going down.
2109 if ((cdm
->pos
.position_type
& CAM_DEV_POS_BUS
)
2110 && (cdm
->pos
.cookie
.bus
== bus
)
2111 && (cdm
->pos
.position_type
& CAM_DEV_POS_TARGET
)
2112 && (cdm
->pos
.cookie
.target
!= NULL
))
2113 retval
= DM_RET_DESCEND
;
2115 retval
= xptbusmatch(cdm
->patterns
, cdm
->num_patterns
, bus
);
2118 * If we got an error, bail out of the search.
2120 if ((retval
& DM_RET_ACTION_MASK
) == DM_RET_ERROR
) {
2121 cdm
->status
= CAM_DEV_MATCH_ERROR
;
2126 * If the copy flag is set, copy this bus out.
2128 if (retval
& DM_RET_COPY
) {
2131 spaceleft
= cdm
->match_buf_len
- (cdm
->num_matches
*
2132 sizeof(struct dev_match_result
));
2135 * If we don't have enough space to put in another
2136 * match result, save our position and tell the
2137 * user there are more devices to check.
2139 if (spaceleft
< sizeof(struct dev_match_result
)) {
2140 bzero(&cdm
->pos
, sizeof(cdm
->pos
));
2141 cdm
->pos
.position_type
=
2142 CAM_DEV_POS_EDT
| CAM_DEV_POS_BUS
;
2144 cdm
->pos
.cookie
.bus
= bus
;
2145 cdm
->pos
.generations
[CAM_BUS_GENERATION
]=
2146 xsoftc
.bus_generation
;
2147 cdm
->status
= CAM_DEV_MATCH_MORE
;
2150 j
= cdm
->num_matches
;
2152 cdm
->matches
[j
].type
= DEV_MATCH_BUS
;
2153 cdm
->matches
[j
].result
.bus_result
.path_id
= bus
->path_id
;
2154 cdm
->matches
[j
].result
.bus_result
.bus_id
= bus
->sim
->bus_id
;
2155 cdm
->matches
[j
].result
.bus_result
.unit_number
=
2156 bus
->sim
->unit_number
;
2157 strncpy(cdm
->matches
[j
].result
.bus_result
.dev_name
,
2158 bus
->sim
->sim_name
, DEV_IDLEN
);
2162 * If the user is only interested in busses, there's no
2163 * reason to descend to the next level in the tree.
2165 if ((retval
& DM_RET_ACTION_MASK
) == DM_RET_STOP
)
2169 * If there is a target generation recorded, check it to
2170 * make sure the target list hasn't changed.
2172 if ((cdm
->pos
.position_type
& CAM_DEV_POS_BUS
)
2173 && (bus
== cdm
->pos
.cookie
.bus
)
2174 && (cdm
->pos
.position_type
& CAM_DEV_POS_TARGET
)
2175 && (cdm
->pos
.generations
[CAM_TARGET_GENERATION
] != 0)
2176 && (cdm
->pos
.generations
[CAM_TARGET_GENERATION
] !=
2178 cdm
->status
= CAM_DEV_MATCH_LIST_CHANGED
;
2182 if ((cdm
->pos
.position_type
& CAM_DEV_POS_BUS
)
2183 && (cdm
->pos
.cookie
.bus
== bus
)
2184 && (cdm
->pos
.position_type
& CAM_DEV_POS_TARGET
)
2185 && (cdm
->pos
.cookie
.target
!= NULL
))
2186 return(xpttargettraverse(bus
,
2187 (struct cam_et
*)cdm
->pos
.cookie
.target
,
2188 xptedttargetfunc
, arg
));
2190 return(xpttargettraverse(bus
, NULL
, xptedttargetfunc
, arg
));
2194 xptedttargetfunc(struct cam_et
*target
, void *arg
)
2196 struct ccb_dev_match
*cdm
;
2198 cdm
= (struct ccb_dev_match
*)arg
;
2201 * If there is a device list generation recorded, check it to
2202 * make sure the device list hasn't changed.
2204 if ((cdm
->pos
.position_type
& CAM_DEV_POS_BUS
)
2205 && (cdm
->pos
.cookie
.bus
== target
->bus
)
2206 && (cdm
->pos
.position_type
& CAM_DEV_POS_TARGET
)
2207 && (cdm
->pos
.cookie
.target
== target
)
2208 && (cdm
->pos
.position_type
& CAM_DEV_POS_DEVICE
)
2209 && (cdm
->pos
.generations
[CAM_DEV_GENERATION
] != 0)
2210 && (cdm
->pos
.generations
[CAM_DEV_GENERATION
] !=
2211 target
->generation
)) {
2212 cdm
->status
= CAM_DEV_MATCH_LIST_CHANGED
;
2216 if ((cdm
->pos
.position_type
& CAM_DEV_POS_BUS
)
2217 && (cdm
->pos
.cookie
.bus
== target
->bus
)
2218 && (cdm
->pos
.position_type
& CAM_DEV_POS_TARGET
)
2219 && (cdm
->pos
.cookie
.target
== target
)
2220 && (cdm
->pos
.position_type
& CAM_DEV_POS_DEVICE
)
2221 && (cdm
->pos
.cookie
.device
!= NULL
))
2222 return(xptdevicetraverse(target
,
2223 (struct cam_ed
*)cdm
->pos
.cookie
.device
,
2224 xptedtdevicefunc
, arg
));
2226 return(xptdevicetraverse(target
, NULL
, xptedtdevicefunc
, arg
));
2230 xptedtdevicefunc(struct cam_ed
*device
, void *arg
)
2233 struct ccb_dev_match
*cdm
;
2234 dev_match_ret retval
;
2236 cdm
= (struct ccb_dev_match
*)arg
;
2239 * If our position is for something deeper in the tree, that means
2240 * that we've already seen this node. So, we keep going down.
2242 if ((cdm
->pos
.position_type
& CAM_DEV_POS_DEVICE
)
2243 && (cdm
->pos
.cookie
.device
== device
)
2244 && (cdm
->pos
.position_type
& CAM_DEV_POS_PERIPH
)
2245 && (cdm
->pos
.cookie
.periph
!= NULL
))
2246 retval
= DM_RET_DESCEND
;
2248 retval
= xptdevicematch(cdm
->patterns
, cdm
->num_patterns
,
2251 if ((retval
& DM_RET_ACTION_MASK
) == DM_RET_ERROR
) {
2252 cdm
->status
= CAM_DEV_MATCH_ERROR
;
2257 * If the copy flag is set, copy this device out.
2259 if (retval
& DM_RET_COPY
) {
2262 spaceleft
= cdm
->match_buf_len
- (cdm
->num_matches
*
2263 sizeof(struct dev_match_result
));
2266 * If we don't have enough space to put in another
2267 * match result, save our position and tell the
2268 * user there are more devices to check.
2270 if (spaceleft
< sizeof(struct dev_match_result
)) {
2271 bzero(&cdm
->pos
, sizeof(cdm
->pos
));
2272 cdm
->pos
.position_type
=
2273 CAM_DEV_POS_EDT
| CAM_DEV_POS_BUS
|
2274 CAM_DEV_POS_TARGET
| CAM_DEV_POS_DEVICE
;
2276 cdm
->pos
.cookie
.bus
= device
->target
->bus
;
2277 cdm
->pos
.generations
[CAM_BUS_GENERATION
]=
2278 xsoftc
.bus_generation
;
2279 cdm
->pos
.cookie
.target
= device
->target
;
2280 cdm
->pos
.generations
[CAM_TARGET_GENERATION
] =
2281 device
->target
->bus
->generation
;
2282 cdm
->pos
.cookie
.device
= device
;
2283 cdm
->pos
.generations
[CAM_DEV_GENERATION
] =
2284 device
->target
->generation
;
2285 cdm
->status
= CAM_DEV_MATCH_MORE
;
2288 j
= cdm
->num_matches
;
2290 cdm
->matches
[j
].type
= DEV_MATCH_DEVICE
;
2291 cdm
->matches
[j
].result
.device_result
.path_id
=
2292 device
->target
->bus
->path_id
;
2293 cdm
->matches
[j
].result
.device_result
.target_id
=
2294 device
->target
->target_id
;
2295 cdm
->matches
[j
].result
.device_result
.target_lun
=
2297 bcopy(&device
->inq_data
,
2298 &cdm
->matches
[j
].result
.device_result
.inq_data
,
2299 sizeof(struct scsi_inquiry_data
));
2301 /* Let the user know whether this device is unconfigured */
2302 if (device
->flags
& CAM_DEV_UNCONFIGURED
)
2303 cdm
->matches
[j
].result
.device_result
.flags
=
2304 DEV_RESULT_UNCONFIGURED
;
2306 cdm
->matches
[j
].result
.device_result
.flags
=
2311 * If the user isn't interested in peripherals, don't descend
2312 * the tree any further.
2314 if ((retval
& DM_RET_ACTION_MASK
) == DM_RET_STOP
)
2318 * If there is a peripheral list generation recorded, make sure
2319 * it hasn't changed.
2321 if ((cdm
->pos
.position_type
& CAM_DEV_POS_BUS
)
2322 && (device
->target
->bus
== cdm
->pos
.cookie
.bus
)
2323 && (cdm
->pos
.position_type
& CAM_DEV_POS_TARGET
)
2324 && (device
->target
== cdm
->pos
.cookie
.target
)
2325 && (cdm
->pos
.position_type
& CAM_DEV_POS_DEVICE
)
2326 && (device
== cdm
->pos
.cookie
.device
)
2327 && (cdm
->pos
.position_type
& CAM_DEV_POS_PERIPH
)
2328 && (cdm
->pos
.generations
[CAM_PERIPH_GENERATION
] != 0)
2329 && (cdm
->pos
.generations
[CAM_PERIPH_GENERATION
] !=
2330 device
->generation
)){
2331 cdm
->status
= CAM_DEV_MATCH_LIST_CHANGED
;
2335 if ((cdm
->pos
.position_type
& CAM_DEV_POS_BUS
)
2336 && (cdm
->pos
.cookie
.bus
== device
->target
->bus
)
2337 && (cdm
->pos
.position_type
& CAM_DEV_POS_TARGET
)
2338 && (cdm
->pos
.cookie
.target
== device
->target
)
2339 && (cdm
->pos
.position_type
& CAM_DEV_POS_DEVICE
)
2340 && (cdm
->pos
.cookie
.device
== device
)
2341 && (cdm
->pos
.position_type
& CAM_DEV_POS_PERIPH
)
2342 && (cdm
->pos
.cookie
.periph
!= NULL
))
2343 return(xptperiphtraverse(device
,
2344 (struct cam_periph
*)cdm
->pos
.cookie
.periph
,
2345 xptedtperiphfunc
, arg
));
2347 return(xptperiphtraverse(device
, NULL
, xptedtperiphfunc
, arg
));
2351 xptedtperiphfunc(struct cam_periph
*periph
, void *arg
)
2353 struct ccb_dev_match
*cdm
;
2354 dev_match_ret retval
;
2356 cdm
= (struct ccb_dev_match
*)arg
;
2358 retval
= xptperiphmatch(cdm
->patterns
, cdm
->num_patterns
, periph
);
2360 if ((retval
& DM_RET_ACTION_MASK
) == DM_RET_ERROR
) {
2361 cdm
->status
= CAM_DEV_MATCH_ERROR
;
2366 * If the copy flag is set, copy this peripheral out.
2368 if (retval
& DM_RET_COPY
) {
2371 spaceleft
= cdm
->match_buf_len
- (cdm
->num_matches
*
2372 sizeof(struct dev_match_result
));
2375 * If we don't have enough space to put in another
2376 * match result, save our position and tell the
2377 * user there are more devices to check.
2379 if (spaceleft
< sizeof(struct dev_match_result
)) {
2380 bzero(&cdm
->pos
, sizeof(cdm
->pos
));
2381 cdm
->pos
.position_type
=
2382 CAM_DEV_POS_EDT
| CAM_DEV_POS_BUS
|
2383 CAM_DEV_POS_TARGET
| CAM_DEV_POS_DEVICE
|
2386 cdm
->pos
.cookie
.bus
= periph
->path
->bus
;
2387 cdm
->pos
.generations
[CAM_BUS_GENERATION
]=
2388 xsoftc
.bus_generation
;
2389 cdm
->pos
.cookie
.target
= periph
->path
->target
;
2390 cdm
->pos
.generations
[CAM_TARGET_GENERATION
] =
2391 periph
->path
->bus
->generation
;
2392 cdm
->pos
.cookie
.device
= periph
->path
->device
;
2393 cdm
->pos
.generations
[CAM_DEV_GENERATION
] =
2394 periph
->path
->target
->generation
;
2395 cdm
->pos
.cookie
.periph
= periph
;
2396 cdm
->pos
.generations
[CAM_PERIPH_GENERATION
] =
2397 periph
->path
->device
->generation
;
2398 cdm
->status
= CAM_DEV_MATCH_MORE
;
2402 j
= cdm
->num_matches
;
2404 cdm
->matches
[j
].type
= DEV_MATCH_PERIPH
;
2405 cdm
->matches
[j
].result
.periph_result
.path_id
=
2406 periph
->path
->bus
->path_id
;
2407 cdm
->matches
[j
].result
.periph_result
.target_id
=
2408 periph
->path
->target
->target_id
;
2409 cdm
->matches
[j
].result
.periph_result
.target_lun
=
2410 periph
->path
->device
->lun_id
;
2411 cdm
->matches
[j
].result
.periph_result
.unit_number
=
2412 periph
->unit_number
;
2413 strncpy(cdm
->matches
[j
].result
.periph_result
.periph_name
,
2414 periph
->periph_name
, DEV_IDLEN
);
2421 xptedtmatch(struct ccb_dev_match
*cdm
)
2425 cdm
->num_matches
= 0;
2428 * Check the bus list generation. If it has changed, the user
2429 * needs to reset everything and start over.
2431 if ((cdm
->pos
.position_type
& CAM_DEV_POS_BUS
)
2432 && (cdm
->pos
.generations
[CAM_BUS_GENERATION
] != 0)
2433 && (cdm
->pos
.generations
[CAM_BUS_GENERATION
] != xsoftc
.bus_generation
)) {
2434 cdm
->status
= CAM_DEV_MATCH_LIST_CHANGED
;
2438 if ((cdm
->pos
.position_type
& CAM_DEV_POS_BUS
)
2439 && (cdm
->pos
.cookie
.bus
!= NULL
))
2440 ret
= xptbustraverse((struct cam_eb
*)cdm
->pos
.cookie
.bus
,
2441 xptedtbusfunc
, cdm
);
2443 ret
= xptbustraverse(NULL
, xptedtbusfunc
, cdm
);
2446 * If we get back 0, that means that we had to stop before fully
2447 * traversing the EDT. It also means that one of the subroutines
2448 * has set the status field to the proper value. If we get back 1,
2449 * we've fully traversed the EDT and copied out any matching entries.
2452 cdm
->status
= CAM_DEV_MATCH_LAST
;
2458 xptplistpdrvfunc(struct periph_driver
**pdrv
, void *arg
)
2460 struct ccb_dev_match
*cdm
;
2462 cdm
= (struct ccb_dev_match
*)arg
;
2464 if ((cdm
->pos
.position_type
& CAM_DEV_POS_PDPTR
)
2465 && (cdm
->pos
.cookie
.pdrv
== pdrv
)
2466 && (cdm
->pos
.position_type
& CAM_DEV_POS_PERIPH
)
2467 && (cdm
->pos
.generations
[CAM_PERIPH_GENERATION
] != 0)
2468 && (cdm
->pos
.generations
[CAM_PERIPH_GENERATION
] !=
2469 (*pdrv
)->generation
)) {
2470 cdm
->status
= CAM_DEV_MATCH_LIST_CHANGED
;
2474 if ((cdm
->pos
.position_type
& CAM_DEV_POS_PDPTR
)
2475 && (cdm
->pos
.cookie
.pdrv
== pdrv
)
2476 && (cdm
->pos
.position_type
& CAM_DEV_POS_PERIPH
)
2477 && (cdm
->pos
.cookie
.periph
!= NULL
))
2478 return(xptpdperiphtraverse(pdrv
,
2479 (struct cam_periph
*)cdm
->pos
.cookie
.periph
,
2480 xptplistperiphfunc
, arg
));
2482 return(xptpdperiphtraverse(pdrv
, NULL
,xptplistperiphfunc
, arg
));
2486 xptplistperiphfunc(struct cam_periph
*periph
, void *arg
)
2488 struct ccb_dev_match
*cdm
;
2489 dev_match_ret retval
;
2491 cdm
= (struct ccb_dev_match
*)arg
;
2493 retval
= xptperiphmatch(cdm
->patterns
, cdm
->num_patterns
, periph
);
2495 if ((retval
& DM_RET_ACTION_MASK
) == DM_RET_ERROR
) {
2496 cdm
->status
= CAM_DEV_MATCH_ERROR
;
2501 * If the copy flag is set, copy this peripheral out.
2503 if (retval
& DM_RET_COPY
) {
2506 spaceleft
= cdm
->match_buf_len
- (cdm
->num_matches
*
2507 sizeof(struct dev_match_result
));
2510 * If we don't have enough space to put in another
2511 * match result, save our position and tell the
2512 * user there are more devices to check.
2514 if (spaceleft
< sizeof(struct dev_match_result
)) {
2515 struct periph_driver
**pdrv
;
2518 bzero(&cdm
->pos
, sizeof(cdm
->pos
));
2519 cdm
->pos
.position_type
=
2520 CAM_DEV_POS_PDRV
| CAM_DEV_POS_PDPTR
|
2524 * This may look a bit non-sensical, but it is
2525 * actually quite logical. There are very few
2526 * peripheral drivers, and bloating every peripheral
2527 * structure with a pointer back to its parent
2528 * peripheral driver linker set entry would cost
2529 * more in the long run than doing this quick lookup.
2531 for (pdrv
= periph_drivers
; *pdrv
!= NULL
; pdrv
++) {
2532 if (strcmp((*pdrv
)->driver_name
,
2533 periph
->periph_name
) == 0)
2537 if (*pdrv
== NULL
) {
2538 cdm
->status
= CAM_DEV_MATCH_ERROR
;
2542 cdm
->pos
.cookie
.pdrv
= pdrv
;
2544 * The periph generation slot does double duty, as
2545 * does the periph pointer slot. They are used for
2546 * both edt and pdrv lookups and positioning.
2548 cdm
->pos
.cookie
.periph
= periph
;
2549 cdm
->pos
.generations
[CAM_PERIPH_GENERATION
] =
2550 (*pdrv
)->generation
;
2551 cdm
->status
= CAM_DEV_MATCH_MORE
;
2555 j
= cdm
->num_matches
;
2557 cdm
->matches
[j
].type
= DEV_MATCH_PERIPH
;
2558 cdm
->matches
[j
].result
.periph_result
.path_id
=
2559 periph
->path
->bus
->path_id
;
2562 * The transport layer peripheral doesn't have a target or
2565 if (periph
->path
->target
)
2566 cdm
->matches
[j
].result
.periph_result
.target_id
=
2567 periph
->path
->target
->target_id
;
2569 cdm
->matches
[j
].result
.periph_result
.target_id
= -1;
2571 if (periph
->path
->device
)
2572 cdm
->matches
[j
].result
.periph_result
.target_lun
=
2573 periph
->path
->device
->lun_id
;
2575 cdm
->matches
[j
].result
.periph_result
.target_lun
= -1;
2577 cdm
->matches
[j
].result
.periph_result
.unit_number
=
2578 periph
->unit_number
;
2579 strncpy(cdm
->matches
[j
].result
.periph_result
.periph_name
,
2580 periph
->periph_name
, DEV_IDLEN
);
2587 xptperiphlistmatch(struct ccb_dev_match
*cdm
)
2591 cdm
->num_matches
= 0;
2594 * At this point in the edt traversal function, we check the bus
2595 * list generation to make sure that no busses have been added or
2596 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2597 * For the peripheral driver list traversal function, however, we
2598 * don't have to worry about new peripheral driver types coming or
2599 * going; they're in a linker set, and therefore can't change
2600 * without a recompile.
2603 if ((cdm
->pos
.position_type
& CAM_DEV_POS_PDPTR
)
2604 && (cdm
->pos
.cookie
.pdrv
!= NULL
))
2605 ret
= xptpdrvtraverse(
2606 (struct periph_driver
**)cdm
->pos
.cookie
.pdrv
,
2607 xptplistpdrvfunc
, cdm
);
2609 ret
= xptpdrvtraverse(NULL
, xptplistpdrvfunc
, cdm
);
2612 * If we get back 0, that means that we had to stop before fully
2613 * traversing the peripheral driver tree. It also means that one of
2614 * the subroutines has set the status field to the proper value. If
2615 * we get back 1, we've fully traversed the EDT and copied out any
2619 cdm
->status
= CAM_DEV_MATCH_LAST
;
2625 xptbustraverse(struct cam_eb
*start_bus
, xpt_busfunc_t
*tr_func
, void *arg
)
2627 struct cam_eb
*bus
, *next_bus
;
2632 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
2633 for (bus
= (start_bus
? start_bus
: TAILQ_FIRST(&xsoftc
.xpt_busses
));
2636 next_bus
= TAILQ_NEXT(bus
, links
);
2638 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
2639 CAM_SIM_LOCK(bus
->sim
);
2640 retval
= tr_func(bus
, arg
);
2641 CAM_SIM_UNLOCK(bus
->sim
);
2644 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
2646 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
2652 xpttargettraverse(struct cam_eb
*bus
, struct cam_et
*start_target
,
2653 xpt_targetfunc_t
*tr_func
, void *arg
)
2655 struct cam_et
*target
, *next_target
;
2659 for (target
= (start_target
? start_target
:
2660 TAILQ_FIRST(&bus
->et_entries
));
2661 target
!= NULL
; target
= next_target
) {
2663 next_target
= TAILQ_NEXT(target
, links
);
2665 retval
= tr_func(target
, arg
);
2675 xptdevicetraverse(struct cam_et
*target
, struct cam_ed
*start_device
,
2676 xpt_devicefunc_t
*tr_func
, void *arg
)
2678 struct cam_ed
*device
, *next_device
;
2682 for (device
= (start_device
? start_device
:
2683 TAILQ_FIRST(&target
->ed_entries
));
2685 device
= next_device
) {
2687 next_device
= TAILQ_NEXT(device
, links
);
2689 retval
= tr_func(device
, arg
);
2699 xptperiphtraverse(struct cam_ed
*device
, struct cam_periph
*start_periph
,
2700 xpt_periphfunc_t
*tr_func
, void *arg
)
2702 struct cam_periph
*periph
, *next_periph
;
2707 for (periph
= (start_periph
? start_periph
:
2708 SLIST_FIRST(&device
->periphs
));
2710 periph
= next_periph
) {
2712 next_periph
= SLIST_NEXT(periph
, periph_links
);
2714 retval
= tr_func(periph
, arg
);
2723 xptpdrvtraverse(struct periph_driver
**start_pdrv
,
2724 xpt_pdrvfunc_t
*tr_func
, void *arg
)
2726 struct periph_driver
**pdrv
;
2732 * We don't traverse the peripheral driver list like we do the
2733 * other lists, because it is a linker set, and therefore cannot be
2734 * changed during runtime. If the peripheral driver list is ever
2735 * re-done to be something other than a linker set (i.e. it can
2736 * change while the system is running), the list traversal should
2737 * be modified to work like the other traversal functions.
2739 for (pdrv
= (start_pdrv
? start_pdrv
: periph_drivers
);
2740 *pdrv
!= NULL
; pdrv
++) {
2741 retval
= tr_func(pdrv
, arg
);
2751 xptpdperiphtraverse(struct periph_driver
**pdrv
,
2752 struct cam_periph
*start_periph
,
2753 xpt_periphfunc_t
*tr_func
, void *arg
)
2755 struct cam_periph
*periph
, *next_periph
;
2760 for (periph
= (start_periph
? start_periph
:
2761 TAILQ_FIRST(&(*pdrv
)->units
)); periph
!= NULL
;
2762 periph
= next_periph
) {
2764 next_periph
= TAILQ_NEXT(periph
, unit_links
);
2766 retval
= tr_func(periph
, arg
);
2774 xptdefbusfunc(struct cam_eb
*bus
, void *arg
)
2776 struct xpt_traverse_config
*tr_config
;
2778 tr_config
= (struct xpt_traverse_config
*)arg
;
2780 if (tr_config
->depth
== XPT_DEPTH_BUS
) {
2781 xpt_busfunc_t
*tr_func
;
2783 tr_func
= (xpt_busfunc_t
*)tr_config
->tr_func
;
2785 return(tr_func(bus
, tr_config
->tr_arg
));
2787 return(xpttargettraverse(bus
, NULL
, xptdeftargetfunc
, arg
));
2791 xptdeftargetfunc(struct cam_et
*target
, void *arg
)
2793 struct xpt_traverse_config
*tr_config
;
2795 tr_config
= (struct xpt_traverse_config
*)arg
;
2797 if (tr_config
->depth
== XPT_DEPTH_TARGET
) {
2798 xpt_targetfunc_t
*tr_func
;
2800 tr_func
= (xpt_targetfunc_t
*)tr_config
->tr_func
;
2802 return(tr_func(target
, tr_config
->tr_arg
));
2804 return(xptdevicetraverse(target
, NULL
, xptdefdevicefunc
, arg
));
2808 xptdefdevicefunc(struct cam_ed
*device
, void *arg
)
2810 struct xpt_traverse_config
*tr_config
;
2812 tr_config
= (struct xpt_traverse_config
*)arg
;
2814 if (tr_config
->depth
== XPT_DEPTH_DEVICE
) {
2815 xpt_devicefunc_t
*tr_func
;
2817 tr_func
= (xpt_devicefunc_t
*)tr_config
->tr_func
;
2819 return(tr_func(device
, tr_config
->tr_arg
));
2821 return(xptperiphtraverse(device
, NULL
, xptdefperiphfunc
, arg
));
2825 xptdefperiphfunc(struct cam_periph
*periph
, void *arg
)
2827 struct xpt_traverse_config
*tr_config
;
2828 xpt_periphfunc_t
*tr_func
;
2830 tr_config
= (struct xpt_traverse_config
*)arg
;
2832 tr_func
= (xpt_periphfunc_t
*)tr_config
->tr_func
;
2835 * Unlike the other default functions, we don't check for depth
2836 * here. The peripheral driver level is the last level in the EDT,
2837 * so if we're here, we should execute the function in question.
2839 return(tr_func(periph
, tr_config
->tr_arg
));
2843 * Execute the given function for every bus in the EDT.
2846 xpt_for_all_busses(xpt_busfunc_t
*tr_func
, void *arg
)
2848 struct xpt_traverse_config tr_config
;
2850 tr_config
.depth
= XPT_DEPTH_BUS
;
2851 tr_config
.tr_func
= tr_func
;
2852 tr_config
.tr_arg
= arg
;
2854 return(xptbustraverse(NULL
, xptdefbusfunc
, &tr_config
));
2858 * Execute the given function for every device in the EDT.
2861 xpt_for_all_devices(xpt_devicefunc_t
*tr_func
, void *arg
)
2863 struct xpt_traverse_config tr_config
;
2865 tr_config
.depth
= XPT_DEPTH_DEVICE
;
2866 tr_config
.tr_func
= tr_func
;
2867 tr_config
.tr_arg
= arg
;
2869 return(xptbustraverse(NULL
, xptdefbusfunc
, &tr_config
));
2873 xptsetasyncfunc(struct cam_ed
*device
, void *arg
)
2875 struct cam_path path
;
2876 struct ccb_getdev cgd
;
2877 struct async_node
*cur_entry
;
2879 cur_entry
= (struct async_node
*)arg
;
2882 * Don't report unconfigured devices (Wildcard devs,
2883 * devices only for target mode, device instances
2884 * that have been invalidated but are waiting for
2885 * their last reference count to be released).
2887 if ((device
->flags
& CAM_DEV_UNCONFIGURED
) != 0)
2890 xpt_compile_path(&path
,
2892 device
->target
->bus
->path_id
,
2893 device
->target
->target_id
,
2895 xpt_setup_ccb(&cgd
.ccb_h
, &path
, /*priority*/1);
2896 cgd
.ccb_h
.func_code
= XPT_GDEV_TYPE
;
2897 xpt_action((union ccb
*)&cgd
);
2898 cur_entry
->callback(cur_entry
->callback_arg
,
2901 xpt_release_path(&path
);
2907 xptsetasyncbusfunc(struct cam_eb
*bus
, void *arg
)
2909 struct cam_path path
;
2910 struct ccb_pathinq cpi
;
2911 struct async_node
*cur_entry
;
2913 cur_entry
= (struct async_node
*)arg
;
2915 xpt_compile_path(&path
, /*periph*/NULL
,
2917 CAM_TARGET_WILDCARD
,
2919 xpt_setup_ccb(&cpi
.ccb_h
, &path
, /*priority*/1);
2920 cpi
.ccb_h
.func_code
= XPT_PATH_INQ
;
2921 xpt_action((union ccb
*)&cpi
);
2922 cur_entry
->callback(cur_entry
->callback_arg
,
2925 xpt_release_path(&path
);
2931 xpt_action_sasync_cb(void *context
, int pending
)
2933 struct async_node
*cur_entry
;
2934 struct xpt_task
*task
;
2937 task
= (struct xpt_task
*)context
;
2938 cur_entry
= (struct async_node
*)task
->data1
;
2939 added
= task
->data2
;
2941 if ((added
& AC_FOUND_DEVICE
) != 0) {
2943 * Get this peripheral up to date with all
2944 * the currently existing devices.
2946 xpt_for_all_devices(xptsetasyncfunc
, cur_entry
);
2948 if ((added
& AC_PATH_REGISTERED
) != 0) {
2950 * Get this peripheral up to date with all
2951 * the currently existing busses.
2953 xpt_for_all_busses(xptsetasyncbusfunc
, cur_entry
);
2956 kfree(task
, M_CAMXPT
);
2960 xpt_action(union ccb
*start_ccb
)
2962 CAM_DEBUG(start_ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
, ("xpt_action\n"));
2964 start_ccb
->ccb_h
.status
= CAM_REQ_INPROG
;
2966 switch (start_ccb
->ccb_h
.func_code
) {
2969 struct cam_ed
*device
;
2971 char cdb_str
[(SCSI_MAX_CDBLEN
* 3) + 1];
2972 struct cam_path
*path
;
2974 path
= start_ccb
->ccb_h
.path
;
2978 * For the sake of compatibility with SCSI-1
2979 * devices that may not understand the identify
2980 * message, we include lun information in the
2981 * second byte of all commands. SCSI-1 specifies
2982 * that luns are a 3 bit value and reserves only 3
2983 * bits for lun information in the CDB. Later
2984 * revisions of the SCSI spec allow for more than 8
2985 * luns, but have deprecated lun information in the
2986 * CDB. So, if the lun won't fit, we must omit.
2988 * Also be aware that during initial probing for devices,
2989 * the inquiry information is unknown but initialized to 0.
2990 * This means that this code will be exercised while probing
2991 * devices with an ANSI revision greater than 2.
2993 device
= start_ccb
->ccb_h
.path
->device
;
2994 if (device
->protocol_version
<= SCSI_REV_2
2995 && start_ccb
->ccb_h
.target_lun
< 8
2996 && (start_ccb
->ccb_h
.flags
& CAM_CDB_POINTER
) == 0) {
2998 start_ccb
->csio
.cdb_io
.cdb_bytes
[1] |=
2999 start_ccb
->ccb_h
.target_lun
<< 5;
3001 start_ccb
->csio
.scsi_status
= SCSI_STATUS_OK
;
3002 CAM_DEBUG(path
, CAM_DEBUG_CDB
,("%s. CDB: %s\n",
3003 scsi_op_desc(start_ccb
->csio
.cdb_io
.cdb_bytes
[0],
3004 &path
->device
->inq_data
),
3005 scsi_cdb_string(start_ccb
->csio
.cdb_io
.cdb_bytes
,
3006 cdb_str
, sizeof(cdb_str
))));
3010 case XPT_CONT_TARGET_IO
:
3011 start_ccb
->csio
.sense_resid
= 0;
3012 start_ccb
->csio
.resid
= 0;
3017 struct cam_path
*path
;
3018 struct cam_sim
*sim
;
3021 path
= start_ccb
->ccb_h
.path
;
3023 sim
= path
->bus
->sim
;
3024 if (SIM_DEAD(sim
)) {
3025 /* The SIM has gone; just execute the CCB directly. */
3026 cam_ccbq_send_ccb(&path
->device
->ccbq
, start_ccb
);
3027 (*(sim
->sim_action
))(sim
, start_ccb
);
3031 cam_ccbq_insert_ccb(&path
->device
->ccbq
, start_ccb
);
3032 if (path
->device
->qfrozen_cnt
== 0)
3033 runq
= xpt_schedule_dev_sendq(path
->bus
, path
->device
);
3037 xpt_run_dev_sendq(path
->bus
);
3040 case XPT_SET_TRAN_SETTINGS
:
3042 xpt_set_transfer_settings(&start_ccb
->cts
,
3043 start_ccb
->ccb_h
.path
->device
,
3044 /*async_update*/FALSE
);
3047 case XPT_CALC_GEOMETRY
:
3049 struct cam_sim
*sim
;
3051 /* Filter out garbage */
3052 if (start_ccb
->ccg
.block_size
== 0
3053 || start_ccb
->ccg
.volume_size
== 0) {
3054 start_ccb
->ccg
.cylinders
= 0;
3055 start_ccb
->ccg
.heads
= 0;
3056 start_ccb
->ccg
.secs_per_track
= 0;
3057 start_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
3060 sim
= start_ccb
->ccb_h
.path
->bus
->sim
;
3061 (*(sim
->sim_action
))(sim
, start_ccb
);
3066 union ccb
* abort_ccb
;
3068 abort_ccb
= start_ccb
->cab
.abort_ccb
;
3069 if (XPT_FC_IS_DEV_QUEUED(abort_ccb
)) {
3071 if (abort_ccb
->ccb_h
.pinfo
.index
>= 0) {
3072 struct cam_ccbq
*ccbq
;
3074 ccbq
= &abort_ccb
->ccb_h
.path
->device
->ccbq
;
3075 cam_ccbq_remove_ccb(ccbq
, abort_ccb
);
3076 abort_ccb
->ccb_h
.status
=
3077 CAM_REQ_ABORTED
|CAM_DEV_QFRZN
;
3078 xpt_freeze_devq(abort_ccb
->ccb_h
.path
, 1);
3079 xpt_done(abort_ccb
);
3080 start_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
3083 if (abort_ccb
->ccb_h
.pinfo
.index
== CAM_UNQUEUED_INDEX
3084 && (abort_ccb
->ccb_h
.status
& CAM_SIM_QUEUED
) == 0) {
3086 * We've caught this ccb en route to
3087 * the SIM. Flag it for abort and the
3088 * SIM will do so just before starting
3089 * real work on the CCB.
3091 abort_ccb
->ccb_h
.status
=
3092 CAM_REQ_ABORTED
|CAM_DEV_QFRZN
;
3093 xpt_freeze_devq(abort_ccb
->ccb_h
.path
, 1);
3094 start_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
3098 if (XPT_FC_IS_QUEUED(abort_ccb
)
3099 && (abort_ccb
->ccb_h
.pinfo
.index
== CAM_DONEQ_INDEX
)) {
3101 * It's already completed but waiting
3102 * for our SWI to get to it.
3104 start_ccb
->ccb_h
.status
= CAM_UA_ABORT
;
3108 * If we weren't able to take care of the abort request
3109 * in the XPT, pass the request down to the SIM for processing.
3113 case XPT_ACCEPT_TARGET_IO
:
3115 case XPT_IMMED_NOTIFY
:
3116 case XPT_NOTIFY_ACK
:
3117 case XPT_GET_TRAN_SETTINGS
:
3120 struct cam_sim
*sim
;
3122 sim
= start_ccb
->ccb_h
.path
->bus
->sim
;
3123 (*(sim
->sim_action
))(sim
, start_ccb
);
3128 struct cam_sim
*sim
;
3130 sim
= start_ccb
->ccb_h
.path
->bus
->sim
;
3131 (*(sim
->sim_action
))(sim
, start_ccb
);
3134 case XPT_PATH_STATS
:
3135 start_ccb
->cpis
.last_reset
=
3136 start_ccb
->ccb_h
.path
->bus
->last_reset
;
3137 start_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
3143 dev
= start_ccb
->ccb_h
.path
->device
;
3144 if ((dev
->flags
& CAM_DEV_UNCONFIGURED
) != 0) {
3145 start_ccb
->ccb_h
.status
= CAM_DEV_NOT_THERE
;
3147 struct ccb_getdev
*cgd
;
3151 cgd
= &start_ccb
->cgd
;
3152 bus
= cgd
->ccb_h
.path
->bus
;
3153 tar
= cgd
->ccb_h
.path
->target
;
3154 cgd
->inq_data
= dev
->inq_data
;
3155 cgd
->ccb_h
.status
= CAM_REQ_CMP
;
3156 cgd
->serial_num_len
= dev
->serial_num_len
;
3157 if ((dev
->serial_num_len
> 0)
3158 && (dev
->serial_num
!= NULL
))
3159 bcopy(dev
->serial_num
, cgd
->serial_num
,
3160 dev
->serial_num_len
);
3164 case XPT_GDEV_STATS
:
3168 dev
= start_ccb
->ccb_h
.path
->device
;
3169 if ((dev
->flags
& CAM_DEV_UNCONFIGURED
) != 0) {
3170 start_ccb
->ccb_h
.status
= CAM_DEV_NOT_THERE
;
3172 struct ccb_getdevstats
*cgds
;
3176 cgds
= &start_ccb
->cgds
;
3177 bus
= cgds
->ccb_h
.path
->bus
;
3178 tar
= cgds
->ccb_h
.path
->target
;
3179 cgds
->dev_openings
= dev
->ccbq
.dev_openings
;
3180 cgds
->dev_active
= dev
->ccbq
.dev_active
;
3181 cgds
->devq_openings
= dev
->ccbq
.devq_openings
;
3182 cgds
->devq_queued
= dev
->ccbq
.queue
.entries
;
3183 cgds
->held
= dev
->ccbq
.held
;
3184 cgds
->last_reset
= tar
->last_reset
;
3185 cgds
->maxtags
= dev
->quirk
->maxtags
;
3186 cgds
->mintags
= dev
->quirk
->mintags
;
3187 if (timevalcmp(&tar
->last_reset
, &bus
->last_reset
, <))
3188 cgds
->last_reset
= bus
->last_reset
;
3189 cgds
->ccb_h
.status
= CAM_REQ_CMP
;
3195 struct cam_periph
*nperiph
;
3196 struct periph_list
*periph_head
;
3197 struct ccb_getdevlist
*cgdl
;
3199 struct cam_ed
*device
;
3206 * Don't want anyone mucking with our data.
3208 device
= start_ccb
->ccb_h
.path
->device
;
3209 periph_head
= &device
->periphs
;
3210 cgdl
= &start_ccb
->cgdl
;
3213 * Check and see if the list has changed since the user
3214 * last requested a list member. If so, tell them that the
3215 * list has changed, and therefore they need to start over
3216 * from the beginning.
3218 if ((cgdl
->index
!= 0) &&
3219 (cgdl
->generation
!= device
->generation
)) {
3220 cgdl
->status
= CAM_GDEVLIST_LIST_CHANGED
;
3225 * Traverse the list of peripherals and attempt to find
3226 * the requested peripheral.
3228 for (nperiph
= SLIST_FIRST(periph_head
), i
= 0;
3229 (nperiph
!= NULL
) && (i
<= cgdl
->index
);
3230 nperiph
= SLIST_NEXT(nperiph
, periph_links
), i
++) {
3231 if (i
== cgdl
->index
) {
3232 strncpy(cgdl
->periph_name
,
3233 nperiph
->periph_name
,
3235 cgdl
->unit_number
= nperiph
->unit_number
;
3240 cgdl
->status
= CAM_GDEVLIST_ERROR
;
3244 if (nperiph
== NULL
)
3245 cgdl
->status
= CAM_GDEVLIST_LAST_DEVICE
;
3247 cgdl
->status
= CAM_GDEVLIST_MORE_DEVS
;
3250 cgdl
->generation
= device
->generation
;
3252 cgdl
->ccb_h
.status
= CAM_REQ_CMP
;
3257 dev_pos_type position_type
;
3258 struct ccb_dev_match
*cdm
;
3261 cdm
= &start_ccb
->cdm
;
3264 * There are two ways of getting at information in the EDT.
3265 * The first way is via the primary EDT tree. It starts
3266 * with a list of busses, then a list of targets on a bus,
3267 * then devices/luns on a target, and then peripherals on a
3268 * device/lun. The "other" way is by the peripheral driver
3269 * lists. The peripheral driver lists are organized by
3270 * peripheral driver. (obviously) So it makes sense to
3271 * use the peripheral driver list if the user is looking
3272 * for something like "da1", or all "da" devices. If the
3273 * user is looking for something on a particular bus/target
3274 * or lun, it's generally better to go through the EDT tree.
3277 if (cdm
->pos
.position_type
!= CAM_DEV_POS_NONE
)
3278 position_type
= cdm
->pos
.position_type
;
3282 position_type
= CAM_DEV_POS_NONE
;
3284 for (i
= 0; i
< cdm
->num_patterns
; i
++) {
3285 if ((cdm
->patterns
[i
].type
== DEV_MATCH_BUS
)
3286 ||(cdm
->patterns
[i
].type
== DEV_MATCH_DEVICE
)){
3287 position_type
= CAM_DEV_POS_EDT
;
3292 if (cdm
->num_patterns
== 0)
3293 position_type
= CAM_DEV_POS_EDT
;
3294 else if (position_type
== CAM_DEV_POS_NONE
)
3295 position_type
= CAM_DEV_POS_PDRV
;
3298 switch(position_type
& CAM_DEV_POS_TYPEMASK
) {
3299 case CAM_DEV_POS_EDT
:
3300 ret
= xptedtmatch(cdm
);
3302 case CAM_DEV_POS_PDRV
:
3303 ret
= xptperiphlistmatch(cdm
);
3306 cdm
->status
= CAM_DEV_MATCH_ERROR
;
3310 if (cdm
->status
== CAM_DEV_MATCH_ERROR
)
3311 start_ccb
->ccb_h
.status
= CAM_REQ_CMP_ERR
;
3313 start_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
3319 struct ccb_setasync
*csa
;
3320 struct async_node
*cur_entry
;
3321 struct async_list
*async_head
;
3324 csa
= &start_ccb
->csa
;
3325 added
= csa
->event_enable
;
3326 async_head
= &csa
->ccb_h
.path
->device
->asyncs
;
3329 * If there is already an entry for us, simply
3332 cur_entry
= SLIST_FIRST(async_head
);
3333 while (cur_entry
!= NULL
) {
3334 if ((cur_entry
->callback_arg
== csa
->callback_arg
)
3335 && (cur_entry
->callback
== csa
->callback
))
3337 cur_entry
= SLIST_NEXT(cur_entry
, links
);
3340 if (cur_entry
!= NULL
) {
3342 * If the request has no flags set,
3345 added
&= ~cur_entry
->event_enable
;
3346 if (csa
->event_enable
== 0) {
3347 SLIST_REMOVE(async_head
, cur_entry
,
3349 csa
->ccb_h
.path
->device
->refcount
--;
3350 kfree(cur_entry
, M_CAMXPT
);
3352 cur_entry
->event_enable
= csa
->event_enable
;
3355 cur_entry
= kmalloc(sizeof(*cur_entry
), M_CAMXPT
,
3357 cur_entry
->event_enable
= csa
->event_enable
;
3358 cur_entry
->callback_arg
= csa
->callback_arg
;
3359 cur_entry
->callback
= csa
->callback
;
3360 SLIST_INSERT_HEAD(async_head
, cur_entry
, links
);
3361 csa
->ccb_h
.path
->device
->refcount
++;
3365 * Need to decouple this operation via a taskqueue so that
3366 * the locking doesn't become a mess.
3368 if ((added
& (AC_FOUND_DEVICE
| AC_PATH_REGISTERED
)) != 0) {
3369 struct xpt_task
*task
;
3371 task
= kmalloc(sizeof(struct xpt_task
), M_CAMXPT
,
3374 TASK_INIT(&task
->task
, 0, xpt_action_sasync_cb
, task
);
3375 task
->data1
= cur_entry
;
3376 task
->data2
= added
;
3377 taskqueue_enqueue(taskqueue_thread
[mycpuid
],
3381 start_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
3386 struct ccb_relsim
*crs
;
3389 crs
= &start_ccb
->crs
;
3390 dev
= crs
->ccb_h
.path
->device
;
3393 crs
->ccb_h
.status
= CAM_DEV_NOT_THERE
;
3397 if ((crs
->release_flags
& RELSIM_ADJUST_OPENINGS
) != 0) {
3399 if (INQ_DATA_TQ_ENABLED(&dev
->inq_data
)) {
3400 /* Don't ever go below one opening */
3401 if (crs
->openings
> 0) {
3402 xpt_dev_ccbq_resize(crs
->ccb_h
.path
,
3406 xpt_print(crs
->ccb_h
.path
,
3407 "tagged openings now %d\n",
3414 if ((crs
->release_flags
& RELSIM_RELEASE_AFTER_TIMEOUT
) != 0) {
3416 if ((dev
->flags
& CAM_DEV_REL_TIMEOUT_PENDING
) != 0) {
3419 * Just extend the old timeout and decrement
3420 * the freeze count so that a single timeout
3421 * is sufficient for releasing the queue.
3423 start_ccb
->ccb_h
.flags
&= ~CAM_DEV_QFREEZE
;
3424 callout_stop(&dev
->callout
);
3427 start_ccb
->ccb_h
.flags
|= CAM_DEV_QFREEZE
;
3430 callout_reset(&dev
->callout
,
3431 (crs
->release_timeout
* hz
) / 1000,
3432 xpt_release_devq_timeout
, dev
);
3434 dev
->flags
|= CAM_DEV_REL_TIMEOUT_PENDING
;
3438 if ((crs
->release_flags
& RELSIM_RELEASE_AFTER_CMDCMPLT
) != 0) {
3440 if ((dev
->flags
& CAM_DEV_REL_ON_COMPLETE
) != 0) {
3442 * Decrement the freeze count so that a single
3443 * completion is still sufficient to unfreeze
3446 start_ccb
->ccb_h
.flags
&= ~CAM_DEV_QFREEZE
;
3449 dev
->flags
|= CAM_DEV_REL_ON_COMPLETE
;
3450 start_ccb
->ccb_h
.flags
|= CAM_DEV_QFREEZE
;
3454 if ((crs
->release_flags
& RELSIM_RELEASE_AFTER_QEMPTY
) != 0) {
3456 if ((dev
->flags
& CAM_DEV_REL_ON_QUEUE_EMPTY
) != 0
3457 || (dev
->ccbq
.dev_active
== 0)) {
3459 start_ccb
->ccb_h
.flags
&= ~CAM_DEV_QFREEZE
;
3462 dev
->flags
|= CAM_DEV_REL_ON_QUEUE_EMPTY
;
3463 start_ccb
->ccb_h
.flags
|= CAM_DEV_QFREEZE
;
3467 if ((start_ccb
->ccb_h
.flags
& CAM_DEV_QFREEZE
) == 0) {
3469 xpt_release_devq(crs
->ccb_h
.path
, /*count*/1,
3472 start_ccb
->crs
.qfrozen_cnt
= dev
->qfrozen_cnt
;
3473 start_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
3477 xpt_scan_bus(start_ccb
->ccb_h
.path
->periph
, start_ccb
);
3480 xpt_scan_lun(start_ccb
->ccb_h
.path
->periph
,
3481 start_ccb
->ccb_h
.path
, start_ccb
->crcn
.flags
,
3486 #ifdef CAM_DEBUG_DELAY
3487 cam_debug_delay
= CAM_DEBUG_DELAY
;
3489 cam_dflags
= start_ccb
->cdbg
.flags
;
3490 if (cam_dpath
!= NULL
) {
3491 xpt_free_path(cam_dpath
);
3495 if (cam_dflags
!= CAM_DEBUG_NONE
) {
3496 if (xpt_create_path(&cam_dpath
, xpt_periph
,
3497 start_ccb
->ccb_h
.path_id
,
3498 start_ccb
->ccb_h
.target_id
,
3499 start_ccb
->ccb_h
.target_lun
) !=
3501 start_ccb
->ccb_h
.status
= CAM_RESRC_UNAVAIL
;
3502 cam_dflags
= CAM_DEBUG_NONE
;
3504 start_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
3505 xpt_print(cam_dpath
, "debugging flags now %x\n",
3510 start_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
3512 #else /* !CAMDEBUG */
3513 start_ccb
->ccb_h
.status
= CAM_FUNC_NOTAVAIL
;
3514 #endif /* CAMDEBUG */
3518 if ((start_ccb
->ccb_h
.flags
& CAM_DEV_QFREEZE
) != 0)
3519 xpt_freeze_devq(start_ccb
->ccb_h
.path
, 1);
3520 start_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
3527 start_ccb
->ccb_h
.status
= CAM_PROVIDE_FAIL
;
3533 xpt_polled_action(union ccb
*start_ccb
)
3536 struct cam_sim
*sim
;
3537 struct cam_devq
*devq
;
3540 timeout
= start_ccb
->ccb_h
.timeout
;
3541 sim
= start_ccb
->ccb_h
.path
->bus
->sim
;
3543 dev
= start_ccb
->ccb_h
.path
->device
;
3545 sim_lock_assert_owned(sim
->lock
);
3548 * Steal an opening so that no other queued requests
3549 * can get it before us while we simulate interrupts.
3551 dev
->ccbq
.devq_openings
--;
3552 dev
->ccbq
.dev_openings
--;
3554 while(((devq
&& devq
->send_openings
<= 0) || dev
->ccbq
.dev_openings
< 0)
3555 && (--timeout
> 0)) {
3557 (*(sim
->sim_poll
))(sim
);
3558 camisr_runqueue(sim
);
3561 dev
->ccbq
.devq_openings
++;
3562 dev
->ccbq
.dev_openings
++;
3565 xpt_action(start_ccb
);
3566 while(--timeout
> 0) {
3567 (*(sim
->sim_poll
))(sim
);
3568 camisr_runqueue(sim
);
3569 if ((start_ccb
->ccb_h
.status
& CAM_STATUS_MASK
)
3576 * XXX Is it worth adding a sim_timeout entry
3577 * point so we can attempt recovery? If
3578 * this is only used for dumps, I don't think
3581 start_ccb
->ccb_h
.status
= CAM_CMD_TIMEOUT
;
3584 start_ccb
->ccb_h
.status
= CAM_RESRC_UNAVAIL
;
3589 * Schedule a peripheral driver to receive a ccb when it's
3590 * target device has space for more transactions.
3593 xpt_schedule(struct cam_periph
*perph
, u_int32_t new_priority
)
3595 struct cam_ed
*device
;
3596 union ccb
*work_ccb
;
3599 sim_lock_assert_owned(perph
->sim
->lock
);
3601 CAM_DEBUG(perph
->path
, CAM_DEBUG_TRACE
, ("xpt_schedule\n"));
3602 device
= perph
->path
->device
;
3603 if (periph_is_queued(perph
)) {
3604 /* Simply reorder based on new priority */
3605 CAM_DEBUG(perph
->path
, CAM_DEBUG_SUBTRACE
,
3606 (" change priority to %d\n", new_priority
));
3607 if (new_priority
< perph
->pinfo
.priority
) {
3608 camq_change_priority(&device
->drvq
,
3613 } else if (SIM_DEAD(perph
->path
->bus
->sim
)) {
3614 /* The SIM is gone so just call periph_start directly. */
3615 work_ccb
= xpt_get_ccb(perph
->path
->device
);
3616 if (work_ccb
== NULL
)
3618 xpt_setup_ccb(&work_ccb
->ccb_h
, perph
->path
, new_priority
);
3619 perph
->pinfo
.priority
= new_priority
;
3620 perph
->periph_start(perph
, work_ccb
);
3623 /* New entry on the queue */
3624 CAM_DEBUG(perph
->path
, CAM_DEBUG_SUBTRACE
,
3625 (" added periph to queue\n"));
3626 perph
->pinfo
.priority
= new_priority
;
3627 perph
->pinfo
.generation
= ++device
->drvq
.generation
;
3628 camq_insert(&device
->drvq
, &perph
->pinfo
);
3629 runq
= xpt_schedule_dev_allocq(perph
->path
->bus
, device
);
3632 CAM_DEBUG(perph
->path
, CAM_DEBUG_SUBTRACE
,
3633 (" calling xpt_run_devq\n"));
3634 xpt_run_dev_allocq(perph
->path
->bus
);
3640 * Schedule a device to run on a given queue.
3641 * If the device was inserted as a new entry on the queue,
3642 * return 1 meaning the device queue should be run. If we
3643 * were already queued, implying someone else has already
3644 * started the queue, return 0 so the caller doesn't attempt
3648 xpt_schedule_dev(struct camq
*queue
, cam_pinfo
*pinfo
,
3649 u_int32_t new_priority
)
3652 u_int32_t old_priority
;
3654 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
, ("xpt_schedule_dev\n"));
3656 old_priority
= pinfo
->priority
;
3659 * Are we already queued?
3661 if (pinfo
->index
!= CAM_UNQUEUED_INDEX
) {
3662 /* Simply reorder based on new priority */
3663 if (new_priority
< old_priority
) {
3664 camq_change_priority(queue
, pinfo
->index
,
3666 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
,
3667 ("changed priority to %d\n",
3672 /* New entry on the queue */
3673 if (new_priority
< old_priority
)
3674 pinfo
->priority
= new_priority
;
3676 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
,
3677 ("Inserting onto queue\n"));
3678 pinfo
->generation
= ++queue
->generation
;
3679 camq_insert(queue
, pinfo
);
3686 xpt_run_dev_allocq(struct cam_eb
*bus
)
3688 struct cam_devq
*devq
;
3690 if ((devq
= bus
->sim
->devq
) == NULL
) {
3691 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
, ("xpt_run_dev_allocq: NULL devq\n"));
3694 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
, ("xpt_run_dev_allocq\n"));
3696 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
,
3697 (" qfrozen_cnt == 0x%x, entries == %d, "
3698 "openings == %d, active == %d\n",
3699 devq
->alloc_queue
.qfrozen_cnt
,
3700 devq
->alloc_queue
.entries
,
3701 devq
->alloc_openings
,
3702 devq
->alloc_active
));
3704 devq
->alloc_queue
.qfrozen_cnt
++;
3705 while ((devq
->alloc_queue
.entries
> 0)
3706 && (devq
->alloc_openings
> 0)
3707 && (devq
->alloc_queue
.qfrozen_cnt
<= 1)) {
3708 struct cam_ed_qinfo
*qinfo
;
3709 struct cam_ed
*device
;
3710 union ccb
*work_ccb
;
3711 struct cam_periph
*drv
;
3714 qinfo
= (struct cam_ed_qinfo
*)camq_remove(&devq
->alloc_queue
,
3716 device
= qinfo
->device
;
3718 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
,
3719 ("running device %p\n", device
));
3721 drvq
= &device
->drvq
;
3724 if (drvq
->entries
<= 0) {
3725 panic("xpt_run_dev_allocq: "
3726 "Device on queue without any work to do");
3729 if ((work_ccb
= xpt_get_ccb(device
)) != NULL
) {
3730 devq
->alloc_openings
--;
3731 devq
->alloc_active
++;
3732 drv
= (struct cam_periph
*)camq_remove(drvq
, CAMQ_HEAD
);
3733 xpt_setup_ccb(&work_ccb
->ccb_h
, drv
->path
,
3734 drv
->pinfo
.priority
);
3735 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
,
3736 ("calling periph start\n"));
3737 drv
->periph_start(drv
, work_ccb
);
3740 * Malloc failure in alloc_ccb
3743 * XXX add us to a list to be run from free_ccb
3744 * if we don't have any ccbs active on this
3745 * device queue otherwise we may never get run
3751 if (drvq
->entries
> 0) {
3752 /* We have more work. Attempt to reschedule */
3753 xpt_schedule_dev_allocq(bus
, device
);
3756 devq
->alloc_queue
.qfrozen_cnt
--;
3760 xpt_run_dev_sendq(struct cam_eb
*bus
)
3762 struct cam_devq
*devq
;
3764 if ((devq
= bus
->sim
->devq
) == NULL
) {
3765 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
, ("xpt_run_dev_sendq: NULL devq\n"));
3768 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
, ("xpt_run_dev_sendq\n"));
3770 devq
->send_queue
.qfrozen_cnt
++;
3771 while ((devq
->send_queue
.entries
> 0)
3772 && (devq
->send_openings
> 0)) {
3773 struct cam_ed_qinfo
*qinfo
;
3774 struct cam_ed
*device
;
3775 union ccb
*work_ccb
;
3776 struct cam_sim
*sim
;
3778 if (devq
->send_queue
.qfrozen_cnt
> 1) {
3782 qinfo
= (struct cam_ed_qinfo
*)camq_remove(&devq
->send_queue
,
3784 device
= qinfo
->device
;
3787 * If the device has been "frozen", don't attempt
3790 if (device
->qfrozen_cnt
> 0) {
3794 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
,
3795 ("running device %p\n", device
));
3797 work_ccb
= cam_ccbq_peek_ccb(&device
->ccbq
, CAMQ_HEAD
);
3798 if (work_ccb
== NULL
) {
3799 kprintf("device on run queue with no ccbs???\n");
3803 if ((work_ccb
->ccb_h
.flags
& CAM_HIGH_POWER
) != 0) {
3805 lockmgr(&xsoftc
.xpt_lock
, LK_EXCLUSIVE
);
3806 if (xsoftc
.num_highpower
<= 0) {
3808 * We got a high power command, but we
3809 * don't have any available slots. Freeze
3810 * the device queue until we have a slot
3813 device
->qfrozen_cnt
++;
3814 STAILQ_INSERT_TAIL(&xsoftc
.highpowerq
,
3818 lockmgr(&xsoftc
.xpt_lock
, LK_RELEASE
);
3822 * Consume a high power slot while
3825 xsoftc
.num_highpower
--;
3827 lockmgr(&xsoftc
.xpt_lock
, LK_RELEASE
);
3829 devq
->active_dev
= device
;
3830 cam_ccbq_remove_ccb(&device
->ccbq
, work_ccb
);
3832 cam_ccbq_send_ccb(&device
->ccbq
, work_ccb
);
3834 devq
->send_openings
--;
3835 devq
->send_active
++;
3837 if (device
->ccbq
.queue
.entries
> 0)
3838 xpt_schedule_dev_sendq(bus
, device
);
3840 if (work_ccb
&& (work_ccb
->ccb_h
.flags
& CAM_DEV_QFREEZE
) != 0){
3842 * The client wants to freeze the queue
3843 * after this CCB is sent.
3845 device
->qfrozen_cnt
++;
3848 /* In Target mode, the peripheral driver knows best... */
3849 if (work_ccb
->ccb_h
.func_code
== XPT_SCSI_IO
) {
3850 if ((device
->inq_flags
& SID_CmdQue
) != 0
3851 && work_ccb
->csio
.tag_action
!= CAM_TAG_ACTION_NONE
)
3852 work_ccb
->ccb_h
.flags
|= CAM_TAG_ACTION_VALID
;
3855 * Clear this in case of a retried CCB that
3856 * failed due to a rejected tag.
3858 work_ccb
->ccb_h
.flags
&= ~CAM_TAG_ACTION_VALID
;
3862 * Device queues can be shared among multiple sim instances
3863 * that reside on different busses. Use the SIM in the queue
3864 * CCB's path, rather than the one in the bus that was passed
3865 * into this function.
3867 sim
= work_ccb
->ccb_h
.path
->bus
->sim
;
3868 (*(sim
->sim_action
))(sim
, work_ccb
);
3870 devq
->active_dev
= NULL
;
3872 devq
->send_queue
.qfrozen_cnt
--;
3876 * This function merges stuff from the slave ccb into the master ccb, while
3877 * keeping important fields in the master ccb constant.
3880 xpt_merge_ccb(union ccb
*master_ccb
, union ccb
*slave_ccb
)
3883 * Pull fields that are valid for peripheral drivers to set
3884 * into the master CCB along with the CCB "payload".
3886 master_ccb
->ccb_h
.retry_count
= slave_ccb
->ccb_h
.retry_count
;
3887 master_ccb
->ccb_h
.func_code
= slave_ccb
->ccb_h
.func_code
;
3888 master_ccb
->ccb_h
.timeout
= slave_ccb
->ccb_h
.timeout
;
3889 master_ccb
->ccb_h
.flags
= slave_ccb
->ccb_h
.flags
;
3890 bcopy(&(&slave_ccb
->ccb_h
)[1], &(&master_ccb
->ccb_h
)[1],
3891 sizeof(union ccb
) - sizeof(struct ccb_hdr
));
3895 xpt_setup_ccb(struct ccb_hdr
*ccb_h
, struct cam_path
*path
, u_int32_t priority
)
3897 CAM_DEBUG(path
, CAM_DEBUG_TRACE
, ("xpt_setup_ccb\n"));
3898 callout_init(&ccb_h
->timeout_ch
);
3899 ccb_h
->pinfo
.priority
= priority
;
3901 ccb_h
->path_id
= path
->bus
->path_id
;
3903 ccb_h
->target_id
= path
->target
->target_id
;
3905 ccb_h
->target_id
= CAM_TARGET_WILDCARD
;
3907 ccb_h
->target_lun
= path
->device
->lun_id
;
3908 ccb_h
->pinfo
.generation
= ++path
->device
->ccbq
.queue
.generation
;
3910 ccb_h
->target_lun
= CAM_TARGET_WILDCARD
;
3912 ccb_h
->pinfo
.index
= CAM_UNQUEUED_INDEX
;
3916 /* Path manipulation functions */
3918 xpt_create_path(struct cam_path
**new_path_ptr
, struct cam_periph
*perph
,
3919 path_id_t path_id
, target_id_t target_id
, lun_id_t lun_id
)
3921 struct cam_path
*path
;
3924 path
= kmalloc(sizeof(*path
), M_CAMXPT
, M_INTWAIT
);
3925 status
= xpt_compile_path(path
, perph
, path_id
, target_id
, lun_id
);
3926 if (status
!= CAM_REQ_CMP
) {
3927 kfree(path
, M_CAMXPT
);
3930 *new_path_ptr
= path
;
3935 xpt_create_path_unlocked(struct cam_path
**new_path_ptr
,
3936 struct cam_periph
*periph
, path_id_t path_id
,
3937 target_id_t target_id
, lun_id_t lun_id
)
3939 struct cam_path
*path
;
3940 struct cam_eb
*bus
= NULL
;
3942 int need_unlock
= 0;
3944 path
= (struct cam_path
*)kmalloc(sizeof(*path
), M_CAMXPT
, M_WAITOK
);
3946 if (path_id
!= CAM_BUS_WILDCARD
) {
3947 bus
= xpt_find_bus(path_id
);
3950 CAM_SIM_LOCK(bus
->sim
);
3953 status
= xpt_compile_path(path
, periph
, path_id
, target_id
, lun_id
);
3955 CAM_SIM_UNLOCK(bus
->sim
);
3956 if (status
!= CAM_REQ_CMP
) {
3957 kfree(path
, M_CAMXPT
);
3960 *new_path_ptr
= path
;
3965 xpt_compile_path(struct cam_path
*new_path
, struct cam_periph
*perph
,
3966 path_id_t path_id
, target_id_t target_id
, lun_id_t lun_id
)
3969 struct cam_et
*target
;
3970 struct cam_ed
*device
;
3973 status
= CAM_REQ_CMP
; /* Completed without error */
3974 target
= NULL
; /* Wildcarded */
3975 device
= NULL
; /* Wildcarded */
3978 * We will potentially modify the EDT, so block interrupts
3979 * that may attempt to create cam paths.
3981 bus
= xpt_find_bus(path_id
);
3983 status
= CAM_PATH_INVALID
;
3985 target
= xpt_find_target(bus
, target_id
);
3986 if (target
== NULL
) {
3988 struct cam_et
*new_target
;
3990 new_target
= xpt_alloc_target(bus
, target_id
);
3991 if (new_target
== NULL
) {
3992 status
= CAM_RESRC_UNAVAIL
;
3994 target
= new_target
;
3997 if (target
!= NULL
) {
3998 device
= xpt_find_device(target
, lun_id
);
3999 if (device
== NULL
) {
4001 struct cam_ed
*new_device
;
4003 new_device
= xpt_alloc_device(bus
,
4006 if (new_device
== NULL
) {
4007 status
= CAM_RESRC_UNAVAIL
;
4009 device
= new_device
;
4016 * Only touch the user's data if we are successful.
4018 if (status
== CAM_REQ_CMP
) {
4019 new_path
->periph
= perph
;
4020 new_path
->bus
= bus
;
4021 new_path
->target
= target
;
4022 new_path
->device
= device
;
4023 CAM_DEBUG(new_path
, CAM_DEBUG_TRACE
, ("xpt_compile_path\n"));
4026 xpt_release_device(bus
, target
, device
);
4028 xpt_release_target(bus
, target
);
4030 xpt_release_bus(bus
);
4036 xpt_release_path(struct cam_path
*path
)
4038 CAM_DEBUG(path
, CAM_DEBUG_TRACE
, ("xpt_release_path\n"));
4039 if (path
->device
!= NULL
) {
4040 xpt_release_device(path
->bus
, path
->target
, path
->device
);
4041 path
->device
= NULL
;
4043 if (path
->target
!= NULL
) {
4044 xpt_release_target(path
->bus
, path
->target
);
4045 path
->target
= NULL
;
4047 if (path
->bus
!= NULL
) {
4048 xpt_release_bus(path
->bus
);
4054 xpt_free_path(struct cam_path
*path
)
4056 CAM_DEBUG(path
, CAM_DEBUG_TRACE
, ("xpt_free_path\n"));
4057 xpt_release_path(path
);
4058 kfree(path
, M_CAMXPT
);
4063 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4064 * in path1, 2 for match with wildcards in path2.
4067 xpt_path_comp(struct cam_path
*path1
, struct cam_path
*path2
)
4071 if (path1
->bus
!= path2
->bus
) {
4072 if (path1
->bus
->path_id
== CAM_BUS_WILDCARD
)
4074 else if (path2
->bus
->path_id
== CAM_BUS_WILDCARD
)
4079 if (path1
->target
!= path2
->target
) {
4080 if (path1
->target
->target_id
== CAM_TARGET_WILDCARD
) {
4083 } else if (path2
->target
->target_id
== CAM_TARGET_WILDCARD
)
4088 if (path1
->device
!= path2
->device
) {
4089 if (path1
->device
->lun_id
== CAM_LUN_WILDCARD
) {
4092 } else if (path2
->device
->lun_id
== CAM_LUN_WILDCARD
)
4101 xpt_print_path(struct cam_path
*path
)
4105 kprintf("(nopath): ");
4107 if (path
->periph
!= NULL
)
4108 kprintf("(%s%d:", path
->periph
->periph_name
,
4109 path
->periph
->unit_number
);
4111 kprintf("(noperiph:");
4113 if (path
->bus
!= NULL
)
4114 kprintf("%s%d:%d:", path
->bus
->sim
->sim_name
,
4115 path
->bus
->sim
->unit_number
,
4116 path
->bus
->sim
->bus_id
);
4120 if (path
->target
!= NULL
)
4121 kprintf("%d:", path
->target
->target_id
);
4125 if (path
->device
!= NULL
)
4126 kprintf("%d): ", path
->device
->lun_id
);
4133 xpt_print(struct cam_path
*path
, const char *fmt
, ...)
4136 xpt_print_path(path
);
4137 __va_start(ap
, fmt
);
4143 xpt_path_string(struct cam_path
*path
, char *str
, size_t str_len
)
4147 sim_lock_assert_owned(path
->bus
->sim
->lock
);
4149 sbuf_new(&sb
, str
, str_len
, 0);
4152 sbuf_printf(&sb
, "(nopath): ");
4154 if (path
->periph
!= NULL
)
4155 sbuf_printf(&sb
, "(%s%d:", path
->periph
->periph_name
,
4156 path
->periph
->unit_number
);
4158 sbuf_printf(&sb
, "(noperiph:");
4160 if (path
->bus
!= NULL
)
4161 sbuf_printf(&sb
, "%s%d:%d:", path
->bus
->sim
->sim_name
,
4162 path
->bus
->sim
->unit_number
,
4163 path
->bus
->sim
->bus_id
);
4165 sbuf_printf(&sb
, "nobus:");
4167 if (path
->target
!= NULL
)
4168 sbuf_printf(&sb
, "%d:", path
->target
->target_id
);
4170 sbuf_printf(&sb
, "X:");
4172 if (path
->device
!= NULL
)
4173 sbuf_printf(&sb
, "%d): ", path
->device
->lun_id
);
4175 sbuf_printf(&sb
, "X): ");
4179 return(sbuf_len(&sb
));
4183 xpt_path_path_id(struct cam_path
*path
)
4185 sim_lock_assert_owned(path
->bus
->sim
->lock
);
4187 return(path
->bus
->path_id
);
4191 xpt_path_target_id(struct cam_path
*path
)
4193 sim_lock_assert_owned(path
->bus
->sim
->lock
);
4195 if (path
->target
!= NULL
)
4196 return (path
->target
->target_id
);
4198 return (CAM_TARGET_WILDCARD
);
4202 xpt_path_lun_id(struct cam_path
*path
)
4204 sim_lock_assert_owned(path
->bus
->sim
->lock
);
4206 if (path
->device
!= NULL
)
4207 return (path
->device
->lun_id
);
4209 return (CAM_LUN_WILDCARD
);
4213 xpt_path_sim(struct cam_path
*path
)
4215 return (path
->bus
->sim
);
4219 xpt_path_periph(struct cam_path
*path
)
4221 sim_lock_assert_owned(path
->bus
->sim
->lock
);
4223 return (path
->periph
);
4227 * Release a CAM control block for the caller. Remit the cost of the structure
4228 * to the device referenced by the path. If the this device had no 'credits'
4229 * and peripheral drivers have registered async callbacks for this notification
4233 xpt_release_ccb(union ccb
*free_ccb
)
4235 struct cam_path
*path
;
4236 struct cam_ed
*device
;
4238 struct cam_sim
*sim
;
4240 CAM_DEBUG_PRINT(CAM_DEBUG_XPT
, ("xpt_release_ccb\n"));
4241 path
= free_ccb
->ccb_h
.path
;
4242 device
= path
->device
;
4246 sim_lock_assert_owned(sim
->lock
);
4248 cam_ccbq_release_opening(&device
->ccbq
);
4249 if (sim
->ccb_count
> sim
->max_ccbs
) {
4250 xpt_free_ccb(free_ccb
);
4253 SLIST_INSERT_HEAD(&sim
->ccb_freeq
, &free_ccb
->ccb_h
,
4256 if (sim
->devq
== NULL
) {
4259 sim
->devq
->alloc_openings
++;
4260 sim
->devq
->alloc_active
--;
4261 /* XXX Turn this into an inline function - xpt_run_device?? */
4262 if ((device_is_alloc_queued(device
) == 0)
4263 && (device
->drvq
.entries
> 0)) {
4264 xpt_schedule_dev_allocq(bus
, device
);
4266 if (dev_allocq_is_runnable(sim
->devq
))
4267 xpt_run_dev_allocq(bus
);
4270 /* Functions accessed by SIM drivers */
4273 * A sim structure, listing the SIM entry points and instance
4274 * identification info is passed to xpt_bus_register to hook the SIM
4275 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4276 * for this new bus and places it in the array of busses and assigns
4277 * it a path_id. The path_id may be influenced by "hard wiring"
4278 * information specified by the user. Once interrupt services are
4279 * availible, the bus will be probed.
4282 xpt_bus_register(struct cam_sim
*sim
, u_int32_t bus
)
4284 struct cam_eb
*new_bus
;
4285 struct cam_eb
*old_bus
;
4286 struct ccb_pathinq cpi
;
4288 sim_lock_assert_owned(sim
->lock
);
4291 new_bus
= kmalloc(sizeof(*new_bus
), M_CAMXPT
, M_INTWAIT
);
4293 if (strcmp(sim
->sim_name
, "xpt") != 0) {
4295 xptpathid(sim
->sim_name
, sim
->unit_number
, sim
->bus_id
);
4298 TAILQ_INIT(&new_bus
->et_entries
);
4299 new_bus
->path_id
= sim
->path_id
;
4302 timevalclear(&new_bus
->last_reset
);
4304 new_bus
->refcount
= 1; /* Held until a bus_deregister event */
4305 new_bus
->generation
= 0;
4306 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
4307 old_bus
= TAILQ_FIRST(&xsoftc
.xpt_busses
);
4308 while (old_bus
!= NULL
4309 && old_bus
->path_id
< new_bus
->path_id
)
4310 old_bus
= TAILQ_NEXT(old_bus
, links
);
4311 if (old_bus
!= NULL
)
4312 TAILQ_INSERT_BEFORE(old_bus
, new_bus
, links
);
4314 TAILQ_INSERT_TAIL(&xsoftc
.xpt_busses
, new_bus
, links
);
4315 xsoftc
.bus_generation
++;
4316 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
4318 /* Notify interested parties */
4319 if (sim
->path_id
!= CAM_XPT_PATH_ID
) {
4320 struct cam_path path
;
4322 xpt_compile_path(&path
, /*periph*/NULL
, sim
->path_id
,
4323 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
);
4324 xpt_setup_ccb(&cpi
.ccb_h
, &path
, /*priority*/1);
4325 cpi
.ccb_h
.func_code
= XPT_PATH_INQ
;
4326 xpt_action((union ccb
*)&cpi
);
4327 xpt_async(AC_PATH_REGISTERED
, &path
, &cpi
);
4328 xpt_release_path(&path
);
4330 return (CAM_SUCCESS
);
4334 * Deregister a bus. We must clean out all transactions pending on the bus.
4335 * This routine is typically called prior to cam_sim_free() (e.g. see
4336 * dev/usbmisc/umass/umass.c)
4339 xpt_bus_deregister(path_id_t pathid
)
4341 struct cam_path bus_path
;
4342 struct cam_ed
*device
;
4343 struct cam_ed_qinfo
*qinfo
;
4344 struct cam_devq
*devq
;
4345 struct cam_periph
*periph
;
4346 struct cam_sim
*ccbsim
;
4347 union ccb
*work_ccb
;
4350 status
= xpt_compile_path(&bus_path
, NULL
, pathid
,
4351 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
);
4352 if (status
!= CAM_REQ_CMP
)
4356 * This should clear out all pending requests and timeouts, but
4357 * the ccb's may be queued to a software interrupt.
4359 * XXX AC_LOST_DEVICE does not precisely abort the pending requests,
4360 * and it really ought to.
4362 xpt_async(AC_LOST_DEVICE
, &bus_path
, NULL
);
4363 xpt_async(AC_PATH_DEREGISTERED
, &bus_path
, NULL
);
4365 /* The SIM may be gone, so use a dummy SIM for any stray operations. */
4366 devq
= bus_path
.bus
->sim
->devq
;
4367 ccbsim
= bus_path
.bus
->sim
;
4368 bus_path
.bus
->sim
= &cam_dead_sim
;
4370 /* Execute any pending operations now. */
4371 while ((qinfo
= (struct cam_ed_qinfo
*)camq_remove(&devq
->send_queue
,
4372 CAMQ_HEAD
)) != NULL
||
4373 (qinfo
= (struct cam_ed_qinfo
*)camq_remove(&devq
->alloc_queue
,
4374 CAMQ_HEAD
)) != NULL
) {
4376 device
= qinfo
->device
;
4377 work_ccb
= cam_ccbq_peek_ccb(&device
->ccbq
, CAMQ_HEAD
);
4378 if (work_ccb
!= NULL
) {
4379 devq
->active_dev
= device
;
4380 cam_ccbq_remove_ccb(&device
->ccbq
, work_ccb
);
4381 cam_ccbq_send_ccb(&device
->ccbq
, work_ccb
);
4382 (*(ccbsim
->sim_action
))(ccbsim
, work_ccb
);
4385 periph
= (struct cam_periph
*)camq_remove(&device
->drvq
,
4388 xpt_schedule(periph
, periph
->pinfo
.priority
);
4389 } while (work_ccb
!= NULL
|| periph
!= NULL
);
4392 /* Make sure all completed CCBs are processed. */
4393 while (!TAILQ_EMPTY(&ccbsim
->sim_doneq
)) {
4394 camisr_runqueue(ccbsim
);
4396 /* Repeat the async's for the benefit of any new devices. */
4397 xpt_async(AC_LOST_DEVICE
, &bus_path
, NULL
);
4398 xpt_async(AC_PATH_DEREGISTERED
, &bus_path
, NULL
);
4401 /* Release the reference count held while registered. */
4402 xpt_release_bus(bus_path
.bus
);
4403 xpt_release_path(&bus_path
);
4405 return (CAM_REQ_CMP
);
4409 xptnextfreepathid(void)
4416 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
4417 bus
= TAILQ_FIRST(&xsoftc
.xpt_busses
);
4419 /* Find an unoccupied pathid */
4420 while (bus
!= NULL
&& bus
->path_id
<= pathid
) {
4421 if (bus
->path_id
== pathid
)
4423 bus
= TAILQ_NEXT(bus
, links
);
4425 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
4428 * Ensure that this pathid is not reserved for
4429 * a bus that may be registered in the future.
4431 if (resource_string_value("scbus", pathid
, "at", &strval
) == 0) {
4433 /* Start the search over */
4434 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
4441 xptpathid(const char *sim_name
, int sim_unit
, int sim_bus
)
4447 pathid
= CAM_XPT_PATH_ID
;
4448 ksnprintf(buf
, sizeof(buf
), "%s%d", sim_name
, sim_unit
);
4450 while ((i
= resource_query_string(i
, "at", buf
)) != -1) {
4451 if (strcmp(resource_query_name(i
), "scbus")) {
4452 /* Avoid a bit of foot shooting. */
4455 dunit
= resource_query_unit(i
);
4456 if (dunit
< 0) /* unwired?! */
4458 if (resource_int_value("scbus", dunit
, "bus", &val
) == 0) {
4459 if (sim_bus
== val
) {
4463 } else if (sim_bus
== 0) {
4464 /* Unspecified matches bus 0 */
4468 kprintf("Ambiguous scbus configuration for %s%d "
4469 "bus %d, cannot wire down. The kernel "
4470 "config entry for scbus%d should "
4471 "specify a controller bus.\n"
4472 "Scbus will be assigned dynamically.\n",
4473 sim_name
, sim_unit
, sim_bus
, dunit
);
4478 if (pathid
== CAM_XPT_PATH_ID
)
4479 pathid
= xptnextfreepathid();
4484 xpt_async(u_int32_t async_code
, struct cam_path
*path
, void *async_arg
)
4487 struct cam_et
*target
, *next_target
;
4488 struct cam_ed
*device
, *next_device
;
4490 sim_lock_assert_owned(path
->bus
->sim
->lock
);
4492 CAM_DEBUG(path
, CAM_DEBUG_TRACE
, ("xpt_async\n"));
4495 * Most async events come from a CAM interrupt context. In
4496 * a few cases, the error recovery code at the peripheral layer,
4497 * which may run from our SWI or a process context, may signal
4498 * deferred events with a call to xpt_async.
4503 if (async_code
== AC_BUS_RESET
) {
4504 /* Update our notion of when the last reset occurred */
4505 microuptime(&bus
->last_reset
);
4508 for (target
= TAILQ_FIRST(&bus
->et_entries
);
4510 target
= next_target
) {
4512 next_target
= TAILQ_NEXT(target
, links
);
4514 if (path
->target
!= target
4515 && path
->target
->target_id
!= CAM_TARGET_WILDCARD
4516 && target
->target_id
!= CAM_TARGET_WILDCARD
)
4519 if (async_code
== AC_SENT_BDR
) {
4520 /* Update our notion of when the last reset occurred */
4521 microuptime(&path
->target
->last_reset
);
4524 for (device
= TAILQ_FIRST(&target
->ed_entries
);
4526 device
= next_device
) {
4528 next_device
= TAILQ_NEXT(device
, links
);
4530 if (path
->device
!= device
4531 && path
->device
->lun_id
!= CAM_LUN_WILDCARD
4532 && device
->lun_id
!= CAM_LUN_WILDCARD
)
4535 xpt_dev_async(async_code
, bus
, target
,
4538 xpt_async_bcast(&device
->asyncs
, async_code
,
4544 * If this wasn't a fully wildcarded async, tell all
4545 * clients that want all async events.
4547 if (bus
!= xpt_periph
->path
->bus
)
4548 xpt_async_bcast(&xpt_periph
->path
->device
->asyncs
, async_code
,
4553 xpt_async_bcast(struct async_list
*async_head
,
4554 u_int32_t async_code
,
4555 struct cam_path
*path
, void *async_arg
)
4557 struct async_node
*cur_entry
;
4559 cur_entry
= SLIST_FIRST(async_head
);
4560 while (cur_entry
!= NULL
) {
4561 struct async_node
*next_entry
;
4563 * Grab the next list entry before we call the current
4564 * entry's callback. This is because the callback function
4565 * can delete its async callback entry.
4567 next_entry
= SLIST_NEXT(cur_entry
, links
);
4568 if ((cur_entry
->event_enable
& async_code
) != 0)
4569 cur_entry
->callback(cur_entry
->callback_arg
,
4572 cur_entry
= next_entry
;
4577 * Handle any per-device event notifications that require action by the XPT.
4580 xpt_dev_async(u_int32_t async_code
, struct cam_eb
*bus
, struct cam_et
*target
,
4581 struct cam_ed
*device
, void *async_arg
)
4584 struct cam_path newpath
;
4587 * We only need to handle events for real devices.
4589 if (target
->target_id
== CAM_TARGET_WILDCARD
4590 || device
->lun_id
== CAM_LUN_WILDCARD
)
4594 * We need our own path with wildcards expanded to
4595 * handle certain types of events.
4597 if ((async_code
== AC_SENT_BDR
)
4598 || (async_code
== AC_BUS_RESET
)
4599 || (async_code
== AC_INQ_CHANGED
))
4600 status
= xpt_compile_path(&newpath
, NULL
,
4605 status
= CAM_REQ_CMP_ERR
;
4607 if (status
== CAM_REQ_CMP
) {
4610 * Allow transfer negotiation to occur in a
4611 * tag free environment.
4613 if (async_code
== AC_SENT_BDR
4614 || async_code
== AC_BUS_RESET
)
4615 xpt_toggle_tags(&newpath
);
4617 if (async_code
== AC_INQ_CHANGED
) {
4619 * We've sent a start unit command, or
4620 * something similar to a device that
4621 * may have caused its inquiry data to
4622 * change. So we re-scan the device to
4623 * refresh the inquiry data for it.
4625 xpt_scan_lun(newpath
.periph
, &newpath
,
4626 CAM_EXPECT_INQ_CHANGE
, NULL
);
4628 xpt_release_path(&newpath
);
4629 } else if (async_code
== AC_LOST_DEVICE
) {
4631 * When we lose a device the device may be about to detach
4632 * the sim, we have to clear out all pending timeouts and
4633 * requests before that happens. XXX it would be nice if
4634 * we could abort the requests pertaining to the device.
4636 xpt_release_devq_timeout(device
);
4637 if ((device
->flags
& CAM_DEV_UNCONFIGURED
) == 0) {
4638 device
->flags
|= CAM_DEV_UNCONFIGURED
;
4639 xpt_release_device(bus
, target
, device
);
4641 } else if (async_code
== AC_TRANSFER_NEG
) {
4642 struct ccb_trans_settings
*settings
;
4644 settings
= (struct ccb_trans_settings
*)async_arg
;
4645 xpt_set_transfer_settings(settings
, device
,
4646 /*async_update*/TRUE
);
4651 xpt_freeze_devq(struct cam_path
*path
, u_int count
)
4653 struct ccb_hdr
*ccbh
;
4655 sim_lock_assert_owned(path
->bus
->sim
->lock
);
4657 path
->device
->qfrozen_cnt
+= count
;
4660 * Mark the last CCB in the queue as needing
4661 * to be requeued if the driver hasn't
4662 * changed it's state yet. This fixes a race
4663 * where a ccb is just about to be queued to
4664 * a controller driver when it's interrupt routine
4665 * freezes the queue. To completly close the
4666 * hole, controller drives must check to see
4667 * if a ccb's status is still CAM_REQ_INPROG
4668 * just before they queue
4669 * the CCB. See ahc_action/ahc_freeze_devq for
4672 ccbh
= TAILQ_LAST(&path
->device
->ccbq
.active_ccbs
, ccb_hdr_tailq
);
4673 if (ccbh
&& ccbh
->status
== CAM_REQ_INPROG
)
4674 ccbh
->status
= CAM_REQUEUE_REQ
;
4675 return (path
->device
->qfrozen_cnt
);
4679 xpt_freeze_simq(struct cam_sim
*sim
, u_int count
)
4681 sim_lock_assert_owned(sim
->lock
);
4683 if (sim
->devq
== NULL
)
4685 sim
->devq
->send_queue
.qfrozen_cnt
+= count
;
4686 if (sim
->devq
->active_dev
!= NULL
) {
4687 struct ccb_hdr
*ccbh
;
4689 ccbh
= TAILQ_LAST(&sim
->devq
->active_dev
->ccbq
.active_ccbs
,
4691 if (ccbh
&& ccbh
->status
== CAM_REQ_INPROG
)
4692 ccbh
->status
= CAM_REQUEUE_REQ
;
4694 return (sim
->devq
->send_queue
.qfrozen_cnt
);
4698 * WARNING: most devices, especially USB/UMASS, may detach their sim early.
4699 * We ref-count the sim (and the bus only NULLs it out when the bus has been
4700 * freed, which is not the case here), but the device queue is also freed XXX
4701 * and we have to check that here.
4703 * XXX fixme: could we simply not null-out the device queue via
4707 xpt_release_devq_timeout(void *arg
)
4709 struct cam_ed
*device
;
4711 device
= (struct cam_ed
*)arg
;
4713 xpt_release_devq_device(device
, /*count*/1, /*run_queue*/TRUE
);
4717 xpt_release_devq(struct cam_path
*path
, u_int count
, int run_queue
)
4719 sim_lock_assert_owned(path
->bus
->sim
->lock
);
4721 xpt_release_devq_device(path
->device
, count
, run_queue
);
4725 xpt_release_devq_device(struct cam_ed
*dev
, u_int count
, int run_queue
)
4731 if (dev
->qfrozen_cnt
> 0) {
4733 count
= (count
> dev
->qfrozen_cnt
) ? dev
->qfrozen_cnt
: count
;
4734 dev
->qfrozen_cnt
-= count
;
4735 if (dev
->qfrozen_cnt
== 0) {
4738 * No longer need to wait for a successful
4739 * command completion.
4741 dev
->flags
&= ~CAM_DEV_REL_ON_COMPLETE
;
4744 * Remove any timeouts that might be scheduled
4745 * to release this queue.
4747 if ((dev
->flags
& CAM_DEV_REL_TIMEOUT_PENDING
) != 0) {
4748 callout_stop(&dev
->callout
);
4749 dev
->flags
&= ~CAM_DEV_REL_TIMEOUT_PENDING
;
4753 * Now that we are unfrozen schedule the
4754 * device so any pending transactions are
4757 if ((dev
->ccbq
.queue
.entries
> 0)
4758 && (xpt_schedule_dev_sendq(dev
->target
->bus
, dev
))
4759 && (run_queue
!= 0)) {
4765 xpt_run_dev_sendq(dev
->target
->bus
);
4769 xpt_release_simq(struct cam_sim
*sim
, int run_queue
)
4773 sim_lock_assert_owned(sim
->lock
);
4775 if (sim
->devq
== NULL
)
4778 sendq
= &(sim
->devq
->send_queue
);
4779 if (sendq
->qfrozen_cnt
> 0) {
4780 sendq
->qfrozen_cnt
--;
4781 if (sendq
->qfrozen_cnt
== 0) {
4785 * If there is a timeout scheduled to release this
4786 * sim queue, remove it. The queue frozen count is
4789 if ((sim
->flags
& CAM_SIM_REL_TIMEOUT_PENDING
) != 0){
4790 callout_stop(&sim
->callout
);
4791 sim
->flags
&= ~CAM_SIM_REL_TIMEOUT_PENDING
;
4793 bus
= xpt_find_bus(sim
->path_id
);
4797 * Now that we are unfrozen run the send queue.
4799 xpt_run_dev_sendq(bus
);
4801 xpt_release_bus(bus
);
4807 xpt_done(union ccb
*done_ccb
)
4809 struct cam_sim
*sim
;
4811 CAM_DEBUG(done_ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
, ("xpt_done\n"));
4812 if ((done_ccb
->ccb_h
.func_code
& XPT_FC_QUEUED
) != 0) {
4814 * Queue up the request for handling by our SWI handler
4815 * any of the "non-immediate" type of ccbs.
4817 sim
= done_ccb
->ccb_h
.path
->bus
->sim
;
4818 switch (done_ccb
->ccb_h
.path
->periph
->type
) {
4819 case CAM_PERIPH_BIO
:
4820 spin_lock_wr(&sim
->sim_spin
);
4821 TAILQ_INSERT_TAIL(&sim
->sim_doneq
, &done_ccb
->ccb_h
,
4823 done_ccb
->ccb_h
.pinfo
.index
= CAM_DONEQ_INDEX
;
4824 spin_unlock_wr(&sim
->sim_spin
);
4825 if ((sim
->flags
& CAM_SIM_ON_DONEQ
) == 0) {
4826 spin_lock_wr(&cam_simq_spin
);
4827 if ((sim
->flags
& CAM_SIM_ON_DONEQ
) == 0) {
4828 TAILQ_INSERT_TAIL(&cam_simq
, sim
,
4830 sim
->flags
|= CAM_SIM_ON_DONEQ
;
4832 spin_unlock_wr(&cam_simq_spin
);
4834 if ((done_ccb
->ccb_h
.path
->periph
->flags
&
4835 CAM_PERIPH_POLLED
) == 0)
4839 panic("unknown periph type %d",
4840 done_ccb
->ccb_h
.path
->periph
->type
);
4850 new_ccb
= kmalloc(sizeof(*new_ccb
), M_CAMXPT
, M_INTWAIT
| M_ZERO
);
4855 xpt_free_ccb(union ccb
*free_ccb
)
4857 kfree(free_ccb
, M_CAMXPT
);
4862 /* Private XPT functions */
4865 * Get a CAM control block for the caller. Charge the structure to the device
4866 * referenced by the path. If the this device has no 'credits' then the
4867 * device already has the maximum number of outstanding operations under way
4868 * and we return NULL. If we don't have sufficient resources to allocate more
4869 * ccbs, we also return NULL.
4872 xpt_get_ccb(struct cam_ed
*device
)
4875 struct cam_sim
*sim
;
4878 if ((new_ccb
= (union ccb
*)SLIST_FIRST(&sim
->ccb_freeq
)) == NULL
) {
4879 new_ccb
= xpt_alloc_ccb();
4880 if ((sim
->flags
& CAM_SIM_MPSAFE
) == 0)
4881 callout_init(&new_ccb
->ccb_h
.timeout_ch
);
4882 SLIST_INSERT_HEAD(&sim
->ccb_freeq
, &new_ccb
->ccb_h
,
4886 cam_ccbq_take_opening(&device
->ccbq
);
4887 SLIST_REMOVE_HEAD(&sim
->ccb_freeq
, xpt_links
.sle
);
4892 xpt_release_bus(struct cam_eb
*bus
)
4895 if ((--bus
->refcount
== 0)
4896 && (TAILQ_FIRST(&bus
->et_entries
) == NULL
)) {
4897 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
4898 TAILQ_REMOVE(&xsoftc
.xpt_busses
, bus
, links
);
4899 xsoftc
.bus_generation
++;
4900 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
4901 kfree(bus
, M_CAMXPT
);
4905 static struct cam_et
*
4906 xpt_alloc_target(struct cam_eb
*bus
, target_id_t target_id
)
4908 struct cam_et
*target
;
4909 struct cam_et
*cur_target
;
4911 target
= kmalloc(sizeof(*target
), M_CAMXPT
, M_INTWAIT
);
4913 TAILQ_INIT(&target
->ed_entries
);
4915 target
->target_id
= target_id
;
4916 target
->refcount
= 1;
4917 target
->generation
= 0;
4918 timevalclear(&target
->last_reset
);
4920 * Hold a reference to our parent bus so it
4921 * will not go away before we do.
4925 /* Insertion sort into our bus's target list */
4926 cur_target
= TAILQ_FIRST(&bus
->et_entries
);
4927 while (cur_target
!= NULL
&& cur_target
->target_id
< target_id
)
4928 cur_target
= TAILQ_NEXT(cur_target
, links
);
4930 if (cur_target
!= NULL
) {
4931 TAILQ_INSERT_BEFORE(cur_target
, target
, links
);
4933 TAILQ_INSERT_TAIL(&bus
->et_entries
, target
, links
);
4940 xpt_release_target(struct cam_eb
*bus
, struct cam_et
*target
)
4942 if (target
->refcount
== 1) {
4943 KKASSERT(TAILQ_FIRST(&target
->ed_entries
) == NULL
);
4944 TAILQ_REMOVE(&bus
->et_entries
, target
, links
);
4946 xpt_release_bus(bus
);
4947 KKASSERT(target
->refcount
== 1);
4948 kfree(target
, M_CAMXPT
);
4954 static struct cam_ed
*
4955 xpt_alloc_device(struct cam_eb
*bus
, struct cam_et
*target
, lun_id_t lun_id
)
4957 struct cam_path path
;
4958 struct cam_ed
*device
;
4959 struct cam_devq
*devq
;
4962 if (SIM_DEAD(bus
->sim
))
4965 /* Make space for us in the device queue on our bus */
4966 if (bus
->sim
->devq
== NULL
)
4968 devq
= bus
->sim
->devq
;
4969 status
= cam_devq_resize(devq
, devq
->alloc_queue
.array_size
+ 1);
4971 if (status
!= CAM_REQ_CMP
) {
4974 device
= kmalloc(sizeof(*device
), M_CAMXPT
, M_INTWAIT
);
4977 if (device
!= NULL
) {
4978 struct cam_ed
*cur_device
;
4980 cam_init_pinfo(&device
->alloc_ccb_entry
.pinfo
);
4981 device
->alloc_ccb_entry
.device
= device
;
4982 cam_init_pinfo(&device
->send_ccb_entry
.pinfo
);
4983 device
->send_ccb_entry
.device
= device
;
4984 device
->target
= target
;
4985 device
->lun_id
= lun_id
;
4986 device
->sim
= bus
->sim
;
4987 /* Initialize our queues */
4988 if (camq_init(&device
->drvq
, 0) != 0) {
4989 kfree(device
, M_CAMXPT
);
4992 if (cam_ccbq_init(&device
->ccbq
,
4993 bus
->sim
->max_dev_openings
) != 0) {
4994 camq_fini(&device
->drvq
);
4995 kfree(device
, M_CAMXPT
);
4998 SLIST_INIT(&device
->asyncs
);
4999 SLIST_INIT(&device
->periphs
);
5000 device
->generation
= 0;
5001 device
->owner
= NULL
;
5003 * Take the default quirk entry until we have inquiry
5004 * data and can determine a better quirk to use.
5006 device
->quirk
= &xpt_quirk_table
[xpt_quirk_table_size
- 1];
5007 bzero(&device
->inq_data
, sizeof(device
->inq_data
));
5008 device
->inq_flags
= 0;
5009 device
->queue_flags
= 0;
5010 device
->serial_num
= NULL
;
5011 device
->serial_num_len
= 0;
5012 device
->qfrozen_cnt
= 0;
5013 device
->flags
= CAM_DEV_UNCONFIGURED
;
5014 device
->tag_delay_count
= 0;
5015 device
->tag_saved_openings
= 0;
5016 device
->refcount
= 1;
5017 callout_init(&device
->callout
);
5020 * Hold a reference to our parent target so it
5021 * will not go away before we do.
5026 * XXX should be limited by number of CCBs this bus can
5029 bus
->sim
->max_ccbs
+= device
->ccbq
.devq_openings
;
5030 /* Insertion sort into our target's device list */
5031 cur_device
= TAILQ_FIRST(&target
->ed_entries
);
5032 while (cur_device
!= NULL
&& cur_device
->lun_id
< lun_id
)
5033 cur_device
= TAILQ_NEXT(cur_device
, links
);
5034 if (cur_device
!= NULL
) {
5035 TAILQ_INSERT_BEFORE(cur_device
, device
, links
);
5037 TAILQ_INSERT_TAIL(&target
->ed_entries
, device
, links
);
5039 target
->generation
++;
5040 if (lun_id
!= CAM_LUN_WILDCARD
) {
5041 xpt_compile_path(&path
,
5046 xpt_devise_transport(&path
);
5047 xpt_release_path(&path
);
5054 xpt_reference_device(struct cam_ed
*device
)
5060 xpt_release_device(struct cam_eb
*bus
, struct cam_et
*target
,
5061 struct cam_ed
*device
)
5063 struct cam_devq
*devq
;
5065 if (device
->refcount
== 1) {
5066 KKASSERT(device
->flags
& CAM_DEV_UNCONFIGURED
);
5068 if (device
->alloc_ccb_entry
.pinfo
.index
!= CAM_UNQUEUED_INDEX
5069 || device
->send_ccb_entry
.pinfo
.index
!= CAM_UNQUEUED_INDEX
)
5070 panic("Removing device while still queued for ccbs");
5072 if ((device
->flags
& CAM_DEV_REL_TIMEOUT_PENDING
) != 0) {
5073 device
->flags
&= ~CAM_DEV_REL_TIMEOUT_PENDING
;
5074 callout_stop(&device
->callout
);
5077 TAILQ_REMOVE(&target
->ed_entries
, device
,links
);
5078 target
->generation
++;
5079 bus
->sim
->max_ccbs
-= device
->ccbq
.devq_openings
;
5080 if (!SIM_DEAD(bus
->sim
)) {
5081 /* Release our slot in the devq */
5082 devq
= bus
->sim
->devq
;
5083 cam_devq_resize(devq
, devq
->alloc_queue
.array_size
- 1);
5085 camq_fini(&device
->drvq
);
5086 camq_fini(&device
->ccbq
.queue
);
5087 xpt_release_target(bus
, target
);
5088 KKASSERT(device
->refcount
== 1);
5089 kfree(device
, M_CAMXPT
);
5096 xpt_dev_ccbq_resize(struct cam_path
*path
, int newopenings
)
5104 diff
= newopenings
- (dev
->ccbq
.dev_active
+ dev
->ccbq
.dev_openings
);
5105 result
= cam_ccbq_resize(&dev
->ccbq
, newopenings
);
5106 if (result
== CAM_REQ_CMP
&& (diff
< 0)) {
5107 dev
->flags
|= CAM_DEV_RESIZE_QUEUE_NEEDED
;
5109 if ((dev
->flags
& CAM_DEV_TAG_AFTER_COUNT
) != 0
5110 || (dev
->inq_flags
& SID_CmdQue
) != 0)
5111 dev
->tag_saved_openings
= newopenings
;
5112 /* Adjust the global limit */
5113 dev
->sim
->max_ccbs
+= diff
;
5117 static struct cam_eb
*
5118 xpt_find_bus(path_id_t path_id
)
5122 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
5123 TAILQ_FOREACH(bus
, &xsoftc
.xpt_busses
, links
) {
5124 if (bus
->path_id
== path_id
) {
5129 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
5133 static struct cam_et
*
5134 xpt_find_target(struct cam_eb
*bus
, target_id_t target_id
)
5136 struct cam_et
*target
;
5138 TAILQ_FOREACH(target
, &bus
->et_entries
, links
) {
5139 if (target
->target_id
== target_id
) {
5147 static struct cam_ed
*
5148 xpt_find_device(struct cam_et
*target
, lun_id_t lun_id
)
5150 struct cam_ed
*device
;
5152 TAILQ_FOREACH(device
, &target
->ed_entries
, links
) {
5153 if (device
->lun_id
== lun_id
) {
5162 union ccb
*request_ccb
;
5163 struct ccb_pathinq
*cpi
;
5165 } xpt_scan_bus_info
;
5168 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5169 * As the scan progresses, xpt_scan_bus is used as the
5170 * callback on completion function.
5173 xpt_scan_bus(struct cam_periph
*periph
, union ccb
*request_ccb
)
5175 CAM_DEBUG(request_ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
,
5176 ("xpt_scan_bus\n"));
5177 switch (request_ccb
->ccb_h
.func_code
) {
5180 xpt_scan_bus_info
*scan_info
;
5181 union ccb
*work_ccb
;
5182 struct cam_path
*path
;
5187 /* Find out the characteristics of the bus */
5188 work_ccb
= xpt_alloc_ccb();
5189 xpt_setup_ccb(&work_ccb
->ccb_h
, request_ccb
->ccb_h
.path
,
5190 request_ccb
->ccb_h
.pinfo
.priority
);
5191 work_ccb
->ccb_h
.func_code
= XPT_PATH_INQ
;
5192 xpt_action(work_ccb
);
5193 if (work_ccb
->ccb_h
.status
!= CAM_REQ_CMP
) {
5194 request_ccb
->ccb_h
.status
= work_ccb
->ccb_h
.status
;
5195 xpt_free_ccb(work_ccb
);
5196 xpt_done(request_ccb
);
5200 if ((work_ccb
->cpi
.hba_misc
& PIM_NOINITIATOR
) != 0) {
5202 * Can't scan the bus on an adapter that
5203 * cannot perform the initiator role.
5205 request_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
5206 xpt_free_ccb(work_ccb
);
5207 xpt_done(request_ccb
);
5211 /* Save some state for use while we probe for devices */
5212 scan_info
= (xpt_scan_bus_info
*)
5213 kmalloc(sizeof(xpt_scan_bus_info
), M_CAMXPT
, M_INTWAIT
);
5214 scan_info
->request_ccb
= request_ccb
;
5215 scan_info
->cpi
= &work_ccb
->cpi
;
5217 /* Cache on our stack so we can work asynchronously */
5218 max_target
= scan_info
->cpi
->max_target
;
5219 initiator_id
= scan_info
->cpi
->initiator_id
;
5223 * We can scan all targets in parallel, or do it sequentially.
5225 if (scan_info
->cpi
->hba_misc
& PIM_SEQSCAN
) {
5227 scan_info
->counter
= 0;
5229 scan_info
->counter
= scan_info
->cpi
->max_target
+ 1;
5230 if (scan_info
->cpi
->initiator_id
< scan_info
->counter
) {
5231 scan_info
->counter
--;
5235 for (i
= 0; i
<= max_target
; i
++) {
5237 if (i
== initiator_id
)
5240 status
= xpt_create_path(&path
, xpt_periph
,
5241 request_ccb
->ccb_h
.path_id
,
5243 if (status
!= CAM_REQ_CMP
) {
5244 kprintf("xpt_scan_bus: xpt_create_path failed"
5245 " with status %#x, bus scan halted\n",
5247 kfree(scan_info
, M_CAMXPT
);
5248 request_ccb
->ccb_h
.status
= status
;
5249 xpt_free_ccb(work_ccb
);
5250 xpt_done(request_ccb
);
5253 work_ccb
= xpt_alloc_ccb();
5254 xpt_setup_ccb(&work_ccb
->ccb_h
, path
,
5255 request_ccb
->ccb_h
.pinfo
.priority
);
5256 work_ccb
->ccb_h
.func_code
= XPT_SCAN_LUN
;
5257 work_ccb
->ccb_h
.cbfcnp
= xpt_scan_bus
;
5258 work_ccb
->ccb_h
.ppriv_ptr0
= scan_info
;
5259 work_ccb
->crcn
.flags
= request_ccb
->crcn
.flags
;
5260 xpt_action(work_ccb
);
5267 struct cam_path
*path
;
5268 xpt_scan_bus_info
*scan_info
;
5270 target_id_t target_id
;
5273 /* Reuse the same CCB to query if a device was really found */
5274 scan_info
= (xpt_scan_bus_info
*)request_ccb
->ccb_h
.ppriv_ptr0
;
5275 xpt_setup_ccb(&request_ccb
->ccb_h
, request_ccb
->ccb_h
.path
,
5276 request_ccb
->ccb_h
.pinfo
.priority
);
5277 request_ccb
->ccb_h
.func_code
= XPT_GDEV_TYPE
;
5279 path_id
= request_ccb
->ccb_h
.path_id
;
5280 target_id
= request_ccb
->ccb_h
.target_id
;
5281 lun_id
= request_ccb
->ccb_h
.target_lun
;
5282 xpt_action(request_ccb
);
5284 if (request_ccb
->ccb_h
.status
!= CAM_REQ_CMP
) {
5285 struct cam_ed
*device
;
5286 struct cam_et
*target
;
5290 * If we already probed lun 0 successfully, or
5291 * we have additional configured luns on this
5292 * target that might have "gone away", go onto
5295 target
= request_ccb
->ccb_h
.path
->target
;
5297 * We may touch devices that we don't
5298 * hold references too, so ensure they
5299 * don't disappear out from under us.
5300 * The target above is referenced by the
5301 * path in the request ccb.
5304 device
= TAILQ_FIRST(&target
->ed_entries
);
5305 if (device
!= NULL
) {
5306 phl
= CAN_SRCH_HI_SPARSE(device
);
5307 if (device
->lun_id
== 0)
5308 device
= TAILQ_NEXT(device
, links
);
5310 if ((lun_id
!= 0) || (device
!= NULL
)) {
5311 if (lun_id
< (CAM_SCSI2_MAXLUN
-1) || phl
)
5315 struct cam_ed
*device
;
5317 device
= request_ccb
->ccb_h
.path
->device
;
5319 if ((device
->quirk
->quirks
& CAM_QUIRK_NOLUNS
) == 0) {
5320 /* Try the next lun */
5321 if (lun_id
< (CAM_SCSI2_MAXLUN
-1)
5322 || CAN_SRCH_HI_DENSE(device
))
5328 * Free the current request path- we're done with it.
5330 xpt_free_path(request_ccb
->ccb_h
.path
);
5333 * Check to see if we scan any further luns.
5335 if (lun_id
== request_ccb
->ccb_h
.target_lun
5336 || lun_id
> scan_info
->cpi
->max_lun
) {
5341 if (scan_info
->cpi
->hba_misc
& PIM_SEQSCAN
) {
5342 scan_info
->counter
++;
5343 if (scan_info
->counter
==
5344 scan_info
->cpi
->initiator_id
) {
5345 scan_info
->counter
++;
5347 if (scan_info
->counter
>=
5348 scan_info
->cpi
->max_target
+1) {
5352 scan_info
->counter
--;
5353 if (scan_info
->counter
== 0) {
5358 xpt_free_ccb(request_ccb
);
5359 xpt_free_ccb((union ccb
*)scan_info
->cpi
);
5360 request_ccb
= scan_info
->request_ccb
;
5361 kfree(scan_info
, M_CAMXPT
);
5362 request_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
5363 xpt_done(request_ccb
);
5367 if ((scan_info
->cpi
->hba_misc
& PIM_SEQSCAN
) == 0) {
5370 status
= xpt_create_path(&path
, xpt_periph
,
5371 scan_info
->request_ccb
->ccb_h
.path_id
,
5372 scan_info
->counter
, 0);
5373 if (status
!= CAM_REQ_CMP
) {
5374 kprintf("xpt_scan_bus: xpt_create_path failed"
5375 " with status %#x, bus scan halted\n",
5377 xpt_free_ccb(request_ccb
);
5378 xpt_free_ccb((union ccb
*)scan_info
->cpi
);
5379 request_ccb
= scan_info
->request_ccb
;
5380 kfree(scan_info
, M_CAMXPT
);
5381 request_ccb
->ccb_h
.status
= status
;
5382 xpt_done(request_ccb
);
5385 xpt_setup_ccb(&request_ccb
->ccb_h
, path
,
5386 request_ccb
->ccb_h
.pinfo
.priority
);
5387 request_ccb
->ccb_h
.func_code
= XPT_SCAN_LUN
;
5388 request_ccb
->ccb_h
.cbfcnp
= xpt_scan_bus
;
5389 request_ccb
->ccb_h
.ppriv_ptr0
= scan_info
;
5390 request_ccb
->crcn
.flags
=
5391 scan_info
->request_ccb
->crcn
.flags
;
5393 status
= xpt_create_path(&path
, xpt_periph
,
5394 path_id
, target_id
, lun_id
);
5395 if (status
!= CAM_REQ_CMP
) {
5396 kprintf("xpt_scan_bus: xpt_create_path failed "
5397 "with status %#x, halting LUN scan\n",
5401 xpt_setup_ccb(&request_ccb
->ccb_h
, path
,
5402 request_ccb
->ccb_h
.pinfo
.priority
);
5403 request_ccb
->ccb_h
.func_code
= XPT_SCAN_LUN
;
5404 request_ccb
->ccb_h
.cbfcnp
= xpt_scan_bus
;
5405 request_ccb
->ccb_h
.ppriv_ptr0
= scan_info
;
5406 request_ccb
->crcn
.flags
=
5407 scan_info
->request_ccb
->crcn
.flags
;
5409 xpt_action(request_ccb
);
5419 PROBE_INQUIRY
, /* this counts as DV0 for Basic Domain Validation */
5424 PROBE_TUR_FOR_NEGOTIATION
,
5425 PROBE_INQUIRY_BASIC_DV1
,
5426 PROBE_INQUIRY_BASIC_DV2
,
5431 PROBE_INQUIRY_CKSUM
= 0x01,
5432 PROBE_SERIAL_CKSUM
= 0x02,
5433 PROBE_NO_ANNOUNCE
= 0x04
5437 TAILQ_HEAD(, ccb_hdr
) request_ccbs
;
5438 probe_action action
;
5439 union ccb saved_ccb
;
5442 u_int8_t digest
[16];
5446 xpt_scan_lun(struct cam_periph
*periph
, struct cam_path
*path
,
5447 cam_flags flags
, union ccb
*request_ccb
)
5449 struct ccb_pathinq cpi
;
5451 struct cam_path
*new_path
;
5452 struct cam_periph
*old_periph
;
5454 CAM_DEBUG(request_ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
,
5455 ("xpt_scan_lun\n"));
5457 xpt_setup_ccb(&cpi
.ccb_h
, path
, /*priority*/1);
5458 cpi
.ccb_h
.func_code
= XPT_PATH_INQ
;
5459 xpt_action((union ccb
*)&cpi
);
5461 if (cpi
.ccb_h
.status
!= CAM_REQ_CMP
) {
5462 if (request_ccb
!= NULL
) {
5463 request_ccb
->ccb_h
.status
= cpi
.ccb_h
.status
;
5464 xpt_done(request_ccb
);
5469 if ((cpi
.hba_misc
& PIM_NOINITIATOR
) != 0) {
5471 * Can't scan the bus on an adapter that
5472 * cannot perform the initiator role.
5474 if (request_ccb
!= NULL
) {
5475 request_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
5476 xpt_done(request_ccb
);
5481 if (request_ccb
== NULL
) {
5482 request_ccb
= kmalloc(sizeof(union ccb
), M_CAMXPT
, M_INTWAIT
);
5483 new_path
= kmalloc(sizeof(*new_path
), M_CAMXPT
, M_INTWAIT
);
5484 status
= xpt_compile_path(new_path
, xpt_periph
,
5486 path
->target
->target_id
,
5487 path
->device
->lun_id
);
5489 if (status
!= CAM_REQ_CMP
) {
5490 xpt_print(path
, "xpt_scan_lun: can't compile path, "
5491 "can't continue\n");
5492 kfree(request_ccb
, M_CAMXPT
);
5493 kfree(new_path
, M_CAMXPT
);
5496 xpt_setup_ccb(&request_ccb
->ccb_h
, new_path
, /*priority*/ 1);
5497 request_ccb
->ccb_h
.cbfcnp
= xptscandone
;
5498 request_ccb
->ccb_h
.func_code
= XPT_SCAN_LUN
;
5499 request_ccb
->crcn
.flags
= flags
;
5502 if ((old_periph
= cam_periph_find(path
, "probe")) != NULL
) {
5505 softc
= (probe_softc
*)old_periph
->softc
;
5506 TAILQ_INSERT_TAIL(&softc
->request_ccbs
, &request_ccb
->ccb_h
,
5509 status
= cam_periph_alloc(proberegister
, NULL
, probecleanup
,
5510 probestart
, "probe",
5512 request_ccb
->ccb_h
.path
, NULL
, 0,
5515 if (status
!= CAM_REQ_CMP
) {
5516 xpt_print(path
, "xpt_scan_lun: cam_alloc_periph "
5517 "returned an error, can't continue probe\n");
5518 request_ccb
->ccb_h
.status
= status
;
5519 xpt_done(request_ccb
);
5525 xptscandone(struct cam_periph
*periph
, union ccb
*done_ccb
)
5527 xpt_release_path(done_ccb
->ccb_h
.path
);
5528 kfree(done_ccb
->ccb_h
.path
, M_CAMXPT
);
5529 kfree(done_ccb
, M_CAMXPT
);
5533 proberegister(struct cam_periph
*periph
, void *arg
)
5535 union ccb
*request_ccb
; /* CCB representing the probe request */
5539 request_ccb
= (union ccb
*)arg
;
5540 if (periph
== NULL
) {
5541 kprintf("proberegister: periph was NULL!!\n");
5542 return(CAM_REQ_CMP_ERR
);
5545 if (request_ccb
== NULL
) {
5546 kprintf("proberegister: no probe CCB, "
5547 "can't register device\n");
5548 return(CAM_REQ_CMP_ERR
);
5551 softc
= kmalloc(sizeof(*softc
), M_CAMXPT
, M_INTWAIT
| M_ZERO
);
5552 TAILQ_INIT(&softc
->request_ccbs
);
5553 TAILQ_INSERT_TAIL(&softc
->request_ccbs
, &request_ccb
->ccb_h
,
5556 periph
->softc
= softc
;
5557 status
= cam_periph_acquire(periph
);
5558 if (status
!= CAM_REQ_CMP
) {
5564 * Ensure we've waited at least a bus settle
5565 * delay before attempting to probe the device.
5566 * For HBAs that don't do bus resets, this won't make a difference.
5568 cam_periph_freeze_after_event(periph
, &periph
->path
->bus
->last_reset
,
5570 probeschedule(periph
);
5571 return(CAM_REQ_CMP
);
5575 probeschedule(struct cam_periph
*periph
)
5577 struct ccb_pathinq cpi
;
5581 softc
= (probe_softc
*)periph
->softc
;
5582 ccb
= (union ccb
*)TAILQ_FIRST(&softc
->request_ccbs
);
5584 xpt_setup_ccb(&cpi
.ccb_h
, periph
->path
, /*priority*/1);
5585 cpi
.ccb_h
.func_code
= XPT_PATH_INQ
;
5586 xpt_action((union ccb
*)&cpi
);
5589 * If a device has gone away and another device, or the same one,
5590 * is back in the same place, it should have a unit attention
5591 * condition pending. It will not report the unit attention in
5592 * response to an inquiry, which may leave invalid transfer
5593 * negotiations in effect. The TUR will reveal the unit attention
5594 * condition. Only send the TUR for lun 0, since some devices
5595 * will get confused by commands other than inquiry to non-existent
5596 * luns. If you think a device has gone away start your scan from
5597 * lun 0. This will insure that any bogus transfer settings are
5600 * If we haven't seen the device before and the controller supports
5601 * some kind of transfer negotiation, negotiate with the first
5602 * sent command if no bus reset was performed at startup. This
5603 * ensures that the device is not confused by transfer negotiation
5604 * settings left over by loader or BIOS action.
5606 if (((ccb
->ccb_h
.path
->device
->flags
& CAM_DEV_UNCONFIGURED
) == 0)
5607 && (ccb
->ccb_h
.target_lun
== 0)) {
5608 softc
->action
= PROBE_TUR
;
5609 } else if ((cpi
.hba_inquiry
& (PI_WIDE_32
|PI_WIDE_16
|PI_SDTR_ABLE
)) != 0
5610 && (cpi
.hba_misc
& PIM_NOBUSRESET
) != 0) {
5611 proberequestdefaultnegotiation(periph
);
5612 softc
->action
= PROBE_INQUIRY
;
5614 softc
->action
= PROBE_INQUIRY
;
5617 if (ccb
->crcn
.flags
& CAM_EXPECT_INQ_CHANGE
)
5618 softc
->flags
|= PROBE_NO_ANNOUNCE
;
5620 softc
->flags
&= ~PROBE_NO_ANNOUNCE
;
5622 xpt_schedule(periph
, ccb
->ccb_h
.pinfo
.priority
);
5626 probestart(struct cam_periph
*periph
, union ccb
*start_ccb
)
5628 /* Probe the device that our peripheral driver points to */
5629 struct ccb_scsiio
*csio
;
5632 CAM_DEBUG(start_ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
, ("probestart\n"));
5634 softc
= (probe_softc
*)periph
->softc
;
5635 csio
= &start_ccb
->csio
;
5637 switch (softc
->action
) {
5639 case PROBE_TUR_FOR_NEGOTIATION
:
5642 scsi_test_unit_ready(csio
,
5651 case PROBE_FULL_INQUIRY
:
5652 case PROBE_INQUIRY_BASIC_DV1
:
5653 case PROBE_INQUIRY_BASIC_DV2
:
5656 struct scsi_inquiry_data
*inq_buf
;
5658 inq_buf
= &periph
->path
->device
->inq_data
;
5661 * If the device is currently configured, we calculate an
5662 * MD5 checksum of the inquiry data, and if the serial number
5663 * length is greater than 0, add the serial number data
5664 * into the checksum as well. Once the inquiry and the
5665 * serial number check finish, we attempt to figure out
5666 * whether we still have the same device.
5668 if ((periph
->path
->device
->flags
& CAM_DEV_UNCONFIGURED
) == 0) {
5670 MD5Init(&softc
->context
);
5671 MD5Update(&softc
->context
, (unsigned char *)inq_buf
,
5672 sizeof(struct scsi_inquiry_data
));
5673 softc
->flags
|= PROBE_INQUIRY_CKSUM
;
5674 if (periph
->path
->device
->serial_num_len
> 0) {
5675 MD5Update(&softc
->context
,
5676 periph
->path
->device
->serial_num
,
5677 periph
->path
->device
->serial_num_len
);
5678 softc
->flags
|= PROBE_SERIAL_CKSUM
;
5680 MD5Final(softc
->digest
, &softc
->context
);
5683 if (softc
->action
== PROBE_INQUIRY
)
5684 inquiry_len
= SHORT_INQUIRY_LENGTH
;
5686 inquiry_len
= SID_ADDITIONAL_LENGTH(inq_buf
);
5689 * Some parallel SCSI devices fail to send an
5690 * ignore wide residue message when dealing with
5691 * odd length inquiry requests. Round up to be
5694 inquiry_len
= roundup2(inquiry_len
, 2);
5696 if (softc
->action
== PROBE_INQUIRY_BASIC_DV1
5697 || softc
->action
== PROBE_INQUIRY_BASIC_DV2
) {
5698 inq_buf
= kmalloc(inquiry_len
, M_CAMXPT
, M_INTWAIT
);
5704 (u_int8_t
*)inq_buf
,
5709 /*timeout*/60 * 1000);
5712 case PROBE_MODE_SENSE
:
5717 mode_buf_len
= sizeof(struct scsi_mode_header_6
)
5718 + sizeof(struct scsi_mode_blk_desc
)
5719 + sizeof(struct scsi_control_page
);
5720 mode_buf
= kmalloc(mode_buf_len
, M_CAMXPT
, M_INTWAIT
);
5721 scsi_mode_sense(csio
,
5726 SMS_PAGE_CTRL_CURRENT
,
5727 SMS_CONTROL_MODE_PAGE
,
5734 case PROBE_SERIAL_NUM_0
:
5736 struct scsi_vpd_supported_page_list
*vpd_list
= NULL
;
5737 struct cam_ed
*device
;
5739 device
= periph
->path
->device
;
5740 if ((device
->quirk
->quirks
& CAM_QUIRK_NOSERIAL
) == 0) {
5741 vpd_list
= kmalloc(sizeof(*vpd_list
), M_CAMXPT
,
5742 M_INTWAIT
| M_ZERO
);
5745 if (vpd_list
!= NULL
) {
5750 (u_int8_t
*)vpd_list
,
5753 SVPD_SUPPORTED_PAGE_LIST
,
5755 /*timeout*/60 * 1000);
5759 * We'll have to do without, let our probedone
5760 * routine finish up for us.
5762 start_ccb
->csio
.data_ptr
= NULL
;
5763 probedone(periph
, start_ccb
);
5766 case PROBE_SERIAL_NUM_1
:
5768 struct scsi_vpd_unit_serial_number
*serial_buf
;
5769 struct cam_ed
* device
;
5772 device
= periph
->path
->device
;
5773 device
->serial_num
= NULL
;
5774 device
->serial_num_len
= 0;
5776 serial_buf
= (struct scsi_vpd_unit_serial_number
*)
5777 kmalloc(sizeof(*serial_buf
), M_CAMXPT
,
5778 M_INTWAIT
| M_ZERO
);
5783 (u_int8_t
*)serial_buf
,
5784 sizeof(*serial_buf
),
5786 SVPD_UNIT_SERIAL_NUMBER
,
5788 /*timeout*/60 * 1000);
5792 xpt_action(start_ccb
);
5796 proberequestdefaultnegotiation(struct cam_periph
*periph
)
5798 struct ccb_trans_settings cts
;
5800 xpt_setup_ccb(&cts
.ccb_h
, periph
->path
, /*priority*/1);
5801 cts
.ccb_h
.func_code
= XPT_GET_TRAN_SETTINGS
;
5802 cts
.type
= CTS_TYPE_USER_SETTINGS
;
5803 xpt_action((union ccb
*)&cts
);
5804 if ((cts
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
5807 cts
.ccb_h
.func_code
= XPT_SET_TRAN_SETTINGS
;
5808 cts
.type
= CTS_TYPE_CURRENT_SETTINGS
;
5809 xpt_action((union ccb
*)&cts
);
5813 * Backoff Negotiation Code- only pertinent for SPI devices.
5816 proberequestbackoff(struct cam_periph
*periph
, struct cam_ed
*device
)
5818 struct ccb_trans_settings cts
;
5819 struct ccb_trans_settings_spi
*spi
;
5821 memset(&cts
, 0, sizeof (cts
));
5822 xpt_setup_ccb(&cts
.ccb_h
, periph
->path
, /*priority*/1);
5823 cts
.ccb_h
.func_code
= XPT_GET_TRAN_SETTINGS
;
5824 cts
.type
= CTS_TYPE_CURRENT_SETTINGS
;
5825 xpt_action((union ccb
*)&cts
);
5826 if ((cts
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
5828 xpt_print(periph
->path
,
5829 "failed to get current device settings\n");
5833 if (cts
.transport
!= XPORT_SPI
) {
5835 xpt_print(periph
->path
, "not SPI transport\n");
5839 spi
= &cts
.xport_specific
.spi
;
5842 * We cannot renegotiate sync rate if we don't have one.
5844 if ((spi
->valid
& CTS_SPI_VALID_SYNC_RATE
) == 0) {
5846 xpt_print(periph
->path
, "no sync rate known\n");
5852 * We'll assert that we don't have to touch PPR options- the
5853 * SIM will see what we do with period and offset and adjust
5854 * the PPR options as appropriate.
5858 * A sync rate with unknown or zero offset is nonsensical.
5859 * A sync period of zero means Async.
5861 if ((spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) == 0
5862 || spi
->sync_offset
== 0 || spi
->sync_period
== 0) {
5864 xpt_print(periph
->path
, "no sync rate available\n");
5869 if (device
->flags
& CAM_DEV_DV_HIT_BOTTOM
) {
5870 CAM_DEBUG(periph
->path
, CAM_DEBUG_INFO
,
5871 ("hit async: giving up on DV\n"));
5877 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
5878 * We don't try to remember 'last' settings to see if the SIM actually
5879 * gets into the speed we want to set. We check on the SIM telling
5880 * us that a requested speed is bad, but otherwise don't try and
5881 * check the speed due to the asynchronous and handshake nature
5884 spi
->valid
= CTS_SPI_VALID_SYNC_RATE
| CTS_SPI_VALID_SYNC_OFFSET
;
5887 if (spi
->sync_period
>= 0xf) {
5888 spi
->sync_period
= 0;
5889 spi
->sync_offset
= 0;
5890 CAM_DEBUG(periph
->path
, CAM_DEBUG_INFO
,
5891 ("setting to async for DV\n"));
5893 * Once we hit async, we don't want to try
5894 * any more settings.
5896 device
->flags
|= CAM_DEV_DV_HIT_BOTTOM
;
5897 } else if (bootverbose
) {
5898 CAM_DEBUG(periph
->path
, CAM_DEBUG_INFO
,
5899 ("DV: period 0x%x\n", spi
->sync_period
));
5900 kprintf("setting period to 0x%x\n", spi
->sync_period
);
5902 cts
.ccb_h
.func_code
= XPT_SET_TRAN_SETTINGS
;
5903 cts
.type
= CTS_TYPE_CURRENT_SETTINGS
;
5904 xpt_action((union ccb
*)&cts
);
5905 if ((cts
.ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
) {
5908 CAM_DEBUG(periph
->path
, CAM_DEBUG_INFO
,
5909 ("DV: failed to set period 0x%x\n", spi
->sync_period
));
5910 if (spi
->sync_period
== 0) {
5918 probedone(struct cam_periph
*periph
, union ccb
*done_ccb
)
5921 struct cam_path
*path
;
5924 CAM_DEBUG(done_ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
, ("probedone\n"));
5926 softc
= (probe_softc
*)periph
->softc
;
5927 path
= done_ccb
->ccb_h
.path
;
5928 priority
= done_ccb
->ccb_h
.pinfo
.priority
;
5930 switch (softc
->action
) {
5933 if ((done_ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
5935 if (cam_periph_error(done_ccb
, 0,
5936 SF_NO_PRINT
, NULL
) == ERESTART
)
5938 else if ((done_ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0)
5939 /* Don't wedge the queue */
5940 xpt_release_devq(done_ccb
->ccb_h
.path
,
5944 softc
->action
= PROBE_INQUIRY
;
5945 xpt_release_ccb(done_ccb
);
5946 xpt_schedule(periph
, priority
);
5950 case PROBE_FULL_INQUIRY
:
5952 if ((done_ccb
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
) {
5953 struct scsi_inquiry_data
*inq_buf
;
5954 u_int8_t periph_qual
;
5956 path
->device
->flags
|= CAM_DEV_INQUIRY_DATA_VALID
;
5957 inq_buf
= &path
->device
->inq_data
;
5959 periph_qual
= SID_QUAL(inq_buf
);
5961 switch(periph_qual
) {
5962 case SID_QUAL_LU_CONNECTED
:
5967 * We conservatively request only
5968 * SHORT_INQUIRY_LEN bytes of inquiry
5969 * information during our first try
5970 * at sending an INQUIRY. If the device
5971 * has more information to give,
5972 * perform a second request specifying
5973 * the amount of information the device
5974 * is willing to give.
5976 len
= inq_buf
->additional_length
5977 + offsetof(struct scsi_inquiry_data
,
5978 additional_length
) + 1;
5979 if (softc
->action
== PROBE_INQUIRY
5980 && len
> SHORT_INQUIRY_LENGTH
) {
5981 softc
->action
= PROBE_FULL_INQUIRY
;
5982 xpt_release_ccb(done_ccb
);
5983 xpt_schedule(periph
, priority
);
5987 xpt_find_quirk(path
->device
);
5989 xpt_devise_transport(path
);
5990 if (INQ_DATA_TQ_ENABLED(inq_buf
))
5991 softc
->action
= PROBE_MODE_SENSE
;
5993 softc
->action
= PROBE_SERIAL_NUM_0
;
5995 path
->device
->flags
&= ~CAM_DEV_UNCONFIGURED
;
5996 xpt_reference_device(path
->device
);
5998 xpt_release_ccb(done_ccb
);
5999 xpt_schedule(periph
, priority
);
6005 } else if (cam_periph_error(done_ccb
, 0,
6006 done_ccb
->ccb_h
.target_lun
> 0
6007 ? SF_RETRY_UA
|SF_QUIET_IR
6009 &softc
->saved_ccb
) == ERESTART
) {
6011 } else if ((done_ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0) {
6012 /* Don't wedge the queue */
6013 xpt_release_devq(done_ccb
->ccb_h
.path
, /*count*/1,
6017 * If we get to this point, we got an error status back
6018 * from the inquiry and the error status doesn't require
6019 * automatically retrying the command. Therefore, the
6020 * inquiry failed. If we had inquiry information before
6021 * for this device, but this latest inquiry command failed,
6022 * the device has probably gone away. If this device isn't
6023 * already marked unconfigured, notify the peripheral
6024 * drivers that this device is no more.
6026 if ((path
->device
->flags
& CAM_DEV_UNCONFIGURED
) == 0) {
6027 /* Send the async notification. */
6028 xpt_async(AC_LOST_DEVICE
, path
, NULL
);
6031 xpt_release_ccb(done_ccb
);
6034 case PROBE_MODE_SENSE
:
6036 struct ccb_scsiio
*csio
;
6037 struct scsi_mode_header_6
*mode_hdr
;
6039 csio
= &done_ccb
->csio
;
6040 mode_hdr
= (struct scsi_mode_header_6
*)csio
->data_ptr
;
6041 if ((csio
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
) {
6042 struct scsi_control_page
*page
;
6045 offset
= ((u_int8_t
*)&mode_hdr
[1])
6046 + mode_hdr
->blk_desc_len
;
6047 page
= (struct scsi_control_page
*)offset
;
6048 path
->device
->queue_flags
= page
->queue_flags
;
6049 } else if (cam_periph_error(done_ccb
, 0,
6050 SF_RETRY_UA
|SF_NO_PRINT
,
6051 &softc
->saved_ccb
) == ERESTART
) {
6053 } else if ((done_ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0) {
6054 /* Don't wedge the queue */
6055 xpt_release_devq(done_ccb
->ccb_h
.path
,
6056 /*count*/1, /*run_queue*/TRUE
);
6058 xpt_release_ccb(done_ccb
);
6059 kfree(mode_hdr
, M_CAMXPT
);
6060 softc
->action
= PROBE_SERIAL_NUM_0
;
6061 xpt_schedule(periph
, priority
);
6064 case PROBE_SERIAL_NUM_0
:
6066 struct ccb_scsiio
*csio
;
6067 struct scsi_vpd_supported_page_list
*page_list
;
6068 int length
, serialnum_supported
, i
;
6070 serialnum_supported
= 0;
6071 csio
= &done_ccb
->csio
;
6073 (struct scsi_vpd_supported_page_list
*)csio
->data_ptr
;
6075 if (page_list
== NULL
) {
6077 * Don't process the command as it was never sent
6079 } else if ((csio
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
6080 && (page_list
->length
> 0)) {
6081 length
= min(page_list
->length
,
6082 SVPD_SUPPORTED_PAGES_SIZE
);
6083 for (i
= 0; i
< length
; i
++) {
6084 if (page_list
->list
[i
] ==
6085 SVPD_UNIT_SERIAL_NUMBER
) {
6086 serialnum_supported
= 1;
6090 } else if (cam_periph_error(done_ccb
, 0,
6091 SF_RETRY_UA
|SF_NO_PRINT
,
6092 &softc
->saved_ccb
) == ERESTART
) {
6094 } else if ((done_ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0) {
6095 /* Don't wedge the queue */
6096 xpt_release_devq(done_ccb
->ccb_h
.path
, /*count*/1,
6100 if (page_list
!= NULL
)
6101 kfree(page_list
, M_DEVBUF
);
6103 if (serialnum_supported
) {
6104 xpt_release_ccb(done_ccb
);
6105 softc
->action
= PROBE_SERIAL_NUM_1
;
6106 xpt_schedule(periph
, priority
);
6109 xpt_release_ccb(done_ccb
);
6110 softc
->action
= PROBE_TUR_FOR_NEGOTIATION
;
6111 xpt_schedule(periph
, done_ccb
->ccb_h
.pinfo
.priority
);
6115 case PROBE_SERIAL_NUM_1
:
6117 struct ccb_scsiio
*csio
;
6118 struct scsi_vpd_unit_serial_number
*serial_buf
;
6125 csio
= &done_ccb
->csio
;
6126 priority
= done_ccb
->ccb_h
.pinfo
.priority
;
6128 (struct scsi_vpd_unit_serial_number
*)csio
->data_ptr
;
6130 /* Clean up from previous instance of this device */
6131 if (path
->device
->serial_num
!= NULL
) {
6132 kfree(path
->device
->serial_num
, M_CAMXPT
);
6133 path
->device
->serial_num
= NULL
;
6134 path
->device
->serial_num_len
= 0;
6137 if (serial_buf
== NULL
) {
6139 * Don't process the command as it was never sent
6141 } else if ((csio
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
6142 && (serial_buf
->length
> 0)) {
6145 path
->device
->serial_num
=
6146 kmalloc((serial_buf
->length
+ 1),
6147 M_CAMXPT
, M_INTWAIT
);
6148 bcopy(serial_buf
->serial_num
,
6149 path
->device
->serial_num
,
6150 serial_buf
->length
);
6151 path
->device
->serial_num_len
= serial_buf
->length
;
6152 path
->device
->serial_num
[serial_buf
->length
] = '\0';
6153 } else if (cam_periph_error(done_ccb
, 0,
6154 SF_RETRY_UA
|SF_NO_PRINT
,
6155 &softc
->saved_ccb
) == ERESTART
) {
6157 } else if ((done_ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0) {
6158 /* Don't wedge the queue */
6159 xpt_release_devq(done_ccb
->ccb_h
.path
, /*count*/1,
6164 * Let's see if we have seen this device before.
6166 if ((softc
->flags
& PROBE_INQUIRY_CKSUM
) != 0) {
6168 u_int8_t digest
[16];
6173 (unsigned char *)&path
->device
->inq_data
,
6174 sizeof(struct scsi_inquiry_data
));
6177 MD5Update(&context
, serial_buf
->serial_num
,
6178 serial_buf
->length
);
6180 MD5Final(digest
, &context
);
6181 if (bcmp(softc
->digest
, digest
, 16) == 0)
6185 * XXX Do we need to do a TUR in order to ensure
6186 * that the device really hasn't changed???
6189 && ((softc
->flags
& PROBE_NO_ANNOUNCE
) == 0))
6190 xpt_async(AC_LOST_DEVICE
, path
, NULL
);
6192 if (serial_buf
!= NULL
)
6193 kfree(serial_buf
, M_CAMXPT
);
6197 * Now that we have all the necessary
6198 * information to safely perform transfer
6199 * negotiations... Controllers don't perform
6200 * any negotiation or tagged queuing until
6201 * after the first XPT_SET_TRAN_SETTINGS ccb is
6202 * received. So, on a new device, just retrieve
6203 * the user settings, and set them as the current
6204 * settings to set the device up.
6206 proberequestdefaultnegotiation(periph
);
6207 xpt_release_ccb(done_ccb
);
6210 * Perform a TUR to allow the controller to
6211 * perform any necessary transfer negotiation.
6213 softc
->action
= PROBE_TUR_FOR_NEGOTIATION
;
6214 xpt_schedule(periph
, priority
);
6217 xpt_release_ccb(done_ccb
);
6220 case PROBE_TUR_FOR_NEGOTIATION
:
6222 if ((done_ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0) {
6223 /* Don't wedge the queue */
6224 xpt_release_devq(done_ccb
->ccb_h
.path
, /*count*/1,
6228 xpt_reference_device(path
->device
);
6230 * Do Domain Validation for lun 0 on devices that claim
6231 * to support Synchronous Transfer modes.
6233 if (softc
->action
== PROBE_TUR_FOR_NEGOTIATION
6234 && done_ccb
->ccb_h
.target_lun
== 0
6235 && (path
->device
->inq_data
.flags
& SID_Sync
) != 0
6236 && (path
->device
->flags
& CAM_DEV_IN_DV
) == 0) {
6237 CAM_DEBUG(periph
->path
, CAM_DEBUG_INFO
,
6238 ("Begin Domain Validation\n"));
6239 path
->device
->flags
|= CAM_DEV_IN_DV
;
6240 xpt_release_ccb(done_ccb
);
6241 softc
->action
= PROBE_INQUIRY_BASIC_DV1
;
6242 xpt_schedule(periph
, priority
);
6245 if (softc
->action
== PROBE_DV_EXIT
) {
6246 CAM_DEBUG(periph
->path
, CAM_DEBUG_INFO
,
6247 ("Leave Domain Validation\n"));
6249 path
->device
->flags
&=
6250 ~(CAM_DEV_UNCONFIGURED
|CAM_DEV_IN_DV
|CAM_DEV_DV_HIT_BOTTOM
);
6251 if ((softc
->flags
& PROBE_NO_ANNOUNCE
) == 0) {
6252 /* Inform the XPT that a new device has been found */
6253 done_ccb
->ccb_h
.func_code
= XPT_GDEV_TYPE
;
6254 xpt_action(done_ccb
);
6255 xpt_async(AC_FOUND_DEVICE
, done_ccb
->ccb_h
.path
,
6258 xpt_release_ccb(done_ccb
);
6260 case PROBE_INQUIRY_BASIC_DV1
:
6261 case PROBE_INQUIRY_BASIC_DV2
:
6263 struct scsi_inquiry_data
*nbuf
;
6264 struct ccb_scsiio
*csio
;
6266 if ((done_ccb
->ccb_h
.status
& CAM_DEV_QFRZN
) != 0) {
6267 /* Don't wedge the queue */
6268 xpt_release_devq(done_ccb
->ccb_h
.path
, /*count*/1,
6271 csio
= &done_ccb
->csio
;
6272 nbuf
= (struct scsi_inquiry_data
*)csio
->data_ptr
;
6273 if (bcmp(nbuf
, &path
->device
->inq_data
, SHORT_INQUIRY_LENGTH
)) {
6275 "inquiry data fails comparison at DV%d step\n",
6276 softc
->action
== PROBE_INQUIRY_BASIC_DV1
? 1 : 2);
6277 if (proberequestbackoff(periph
, path
->device
)) {
6278 path
->device
->flags
&= ~CAM_DEV_IN_DV
;
6279 softc
->action
= PROBE_TUR_FOR_NEGOTIATION
;
6282 softc
->action
= PROBE_DV_EXIT
;
6284 kfree(nbuf
, M_CAMXPT
);
6285 xpt_release_ccb(done_ccb
);
6286 xpt_schedule(periph
, priority
);
6289 kfree(nbuf
, M_CAMXPT
);
6290 if (softc
->action
== PROBE_INQUIRY_BASIC_DV1
) {
6291 softc
->action
= PROBE_INQUIRY_BASIC_DV2
;
6292 xpt_release_ccb(done_ccb
);
6293 xpt_schedule(periph
, priority
);
6296 if (softc
->action
== PROBE_DV_EXIT
) {
6297 CAM_DEBUG(periph
->path
, CAM_DEBUG_INFO
,
6298 ("Leave Domain Validation Successfully\n"));
6300 path
->device
->flags
&=
6301 ~(CAM_DEV_UNCONFIGURED
|CAM_DEV_IN_DV
|CAM_DEV_DV_HIT_BOTTOM
);
6302 if ((softc
->flags
& PROBE_NO_ANNOUNCE
) == 0) {
6303 /* Inform the XPT that a new device has been found */
6304 done_ccb
->ccb_h
.func_code
= XPT_GDEV_TYPE
;
6305 xpt_action(done_ccb
);
6306 xpt_async(AC_FOUND_DEVICE
, done_ccb
->ccb_h
.path
,
6309 xpt_release_ccb(done_ccb
);
6313 done_ccb
= (union ccb
*)TAILQ_FIRST(&softc
->request_ccbs
);
6314 TAILQ_REMOVE(&softc
->request_ccbs
, &done_ccb
->ccb_h
, periph_links
.tqe
);
6315 done_ccb
->ccb_h
.status
= CAM_REQ_CMP
;
6317 if (TAILQ_FIRST(&softc
->request_ccbs
) == NULL
) {
6318 cam_periph_invalidate(periph
);
6319 cam_periph_release(periph
);
6321 probeschedule(periph
);
6326 probecleanup(struct cam_periph
*periph
)
6328 kfree(periph
->softc
, M_CAMXPT
);
6332 xpt_find_quirk(struct cam_ed
*device
)
6336 match
= cam_quirkmatch((caddr_t
)&device
->inq_data
,
6337 (caddr_t
)xpt_quirk_table
,
6338 sizeof(xpt_quirk_table
)/sizeof(*xpt_quirk_table
),
6339 sizeof(*xpt_quirk_table
), scsi_inquiry_match
);
6342 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6344 device
->quirk
= (struct xpt_quirk_entry
*)match
;
6348 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS
)
6353 error
= sysctl_handle_int(oidp
, &bool, 0, req
);
6354 if (error
!= 0 || req
->newptr
== NULL
)
6356 if (bool == 0 || bool == 1) {
6365 xpt_devise_transport(struct cam_path
*path
)
6367 struct ccb_pathinq cpi
;
6368 struct ccb_trans_settings cts
;
6369 struct scsi_inquiry_data
*inq_buf
;
6371 /* Get transport information from the SIM */
6372 xpt_setup_ccb(&cpi
.ccb_h
, path
, /*priority*/1);
6373 cpi
.ccb_h
.func_code
= XPT_PATH_INQ
;
6374 xpt_action((union ccb
*)&cpi
);
6377 if ((path
->device
->flags
& CAM_DEV_INQUIRY_DATA_VALID
) != 0)
6378 inq_buf
= &path
->device
->inq_data
;
6379 path
->device
->protocol
= PROTO_SCSI
;
6380 path
->device
->protocol_version
=
6381 inq_buf
!= NULL
? SID_ANSI_REV(inq_buf
) : cpi
.protocol_version
;
6382 path
->device
->transport
= cpi
.transport
;
6383 path
->device
->transport_version
= cpi
.transport_version
;
6386 * Any device not using SPI3 features should
6387 * be considered SPI2 or lower.
6389 if (inq_buf
!= NULL
) {
6390 if (path
->device
->transport
== XPORT_SPI
6391 && (inq_buf
->spi3data
& SID_SPI_MASK
) == 0
6392 && path
->device
->transport_version
> 2)
6393 path
->device
->transport_version
= 2;
6395 struct cam_ed
* otherdev
;
6397 for (otherdev
= TAILQ_FIRST(&path
->target
->ed_entries
);
6399 otherdev
= TAILQ_NEXT(otherdev
, links
)) {
6400 if (otherdev
!= path
->device
)
6404 if (otherdev
!= NULL
) {
6406 * Initially assume the same versioning as
6407 * prior luns for this target.
6409 path
->device
->protocol_version
=
6410 otherdev
->protocol_version
;
6411 path
->device
->transport_version
=
6412 otherdev
->transport_version
;
6414 /* Until we know better, opt for safty */
6415 path
->device
->protocol_version
= 2;
6416 if (path
->device
->transport
== XPORT_SPI
)
6417 path
->device
->transport_version
= 2;
6419 path
->device
->transport_version
= 0;
6425 * For a device compliant with SPC-2 we should be able
6426 * to determine the transport version supported by
6427 * scrutinizing the version descriptors in the
6431 /* Tell the controller what we think */
6432 xpt_setup_ccb(&cts
.ccb_h
, path
, /*priority*/1);
6433 cts
.ccb_h
.func_code
= XPT_SET_TRAN_SETTINGS
;
6434 cts
.type
= CTS_TYPE_CURRENT_SETTINGS
;
6435 cts
.transport
= path
->device
->transport
;
6436 cts
.transport_version
= path
->device
->transport_version
;
6437 cts
.protocol
= path
->device
->protocol
;
6438 cts
.protocol_version
= path
->device
->protocol_version
;
6439 cts
.proto_specific
.valid
= 0;
6440 cts
.xport_specific
.valid
= 0;
6441 xpt_action((union ccb
*)&cts
);
6445 xpt_set_transfer_settings(struct ccb_trans_settings
*cts
, struct cam_ed
*device
,
6448 struct ccb_pathinq cpi
;
6449 struct ccb_trans_settings cur_cts
;
6450 struct ccb_trans_settings_scsi
*scsi
;
6451 struct ccb_trans_settings_scsi
*cur_scsi
;
6452 struct cam_sim
*sim
;
6453 struct scsi_inquiry_data
*inq_data
;
6455 if (device
== NULL
) {
6456 cts
->ccb_h
.status
= CAM_PATH_INVALID
;
6457 xpt_done((union ccb
*)cts
);
6461 if (cts
->protocol
== PROTO_UNKNOWN
6462 || cts
->protocol
== PROTO_UNSPECIFIED
) {
6463 cts
->protocol
= device
->protocol
;
6464 cts
->protocol_version
= device
->protocol_version
;
6467 if (cts
->protocol_version
== PROTO_VERSION_UNKNOWN
6468 || cts
->protocol_version
== PROTO_VERSION_UNSPECIFIED
)
6469 cts
->protocol_version
= device
->protocol_version
;
6471 if (cts
->protocol
!= device
->protocol
) {
6472 xpt_print(cts
->ccb_h
.path
, "Uninitialized Protocol %x:%x?\n",
6473 cts
->protocol
, device
->protocol
);
6474 cts
->protocol
= device
->protocol
;
6477 if (cts
->protocol_version
> device
->protocol_version
) {
6479 xpt_print(cts
->ccb_h
.path
, "Down reving Protocol "
6480 "Version from %d to %d?\n", cts
->protocol_version
,
6481 device
->protocol_version
);
6483 cts
->protocol_version
= device
->protocol_version
;
6486 if (cts
->transport
== XPORT_UNKNOWN
6487 || cts
->transport
== XPORT_UNSPECIFIED
) {
6488 cts
->transport
= device
->transport
;
6489 cts
->transport_version
= device
->transport_version
;
6492 if (cts
->transport_version
== XPORT_VERSION_UNKNOWN
6493 || cts
->transport_version
== XPORT_VERSION_UNSPECIFIED
)
6494 cts
->transport_version
= device
->transport_version
;
6496 if (cts
->transport
!= device
->transport
) {
6497 xpt_print(cts
->ccb_h
.path
, "Uninitialized Transport %x:%x?\n",
6498 cts
->transport
, device
->transport
);
6499 cts
->transport
= device
->transport
;
6502 if (cts
->transport_version
> device
->transport_version
) {
6504 xpt_print(cts
->ccb_h
.path
, "Down reving Transport "
6505 "Version from %d to %d?\n", cts
->transport_version
,
6506 device
->transport_version
);
6508 cts
->transport_version
= device
->transport_version
;
6511 sim
= cts
->ccb_h
.path
->bus
->sim
;
6514 * Nothing more of interest to do unless
6515 * this is a device connected via the
6518 if (cts
->protocol
!= PROTO_SCSI
) {
6519 if (async_update
== FALSE
)
6520 (*(sim
->sim_action
))(sim
, (union ccb
*)cts
);
6524 inq_data
= &device
->inq_data
;
6525 scsi
= &cts
->proto_specific
.scsi
;
6526 xpt_setup_ccb(&cpi
.ccb_h
, cts
->ccb_h
.path
, /*priority*/1);
6527 cpi
.ccb_h
.func_code
= XPT_PATH_INQ
;
6528 xpt_action((union ccb
*)&cpi
);
6530 /* SCSI specific sanity checking */
6531 if ((cpi
.hba_inquiry
& PI_TAG_ABLE
) == 0
6532 || (INQ_DATA_TQ_ENABLED(inq_data
)) == 0
6533 || (device
->queue_flags
& SCP_QUEUE_DQUE
) != 0
6534 || (device
->quirk
->mintags
== 0)) {
6536 * Can't tag on hardware that doesn't support tags,
6537 * doesn't have it enabled, or has broken tag support.
6539 scsi
->flags
&= ~CTS_SCSI_FLAGS_TAG_ENB
;
6542 if (async_update
== FALSE
) {
6544 * Perform sanity checking against what the
6545 * controller and device can do.
6547 xpt_setup_ccb(&cur_cts
.ccb_h
, cts
->ccb_h
.path
, /*priority*/1);
6548 cur_cts
.ccb_h
.func_code
= XPT_GET_TRAN_SETTINGS
;
6549 cur_cts
.type
= cts
->type
;
6550 xpt_action((union ccb
*)&cur_cts
);
6551 if ((cur_cts
.ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
6554 cur_scsi
= &cur_cts
.proto_specific
.scsi
;
6555 if ((scsi
->valid
& CTS_SCSI_VALID_TQ
) == 0) {
6556 scsi
->flags
&= ~CTS_SCSI_FLAGS_TAG_ENB
;
6557 scsi
->flags
|= cur_scsi
->flags
& CTS_SCSI_FLAGS_TAG_ENB
;
6559 if ((cur_scsi
->valid
& CTS_SCSI_VALID_TQ
) == 0)
6560 scsi
->flags
&= ~CTS_SCSI_FLAGS_TAG_ENB
;
6563 /* SPI specific sanity checking */
6564 if (cts
->transport
== XPORT_SPI
&& async_update
== FALSE
) {
6566 struct ccb_trans_settings_spi
*spi
;
6567 struct ccb_trans_settings_spi
*cur_spi
;
6569 spi
= &cts
->xport_specific
.spi
;
6571 cur_spi
= &cur_cts
.xport_specific
.spi
;
6573 /* Fill in any gaps in what the user gave us */
6574 if ((spi
->valid
& CTS_SPI_VALID_SYNC_RATE
) == 0)
6575 spi
->sync_period
= cur_spi
->sync_period
;
6576 if ((cur_spi
->valid
& CTS_SPI_VALID_SYNC_RATE
) == 0)
6577 spi
->sync_period
= 0;
6578 if ((spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) == 0)
6579 spi
->sync_offset
= cur_spi
->sync_offset
;
6580 if ((cur_spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) == 0)
6581 spi
->sync_offset
= 0;
6582 if ((spi
->valid
& CTS_SPI_VALID_PPR_OPTIONS
) == 0)
6583 spi
->ppr_options
= cur_spi
->ppr_options
;
6584 if ((cur_spi
->valid
& CTS_SPI_VALID_PPR_OPTIONS
) == 0)
6585 spi
->ppr_options
= 0;
6586 if ((spi
->valid
& CTS_SPI_VALID_BUS_WIDTH
) == 0)
6587 spi
->bus_width
= cur_spi
->bus_width
;
6588 if ((cur_spi
->valid
& CTS_SPI_VALID_BUS_WIDTH
) == 0)
6590 if ((spi
->valid
& CTS_SPI_VALID_DISC
) == 0) {
6591 spi
->flags
&= ~CTS_SPI_FLAGS_DISC_ENB
;
6592 spi
->flags
|= cur_spi
->flags
& CTS_SPI_FLAGS_DISC_ENB
;
6594 if ((cur_spi
->valid
& CTS_SPI_VALID_DISC
) == 0)
6595 spi
->flags
&= ~CTS_SPI_FLAGS_DISC_ENB
;
6596 if (((device
->flags
& CAM_DEV_INQUIRY_DATA_VALID
) != 0
6597 && (inq_data
->flags
& SID_Sync
) == 0
6598 && cts
->type
== CTS_TYPE_CURRENT_SETTINGS
)
6599 || ((cpi
.hba_inquiry
& PI_SDTR_ABLE
) == 0)
6600 || (spi
->sync_offset
== 0)
6601 || (spi
->sync_period
== 0)) {
6603 spi
->sync_period
= 0;
6604 spi
->sync_offset
= 0;
6607 switch (spi
->bus_width
) {
6608 case MSG_EXT_WDTR_BUS_32_BIT
:
6609 if (((device
->flags
& CAM_DEV_INQUIRY_DATA_VALID
) == 0
6610 || (inq_data
->flags
& SID_WBus32
) != 0
6611 || cts
->type
== CTS_TYPE_USER_SETTINGS
)
6612 && (cpi
.hba_inquiry
& PI_WIDE_32
) != 0)
6614 /* Fall Through to 16-bit */
6615 case MSG_EXT_WDTR_BUS_16_BIT
:
6616 if (((device
->flags
& CAM_DEV_INQUIRY_DATA_VALID
) == 0
6617 || (inq_data
->flags
& SID_WBus16
) != 0
6618 || cts
->type
== CTS_TYPE_USER_SETTINGS
)
6619 && (cpi
.hba_inquiry
& PI_WIDE_16
) != 0) {
6620 spi
->bus_width
= MSG_EXT_WDTR_BUS_16_BIT
;
6623 /* Fall Through to 8-bit */
6624 default: /* New bus width?? */
6625 case MSG_EXT_WDTR_BUS_8_BIT
:
6626 /* All targets can do this */
6627 spi
->bus_width
= MSG_EXT_WDTR_BUS_8_BIT
;
6631 spi3caps
= cpi
.xport_specific
.spi
.ppr_options
;
6632 if ((device
->flags
& CAM_DEV_INQUIRY_DATA_VALID
) != 0
6633 && cts
->type
== CTS_TYPE_CURRENT_SETTINGS
)
6634 spi3caps
&= inq_data
->spi3data
;
6636 if ((spi3caps
& SID_SPI_CLOCK_DT
) == 0)
6637 spi
->ppr_options
&= ~MSG_EXT_PPR_DT_REQ
;
6639 if ((spi3caps
& SID_SPI_IUS
) == 0)
6640 spi
->ppr_options
&= ~MSG_EXT_PPR_IU_REQ
;
6642 if ((spi3caps
& SID_SPI_QAS
) == 0)
6643 spi
->ppr_options
&= ~MSG_EXT_PPR_QAS_REQ
;
6645 /* No SPI Transfer settings are allowed unless we are wide */
6646 if (spi
->bus_width
== 0)
6647 spi
->ppr_options
= 0;
6649 if ((spi
->flags
& CTS_SPI_FLAGS_DISC_ENB
) == 0) {
6651 * Can't tag queue without disconnection.
6653 scsi
->flags
&= ~CTS_SCSI_FLAGS_TAG_ENB
;
6654 scsi
->valid
|= CTS_SCSI_VALID_TQ
;
6658 * If we are currently performing tagged transactions to
6659 * this device and want to change its negotiation parameters,
6660 * go non-tagged for a bit to give the controller a chance to
6661 * negotiate unhampered by tag messages.
6663 if (cts
->type
== CTS_TYPE_CURRENT_SETTINGS
6664 && (device
->inq_flags
& SID_CmdQue
) != 0
6665 && (scsi
->flags
& CTS_SCSI_FLAGS_TAG_ENB
) != 0
6666 && (spi
->flags
& (CTS_SPI_VALID_SYNC_RATE
|
6667 CTS_SPI_VALID_SYNC_OFFSET
|
6668 CTS_SPI_VALID_BUS_WIDTH
)) != 0)
6669 xpt_toggle_tags(cts
->ccb_h
.path
);
6672 if (cts
->type
== CTS_TYPE_CURRENT_SETTINGS
6673 && (scsi
->valid
& CTS_SCSI_VALID_TQ
) != 0) {
6677 * If we are transitioning from tags to no-tags or
6678 * vice-versa, we need to carefully freeze and restart
6679 * the queue so that we don't overlap tagged and non-tagged
6680 * commands. We also temporarily stop tags if there is
6681 * a change in transfer negotiation settings to allow
6682 * "tag-less" negotiation.
6684 if ((device
->flags
& CAM_DEV_TAG_AFTER_COUNT
) != 0
6685 || (device
->inq_flags
& SID_CmdQue
) != 0)
6686 device_tagenb
= TRUE
;
6688 device_tagenb
= FALSE
;
6690 if (((scsi
->flags
& CTS_SCSI_FLAGS_TAG_ENB
) != 0
6691 && device_tagenb
== FALSE
)
6692 || ((scsi
->flags
& CTS_SCSI_FLAGS_TAG_ENB
) == 0
6693 && device_tagenb
== TRUE
)) {
6695 if ((scsi
->flags
& CTS_SCSI_FLAGS_TAG_ENB
) != 0) {
6697 * Delay change to use tags until after a
6698 * few commands have gone to this device so
6699 * the controller has time to perform transfer
6700 * negotiations without tagged messages getting
6703 device
->tag_delay_count
= CAM_TAG_DELAY_COUNT
;
6704 device
->flags
|= CAM_DEV_TAG_AFTER_COUNT
;
6706 struct ccb_relsim crs
;
6708 xpt_freeze_devq(cts
->ccb_h
.path
, /*count*/1);
6709 device
->inq_flags
&= ~SID_CmdQue
;
6710 xpt_dev_ccbq_resize(cts
->ccb_h
.path
,
6711 sim
->max_dev_openings
);
6712 device
->flags
&= ~CAM_DEV_TAG_AFTER_COUNT
;
6713 device
->tag_delay_count
= 0;
6715 xpt_setup_ccb(&crs
.ccb_h
, cts
->ccb_h
.path
,
6717 crs
.ccb_h
.func_code
= XPT_REL_SIMQ
;
6718 crs
.release_flags
= RELSIM_RELEASE_AFTER_QEMPTY
;
6720 = crs
.release_timeout
6723 xpt_action((union ccb
*)&crs
);
6727 if (async_update
== FALSE
)
6728 (*(sim
->sim_action
))(sim
, (union ccb
*)cts
);
6732 xpt_toggle_tags(struct cam_path
*path
)
6737 * Give controllers a chance to renegotiate
6738 * before starting tag operations. We
6739 * "toggle" tagged queuing off then on
6740 * which causes the tag enable command delay
6741 * counter to come into effect.
6744 if ((dev
->flags
& CAM_DEV_TAG_AFTER_COUNT
) != 0
6745 || ((dev
->inq_flags
& SID_CmdQue
) != 0
6746 && (dev
->inq_flags
& (SID_Sync
|SID_WBus16
|SID_WBus32
)) != 0)) {
6747 struct ccb_trans_settings cts
;
6749 xpt_setup_ccb(&cts
.ccb_h
, path
, 1);
6750 cts
.protocol
= PROTO_SCSI
;
6751 cts
.protocol_version
= PROTO_VERSION_UNSPECIFIED
;
6752 cts
.transport
= XPORT_UNSPECIFIED
;
6753 cts
.transport_version
= XPORT_VERSION_UNSPECIFIED
;
6754 cts
.proto_specific
.scsi
.flags
= 0;
6755 cts
.proto_specific
.scsi
.valid
= CTS_SCSI_VALID_TQ
;
6756 xpt_set_transfer_settings(&cts
, path
->device
,
6757 /*async_update*/TRUE
);
6758 cts
.proto_specific
.scsi
.flags
= CTS_SCSI_FLAGS_TAG_ENB
;
6759 xpt_set_transfer_settings(&cts
, path
->device
,
6760 /*async_update*/TRUE
);
6765 xpt_start_tags(struct cam_path
*path
)
6767 struct ccb_relsim crs
;
6768 struct cam_ed
*device
;
6769 struct cam_sim
*sim
;
6772 device
= path
->device
;
6773 sim
= path
->bus
->sim
;
6774 device
->flags
&= ~CAM_DEV_TAG_AFTER_COUNT
;
6775 xpt_freeze_devq(path
, /*count*/1);
6776 device
->inq_flags
|= SID_CmdQue
;
6777 if (device
->tag_saved_openings
!= 0)
6778 newopenings
= device
->tag_saved_openings
;
6780 newopenings
= min(device
->quirk
->maxtags
,
6781 sim
->max_tagged_dev_openings
);
6782 xpt_dev_ccbq_resize(path
, newopenings
);
6783 xpt_setup_ccb(&crs
.ccb_h
, path
, /*priority*/1);
6784 crs
.ccb_h
.func_code
= XPT_REL_SIMQ
;
6785 crs
.release_flags
= RELSIM_RELEASE_AFTER_QEMPTY
;
6787 = crs
.release_timeout
6790 xpt_action((union ccb
*)&crs
);
6793 static int busses_to_config
;
6794 static int busses_to_reset
;
6797 xptconfigbuscountfunc(struct cam_eb
*bus
, void *arg
)
6800 sim_lock_assert_owned(bus
->sim
->lock
);
6802 if (bus
->path_id
!= CAM_XPT_PATH_ID
) {
6803 struct cam_path path
;
6804 struct ccb_pathinq cpi
;
6808 xpt_compile_path(&path
, NULL
, bus
->path_id
,
6809 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
);
6810 xpt_setup_ccb(&cpi
.ccb_h
, &path
, /*priority*/1);
6811 cpi
.ccb_h
.func_code
= XPT_PATH_INQ
;
6812 xpt_action((union ccb
*)&cpi
);
6813 can_negotiate
= cpi
.hba_inquiry
;
6814 can_negotiate
&= (PI_WIDE_32
|PI_WIDE_16
|PI_SDTR_ABLE
);
6815 if ((cpi
.hba_misc
& PIM_NOBUSRESET
) == 0
6818 xpt_release_path(&path
);
6825 xptconfigfunc(struct cam_eb
*bus
, void *arg
)
6827 struct cam_path
*path
;
6828 union ccb
*work_ccb
;
6830 sim_lock_assert_owned(bus
->sim
->lock
);
6832 if (bus
->path_id
!= CAM_XPT_PATH_ID
) {
6836 work_ccb
= xpt_alloc_ccb();
6837 if ((status
= xpt_create_path(&path
, xpt_periph
, bus
->path_id
,
6838 CAM_TARGET_WILDCARD
,
6839 CAM_LUN_WILDCARD
)) !=CAM_REQ_CMP
){
6840 kprintf("xptconfigfunc: xpt_create_path failed with "
6841 "status %#x for bus %d\n", status
, bus
->path_id
);
6842 kprintf("xptconfigfunc: halting bus configuration\n");
6843 xpt_free_ccb(work_ccb
);
6845 xpt_finishconfig(xpt_periph
, NULL
);
6848 xpt_setup_ccb(&work_ccb
->ccb_h
, path
, /*priority*/1);
6849 work_ccb
->ccb_h
.func_code
= XPT_PATH_INQ
;
6850 xpt_action(work_ccb
);
6851 if (work_ccb
->ccb_h
.status
!= CAM_REQ_CMP
) {
6852 kprintf("xptconfigfunc: CPI failed on bus %d "
6853 "with status %d\n", bus
->path_id
,
6854 work_ccb
->ccb_h
.status
);
6855 xpt_finishconfig(xpt_periph
, work_ccb
);
6859 can_negotiate
= work_ccb
->cpi
.hba_inquiry
;
6860 can_negotiate
&= (PI_WIDE_32
|PI_WIDE_16
|PI_SDTR_ABLE
);
6861 if ((work_ccb
->cpi
.hba_misc
& PIM_NOBUSRESET
) == 0
6862 && (can_negotiate
!= 0)) {
6863 xpt_setup_ccb(&work_ccb
->ccb_h
, path
, /*priority*/1);
6864 work_ccb
->ccb_h
.func_code
= XPT_RESET_BUS
;
6865 work_ccb
->ccb_h
.cbfcnp
= NULL
;
6866 CAM_DEBUG(path
, CAM_DEBUG_SUBTRACE
,
6867 ("Resetting Bus\n"));
6868 xpt_action(work_ccb
);
6869 xpt_finishconfig(xpt_periph
, work_ccb
);
6871 /* Act as though we performed a successful BUS RESET */
6872 work_ccb
->ccb_h
.func_code
= XPT_RESET_BUS
;
6873 xpt_finishconfig(xpt_periph
, work_ccb
);
6881 xpt_config(void *arg
)
6884 * Now that interrupts are enabled, go find our devices
6888 /* Setup debugging flags and path */
6889 #ifdef CAM_DEBUG_FLAGS
6890 cam_dflags
= CAM_DEBUG_FLAGS
;
6891 #else /* !CAM_DEBUG_FLAGS */
6892 cam_dflags
= CAM_DEBUG_NONE
;
6893 #endif /* CAM_DEBUG_FLAGS */
6894 #ifdef CAM_DEBUG_BUS
6895 if (cam_dflags
!= CAM_DEBUG_NONE
) {
6897 * Locking is specifically omitted here. No SIMs have
6898 * registered yet, so xpt_create_path will only be searching
6899 * empty lists of targets and devices.
6901 if (xpt_create_path(&cam_dpath
, xpt_periph
,
6902 CAM_DEBUG_BUS
, CAM_DEBUG_TARGET
,
6903 CAM_DEBUG_LUN
) != CAM_REQ_CMP
) {
6904 kprintf("xpt_config: xpt_create_path() failed for debug"
6905 " target %d:%d:%d, debugging disabled\n",
6906 CAM_DEBUG_BUS
, CAM_DEBUG_TARGET
, CAM_DEBUG_LUN
);
6907 cam_dflags
= CAM_DEBUG_NONE
;
6911 #else /* !CAM_DEBUG_BUS */
6913 #endif /* CAM_DEBUG_BUS */
6914 #endif /* CAMDEBUG */
6917 * Scan all installed busses.
6919 xpt_for_all_busses(xptconfigbuscountfunc
, NULL
);
6921 if (busses_to_config
== 0) {
6922 /* Call manually because we don't have any busses */
6923 xpt_finishconfig(xpt_periph
, NULL
);
6925 if (busses_to_reset
> 0 && scsi_delay
>= 2000) {
6926 kprintf("Waiting %d seconds for SCSI "
6927 "devices to settle\n", scsi_delay
/1000);
6929 xpt_for_all_busses(xptconfigfunc
, NULL
);
6934 * If the given device only has one peripheral attached to it, and if that
6935 * peripheral is the passthrough driver, announce it. This insures that the
6936 * user sees some sort of announcement for every peripheral in their system.
6939 xptpassannouncefunc(struct cam_ed
*device
, void *arg
)
6941 struct cam_periph
*periph
;
6944 for (periph
= SLIST_FIRST(&device
->periphs
), i
= 0; periph
!= NULL
;
6945 periph
= SLIST_NEXT(periph
, periph_links
), i
++);
6947 periph
= SLIST_FIRST(&device
->periphs
);
6949 && (strncmp(periph
->periph_name
, "pass", 4) == 0))
6950 xpt_announce_periph(periph
, NULL
);
6956 xpt_finishconfig_task(void *context
, int pending
)
6958 struct periph_driver
**p_drv
;
6961 if (busses_to_config
== 0) {
6962 /* Register all the peripheral drivers */
6963 /* XXX This will have to change when we have loadable modules */
6964 p_drv
= periph_drivers
;
6965 for (i
= 0; p_drv
[i
] != NULL
; i
++) {
6966 (*p_drv
[i
]->init
)();
6970 * Check for devices with no "standard" peripheral driver
6971 * attached. For any devices like that, announce the
6972 * passthrough driver so the user will see something.
6974 xpt_for_all_devices(xptpassannouncefunc
, NULL
);
6976 /* Release our hook so that the boot can continue. */
6977 config_intrhook_disestablish(xsoftc
.xpt_config_hook
);
6978 kfree(xsoftc
.xpt_config_hook
, M_CAMXPT
);
6979 xsoftc
.xpt_config_hook
= NULL
;
6982 kfree(context
, M_CAMXPT
);
6986 xpt_finishconfig(struct cam_periph
*periph
, union ccb
*done_ccb
)
6988 struct xpt_task
*task
;
6990 if (done_ccb
!= NULL
) {
6991 CAM_DEBUG(done_ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
,
6992 ("xpt_finishconfig\n"));
6993 switch(done_ccb
->ccb_h
.func_code
) {
6995 if (done_ccb
->ccb_h
.status
== CAM_REQ_CMP
) {
6996 done_ccb
->ccb_h
.func_code
= XPT_SCAN_BUS
;
6997 done_ccb
->ccb_h
.cbfcnp
= xpt_finishconfig
;
6998 done_ccb
->crcn
.flags
= 0;
6999 xpt_action(done_ccb
);
7005 xpt_free_path(done_ccb
->ccb_h
.path
);
7011 if (busses_to_config
== 0) {
7012 task
= kmalloc(sizeof(struct xpt_task
), M_CAMXPT
, M_INTWAIT
);
7013 TASK_INIT(&task
->task
, 0, xpt_finishconfig_task
, task
);
7014 taskqueue_enqueue(taskqueue_thread
[mycpuid
], &task
->task
);
7017 if (done_ccb
!= NULL
)
7018 xpt_free_ccb(done_ccb
);
7022 xpt_register_async(int event
, ac_callback_t
*cbfunc
, void *cbarg
,
7023 struct cam_path
*path
)
7025 struct ccb_setasync csa
;
7030 lockmgr(&xsoftc
.xpt_lock
, LK_EXCLUSIVE
);
7031 status
= xpt_create_path(&path
, /*periph*/NULL
, CAM_XPT_PATH_ID
,
7032 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
);
7033 if (status
!= CAM_REQ_CMP
) {
7034 lockmgr(&xsoftc
.xpt_lock
, LK_RELEASE
);
7040 xpt_setup_ccb(&csa
.ccb_h
, path
, /*priority*/5);
7041 csa
.ccb_h
.func_code
= XPT_SASYNC_CB
;
7042 csa
.event_enable
= event
;
7043 csa
.callback
= cbfunc
;
7044 csa
.callback_arg
= cbarg
;
7045 xpt_action((union ccb
*)&csa
);
7046 status
= csa
.ccb_h
.status
;
7048 xpt_free_path(path
);
7049 lockmgr(&xsoftc
.xpt_lock
, LK_RELEASE
);
7055 xptaction(struct cam_sim
*sim
, union ccb
*work_ccb
)
7057 CAM_DEBUG(work_ccb
->ccb_h
.path
, CAM_DEBUG_TRACE
, ("xptaction\n"));
7059 switch (work_ccb
->ccb_h
.func_code
) {
7060 /* Common cases first */
7061 case XPT_PATH_INQ
: /* Path routing inquiry */
7063 struct ccb_pathinq
*cpi
;
7065 cpi
= &work_ccb
->cpi
;
7066 cpi
->version_num
= 1; /* XXX??? */
7067 cpi
->hba_inquiry
= 0;
7068 cpi
->target_sprt
= 0;
7070 cpi
->hba_eng_cnt
= 0;
7071 cpi
->max_target
= 0;
7073 cpi
->initiator_id
= 0;
7074 strncpy(cpi
->sim_vid
, "FreeBSD", SIM_IDLEN
);
7075 strncpy(cpi
->hba_vid
, "", HBA_IDLEN
);
7076 strncpy(cpi
->dev_name
, sim
->sim_name
, DEV_IDLEN
);
7077 cpi
->unit_number
= sim
->unit_number
;
7078 cpi
->bus_id
= sim
->bus_id
;
7079 cpi
->base_transfer_speed
= 0;
7080 cpi
->protocol
= PROTO_UNSPECIFIED
;
7081 cpi
->protocol_version
= PROTO_VERSION_UNSPECIFIED
;
7082 cpi
->transport
= XPORT_UNSPECIFIED
;
7083 cpi
->transport_version
= XPORT_VERSION_UNSPECIFIED
;
7084 cpi
->ccb_h
.status
= CAM_REQ_CMP
;
7089 work_ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
7096 * The xpt as a "controller" has no interrupt sources, so polling
7100 xptpoll(struct cam_sim
*sim
)
7105 xpt_lock_buses(void)
7107 lockmgr(&xsoftc
.xpt_topo_lock
, LK_EXCLUSIVE
);
7111 xpt_unlock_buses(void)
7113 lockmgr(&xsoftc
.xpt_topo_lock
, LK_RELEASE
);
7118 * Should only be called by the machine interrupt dispatch routines,
7119 * so put these prototypes here instead of in the header.
7123 swi_cambio(void *arg
, void *frame
)
7132 struct cam_sim
*sim
;
7134 spin_lock_wr(&cam_simq_spin
);
7136 TAILQ_CONCAT(&queue
, &cam_simq
, links
);
7137 spin_unlock_wr(&cam_simq_spin
);
7139 while ((sim
= TAILQ_FIRST(&queue
)) != NULL
) {
7140 TAILQ_REMOVE(&queue
, sim
, links
);
7142 sim
->flags
&= ~CAM_SIM_ON_DONEQ
;
7143 camisr_runqueue(sim
);
7144 CAM_SIM_UNLOCK(sim
);
7149 camisr_runqueue(struct cam_sim
*sim
)
7151 struct ccb_hdr
*ccb_h
;
7154 spin_lock_wr(&sim
->sim_spin
);
7155 while ((ccb_h
= TAILQ_FIRST(&sim
->sim_doneq
)) != NULL
) {
7156 TAILQ_REMOVE(&sim
->sim_doneq
, ccb_h
, sim_links
.tqe
);
7157 spin_unlock_wr(&sim
->sim_spin
);
7158 ccb_h
->pinfo
.index
= CAM_UNQUEUED_INDEX
;
7160 CAM_DEBUG(ccb_h
->path
, CAM_DEBUG_TRACE
,
7165 if (ccb_h
->flags
& CAM_HIGH_POWER
) {
7166 struct highpowerlist
*hphead
;
7167 struct cam_ed
*device
;
7168 union ccb
*send_ccb
;
7170 lockmgr(&xsoftc
.xpt_lock
, LK_EXCLUSIVE
);
7171 hphead
= &xsoftc
.highpowerq
;
7173 send_ccb
= (union ccb
*)STAILQ_FIRST(hphead
);
7176 * Increment the count since this command is done.
7178 xsoftc
.num_highpower
++;
7181 * Any high powered commands queued up?
7183 if (send_ccb
!= NULL
) {
7184 device
= send_ccb
->ccb_h
.path
->device
;
7186 STAILQ_REMOVE_HEAD(hphead
, xpt_links
.stqe
);
7187 lockmgr(&xsoftc
.xpt_lock
, LK_RELEASE
);
7189 xpt_release_devq(send_ccb
->ccb_h
.path
,
7190 /*count*/1, /*runqueue*/TRUE
);
7192 lockmgr(&xsoftc
.xpt_lock
, LK_RELEASE
);
7195 if ((ccb_h
->func_code
& XPT_FC_USER_CCB
) == 0) {
7198 dev
= ccb_h
->path
->device
;
7200 cam_ccbq_ccb_done(&dev
->ccbq
, (union ccb
*)ccb_h
);
7202 if (!SIM_DEAD(ccb_h
->path
->bus
->sim
)) {
7203 ccb_h
->path
->bus
->sim
->devq
->send_active
--;
7204 ccb_h
->path
->bus
->sim
->devq
->send_openings
++;
7207 if (((dev
->flags
& CAM_DEV_REL_ON_COMPLETE
) != 0
7208 && (ccb_h
->status
&CAM_STATUS_MASK
) != CAM_REQUEUE_REQ
)
7209 || ((dev
->flags
& CAM_DEV_REL_ON_QUEUE_EMPTY
) != 0
7210 && (dev
->ccbq
.dev_active
== 0))) {
7212 xpt_release_devq(ccb_h
->path
, /*count*/1,
7216 if ((dev
->flags
& CAM_DEV_TAG_AFTER_COUNT
) != 0
7217 && (--dev
->tag_delay_count
== 0))
7218 xpt_start_tags(ccb_h
->path
);
7220 if ((dev
->ccbq
.queue
.entries
> 0)
7221 && (dev
->qfrozen_cnt
== 0)
7222 && (device_is_send_queued(dev
) == 0)) {
7223 runq
= xpt_schedule_dev_sendq(ccb_h
->path
->bus
,
7228 if (ccb_h
->status
& CAM_RELEASE_SIMQ
) {
7229 xpt_release_simq(ccb_h
->path
->bus
->sim
,
7231 ccb_h
->status
&= ~CAM_RELEASE_SIMQ
;
7235 if ((ccb_h
->flags
& CAM_DEV_QFRZDIS
)
7236 && (ccb_h
->status
& CAM_DEV_QFRZN
)) {
7237 xpt_release_devq(ccb_h
->path
, /*count*/1,
7239 ccb_h
->status
&= ~CAM_DEV_QFRZN
;
7241 xpt_run_dev_sendq(ccb_h
->path
->bus
);
7244 /* Call the peripheral driver's callback */
7245 (*ccb_h
->cbfcnp
)(ccb_h
->path
->periph
, (union ccb
*)ccb_h
);
7246 spin_lock_wr(&sim
->sim_spin
);
7248 spin_unlock_wr(&sim
->sim_spin
);
7252 dead_sim_action(struct cam_sim
*sim
, union ccb
*ccb
)
7255 ccb
->ccb_h
.status
= CAM_DEV_NOT_THERE
;
7260 dead_sim_poll(struct cam_sim
*sim
)