168ffc204787d037c409400fe0196c02f58f8fc5
[dragonfly.git] / sys / bus / cam / cam_xpt.c
blob168ffc204787d037c409400fe0196c02f58f8fc5
1 /*
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.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
27 * SUCH DAMAGE.
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>
37 #include <sys/time.h>
38 #include <sys/conf.h>
39 #include <sys/device.h>
40 #include <sys/fcntl.h>
41 #include <sys/md5.h>
42 #include <sys/devicestat.h>
43 #include <sys/interrupt.h>
44 #include <sys/sbuf.h>
45 #include <sys/taskqueue.h>
46 #include <sys/bus.h>
47 #include <sys/thread.h>
48 #include <sys/lock.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>
56 #include "cam.h"
57 #include "cam_ccb.h"
58 #include "cam_periph.h"
59 #include "cam_sim.h"
60 #include "cam_xpt.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>
69 #include "opt_cam.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 */
75 struct xpt_task {
76 struct task task;
77 void *data1;
78 uintptr_t data2;
82 * Definition of an async handler callback block. These are used to add
83 * SIMs and peripherals to the async callback lists.
85 struct async_node {
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);
90 void *callback_arg;
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
102 #endif
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 {
110 cam_pinfo pinfo;
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.
119 struct cam_ed {
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;
124 struct cam_sim *sim;
125 lun_id_t lun_id;
126 struct camq drvq; /*
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 */
137 cam_proto protocol;
138 u_int protocol_version;
139 cam_xport transport;
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;
152 u_int32_t flags;
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;
165 u_int32_t refcount;
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.
175 struct cam_et {
176 TAILQ_HEAD(, cam_ed) ed_entries;
177 TAILQ_ENTRY(cam_et) links;
178 struct cam_eb *bus;
179 target_id_t target_id;
180 u_int32_t refcount;
181 u_int generation;
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.
190 struct cam_eb {
191 TAILQ_HEAD(, cam_et) et_entries;
192 TAILQ_ENTRY(cam_eb) links;
193 path_id_t path_id;
194 struct cam_sim *sim;
195 struct timeval last_reset; /* uptime of last reset */
196 u_int32_t flags;
197 #define CAM_EB_RUNQ_SCHEDULED 0x01
198 u_int32_t refcount;
199 u_int generation;
202 struct cam_path {
203 struct cam_periph *periph;
204 struct cam_eb *bus;
205 struct cam_et *target;
206 struct cam_ed *device;
209 struct xpt_quirk_entry {
210 struct scsi_inquiry_pattern inq_pat;
211 u_int8_t quirks;
212 #define CAM_QUIRK_NOLUNS 0x01
213 #define CAM_QUIRK_NOSERIAL 0x02
214 #define CAM_QUIRK_HILUNS 0x04
215 #define CAM_QUIRK_NOHILUNS 0x08
216 u_int mintags;
217 u_int maxtags;
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)))
245 typedef enum {
246 XPT_FLAG_OPEN = 0x01
247 } xpt_flags;
249 struct xpt_softc {
250 xpt_flags flags;
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;
255 int num_highpower;
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
321 * with the problem.
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.
384 * PR: kern/10398
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"
391 * firmware revision.
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
397 * DCAS drives.
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.
420 * Submitted by:
421 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
422 * in PR kern/9535
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
430 * 8MB/sec.)
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
441 * 8MB/sec.)
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", "*", "*" },
466 CAM_QUIRK_NOLUNS,
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",
495 "EXB-8200*", "*"
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",
506 "IPL-6860*", "*"
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.
516 * PR: misc/18793
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.
526 * PR: 23536
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",
547 " TDC 3600", "U07:"
549 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
553 * Would repond to all LUNs if asked for.
556 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
557 "CP150", "*"
559 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
563 * Would repond to all LUNs if asked for.
566 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
567 "96X2*", "*"
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",
597 "Tahiti 1", "*"
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);
623 typedef enum {
624 DM_RET_COPY = 0x01,
625 DM_RET_FLAG_MASK = 0x0f,
626 DM_RET_NONE = 0x00,
627 DM_RET_STOP = 0x10,
628 DM_RET_DESCEND = 0x20,
629 DM_RET_ERROR = 0x30,
630 DM_RET_ACTION_MASK = 0xf0
631 } dev_match_ret;
633 typedef enum {
634 XPT_DEPTH_BUS,
635 XPT_DEPTH_TARGET,
636 XPT_DEPTH_DEVICE,
637 XPT_DEPTH_PERIPH
638 } xpt_traverse_depth;
640 struct xpt_traverse_config {
641 xpt_traverse_depth depth;
642 void *tr_func;
643 void *tr_arg;
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 },
690 .d_open = xptopen,
691 .d_close = xptclose,
692 .d_ioctl = xptioctl
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 */
708 #ifdef CAMDEBUG
709 struct cam_path *cam_dpath;
710 u_int32_t cam_dflags;
711 u_int32_t cam_debug_delay;
712 #endif
714 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
715 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
716 #endif
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)
725 #ifdef CAMDEBUG
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 \
729 and CAM_DEBUG_LUN"
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 = {
740 "cam",
741 cam_module_event_handler,
742 NULL
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,
753 path_id_t path_id,
754 target_id_t target_id,
755 lun_id_t lun_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,
762 void *async_arg);
763 static void xpt_dev_async(u_int32_t async_code,
764 struct cam_eb *bus,
765 struct cam_et *target,
766 struct cam_ed *device,
767 void *async_arg);
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,
778 int run_queue);
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,
784 lun_id_t lun_id);
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,
797 union ccb *ccb);
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,
812 u_int num_patterns,
813 struct cam_ed *device);
814 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
815 u_int num_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,
841 void *arg);
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,
848 void *arg);
849 static xpt_devicefunc_t xptsetasyncfunc;
850 static xpt_busfunc_t xptsetasyncbusfunc;
851 static cam_status xptregister(struct cam_periph *periph,
852 void *arg);
853 static cam_status proberegister(struct cam_periph *periph,
854 void *arg);
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,
866 int async_update);
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,
870 struct cam_ed *dev);
871 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
872 struct cam_ed *dev);
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);
878 static __inline int
879 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
881 int retval;
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
893 * enqueued.
895 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
896 &dev->alloc_ccb_entry.pinfo,
897 CAMQ_GET_HEAD(&dev->drvq)->priority);
898 } else {
899 retval = 0;
902 return (retval);
905 static __inline int
906 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
908 int retval;
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
914 * enqueued.
916 retval =
917 xpt_schedule_dev(&bus->sim->devq->send_queue,
918 &dev->send_ccb_entry.pinfo,
919 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
920 } else {
921 retval = 0;
923 return (retval);
926 static __inline int
927 periph_is_queued(struct cam_periph *periph)
929 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
932 static __inline int
933 device_is_alloc_queued(struct cam_ed *device)
935 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
938 static __inline int
939 device_is_send_queued(struct cam_ed *device)
941 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
944 static __inline int
945 dev_allocq_is_runnable(struct cam_devq *devq)
948 * Have work to do.
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));
957 static void
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");
964 static void
965 probe_periph_init(void)
970 static void
971 xptdone(struct cam_periph *periph, union ccb *done_ccb)
973 /* Caller will release the CCB */
974 wakeup(&done_ccb->ccb_h.cbfcnp);
977 static int
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))
986 return(EPERM);
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));
993 return(ENODEV);
996 /* Mark ourselves open */
997 lockmgr(&xsoftc.xpt_lock, LK_EXCLUSIVE);
998 xsoftc.flags |= XPT_FLAG_OPEN;
999 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
1001 return(0);
1004 static int
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);
1013 return(0);
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.
1022 static int
1023 xptioctl(struct dev_ioctl_args *ap)
1025 int error;
1027 error = 0;
1029 switch(ap->a_cmd) {
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: {
1036 union ccb *ccb;
1037 union ccb *inccb;
1038 struct cam_eb *bus;
1040 inccb = (union ccb *)ap->a_data;
1042 bus = xpt_find_bus(inccb->ccb_h.path_id);
1043 if (bus == NULL) {
1044 error = EINVAL;
1045 break;
1048 switch(inccb->ccb_h.func_code) {
1049 case XPT_SCAN_BUS:
1050 case XPT_RESET_BUS:
1051 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1052 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1053 error = EINVAL;
1054 break;
1056 /* FALLTHROUGH */
1057 case XPT_PATH_INQ:
1058 case XPT_ENG_INQ:
1059 case XPT_SCAN_LUN:
1061 ccb = xpt_alloc_ccb();
1063 CAM_SIM_LOCK(bus->sim);
1066 * Create a path using the bus, target, and lun the
1067 * user passed in.
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) !=
1073 CAM_REQ_CMP){
1074 error = EINVAL;
1075 CAM_SIM_UNLOCK(bus->sim);
1076 xpt_free_ccb(ccb);
1077 break;
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);
1087 xpt_free_ccb(ccb);
1088 CAM_SIM_UNLOCK(bus->sim);
1089 break;
1091 case XPT_DEBUG: {
1092 union ccb ccb;
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
1103 * user passed in.
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) !=
1109 CAM_REQ_CMP){
1110 error = EINVAL;
1111 CAM_SIM_UNLOCK(bus->sim);
1112 break;
1114 /* Ensure all of our fields are correct */
1115 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1116 inccb->ccb_h.pinfo.priority);
1117 xpt_merge_ccb(&ccb, inccb);
1118 ccb.ccb_h.cbfcnp = xptdone;
1119 xpt_action(&ccb);
1120 CAM_SIM_UNLOCK(bus->sim);
1121 bcopy(&ccb, inccb, sizeof(union ccb));
1122 xpt_free_path(ccb.ccb_h.path);
1123 break;
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) {
1135 error = EINVAL;
1136 break;
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);
1161 if (error) {
1162 inccb->ccb_h.path = old_path;
1163 break;
1167 * This is an immediate CCB, we can send it on directly.
1169 xpt_action(inccb);
1172 * Map the buffers back into user space.
1174 cam_periph_unmapmem(inccb, &mapinfo);
1176 inccb->ccb_h.path = old_path;
1178 error = 0;
1179 break;
1181 default:
1182 error = ENOTSUP;
1183 break;
1185 xpt_release_bus(bus);
1186 break;
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: {
1203 union ccb *ccb;
1204 struct cam_periph *periph;
1205 struct periph_driver **p_drv;
1206 char *name;
1207 u_int unit;
1208 u_int cur_generation;
1209 int base_periph_found;
1210 int splbreaknum;
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
1220 * large systems.
1222 splbreaknum = 100;
1224 ccb = (union ccb *)ap->a_data;
1226 base_periph_found = 0;
1229 * Sanity check -- make sure we don't get a null peripheral
1230 * driver name.
1232 if (*ccb->cgdl.periph_name == '\0') {
1233 error = EINVAL;
1234 break;
1237 /* Keep the list from changing while we traverse it */
1238 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
1239 ptstartover:
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)
1245 break;
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;
1254 error = ENOENT;
1255 break;
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) {
1268 break;
1269 } else if (--splbreaknum == 0) {
1270 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
1271 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
1272 splbreaknum = 100;
1273 if (cur_generation != xsoftc.xpt_generation)
1274 goto ptstartover;
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;
1284 int i;
1286 base_periph_found = 1;
1287 device = periph->path->device;
1288 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1289 periph != NULL;
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))
1304 ccb->cgdl.status =
1305 CAM_GDEVLIST_MORE_DEVS;
1306 else
1307 ccb->cgdl.status =
1308 CAM_GDEVLIST_LAST_DEVICE;
1309 ccb->cgdl.generation =
1310 device->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;
1323 break;
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;
1345 error = ENOENT;
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
1351 * kernel.
1353 if (base_periph_found == 1) {
1354 kprintf("xptioctl: pass driver is not in the "
1355 "kernel\n");
1356 kprintf("xptioctl: put \"device pass\" in "
1357 "your kernel config file\n");
1360 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
1361 break;
1363 default:
1364 error = ENOTTY;
1365 break;
1368 return(error);
1371 static int
1372 cam_module_event_handler(module_t mod, int what, void *arg)
1374 int error;
1376 switch (what) {
1377 case MOD_LOAD:
1378 if ((error = xpt_init(NULL)) != 0)
1379 return (error);
1380 break;
1381 case MOD_UNLOAD:
1382 return EBUSY;
1383 default:
1384 return EOPNOTSUPP;
1387 return 0;
1390 /* thread to handle bus rescans */
1391 static void
1392 xpt_scanner_thread(void *dummy)
1394 cam_isrq_t queue;
1395 union ccb *ccb;
1396 struct cam_sim *sim;
1398 for (;;) {
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
1403 * processed.
1405 crit_enter();
1406 tsleep_interlock(&xsoftc.ccb_scanq);
1407 xpt_unlock_buses();
1408 tsleep(&xsoftc.ccb_scanq, 0, "ccb_scanq", 0);
1409 xpt_lock_buses();
1410 crit_exit();
1411 TAILQ_INIT(&queue);
1412 TAILQ_CONCAT(&queue, &xsoftc.ccb_scanq, sim_links.tqe);
1413 xpt_unlock_buses();
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;
1419 CAM_SIM_LOCK(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);
1426 xpt_free_ccb(ccb);
1427 CAM_SIM_UNLOCK(sim);
1432 void
1433 xpt_rescan(union ccb *ccb)
1435 struct ccb_hdr *hdr;
1438 * Don't make duplicate entries for the same paths.
1440 xpt_lock_buses();
1441 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
1442 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
1443 xpt_unlock_buses();
1444 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
1445 xpt_free_path(ccb->ccb_h.path);
1446 xpt_free_ccb(ccb);
1447 return;
1450 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
1451 wakeup(&xsoftc.ccb_scanq);
1452 xpt_unlock_buses();
1456 /* Functions accessed by the peripheral drivers */
1457 static int
1458 xpt_init(void *dummy)
1460 struct cam_sim *xpt_sim;
1461 struct cam_path *path;
1462 struct cam_devq *devq;
1463 cam_status status;
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,
1483 xptpoll,
1484 "xpt",
1485 /*softc*/NULL,
1486 /*unit*/0,
1487 /*lock*/&xsoftc.xpt_lock,
1488 /*max_dev_transactions*/0,
1489 /*max_tagged_dev_transactions*/0,
1490 devq);
1491 cam_simq_release(devq);
1492 if (xpt_sim == NULL)
1493 return (ENOMEM);
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);
1501 return (EINVAL);
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);
1514 return (EINVAL);
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);
1544 return (0);
1547 static cam_status
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);
1565 int32_t
1566 xpt_add_periph(struct cam_periph *periph)
1568 struct cam_ed *device;
1569 int32_t status;
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);
1598 return (status);
1601 void
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);
1628 void
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;
1634 u_int speed;
1635 u_int freq;
1636 u_int mb;
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) {
1664 return;
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;
1673 freq = 0;
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);
1681 speed = freq;
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;
1701 mb = speed / 1000;
1702 if (mb > 0)
1703 kprintf("%s%d: %d.%03dMB/s transfers",
1704 periph->periph_name, periph->unit_number,
1705 mb, speed % 1000);
1706 else
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;
1714 if (freq != 0) {
1715 kprintf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1716 freq % 1000,
1717 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1718 ? " DT" : "",
1719 spi->sync_offset);
1721 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1722 && spi->bus_width > 0) {
1723 if (freq != 0) {
1724 kprintf(", ");
1725 } else {
1726 kprintf(" (");
1728 kprintf("%dbit)", 8 * (0x01 << spi->bus_width));
1729 } else if (freq != 0) {
1730 kprintf(")");
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);
1750 kprintf("\n");
1753 * We only want to print the caller's announce string if they've
1754 * passed one in..
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,
1763 struct cam_eb *bus)
1765 dev_match_ret retval;
1766 int i;
1768 retval = DM_RET_NONE;
1771 * If we aren't given something to match against, that's an error.
1773 if (bus == NULL)
1774 return(DM_RET_ERROR);
1777 * If there are no match entries, then this bus matches no
1778 * matter what.
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
1791 * EDT elements.
1793 if (patterns[i].type != DEV_MATCH_BUS) {
1794 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1795 retval |= DM_RET_DESCEND;
1796 continue;
1799 cur_pattern = &patterns[i].pattern.bus_pattern;
1802 * If they want to match any bus node, we give them any
1803 * device node.
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
1811 * and return.
1813 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1814 return(retval);
1818 * Not sure why someone would do this...
1820 if (cur_pattern->flags == BUS_MATCH_NONE)
1821 continue;
1823 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1824 && (cur_pattern->path_id != bus->path_id))
1825 continue;
1827 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1828 && (cur_pattern->bus_id != bus->sim->bus_id))
1829 continue;
1831 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1832 && (cur_pattern->unit_number != bus->sim->unit_number))
1833 continue;
1835 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1836 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1837 DEV_IDLEN) != 0))
1838 continue;
1841 * If we get to this point, the user definitely wants
1842 * information on this bus. So tell the caller to copy the
1843 * data out.
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)
1858 return(retval);
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;
1870 return(retval);
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;
1878 int i;
1880 retval = DM_RET_NONE;
1883 * If we aren't given something to match against, that's an error.
1885 if (device == NULL)
1886 return(DM_RET_ERROR);
1889 * If there are no match entries, then this device matches no
1890 * matter what.
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;
1906 continue;
1909 cur_pattern = &patterns[i].pattern.device_pattern;
1912 * If they want to match any device node, we give them any
1913 * device node.
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
1922 * and return.
1924 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1925 return(retval);
1929 * Not sure why someone would do this...
1931 if (cur_pattern->flags == DEV_MATCH_NONE)
1932 continue;
1934 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1935 && (cur_pattern->path_id != device->target->bus->path_id))
1936 continue;
1938 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1939 && (cur_pattern->target_id != device->target->target_id))
1940 continue;
1942 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1943 && (cur_pattern->target_lun != device->lun_id))
1944 continue;
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))
1951 continue;
1954 * If we get to this point, the user definitely wants
1955 * information on this device. So tell the caller to copy
1956 * the data out.
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)
1971 return(retval);
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;
1983 return(retval);
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;
1994 int i;
1997 * If we aren't given something to match against, that's an error.
1999 if (periph == NULL)
2000 return(DM_RET_ERROR);
2003 * If there are no match entries, then this peripheral matches no
2004 * matter what.
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)
2023 continue;
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
2037 * the tree.
2039 return(retval);
2043 * Not sure why someone would do this...
2045 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2046 continue;
2048 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2049 && (cur_pattern->path_id != periph->path->bus->path_id))
2050 continue;
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)))
2060 continue;
2062 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2063 && ((periph->path->device == NULL)
2064 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2065 continue;
2067 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2068 && (cur_pattern->unit_number != periph->unit_number))
2069 continue;
2071 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2072 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2073 DEV_IDLEN) != 0))
2074 continue;
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.
2087 return(retval);
2091 * If we get to this point, the peripheral that was passed in
2092 * doesn't match any of the patterns.
2094 return(retval);
2097 static int
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;
2114 else
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;
2122 return(0);
2126 * If the copy flag is set, copy this bus out.
2128 if (retval & DM_RET_COPY) {
2129 int spaceleft, j;
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;
2148 return(0);
2150 j = cdm->num_matches;
2151 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)
2166 return(1);
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] !=
2177 bus->generation)) {
2178 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2179 return(0);
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));
2189 else
2190 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2193 static int
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;
2213 return(0);
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));
2225 else
2226 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2229 static int
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;
2247 else
2248 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2249 device);
2251 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2252 cdm->status = CAM_DEV_MATCH_ERROR;
2253 return(0);
2257 * If the copy flag is set, copy this device out.
2259 if (retval & DM_RET_COPY) {
2260 int spaceleft, j;
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;
2286 return(0);
2288 j = cdm->num_matches;
2289 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 =
2296 device->lun_id;
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;
2305 else
2306 cdm->matches[j].result.device_result.flags =
2307 DEV_RESULT_NOFLAG;
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)
2315 return(1);
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;
2332 return(0);
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));
2346 else
2347 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2350 static int
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;
2362 return(0);
2366 * If the copy flag is set, copy this peripheral out.
2368 if (retval & DM_RET_COPY) {
2369 int spaceleft, j;
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 |
2384 CAM_DEV_POS_PERIPH;
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;
2399 return(0);
2402 j = cdm->num_matches;
2403 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);
2417 return(1);
2420 static int
2421 xptedtmatch(struct ccb_dev_match *cdm)
2423 int ret;
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;
2435 return(0);
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);
2442 else
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.
2451 if (ret == 1)
2452 cdm->status = CAM_DEV_MATCH_LAST;
2454 return(ret);
2457 static int
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;
2471 return(0);
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));
2481 else
2482 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2485 static int
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;
2497 return(0);
2501 * If the copy flag is set, copy this peripheral out.
2503 if (retval & DM_RET_COPY) {
2504 int spaceleft, j;
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;
2517 pdrv = NULL;
2518 bzero(&cdm->pos, sizeof(cdm->pos));
2519 cdm->pos.position_type =
2520 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2521 CAM_DEV_POS_PERIPH;
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)
2534 break;
2537 if (*pdrv == NULL) {
2538 cdm->status = CAM_DEV_MATCH_ERROR;
2539 return(0);
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;
2552 return(0);
2555 j = cdm->num_matches;
2556 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
2563 * lun.
2565 if (periph->path->target)
2566 cdm->matches[j].result.periph_result.target_id =
2567 periph->path->target->target_id;
2568 else
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;
2574 else
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);
2583 return(1);
2586 static int
2587 xptperiphlistmatch(struct ccb_dev_match *cdm)
2589 int ret;
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);
2608 else
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
2616 * matching entries.
2618 if (ret == 1)
2619 cdm->status = CAM_DEV_MATCH_LAST;
2621 return(ret);
2624 static int
2625 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2627 struct cam_eb *bus, *next_bus;
2628 int retval;
2630 retval = 1;
2632 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
2633 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2634 bus != NULL;
2635 bus = next_bus) {
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);
2642 if (retval == 0)
2643 return(retval);
2644 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
2646 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
2648 return(retval);
2651 static int
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;
2656 int retval;
2658 retval = 1;
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);
2667 if (retval == 0)
2668 return(retval);
2671 return(retval);
2674 static int
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;
2679 int retval;
2681 retval = 1;
2682 for (device = (start_device ? start_device :
2683 TAILQ_FIRST(&target->ed_entries));
2684 device != NULL;
2685 device = next_device) {
2687 next_device = TAILQ_NEXT(device, links);
2689 retval = tr_func(device, arg);
2691 if (retval == 0)
2692 return(retval);
2695 return(retval);
2698 static int
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;
2703 int retval;
2705 retval = 1;
2707 for (periph = (start_periph ? start_periph :
2708 SLIST_FIRST(&device->periphs));
2709 periph != NULL;
2710 periph = next_periph) {
2712 next_periph = SLIST_NEXT(periph, periph_links);
2714 retval = tr_func(periph, arg);
2715 if (retval == 0)
2716 return(retval);
2719 return(retval);
2722 static int
2723 xptpdrvtraverse(struct periph_driver **start_pdrv,
2724 xpt_pdrvfunc_t *tr_func, void *arg)
2726 struct periph_driver **pdrv;
2727 int retval;
2729 retval = 1;
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);
2743 if (retval == 0)
2744 return(retval);
2747 return(retval);
2750 static int
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;
2756 int retval;
2758 retval = 1;
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);
2767 if (retval == 0)
2768 return(retval);
2770 return(retval);
2773 static int
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));
2786 } else
2787 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2790 static int
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));
2803 } else
2804 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2807 static int
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));
2820 } else
2821 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2824 static int
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.
2845 static int
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.
2860 static int
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));
2872 static int
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)
2888 return (1);
2890 xpt_compile_path(&path,
2891 NULL,
2892 device->target->bus->path_id,
2893 device->target->target_id,
2894 device->lun_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,
2899 AC_FOUND_DEVICE,
2900 &path, &cgd);
2901 xpt_release_path(&path);
2903 return(1);
2906 static int
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,
2916 bus->sim->path_id,
2917 CAM_TARGET_WILDCARD,
2918 CAM_LUN_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,
2923 AC_PATH_REGISTERED,
2924 &path, &cpi);
2925 xpt_release_path(&path);
2927 return(1);
2930 static void
2931 xpt_action_sasync_cb(void *context, int pending)
2933 struct async_node *cur_entry;
2934 struct xpt_task *task;
2935 uint32_t added;
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);
2959 void
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) {
2967 case XPT_SCSI_IO:
2969 struct cam_ed *device;
2970 #ifdef CAMDEBUG
2971 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2972 struct cam_path *path;
2974 path = start_ccb->ccb_h.path;
2975 #endif
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))));
3007 /* FALLTHROUGH */
3009 case XPT_TARGET_IO:
3010 case XPT_CONT_TARGET_IO:
3011 start_ccb->csio.sense_resid = 0;
3012 start_ccb->csio.resid = 0;
3013 /* FALLTHROUGH */
3014 case XPT_RESET_DEV:
3015 case XPT_ENG_EXEC:
3017 struct cam_path *path;
3018 struct cam_sim *sim;
3019 int runq;
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);
3028 break;
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);
3034 else
3035 runq = 0;
3036 if (runq != 0)
3037 xpt_run_dev_sendq(path->bus);
3038 break;
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);
3045 break;
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;
3058 break;
3060 sim = start_ccb->ccb_h.path->bus->sim;
3061 (*(sim->sim_action))(sim, start_ccb);
3062 break;
3064 case XPT_ABORT:
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;
3081 break;
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;
3095 break;
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;
3105 break;
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.
3111 /* FALLTHROUGH */
3113 case XPT_ACCEPT_TARGET_IO:
3114 case XPT_EN_LUN:
3115 case XPT_IMMED_NOTIFY:
3116 case XPT_NOTIFY_ACK:
3117 case XPT_GET_TRAN_SETTINGS:
3118 case XPT_RESET_BUS:
3120 struct cam_sim *sim;
3122 sim = start_ccb->ccb_h.path->bus->sim;
3123 (*(sim->sim_action))(sim, start_ccb);
3124 break;
3126 case XPT_PATH_INQ:
3128 struct cam_sim *sim;
3130 sim = start_ccb->ccb_h.path->bus->sim;
3131 (*(sim->sim_action))(sim, start_ccb);
3132 break;
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;
3138 break;
3139 case XPT_GDEV_TYPE:
3141 struct cam_ed *dev;
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;
3146 } else {
3147 struct ccb_getdev *cgd;
3148 struct cam_eb *bus;
3149 struct cam_et *tar;
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);
3162 break;
3164 case XPT_GDEV_STATS:
3166 struct cam_ed *dev;
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;
3171 } else {
3172 struct ccb_getdevstats *cgds;
3173 struct cam_eb *bus;
3174 struct cam_et *tar;
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;
3191 break;
3193 case XPT_GDEVLIST:
3195 struct cam_periph *nperiph;
3196 struct periph_list *periph_head;
3197 struct ccb_getdevlist *cgdl;
3198 u_int i;
3199 struct cam_ed *device;
3200 int found;
3203 found = 0;
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;
3221 break;
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,
3234 DEV_IDLEN);
3235 cgdl->unit_number = nperiph->unit_number;
3236 found = 1;
3239 if (found == 0) {
3240 cgdl->status = CAM_GDEVLIST_ERROR;
3241 break;
3244 if (nperiph == NULL)
3245 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3246 else
3247 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3249 cgdl->index++;
3250 cgdl->generation = device->generation;
3252 cgdl->ccb_h.status = CAM_REQ_CMP;
3253 break;
3255 case XPT_DEV_MATCH:
3257 dev_pos_type position_type;
3258 struct ccb_dev_match *cdm;
3259 int ret;
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;
3279 else {
3280 u_int i;
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;
3288 break;
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);
3301 break;
3302 case CAM_DEV_POS_PDRV:
3303 ret = xptperiphlistmatch(cdm);
3304 break;
3305 default:
3306 cdm->status = CAM_DEV_MATCH_ERROR;
3307 break;
3310 if (cdm->status == CAM_DEV_MATCH_ERROR)
3311 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3312 else
3313 start_ccb->ccb_h.status = CAM_REQ_CMP;
3315 break;
3317 case XPT_SASYNC_CB:
3319 struct ccb_setasync *csa;
3320 struct async_node *cur_entry;
3321 struct async_list *async_head;
3322 u_int32_t added;
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
3330 * update it.
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))
3336 break;
3337 cur_entry = SLIST_NEXT(cur_entry, links);
3340 if (cur_entry != NULL) {
3342 * If the request has no flags set,
3343 * remove the entry.
3345 added &= ~cur_entry->event_enable;
3346 if (csa->event_enable == 0) {
3347 SLIST_REMOVE(async_head, cur_entry,
3348 async_node, links);
3349 csa->ccb_h.path->device->refcount--;
3350 kfree(cur_entry, M_CAMXPT);
3351 } else {
3352 cur_entry->event_enable = csa->event_enable;
3354 } else {
3355 cur_entry = kmalloc(sizeof(*cur_entry), M_CAMXPT,
3356 M_INTWAIT);
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,
3372 M_INTWAIT);
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],
3378 &task->task);
3381 start_ccb->ccb_h.status = CAM_REQ_CMP;
3382 break;
3384 case XPT_REL_SIMQ:
3386 struct ccb_relsim *crs;
3387 struct cam_ed *dev;
3389 crs = &start_ccb->crs;
3390 dev = crs->ccb_h.path->device;
3391 if (dev == NULL) {
3393 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3394 break;
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,
3403 crs->openings);
3405 if (bootverbose) {
3406 xpt_print(crs->ccb_h.path,
3407 "tagged openings now %d\n",
3408 crs->openings);
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);
3425 } else {
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
3444 * the queue.
3446 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3447 } else {
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;
3460 } else {
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,
3470 /*run_queue*/TRUE);
3472 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3473 start_ccb->ccb_h.status = CAM_REQ_CMP;
3474 break;
3476 case XPT_SCAN_BUS:
3477 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3478 break;
3479 case XPT_SCAN_LUN:
3480 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3481 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3482 start_ccb);
3483 break;
3484 case XPT_DEBUG: {
3485 #ifdef CAMDEBUG
3486 #ifdef CAM_DEBUG_DELAY
3487 cam_debug_delay = CAM_DEBUG_DELAY;
3488 #endif
3489 cam_dflags = start_ccb->cdbg.flags;
3490 if (cam_dpath != NULL) {
3491 xpt_free_path(cam_dpath);
3492 cam_dpath = NULL;
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) !=
3500 CAM_REQ_CMP) {
3501 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3502 cam_dflags = CAM_DEBUG_NONE;
3503 } else {
3504 start_ccb->ccb_h.status = CAM_REQ_CMP;
3505 xpt_print(cam_dpath, "debugging flags now %x\n",
3506 cam_dflags);
3508 } else {
3509 cam_dpath = NULL;
3510 start_ccb->ccb_h.status = CAM_REQ_CMP;
3512 #else /* !CAMDEBUG */
3513 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3514 #endif /* CAMDEBUG */
3515 break;
3517 case XPT_NOOP:
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;
3521 break;
3522 default:
3523 case XPT_SDEV_TYPE:
3524 case XPT_TERM_IO:
3525 case XPT_ENG_INQ:
3526 /* XXX Implement */
3527 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3528 break;
3532 void
3533 xpt_polled_action(union ccb *start_ccb)
3535 u_int32_t timeout;
3536 struct cam_sim *sim;
3537 struct cam_devq *devq;
3538 struct cam_ed *dev;
3540 timeout = start_ccb->ccb_h.timeout;
3541 sim = start_ccb->ccb_h.path->bus->sim;
3542 devq = sim->devq;
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)) {
3556 DELAY(1000);
3557 (*(sim->sim_poll))(sim);
3558 camisr_runqueue(sim);
3561 dev->ccbq.devq_openings++;
3562 dev->ccbq.dev_openings++;
3564 if (timeout != 0) {
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)
3570 != CAM_REQ_INPROG)
3571 break;
3572 DELAY(1000);
3574 if (timeout == 0) {
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
3579 * it is.
3581 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3583 } else {
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.
3592 void
3593 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3595 struct cam_ed *device;
3596 union ccb *work_ccb;
3597 int runq;
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,
3609 perph->pinfo.index,
3610 new_priority);
3612 runq = 0;
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)
3617 return; /* XXX */
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);
3621 return;
3622 } else {
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);
3631 if (runq != 0) {
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
3645 * to run the queue.
3647 static int
3648 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3649 u_int32_t new_priority)
3651 int retval;
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,
3665 new_priority);
3666 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3667 ("changed priority to %d\n",
3668 new_priority));
3670 retval = 0;
3671 } else {
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);
3680 retval = 1;
3682 return (retval);
3685 static void
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"));
3692 return;
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;
3712 struct camq *drvq;
3714 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3715 CAMQ_HEAD);
3716 device = qinfo->device;
3718 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3719 ("running device %p\n", device));
3721 drvq = &device->drvq;
3723 #ifdef CAMDEBUG
3724 if (drvq->entries <= 0) {
3725 panic("xpt_run_dev_allocq: "
3726 "Device on queue without any work to do");
3728 #endif
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);
3738 } else {
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
3746 * again.
3748 break;
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--;
3759 static void
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"));
3766 return;
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) {
3779 break;
3782 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3783 CAMQ_HEAD);
3784 device = qinfo->device;
3787 * If the device has been "frozen", don't attempt
3788 * to run it.
3790 if (device->qfrozen_cnt > 0) {
3791 continue;
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");
3800 continue;
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
3811 * available.
3813 device->qfrozen_cnt++;
3814 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3815 &work_ccb->ccb_h,
3816 xpt_links.stqe);
3818 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
3819 continue;
3820 } else {
3822 * Consume a high power slot while
3823 * this ccb runs.
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;
3853 else
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.
3879 void
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));
3894 void
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;
3900 ccb_h->path = path;
3901 ccb_h->path_id = path->bus->path_id;
3902 if (path->target)
3903 ccb_h->target_id = path->target->target_id;
3904 else
3905 ccb_h->target_id = CAM_TARGET_WILDCARD;
3906 if (path->device) {
3907 ccb_h->target_lun = path->device->lun_id;
3908 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3909 } else {
3910 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3912 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3913 ccb_h->flags = 0;
3916 /* Path manipulation functions */
3917 cam_status
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;
3922 cam_status status;
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);
3928 path = NULL;
3930 *new_path_ptr = path;
3931 return (status);
3934 cam_status
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;
3941 cam_status status;
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);
3948 if (bus != NULL) {
3949 need_unlock = 1;
3950 CAM_SIM_LOCK(bus->sim);
3953 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3954 if (need_unlock)
3955 CAM_SIM_UNLOCK(bus->sim);
3956 if (status != CAM_REQ_CMP) {
3957 kfree(path, M_CAMXPT);
3958 path = NULL;
3960 *new_path_ptr = path;
3961 return (status);
3964 static cam_status
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)
3968 struct cam_eb *bus;
3969 struct cam_et *target;
3970 struct cam_ed *device;
3971 cam_status status;
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);
3982 if (bus == NULL) {
3983 status = CAM_PATH_INVALID;
3984 } else {
3985 target = xpt_find_target(bus, target_id);
3986 if (target == NULL) {
3987 /* Create one */
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;
3993 } else {
3994 target = new_target;
3997 if (target != NULL) {
3998 device = xpt_find_device(target, lun_id);
3999 if (device == NULL) {
4000 /* Create one */
4001 struct cam_ed *new_device;
4003 new_device = xpt_alloc_device(bus,
4004 target,
4005 lun_id);
4006 if (new_device == NULL) {
4007 status = CAM_RESRC_UNAVAIL;
4008 } else {
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"));
4024 } else {
4025 if (device != NULL)
4026 xpt_release_device(bus, target, device);
4027 if (target != NULL)
4028 xpt_release_target(bus, target);
4029 if (bus != NULL)
4030 xpt_release_bus(bus);
4032 return (status);
4035 static void
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);
4049 path->bus = NULL;
4053 void
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)
4069 int retval = 0;
4071 if (path1->bus != path2->bus) {
4072 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4073 retval = 1;
4074 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4075 retval = 2;
4076 else
4077 return (-1);
4079 if (path1->target != path2->target) {
4080 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4081 if (retval == 0)
4082 retval = 1;
4083 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4084 retval = 2;
4085 else
4086 return (-1);
4088 if (path1->device != path2->device) {
4089 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4090 if (retval == 0)
4091 retval = 1;
4092 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4093 retval = 2;
4094 else
4095 return (-1);
4097 return (retval);
4100 void
4101 xpt_print_path(struct cam_path *path)
4104 if (path == NULL)
4105 kprintf("(nopath): ");
4106 else {
4107 if (path->periph != NULL)
4108 kprintf("(%s%d:", path->periph->periph_name,
4109 path->periph->unit_number);
4110 else
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);
4117 else
4118 kprintf("nobus:");
4120 if (path->target != NULL)
4121 kprintf("%d:", path->target->target_id);
4122 else
4123 kprintf("X:");
4125 if (path->device != NULL)
4126 kprintf("%d): ", path->device->lun_id);
4127 else
4128 kprintf("X): ");
4132 void
4133 xpt_print(struct cam_path *path, const char *fmt, ...)
4135 __va_list ap;
4136 xpt_print_path(path);
4137 __va_start(ap, fmt);
4138 kvprintf(fmt, ap);
4139 __va_end(ap);
4143 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4145 struct sbuf sb;
4147 sim_lock_assert_owned(path->bus->sim->lock);
4149 sbuf_new(&sb, str, str_len, 0);
4151 if (path == NULL)
4152 sbuf_printf(&sb, "(nopath): ");
4153 else {
4154 if (path->periph != NULL)
4155 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4156 path->periph->unit_number);
4157 else
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);
4164 else
4165 sbuf_printf(&sb, "nobus:");
4167 if (path->target != NULL)
4168 sbuf_printf(&sb, "%d:", path->target->target_id);
4169 else
4170 sbuf_printf(&sb, "X:");
4172 if (path->device != NULL)
4173 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4174 else
4175 sbuf_printf(&sb, "X): ");
4177 sbuf_finish(&sb);
4179 return(sbuf_len(&sb));
4182 path_id_t
4183 xpt_path_path_id(struct cam_path *path)
4185 sim_lock_assert_owned(path->bus->sim->lock);
4187 return(path->bus->path_id);
4190 target_id_t
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);
4197 else
4198 return (CAM_TARGET_WILDCARD);
4201 lun_id_t
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);
4208 else
4209 return (CAM_LUN_WILDCARD);
4212 struct cam_sim *
4213 xpt_path_sim(struct cam_path *path)
4215 return (path->bus->sim);
4218 struct cam_periph*
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
4230 * call them now.
4232 void
4233 xpt_release_ccb(union ccb *free_ccb)
4235 struct cam_path *path;
4236 struct cam_ed *device;
4237 struct cam_eb *bus;
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;
4243 bus = path->bus;
4244 sim = bus->sim;
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);
4251 sim->ccb_count--;
4252 } else {
4253 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
4254 xpt_links.sle);
4256 if (sim->devq == NULL) {
4257 return;
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.
4281 int32_t
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);
4290 sim->bus_id = bus;
4291 new_bus = kmalloc(sizeof(*new_bus), M_CAMXPT, M_INTWAIT);
4293 if (strcmp(sim->sim_name, "xpt") != 0) {
4294 sim->path_id =
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;
4300 new_bus->sim = sim;
4301 ++sim->refcount;
4302 timevalclear(&new_bus->last_reset);
4303 new_bus->flags = 0;
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);
4313 else
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)
4338 int32_t
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;
4348 cam_status status;
4350 status = xpt_compile_path(&bus_path, NULL, pathid,
4351 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4352 if (status != CAM_REQ_CMP)
4353 return (status);
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) {
4375 do {
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,
4386 CAMQ_HEAD);
4387 if (periph != NULL)
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);
4408 static path_id_t
4409 xptnextfreepathid(void)
4411 struct cam_eb *bus;
4412 path_id_t pathid;
4413 char *strval;
4415 pathid = 0;
4416 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
4417 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4418 retry:
4419 /* Find an unoccupied pathid */
4420 while (bus != NULL && bus->path_id <= pathid) {
4421 if (bus->path_id == pathid)
4422 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) {
4432 ++pathid;
4433 /* Start the search over */
4434 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
4435 goto retry;
4437 return (pathid);
4440 static path_id_t
4441 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4443 path_id_t pathid;
4444 int i, dunit, val;
4445 char buf[32];
4447 pathid = CAM_XPT_PATH_ID;
4448 ksnprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4449 i = -1;
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. */
4453 continue;
4455 dunit = resource_query_unit(i);
4456 if (dunit < 0) /* unwired?! */
4457 continue;
4458 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4459 if (sim_bus == val) {
4460 pathid = dunit;
4461 break;
4463 } else if (sim_bus == 0) {
4464 /* Unspecified matches bus 0 */
4465 pathid = dunit;
4466 break;
4467 } else {
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);
4474 break;
4478 if (pathid == CAM_XPT_PATH_ID)
4479 pathid = xptnextfreepathid();
4480 return (pathid);
4483 void
4484 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4486 struct cam_eb *bus;
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.
4501 bus = path->bus;
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);
4509 target != NULL;
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)
4517 continue;
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);
4525 device != NULL;
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)
4533 continue;
4535 xpt_dev_async(async_code, bus, target,
4536 device, async_arg);
4538 xpt_async_bcast(&device->asyncs, async_code,
4539 path, async_arg);
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,
4549 path, async_arg);
4552 static void
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,
4570 async_code, path,
4571 async_arg);
4572 cur_entry = next_entry;
4577 * Handle any per-device event notifications that require action by the XPT.
4579 static void
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)
4583 cam_status status;
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)
4591 return;
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,
4601 bus->path_id,
4602 target->target_id,
4603 device->lun_id);
4604 else
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);
4650 u_int32_t
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
4670 * an example.
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);
4678 u_int32_t
4679 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4681 sim_lock_assert_owned(sim->lock);
4683 if (sim->devq == NULL)
4684 return(count);
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,
4690 ccb_hdr_tailq);
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
4704 * cam_sim_free()?
4706 static void
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);
4716 void
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);
4724 static void
4725 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4727 int rundevq;
4729 rundevq = 0;
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
4755 * run.
4757 if ((dev->ccbq.queue.entries > 0)
4758 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4759 && (run_queue != 0)) {
4760 rundevq = 1;
4764 if (rundevq != 0)
4765 xpt_run_dev_sendq(dev->target->bus);
4768 void
4769 xpt_release_simq(struct cam_sim *sim, int run_queue)
4771 struct camq *sendq;
4773 sim_lock_assert_owned(sim->lock);
4775 if (sim->devq == NULL)
4776 return;
4778 sendq = &(sim->devq->send_queue);
4779 if (sendq->qfrozen_cnt > 0) {
4780 sendq->qfrozen_cnt--;
4781 if (sendq->qfrozen_cnt == 0) {
4782 struct cam_eb *bus;
4785 * If there is a timeout scheduled to release this
4786 * sim queue, remove it. The queue frozen count is
4787 * already at 0.
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);
4795 if (run_queue) {
4797 * Now that we are unfrozen run the send queue.
4799 xpt_run_dev_sendq(bus);
4801 xpt_release_bus(bus);
4806 void
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,
4822 sim_links.tqe);
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,
4829 links);
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)
4836 setsoftcambio();
4837 break;
4838 default:
4839 panic("unknown periph type %d",
4840 done_ccb->ccb_h.path->periph->type);
4845 union ccb *
4846 xpt_alloc_ccb(void)
4848 union ccb *new_ccb;
4850 new_ccb = kmalloc(sizeof(*new_ccb), M_CAMXPT, M_INTWAIT | M_ZERO);
4851 return (new_ccb);
4854 void
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.
4871 static union ccb *
4872 xpt_get_ccb(struct cam_ed *device)
4874 union ccb *new_ccb;
4875 struct cam_sim *sim;
4877 sim = device->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,
4883 xpt_links.sle);
4884 sim->ccb_count++;
4886 cam_ccbq_take_opening(&device->ccbq);
4887 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4888 return (new_ccb);
4891 static void
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);
4914 target->bus = bus;
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.
4923 bus->refcount++;
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);
4932 } else {
4933 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4935 bus->generation++;
4936 return (target);
4939 static void
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);
4945 bus->generation++;
4946 xpt_release_bus(bus);
4947 KKASSERT(target->refcount == 1);
4948 kfree(target, M_CAMXPT);
4949 } else {
4950 --target->refcount;
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;
4960 cam_status status;
4962 if (SIM_DEAD(bus->sim))
4963 return (NULL);
4965 /* Make space for us in the device queue on our bus */
4966 if (bus->sim->devq == NULL)
4967 return(NULL);
4968 devq = bus->sim->devq;
4969 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4971 if (status != CAM_REQ_CMP) {
4972 device = NULL;
4973 } else {
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);
4990 return (NULL);
4992 if (cam_ccbq_init(&device->ccbq,
4993 bus->sim->max_dev_openings) != 0) {
4994 camq_fini(&device->drvq);
4995 kfree(device, M_CAMXPT);
4996 return (NULL);
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.
5023 target->refcount++;
5026 * XXX should be limited by number of CCBs this bus can
5027 * do.
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);
5036 } else {
5037 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5039 target->generation++;
5040 if (lun_id != CAM_LUN_WILDCARD) {
5041 xpt_compile_path(&path,
5042 NULL,
5043 bus->path_id,
5044 target->target_id,
5045 lun_id);
5046 xpt_devise_transport(&path);
5047 xpt_release_path(&path);
5050 return (device);
5053 static void
5054 xpt_reference_device(struct cam_ed *device)
5056 ++device->refcount;
5059 static void
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);
5090 } else {
5091 --device->refcount;
5095 static u_int32_t
5096 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5098 int diff;
5099 int result;
5100 struct cam_ed *dev;
5102 dev = path->device;
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;
5114 return (result);
5117 static struct cam_eb *
5118 xpt_find_bus(path_id_t path_id)
5120 struct cam_eb *bus;
5122 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
5123 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
5124 if (bus->path_id == path_id) {
5125 bus->refcount++;
5126 break;
5129 lockmgr(&xsoftc.xpt_topo_lock, LK_RELEASE);
5130 return (bus);
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) {
5140 target->refcount++;
5141 break;
5144 return (target);
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) {
5154 device->refcount++;
5155 break;
5158 return (device);
5161 typedef struct {
5162 union ccb *request_ccb;
5163 struct ccb_pathinq *cpi;
5164 int counter;
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.
5172 static void
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) {
5178 case XPT_SCAN_BUS:
5180 xpt_scan_bus_info *scan_info;
5181 union ccb *work_ccb;
5182 struct cam_path *path;
5183 u_int i;
5184 u_int max_target;
5185 u_int initiator_id;
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);
5197 return;
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);
5208 return;
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) {
5226 max_target = 0;
5227 scan_info->counter = 0;
5228 } else {
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++) {
5236 cam_status status;
5237 if (i == initiator_id)
5238 continue;
5240 status = xpt_create_path(&path, xpt_periph,
5241 request_ccb->ccb_h.path_id,
5242 i, 0);
5243 if (status != CAM_REQ_CMP) {
5244 kprintf("xpt_scan_bus: xpt_create_path failed"
5245 " with status %#x, bus scan halted\n",
5246 status);
5247 kfree(scan_info, M_CAMXPT);
5248 request_ccb->ccb_h.status = status;
5249 xpt_free_ccb(work_ccb);
5250 xpt_done(request_ccb);
5251 break;
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);
5262 break;
5264 case XPT_SCAN_LUN:
5266 cam_status status;
5267 struct cam_path *path;
5268 xpt_scan_bus_info *scan_info;
5269 path_id_t path_id;
5270 target_id_t target_id;
5271 lun_id_t lun_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;
5287 int phl;
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
5293 * the next lun.
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.
5303 phl = 0;
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)
5312 lun_id++;
5314 } else {
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))
5323 lun_id++;
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) {
5337 int done;
5339 hop_again:
5340 done = 0;
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) {
5349 done = 1;
5351 } else {
5352 scan_info->counter--;
5353 if (scan_info->counter == 0) {
5354 done = 1;
5357 if (done) {
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);
5364 break;
5367 if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
5368 break;
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",
5376 status);
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);
5383 break;
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;
5392 } else {
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",
5398 status);
5399 goto hop_again;
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);
5410 break;
5412 default:
5413 break;
5417 typedef enum {
5418 PROBE_TUR,
5419 PROBE_INQUIRY, /* this counts as DV0 for Basic Domain Validation */
5420 PROBE_FULL_INQUIRY,
5421 PROBE_MODE_SENSE,
5422 PROBE_SERIAL_NUM_0,
5423 PROBE_SERIAL_NUM_1,
5424 PROBE_TUR_FOR_NEGOTIATION,
5425 PROBE_INQUIRY_BASIC_DV1,
5426 PROBE_INQUIRY_BASIC_DV2,
5427 PROBE_DV_EXIT
5428 } probe_action;
5430 typedef enum {
5431 PROBE_INQUIRY_CKSUM = 0x01,
5432 PROBE_SERIAL_CKSUM = 0x02,
5433 PROBE_NO_ANNOUNCE = 0x04
5434 } probe_flags;
5436 typedef struct {
5437 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5438 probe_action action;
5439 union ccb saved_ccb;
5440 probe_flags flags;
5441 MD5_CTX context;
5442 u_int8_t digest[16];
5443 } probe_softc;
5445 static void
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;
5450 cam_status status;
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);
5466 return;
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);
5478 return;
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,
5485 path->bus->path_id,
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);
5494 return;
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) {
5503 probe_softc *softc;
5505 softc = (probe_softc *)old_periph->softc;
5506 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5507 periph_links.tqe);
5508 } else {
5509 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5510 probestart, "probe",
5511 CAM_PERIPH_BIO,
5512 request_ccb->ccb_h.path, NULL, 0,
5513 request_ccb);
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);
5524 static void
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);
5532 static cam_status
5533 proberegister(struct cam_periph *periph, void *arg)
5535 union ccb *request_ccb; /* CCB representing the probe request */
5536 cam_status status;
5537 probe_softc *softc;
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,
5554 periph_links.tqe);
5555 softc->flags = 0;
5556 periph->softc = softc;
5557 status = cam_periph_acquire(periph);
5558 if (status != CAM_REQ_CMP) {
5559 return (status);
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,
5569 scsi_delay);
5570 probeschedule(periph);
5571 return(CAM_REQ_CMP);
5574 static void
5575 probeschedule(struct cam_periph *periph)
5577 struct ccb_pathinq cpi;
5578 union ccb *ccb;
5579 probe_softc *softc;
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
5598 * invalidated.
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;
5613 } else {
5614 softc->action = PROBE_INQUIRY;
5617 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5618 softc->flags |= PROBE_NO_ANNOUNCE;
5619 else
5620 softc->flags &= ~PROBE_NO_ANNOUNCE;
5622 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5625 static void
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;
5630 probe_softc *softc;
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) {
5638 case PROBE_TUR:
5639 case PROBE_TUR_FOR_NEGOTIATION:
5640 case PROBE_DV_EXIT:
5642 scsi_test_unit_ready(csio,
5643 /*retries*/4,
5644 probedone,
5645 MSG_SIMPLE_Q_TAG,
5646 SSD_FULL_SIZE,
5647 /*timeout*/60000);
5648 break;
5650 case PROBE_INQUIRY:
5651 case PROBE_FULL_INQUIRY:
5652 case PROBE_INQUIRY_BASIC_DV1:
5653 case PROBE_INQUIRY_BASIC_DV2:
5655 u_int inquiry_len;
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;
5685 else
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
5692 * safe.
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);
5700 scsi_inquiry(csio,
5701 /*retries*/4,
5702 probedone,
5703 MSG_SIMPLE_Q_TAG,
5704 (u_int8_t *)inq_buf,
5705 inquiry_len,
5706 /*evpd*/FALSE,
5707 /*page_code*/0,
5708 SSD_MIN_SIZE,
5709 /*timeout*/60 * 1000);
5710 break;
5712 case PROBE_MODE_SENSE:
5714 void *mode_buf;
5715 int mode_buf_len;
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,
5722 /*retries*/4,
5723 probedone,
5724 MSG_SIMPLE_Q_TAG,
5725 /*dbd*/FALSE,
5726 SMS_PAGE_CTRL_CURRENT,
5727 SMS_CONTROL_MODE_PAGE,
5728 mode_buf,
5729 mode_buf_len,
5730 SSD_FULL_SIZE,
5731 /*timeout*/60000);
5732 break;
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) {
5746 scsi_inquiry(csio,
5747 /*retries*/4,
5748 probedone,
5749 MSG_SIMPLE_Q_TAG,
5750 (u_int8_t *)vpd_list,
5751 sizeof(*vpd_list),
5752 /*evpd*/TRUE,
5753 SVPD_SUPPORTED_PAGE_LIST,
5754 SSD_MIN_SIZE,
5755 /*timeout*/60 * 1000);
5756 break;
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);
5764 return;
5766 case PROBE_SERIAL_NUM_1:
5768 struct scsi_vpd_unit_serial_number *serial_buf;
5769 struct cam_ed* device;
5771 serial_buf = NULL;
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);
5779 scsi_inquiry(csio,
5780 /*retries*/4,
5781 probedone,
5782 MSG_SIMPLE_Q_TAG,
5783 (u_int8_t *)serial_buf,
5784 sizeof(*serial_buf),
5785 /*evpd*/TRUE,
5786 SVPD_UNIT_SERIAL_NUMBER,
5787 SSD_MIN_SIZE,
5788 /*timeout*/60 * 1000);
5789 break;
5792 xpt_action(start_ccb);
5795 static void
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) {
5805 return;
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.
5815 static int
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) {
5827 if (bootverbose) {
5828 xpt_print(periph->path,
5829 "failed to get current device settings\n");
5831 return (0);
5833 if (cts.transport != XPORT_SPI) {
5834 if (bootverbose) {
5835 xpt_print(periph->path, "not SPI transport\n");
5837 return (0);
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) {
5845 if (bootverbose) {
5846 xpt_print(periph->path, "no sync rate known\n");
5848 return (0);
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) {
5863 if (bootverbose) {
5864 xpt_print(periph->path, "no sync rate available\n");
5866 return (0);
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"));
5872 return (0);
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
5882 * of speed setting.
5884 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
5885 for (;;) {
5886 spi->sync_period++;
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) {
5906 break;
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) {
5911 return (0);
5914 return (1);
5917 static void
5918 probedone(struct cam_periph *periph, union ccb *done_ccb)
5920 probe_softc *softc;
5921 struct cam_path *path;
5922 u_int32_t priority;
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) {
5931 case PROBE_TUR:
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)
5937 return;
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,
5941 /*count*/1,
5942 /*run_queue*/TRUE);
5944 softc->action = PROBE_INQUIRY;
5945 xpt_release_ccb(done_ccb);
5946 xpt_schedule(periph, priority);
5947 return;
5949 case PROBE_INQUIRY:
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:
5964 u_int8_t len;
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);
5984 return;
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;
5992 else
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);
6000 return;
6002 default:
6003 break;
6005 } else if (cam_periph_error(done_ccb, 0,
6006 done_ccb->ccb_h.target_lun > 0
6007 ? SF_RETRY_UA|SF_QUIET_IR
6008 : SF_RETRY_UA,
6009 &softc->saved_ccb) == ERESTART) {
6010 return;
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,
6014 /*run_queue*/TRUE);
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);
6032 break;
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;
6043 u_int8_t *offset;
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) {
6052 return;
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);
6062 return;
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;
6072 page_list =
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;
6087 break;
6090 } else if (cam_periph_error(done_ccb, 0,
6091 SF_RETRY_UA|SF_NO_PRINT,
6092 &softc->saved_ccb) == ERESTART) {
6093 return;
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,
6097 /*run_queue*/TRUE);
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);
6107 return;
6109 xpt_release_ccb(done_ccb);
6110 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6111 xpt_schedule(periph, done_ccb->ccb_h.pinfo.priority);
6112 return;
6115 case PROBE_SERIAL_NUM_1:
6117 struct ccb_scsiio *csio;
6118 struct scsi_vpd_unit_serial_number *serial_buf;
6119 u_int32_t priority;
6120 int changed;
6121 int have_serialnum;
6123 changed = 1;
6124 have_serialnum = 0;
6125 csio = &done_ccb->csio;
6126 priority = done_ccb->ccb_h.pinfo.priority;
6127 serial_buf =
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)) {
6144 have_serialnum = 1;
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) {
6156 return;
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,
6160 /*run_queue*/TRUE);
6164 * Let's see if we have seen this device before.
6166 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
6167 MD5_CTX context;
6168 u_int8_t digest[16];
6170 MD5Init(&context);
6172 MD5Update(&context,
6173 (unsigned char *)&path->device->inq_data,
6174 sizeof(struct scsi_inquiry_data));
6176 if (have_serialnum)
6177 MD5Update(&context, serial_buf->serial_num,
6178 serial_buf->length);
6180 MD5Final(digest, &context);
6181 if (bcmp(softc->digest, digest, 16) == 0)
6182 changed = 0;
6185 * XXX Do we need to do a TUR in order to ensure
6186 * that the device really hasn't changed???
6188 if ((changed != 0)
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);
6195 if (changed != 0) {
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);
6215 return;
6217 xpt_release_ccb(done_ccb);
6218 break;
6220 case PROBE_TUR_FOR_NEGOTIATION:
6221 case PROBE_DV_EXIT:
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,
6225 /*run_queue*/TRUE);
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);
6243 return;
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,
6256 done_ccb);
6258 xpt_release_ccb(done_ccb);
6259 break;
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,
6269 /*run_queue*/TRUE);
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)) {
6274 xpt_print(path,
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;
6280 } else {
6281 /* give up */
6282 softc->action = PROBE_DV_EXIT;
6284 kfree(nbuf, M_CAMXPT);
6285 xpt_release_ccb(done_ccb);
6286 xpt_schedule(periph, priority);
6287 return;
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);
6294 return;
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,
6307 done_ccb);
6309 xpt_release_ccb(done_ccb);
6310 break;
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;
6316 xpt_done(done_ccb);
6317 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6318 cam_periph_invalidate(periph);
6319 cam_periph_release(periph);
6320 } else {
6321 probeschedule(periph);
6325 static void
6326 probecleanup(struct cam_periph *periph)
6328 kfree(periph->softc, M_CAMXPT);
6331 static void
6332 xpt_find_quirk(struct cam_ed *device)
6334 caddr_t match;
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);
6341 if (match == NULL)
6342 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6344 device->quirk = (struct xpt_quirk_entry *)match;
6347 static int
6348 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
6350 int error, bool;
6352 bool = cam_srch_hi;
6353 error = sysctl_handle_int(oidp, &bool, 0, req);
6354 if (error != 0 || req->newptr == NULL)
6355 return (error);
6356 if (bool == 0 || bool == 1) {
6357 cam_srch_hi = bool;
6358 return (0);
6359 } else {
6360 return (EINVAL);
6364 static void
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);
6376 inq_buf = NULL;
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;
6394 } else {
6395 struct cam_ed* otherdev;
6397 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6398 otherdev != NULL;
6399 otherdev = TAILQ_NEXT(otherdev, links)) {
6400 if (otherdev != path->device)
6401 break;
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;
6413 } else {
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;
6418 else
6419 path->device->transport_version = 0;
6424 * XXX
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
6428 * inquiry buffer.
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);
6444 static void
6445 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6446 int async_update)
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);
6458 return;
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) {
6478 if (bootverbose) {
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) {
6503 if (bootverbose) {
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
6516 * SCSI protocol.
6518 if (cts->protocol != PROTO_SCSI) {
6519 if (async_update == FALSE)
6520 (*(sim->sim_action))(sim, (union ccb *)cts);
6521 return;
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) {
6552 return;
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) {
6565 u_int spi3caps;
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)
6589 spi->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)) {
6602 /* Force async */
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)
6613 break;
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;
6621 break;
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;
6628 break;
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) {
6674 int device_tagenb;
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;
6687 else
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
6701 * in the way.
6703 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6704 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6705 } else {
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,
6716 /*priority*/1);
6717 crs.ccb_h.func_code = XPT_REL_SIMQ;
6718 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6719 crs.openings
6720 = crs.release_timeout
6721 = crs.qfrozen_cnt
6722 = 0;
6723 xpt_action((union ccb *)&crs);
6727 if (async_update == FALSE)
6728 (*(sim->sim_action))(sim, (union ccb *)cts);
6731 static void
6732 xpt_toggle_tags(struct cam_path *path)
6734 struct cam_ed *dev;
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.
6743 dev = path->device;
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);
6764 static void
6765 xpt_start_tags(struct cam_path *path)
6767 struct ccb_relsim crs;
6768 struct cam_ed *device;
6769 struct cam_sim *sim;
6770 int newopenings;
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;
6779 else
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;
6786 crs.openings
6787 = crs.release_timeout
6788 = crs.qfrozen_cnt
6789 = 0;
6790 xpt_action((union ccb *)&crs);
6793 static int busses_to_config;
6794 static int busses_to_reset;
6796 static int
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;
6805 int can_negotiate;
6807 busses_to_config++;
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
6816 && can_negotiate)
6817 busses_to_reset++;
6818 xpt_release_path(&path);
6821 return(1);
6824 static int
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) {
6833 cam_status status;
6834 int can_negotiate;
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);
6844 busses_to_config--;
6845 xpt_finishconfig(xpt_periph, NULL);
6846 return(0);
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);
6856 return(1);
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);
6870 } else {
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);
6877 return(1);
6880 static void
6881 xpt_config(void *arg)
6884 * Now that interrupts are enabled, go find our devices
6887 #ifdef CAMDEBUG
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;
6909 } else
6910 cam_dpath = NULL;
6911 #else /* !CAM_DEBUG_BUS */
6912 cam_dpath = NULL;
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);
6924 } else {
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.
6938 static int
6939 xptpassannouncefunc(struct cam_ed *device, void *arg)
6941 struct cam_periph *periph;
6942 int i;
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);
6948 if ((i == 1)
6949 && (strncmp(periph->periph_name, "pass", 4) == 0))
6950 xpt_announce_periph(periph, NULL);
6952 return(1);
6955 static void
6956 xpt_finishconfig_task(void *context, int pending)
6958 struct periph_driver **p_drv;
6959 int i;
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);
6985 static void
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) {
6994 case XPT_RESET_BUS:
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);
7000 return;
7002 /* FALLTHROUGH */
7003 case XPT_SCAN_BUS:
7004 default:
7005 xpt_free_path(done_ccb->ccb_h.path);
7006 busses_to_config--;
7007 break;
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);
7021 cam_status
7022 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
7023 struct cam_path *path)
7025 struct ccb_setasync csa;
7026 cam_status status;
7027 int xptpath = 0;
7029 if (path == NULL) {
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);
7035 return (status);
7037 xptpath = 1;
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;
7047 if (xptpath) {
7048 xpt_free_path(path);
7049 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
7051 return (status);
7054 static void
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;
7069 cpi->hba_misc = 0;
7070 cpi->hba_eng_cnt = 0;
7071 cpi->max_target = 0;
7072 cpi->max_lun = 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;
7085 xpt_done(work_ccb);
7086 break;
7088 default:
7089 work_ccb->ccb_h.status = CAM_REQ_INVALID;
7090 xpt_done(work_ccb);
7091 break;
7096 * The xpt as a "controller" has no interrupt sources, so polling
7097 * is a no-op.
7099 static void
7100 xptpoll(struct cam_sim *sim)
7104 void
7105 xpt_lock_buses(void)
7107 lockmgr(&xsoftc.xpt_topo_lock, LK_EXCLUSIVE);
7110 void
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.
7122 static void
7123 swi_cambio(void *arg, void *frame)
7125 camisr(NULL);
7128 static void
7129 camisr(void *dummy)
7131 cam_simq_t queue;
7132 struct cam_sim *sim;
7134 spin_lock_wr(&cam_simq_spin);
7135 TAILQ_INIT(&queue);
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);
7141 CAM_SIM_LOCK(sim);
7142 sim->flags &= ~CAM_SIM_ON_DONEQ;
7143 camisr_runqueue(sim);
7144 CAM_SIM_UNLOCK(sim);
7148 static void
7149 camisr_runqueue(struct cam_sim *sim)
7151 struct ccb_hdr *ccb_h;
7152 int runq;
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,
7161 ("camisr\n"));
7163 runq = FALSE;
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);
7191 } else
7192 lockmgr(&xsoftc.xpt_lock, LK_RELEASE);
7195 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7196 struct cam_ed *dev;
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,
7213 /*run_queue*/TRUE);
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,
7224 dev);
7228 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7229 xpt_release_simq(ccb_h->path->bus->sim,
7230 /*run_queue*/TRUE);
7231 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7232 runq = FALSE;
7235 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7236 && (ccb_h->status & CAM_DEV_QFRZN)) {
7237 xpt_release_devq(ccb_h->path, /*count*/1,
7238 /*run_queue*/TRUE);
7239 ccb_h->status &= ~CAM_DEV_QFRZN;
7240 } else if (runq) {
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);
7251 static void
7252 dead_sim_action(struct cam_sim *sim, union ccb *ccb)
7255 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
7256 xpt_done(ccb);
7259 static void
7260 dead_sim_poll(struct cam_sim *sim)