]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/cam_xpt.c
- Catch up to the new swi API changes:
[FreeBSD/FreeBSD.git] / sys / cam / cam_xpt.c
1 /*
2  * Implementation of the Common Access Method Transport (XPT) layer.
3  *
4  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6  * All rights reserved.
7  *
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.
16  *
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.
28  *
29  * $FreeBSD$
30  */
31 #include <sys/param.h>
32 #include <sys/bus.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/fcntl.h>
40 #include <sys/md5.h>
41 #include <sys/devicestat.h>
42 #include <sys/interrupt.h>
43
44 #ifdef PC98
45 #include <pc98/pc98/pc98_machdep.h>     /* geometry translation */
46 #endif
47
48 #include <sys/ipl.h>
49
50 #include <cam/cam.h>
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_periph.h>
53 #include <cam/cam_sim.h>
54 #include <cam/cam_xpt.h>
55 #include <cam/cam_xpt_sim.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_debug.h>
58
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/scsi/scsi_pass.h>
62 #include "opt_cam.h"
63
64 /* Datastructures internal to the xpt layer */
65
66 /*
67  * Definition of an async handler callback block.  These are used to add
68  * SIMs and peripherals to the async callback lists.
69  */
70 struct async_node {
71         SLIST_ENTRY(async_node) links;
72         u_int32_t       event_enable;   /* Async Event enables */
73         void            (*callback)(void *arg, u_int32_t code,
74                                     struct cam_path *path, void *args);
75         void            *callback_arg;
76 };
77
78 SLIST_HEAD(async_list, async_node);
79 SLIST_HEAD(periph_list, cam_periph);
80 static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
81
82 /*
83  * This is the maximum number of high powered commands (e.g. start unit)
84  * that can be outstanding at a particular time.
85  */
86 #ifndef CAM_MAX_HIGHPOWER
87 #define CAM_MAX_HIGHPOWER  4
88 #endif
89
90 /* number of high powered commands that can go through right now */
91 static int num_highpower = CAM_MAX_HIGHPOWER;
92
93 /*
94  * Structure for queueing a device in a run queue.
95  * There is one run queue for allocating new ccbs,
96  * and another for sending ccbs to the controller.
97  */
98 struct cam_ed_qinfo {
99         cam_pinfo pinfo;
100         struct    cam_ed *device;
101 };
102
103 /*
104  * The CAM EDT (Existing Device Table) contains the device information for
105  * all devices for all busses in the system.  The table contains a
106  * cam_ed structure for each device on the bus.
107  */
108 struct cam_ed {
109         TAILQ_ENTRY(cam_ed) links;
110         struct  cam_ed_qinfo alloc_ccb_entry;
111         struct  cam_ed_qinfo send_ccb_entry;
112         struct  cam_et   *target;
113         lun_id_t         lun_id;
114         struct  camq drvq;              /*
115                                          * Queue of type drivers wanting to do
116                                          * work on this device.
117                                          */
118         struct  cam_ccbq ccbq;          /* Queue of pending ccbs */
119         struct  async_list asyncs;      /* Async callback info for this B/T/L */
120         struct  periph_list periphs;    /* All attached devices */
121         u_int   generation;             /* Generation number */
122         struct  cam_periph *owner;      /* Peripheral driver's ownership tag */
123         struct  xpt_quirk_entry *quirk; /* Oddities about this device */
124                                         /* Storage for the inquiry data */
125         struct  scsi_inquiry_data inq_data;
126         u_int8_t         inq_flags;     /*
127                                          * Current settings for inquiry flags.
128                                          * This allows us to override settings
129                                          * like disconnection and tagged
130                                          * queuing for a device.
131                                          */
132         u_int8_t         queue_flags;   /* Queue flags from the control page */
133         u_int8_t         serial_num_len;
134         u_int8_t         *serial_num;
135         u_int32_t        qfrozen_cnt;
136         u_int32_t        flags;
137 #define CAM_DEV_UNCONFIGURED            0x01
138 #define CAM_DEV_REL_TIMEOUT_PENDING     0x02
139 #define CAM_DEV_REL_ON_COMPLETE         0x04
140 #define CAM_DEV_REL_ON_QUEUE_EMPTY      0x08
141 #define CAM_DEV_RESIZE_QUEUE_NEEDED     0x10
142 #define CAM_DEV_TAG_AFTER_COUNT         0x20
143 #define CAM_DEV_INQUIRY_DATA_VALID      0x40
144         u_int32_t        tag_delay_count;
145 #define CAM_TAG_DELAY_COUNT             5
146         u_int32_t        refcount;
147         struct           callout_handle c_handle;
148 };
149
150 /*
151  * Each target is represented by an ET (Existing Target).  These
152  * entries are created when a target is successfully probed with an
153  * identify, and removed when a device fails to respond after a number
154  * of retries, or a bus rescan finds the device missing.
155  */
156 struct cam_et { 
157         TAILQ_HEAD(, cam_ed) ed_entries;
158         TAILQ_ENTRY(cam_et) links;
159         struct  cam_eb  *bus;   
160         target_id_t     target_id;
161         u_int32_t       refcount;       
162         u_int           generation;
163         struct          timeval last_reset;
164 };
165
166 /*
167  * Each bus is represented by an EB (Existing Bus).  These entries
168  * are created by calls to xpt_bus_register and deleted by calls to
169  * xpt_bus_deregister.
170  */
171 struct cam_eb { 
172         TAILQ_HEAD(, cam_et) et_entries;
173         TAILQ_ENTRY(cam_eb)  links;
174         path_id_t            path_id;
175         struct cam_sim       *sim;
176         struct timeval       last_reset;
177         u_int32_t            flags;
178 #define CAM_EB_RUNQ_SCHEDULED   0x01
179         u_int32_t            refcount;
180         u_int                generation;
181 };
182
183 struct cam_path {
184         struct cam_periph *periph;
185         struct cam_eb     *bus;
186         struct cam_et     *target;
187         struct cam_ed     *device;
188 };
189
190 struct xpt_quirk_entry {
191         struct scsi_inquiry_pattern inq_pat;
192         u_int8_t quirks;
193 #define CAM_QUIRK_NOLUNS        0x01
194 #define CAM_QUIRK_NOSERIAL      0x02
195 #define CAM_QUIRK_HILUNS        0x04
196         u_int mintags;
197         u_int maxtags;
198 };
199 #define CAM_SCSI2_MAXLUN        8
200
201 typedef enum {
202         XPT_FLAG_OPEN           = 0x01
203 } xpt_flags;
204
205 struct xpt_softc {
206         xpt_flags       flags;
207         u_int32_t       generation;
208 };
209
210 static const char quantum[] = "QUANTUM";
211 static const char sony[] = "SONY";
212 static const char west_digital[] = "WDIGTL";
213 static const char samsung[] = "SAMSUNG";
214 static const char seagate[] = "SEAGATE";
215 static const char microp[] = "MICROP";
216
217 static struct xpt_quirk_entry xpt_quirk_table[] = 
218 {
219         {
220                 /* Reports QUEUE FULL for temporary resource shortages */
221                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
222                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
223         },
224         {
225                 /* Reports QUEUE FULL for temporary resource shortages */
226                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
227                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
228         },
229         {
230                 /* Reports QUEUE FULL for temporary resource shortages */
231                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
232                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
233         },
234         {
235                 /* Broken tagged queuing drive */
236                 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
237                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
238         },
239         {
240                 /* Broken tagged queuing drive */
241                 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
242                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
243         },
244         {
245                 /* Broken tagged queuing drive */
246                 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
247                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
248         },
249         {
250                 /*
251                  * Unfortunately, the Quantum Atlas III has the same
252                  * problem as the Atlas II drives above.
253                  * Reported by: "Johan Granlund" <johan@granlund.nu>
254                  *
255                  * For future reference, the drive with the problem was:
256                  * QUANTUM QM39100TD-SW N1B0
257                  * 
258                  * It's possible that Quantum will fix the problem in later
259                  * firmware revisions.  If that happens, the quirk entry
260                  * will need to be made specific to the firmware revisions
261                  * with the problem.
262                  * 
263                  */
264                 /* Reports QUEUE FULL for temporary resource shortages */
265                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
266                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
267         },
268         {
269                 /*
270                  * 18 Gig Atlas III, same problem as the 9G version.
271                  * Reported by: Andre Albsmeier
272                  *              <andre.albsmeier@mchp.siemens.de>
273                  *
274                  * For future reference, the drive with the problem was:
275                  * QUANTUM QM318000TD-S N491
276                  */
277                 /* Reports QUEUE FULL for temporary resource shortages */
278                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
279                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
280         },
281         {
282                 /*
283                  * Broken tagged queuing drive
284                  * Reported by: Bret Ford <bford@uop.cs.uop.edu>
285                  *         and: Martin Renters <martin@tdc.on.ca>
286                  */
287                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
288                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
289         },
290                 /*
291                  * The Seagate Medalist Pro drives have very poor write
292                  * performance with anything more than 2 tags.
293                  * 
294                  * Reported by:  Paul van der Zwan <paulz@trantor.xs4all.nl>
295                  * Drive:  <SEAGATE ST36530N 1444>
296                  *
297                  * Reported by:  Jeremy Lea <reg@shale.csir.co.za>
298                  * Drive:  <SEAGATE ST34520W 1281>
299                  *
300                  * No one has actually reported that the 9G version
301                  * (ST39140*) of the Medalist Pro has the same problem, but
302                  * we're assuming that it does because the 4G and 6.5G
303                  * versions of the drive are broken.
304                  */
305         {
306                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
307                 /*quirks*/0, /*mintags*/2, /*maxtags*/2
308         },
309         {
310                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
311                 /*quirks*/0, /*mintags*/2, /*maxtags*/2
312         },
313         {
314                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
315                 /*quirks*/0, /*mintags*/2, /*maxtags*/2
316         },
317         {
318                 /*
319                  * Slow when tagged queueing is enabled.  Write performance
320                  * steadily drops off with more and more concurrent
321                  * transactions.  Best sequential write performance with
322                  * tagged queueing turned off and write caching turned on.
323                  *
324                  * PR:  kern/10398
325                  * Submitted by:  Hideaki Okada <hokada@isl.melco.co.jp>
326                  * Drive:  DCAS-34330 w/ "S65A" firmware.
327                  *
328                  * The drive with the problem had the "S65A" firmware
329                  * revision, and has also been reported (by Stephen J.
330                  * Roznowski <sjr@home.net>) for a drive with the "S61A"
331                  * firmware revision.
332                  *
333                  * Although no one has reported problems with the 2 gig
334                  * version of the DCAS drive, the assumption is that it
335                  * has the same problems as the 4 gig version.  Therefore
336                  * this quirk entries disables tagged queueing for all
337                  * DCAS drives.
338                  */
339                 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
340                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
341         },
342         {
343                 /* Broken tagged queuing drive */
344                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
345                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
346         },
347         {
348                 /* Broken tagged queuing drive */ 
349                 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
350                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
351         },
352         {
353                 /*
354                  * Broken tagged queuing drive.
355                  * Submitted by:
356                  * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
357                  * in PR kern/9535
358                  */
359                 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
360                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
361         },
362         {
363                 /*
364                  * Slow when tagged queueing is enabled. (1.5MB/sec versus
365                  * 8MB/sec.)
366                  * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
367                  * Best performance with these drives is achieved with
368                  * tagged queueing turned off, and write caching turned on.
369                  */
370                 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
371                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
372         },
373         {
374                 /*
375                  * Slow when tagged queueing is enabled. (1.5MB/sec versus
376                  * 8MB/sec.)
377                  * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
378                  * Best performance with these drives is achieved with
379                  * tagged queueing turned off, and write caching turned on.
380                  */
381                 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
382                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
383         },
384         {
385                 /*
386                  * Doesn't handle queue full condition correctly,
387                  * so we need to limit maxtags to what the device
388                  * can handle instead of determining this automatically.
389                  */
390                 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
391                 /*quirks*/0, /*mintags*/2, /*maxtags*/32
392         },
393         {
394                 /* Really only one LUN */
395                 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA*", "*" },
396                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
397         },
398         {
399                 /* I can't believe we need a quirk for DPT volumes. */
400                 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
401                 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
402                 /*mintags*/0, /*maxtags*/255
403         },
404         {
405                 /*
406                  * Many Sony CDROM drives don't like multi-LUN probing.
407                  */
408                 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
409                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
410         },
411         {
412                 /*
413                  * This drive doesn't like multiple LUN probing.
414                  * Submitted by:  Parag Patel <parag@cgt.com>
415                  */
416                 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R   CDU9*", "*" },
417                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
418         },
419         {
420                 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
421                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
422         },
423         {
424                 /*
425                  * The 8200 doesn't like multi-lun probing, and probably
426                  * don't like serial number requests either.
427                  */
428                 {
429                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
430                         "EXB-8200*", "*"
431                 },
432                 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
433         },
434         {
435                 /*
436                  * These Hitachi drives don't like multi-lun probing.
437                  * The PR submitter has a DK319H, but says that the Linux
438                  * kernel has a similar work-around for the DK312 and DK314,
439                  * so all DK31* drives are quirked here.
440                  * PR:            misc/18793
441                  * Submitted by:  Paul Haddad <paul@pth.com>
442                  */
443                 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
444                 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
445         },
446         {
447                 /*
448                  * This old revision of the TDC3600 is also SCSI-1, and
449                  * hangs upon serial number probing.
450                  */
451                 {
452                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
453                         " TDC 3600", "U07:"
454                 },
455                 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
456         },
457         {
458                 /*
459                  * Would repond to all LUNs if asked for.
460                  */
461                 {
462                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
463                         "CP150", "*"
464                 },
465                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
466         },
467         {
468                 /*
469                  * Would repond to all LUNs if asked for.
470                  */
471                 {
472                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
473                         "96X2*", "*"
474                 },
475                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
476         },
477         {
478                 /* Submitted by: Matthew Dodd <winter@jurai.net> */
479                 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
480                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
481         },
482         {
483                 /* Submitted by: Matthew Dodd <winter@jurai.net> */
484                 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
485                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
486         },
487         {
488                 /* TeraSolutions special settings for TRC-22 RAID */
489                 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
490                   /*quirks*/0, /*mintags*/55, /*maxtags*/255
491         },
492         {
493                 /*
494                  * Would respond to all LUNs.  Device type and removable
495                  * flag are jumper-selectable.
496                  */
497                 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
498                   "Tahiti 1", "*"
499                 },
500                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
501         },
502         {
503                 /* Default tagged queuing parameters for all devices */
504                 {
505                   T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
506                   /*vendor*/"*", /*product*/"*", /*revision*/"*"
507                 },
508                 /*quirks*/0, /*mintags*/2, /*maxtags*/255
509         },
510 };
511
512 static const int xpt_quirk_table_size =
513         sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
514
515 typedef enum {
516         DM_RET_COPY             = 0x01,
517         DM_RET_FLAG_MASK        = 0x0f,
518         DM_RET_NONE             = 0x00,
519         DM_RET_STOP             = 0x10,
520         DM_RET_DESCEND          = 0x20,
521         DM_RET_ERROR            = 0x30,
522         DM_RET_ACTION_MASK      = 0xf0
523 } dev_match_ret;
524
525 typedef enum {
526         XPT_DEPTH_BUS,
527         XPT_DEPTH_TARGET,
528         XPT_DEPTH_DEVICE,
529         XPT_DEPTH_PERIPH
530 } xpt_traverse_depth;
531
532 struct xpt_traverse_config {
533         xpt_traverse_depth      depth;
534         void                    *tr_func;
535         void                    *tr_arg;
536 };
537
538 typedef int     xpt_busfunc_t (struct cam_eb *bus, void *arg);
539 typedef int     xpt_targetfunc_t (struct cam_et *target, void *arg);
540 typedef int     xpt_devicefunc_t (struct cam_ed *device, void *arg);
541 typedef int     xpt_periphfunc_t (struct cam_periph *periph, void *arg);
542 typedef int     xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
543
544 /* Transport layer configuration information */
545 static struct xpt_softc xsoftc;
546
547 /* Queues for our software interrupt handler */
548 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
549 static cam_isrq_t cam_bioq;
550 static cam_isrq_t cam_netq;
551
552 /* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
553 static SLIST_HEAD(,ccb_hdr) ccb_freeq;
554 static u_int xpt_max_ccbs;      /*
555                                  * Maximum size of ccb pool.  Modified as
556                                  * devices are added/removed or have their
557                                  * opening counts changed.
558                                  */
559 static u_int xpt_ccb_count;     /* Current count of allocated ccbs */
560
561 struct cam_periph *xpt_periph;
562
563 static periph_init_t xpt_periph_init;
564
565 static periph_init_t probe_periph_init;
566
567 static struct periph_driver xpt_driver =
568 {
569         xpt_periph_init, "xpt",
570         TAILQ_HEAD_INITIALIZER(xpt_driver.units)
571 };
572
573 static struct periph_driver probe_driver =
574 {
575         probe_periph_init, "probe",
576         TAILQ_HEAD_INITIALIZER(probe_driver.units)
577 };
578
579 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
580 PERIPHDRIVER_DECLARE(probe, probe_driver);
581
582 #define XPT_CDEV_MAJOR 104
583
584 static d_open_t xptopen;
585 static d_close_t xptclose;
586 static d_ioctl_t xptioctl;
587
588 static struct cdevsw xpt_cdevsw = {
589         /* open */      xptopen,
590         /* close */     xptclose,
591         /* read */      noread,
592         /* write */     nowrite,
593         /* ioctl */     xptioctl,
594         /* poll */      nopoll,
595         /* mmap */      nommap,
596         /* strategy */  nostrategy,
597         /* name */      "xpt",
598         /* maj */       XPT_CDEV_MAJOR,
599         /* dump */      nodump,
600         /* psize */     nopsize,
601         /* flags */     0,
602         /* bmaj */      -1
603 };
604
605 static struct intr_config_hook *xpt_config_hook;
606
607 /* Registered busses */
608 static TAILQ_HEAD(,cam_eb) xpt_busses;
609 static u_int bus_generation;
610
611 /* Storage for debugging datastructures */
612 #ifdef  CAMDEBUG
613 struct cam_path *cam_dpath;
614 u_int32_t cam_dflags;
615 u_int32_t cam_debug_delay;
616 #endif
617
618 /* Pointers to software interrupt handlers */
619 void *camnet_ih;
620 void *cambio_ih;
621
622 #if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
623 #error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
624 #endif
625
626 /*
627  * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
628  * enabled.  Also, the user must have either none, or all of CAM_DEBUG_BUS,
629  * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
630  */
631 #if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
632     || defined(CAM_DEBUG_LUN)
633 #ifdef CAMDEBUG
634 #if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
635     || !defined(CAM_DEBUG_LUN)
636 #error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
637         and CAM_DEBUG_LUN"
638 #endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
639 #else /* !CAMDEBUG */
640 #error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
641 #endif /* CAMDEBUG */
642 #endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
643
644 /* Our boot-time initialization hook */
645 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
646
647 static moduledata_t cam_moduledata = {
648         "cam",
649         cam_module_event_handler,
650         NULL
651 };
652
653 static void     xpt_init(void *);
654
655 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
656 MODULE_VERSION(cam, 1);
657
658
659 static cam_status       xpt_compile_path(struct cam_path *new_path,
660                                          struct cam_periph *perph,
661                                          path_id_t path_id,
662                                          target_id_t target_id,
663                                          lun_id_t lun_id);
664
665 static void             xpt_release_path(struct cam_path *path);
666
667 static void             xpt_async_bcast(struct async_list *async_head,
668                                         u_int32_t async_code,
669                                         struct cam_path *path,
670                                         void *async_arg);
671 static void             xpt_dev_async(u_int32_t async_code,
672                                       struct cam_eb *bus,
673                                       struct cam_et *target,
674                                       struct cam_ed *device,
675                                       void *async_arg);
676 static path_id_t xptnextfreepathid(void);
677 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
678 static union ccb *xpt_get_ccb(struct cam_ed *device);
679 static int       xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
680                                   u_int32_t new_priority);
681 static void      xpt_run_dev_allocq(struct cam_eb *bus);
682 static void      xpt_run_dev_sendq(struct cam_eb *bus);
683 static timeout_t xpt_release_devq_timeout;
684 static timeout_t xpt_release_simq_timeout;
685 static void      xpt_release_bus(struct cam_eb *bus);
686 static void      xpt_release_devq_device(struct cam_ed *dev, u_int count,
687                                          int run_queue);
688 static struct cam_et*
689                  xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
690 static void      xpt_release_target(struct cam_eb *bus, struct cam_et *target);
691 static struct cam_ed*
692                  xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
693                                   lun_id_t lun_id);
694 static void      xpt_release_device(struct cam_eb *bus, struct cam_et *target,
695                                     struct cam_ed *device);
696 static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
697 static struct cam_eb*
698                  xpt_find_bus(path_id_t path_id);
699 static struct cam_et*
700                  xpt_find_target(struct cam_eb *bus, target_id_t target_id);
701 static struct cam_ed*
702                  xpt_find_device(struct cam_et *target, lun_id_t lun_id);
703 static void      xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
704 static void      xpt_scan_lun(struct cam_periph *periph,
705                               struct cam_path *path, cam_flags flags,
706                               union ccb *ccb);
707 static void      xptscandone(struct cam_periph *periph, union ccb *done_ccb);
708 static xpt_busfunc_t    xptconfigbuscountfunc;
709 static xpt_busfunc_t    xptconfigfunc;
710 static void      xpt_config(void *arg);
711 static xpt_devicefunc_t xptpassannouncefunc;
712 static void      xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
713 static void      xptaction(struct cam_sim *sim, union ccb *work_ccb);
714 static void      xptpoll(struct cam_sim *sim);
715 static void      camisr(void *);
716 #if 0
717 static void      xptstart(struct cam_periph *periph, union ccb *work_ccb);
718 static void      xptasync(struct cam_periph *periph,
719                           u_int32_t code, cam_path *path);
720 #endif
721 static dev_match_ret    xptbusmatch(struct dev_match_pattern *patterns,
722                                     int num_patterns, struct cam_eb *bus);
723 static dev_match_ret    xptdevicematch(struct dev_match_pattern *patterns,
724                                        int num_patterns, struct cam_ed *device);
725 static dev_match_ret    xptperiphmatch(struct dev_match_pattern *patterns,
726                                        int num_patterns,
727                                        struct cam_periph *periph);
728 static xpt_busfunc_t    xptedtbusfunc;
729 static xpt_targetfunc_t xptedttargetfunc;
730 static xpt_devicefunc_t xptedtdevicefunc;
731 static xpt_periphfunc_t xptedtperiphfunc;
732 static xpt_pdrvfunc_t   xptplistpdrvfunc;
733 static xpt_periphfunc_t xptplistperiphfunc;
734 static int              xptedtmatch(struct ccb_dev_match *cdm);
735 static int              xptperiphlistmatch(struct ccb_dev_match *cdm);
736 static int              xptbustraverse(struct cam_eb *start_bus,
737                                        xpt_busfunc_t *tr_func, void *arg);
738 static int              xpttargettraverse(struct cam_eb *bus,
739                                           struct cam_et *start_target,
740                                           xpt_targetfunc_t *tr_func, void *arg);
741 static int              xptdevicetraverse(struct cam_et *target,
742                                           struct cam_ed *start_device,
743                                           xpt_devicefunc_t *tr_func, void *arg);
744 static int              xptperiphtraverse(struct cam_ed *device,
745                                           struct cam_periph *start_periph,
746                                           xpt_periphfunc_t *tr_func, void *arg);
747 static int              xptpdrvtraverse(struct periph_driver **start_pdrv,
748                                         xpt_pdrvfunc_t *tr_func, void *arg);
749 static int              xptpdperiphtraverse(struct periph_driver **pdrv,
750                                             struct cam_periph *start_periph,
751                                             xpt_periphfunc_t *tr_func,
752                                             void *arg);
753 static xpt_busfunc_t    xptdefbusfunc;
754 static xpt_targetfunc_t xptdeftargetfunc;
755 static xpt_devicefunc_t xptdefdevicefunc;
756 static xpt_periphfunc_t xptdefperiphfunc;
757 static int              xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
758 #ifdef notusedyet
759 static int              xpt_for_all_targets(xpt_targetfunc_t *tr_func,
760                                             void *arg);
761 #endif
762 static int              xpt_for_all_devices(xpt_devicefunc_t *tr_func,
763                                             void *arg);
764 #ifdef notusedyet
765 static int              xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
766                                             void *arg);
767 #endif
768 static xpt_devicefunc_t xptsetasyncfunc;
769 static xpt_busfunc_t    xptsetasyncbusfunc;
770 static cam_status       xptregister(struct cam_periph *periph,
771                                     void *arg);
772 static cam_status       proberegister(struct cam_periph *periph,
773                                       void *arg);
774 static void      probeschedule(struct cam_periph *probe_periph);
775 static void      probestart(struct cam_periph *periph, union ccb *start_ccb);
776 static void      proberequestdefaultnegotiation(struct cam_periph *periph);
777 static void      probedone(struct cam_periph *periph, union ccb *done_ccb);
778 static void      probecleanup(struct cam_periph *periph);
779 static void      xpt_find_quirk(struct cam_ed *device);
780 static void      xpt_set_transfer_settings(struct ccb_trans_settings *cts,
781                                            struct cam_ed *device,
782                                            int async_update);
783 static void      xpt_toggle_tags(struct cam_path *path);
784 static void      xpt_start_tags(struct cam_path *path);
785 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
786                                             struct cam_ed *dev);
787 static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
788                                            struct cam_ed *dev);
789 static __inline int periph_is_queued(struct cam_periph *periph);
790 static __inline int device_is_alloc_queued(struct cam_ed *device);
791 static __inline int device_is_send_queued(struct cam_ed *device);
792 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
793
794 static __inline int
795 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
796 {
797         int retval;
798
799         if (dev->ccbq.devq_openings > 0) {
800                 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
801                         cam_ccbq_resize(&dev->ccbq,
802                                         dev->ccbq.dev_openings
803                                         + dev->ccbq.dev_active);
804                         dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
805                 }
806                 /*
807                  * The priority of a device waiting for CCB resources
808                  * is that of the the highest priority peripheral driver
809                  * enqueued.
810                  */
811                 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
812                                           &dev->alloc_ccb_entry.pinfo,
813                                           CAMQ_GET_HEAD(&dev->drvq)->priority); 
814         } else {
815                 retval = 0;
816         }
817
818         return (retval);
819 }
820
821 static __inline int
822 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
823 {
824         int     retval;
825
826         if (dev->ccbq.dev_openings > 0) {
827                 /*
828                  * The priority of a device waiting for controller
829                  * resources is that of the the highest priority CCB
830                  * enqueued.
831                  */
832                 retval =
833                     xpt_schedule_dev(&bus->sim->devq->send_queue,
834                                      &dev->send_ccb_entry.pinfo,
835                                      CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
836         } else {
837                 retval = 0;
838         }
839         return (retval);
840 }
841
842 static __inline int
843 periph_is_queued(struct cam_periph *periph)
844 {
845         return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
846 }
847
848 static __inline int
849 device_is_alloc_queued(struct cam_ed *device)
850 {
851         return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
852 }
853
854 static __inline int
855 device_is_send_queued(struct cam_ed *device)
856 {
857         return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
858 }
859
860 static __inline int
861 dev_allocq_is_runnable(struct cam_devq *devq)
862 {
863         /*
864          * Have work to do.
865          * Have space to do more work.
866          * Allowed to do work.
867          */
868         return ((devq->alloc_queue.qfrozen_cnt == 0)
869              && (devq->alloc_queue.entries > 0)
870              && (devq->alloc_openings > 0));
871 }
872
873 static void
874 xpt_periph_init()
875 {
876         make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
877 }
878
879 static void
880 probe_periph_init()
881 {
882 }
883
884
885 static void
886 xptdone(struct cam_periph *periph, union ccb *done_ccb)
887 {
888         /* Caller will release the CCB */
889         wakeup(&done_ccb->ccb_h.cbfcnp);
890 }
891
892 static int
893 xptopen(dev_t dev, int flags, int fmt, struct proc *p)
894 {
895         int unit;
896
897         unit = minor(dev) & 0xff;
898
899         /*
900          * Only allow read-write access.
901          */
902         if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
903                 return(EPERM);
904
905         /*
906          * We don't allow nonblocking access.
907          */
908         if ((flags & O_NONBLOCK) != 0) {
909                 printf("xpt%d: can't do nonblocking accesss\n", unit);
910                 return(ENODEV);
911         }
912
913         /*
914          * We only have one transport layer right now.  If someone accesses
915          * us via something other than minor number 1, point out their
916          * mistake.
917          */
918         if (unit != 0) {
919                 printf("xptopen: got invalid xpt unit %d\n", unit);
920                 return(ENXIO);
921         }
922
923         /* Mark ourselves open */
924         xsoftc.flags |= XPT_FLAG_OPEN;
925         
926         return(0);
927 }
928
929 static int
930 xptclose(dev_t dev, int flag, int fmt, struct proc *p)
931 {
932         int unit;
933
934         unit = minor(dev) & 0xff;
935
936         /*
937          * We only have one transport layer right now.  If someone accesses
938          * us via something other than minor number 1, point out their
939          * mistake.
940          */
941         if (unit != 0) {
942                 printf("xptclose: got invalid xpt unit %d\n", unit);
943                 return(ENXIO);
944         }
945
946         /* Mark ourselves closed */
947         xsoftc.flags &= ~XPT_FLAG_OPEN;
948
949         return(0);
950 }
951
952 static int
953 xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
954 {
955         int unit, error;
956
957         error = 0;
958         unit = minor(dev) & 0xff;
959
960         /*
961          * We only have one transport layer right now.  If someone accesses
962          * us via something other than minor number 1, point out their
963          * mistake.
964          */
965         if (unit != 0) {
966                 printf("xptioctl: got invalid xpt unit %d\n", unit);
967                 return(ENXIO);
968         }
969
970         switch(cmd) {
971         /*
972          * For the transport layer CAMIOCOMMAND ioctl, we really only want
973          * to accept CCB types that don't quite make sense to send through a
974          * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
975          * in the CAM spec.
976          */
977         case CAMIOCOMMAND: {
978                 union ccb *ccb;
979                 union ccb *inccb;
980
981                 inccb = (union ccb *)addr;
982
983                 switch(inccb->ccb_h.func_code) {
984                 case XPT_SCAN_BUS:
985                 case XPT_RESET_BUS:
986                         if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
987                          || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
988                                 error = EINVAL;
989                                 break;
990                         }
991                         /* FALLTHROUGH */
992                 case XPT_PATH_INQ:
993                 case XPT_ENG_INQ:
994                 case XPT_SCAN_LUN:
995
996                         ccb = xpt_alloc_ccb();
997
998                         /*
999                          * Create a path using the bus, target, and lun the
1000                          * user passed in.
1001                          */
1002                         if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1003                                             inccb->ccb_h.path_id,
1004                                             inccb->ccb_h.target_id,
1005                                             inccb->ccb_h.target_lun) !=
1006                                             CAM_REQ_CMP){
1007                                 error = EINVAL;
1008                                 xpt_free_ccb(ccb);
1009                                 break;
1010                         }
1011                         /* Ensure all of our fields are correct */
1012                         xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1013                                       inccb->ccb_h.pinfo.priority);
1014                         xpt_merge_ccb(ccb, inccb);
1015                         ccb->ccb_h.cbfcnp = xptdone;
1016                         cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1017                         bcopy(ccb, inccb, sizeof(union ccb));
1018                         xpt_free_path(ccb->ccb_h.path);
1019                         xpt_free_ccb(ccb);
1020                         break;
1021
1022                 case XPT_DEBUG: {
1023                         union ccb ccb;
1024
1025                         /*
1026                          * This is an immediate CCB, so it's okay to
1027                          * allocate it on the stack.
1028                          */
1029
1030                         /*
1031                          * Create a path using the bus, target, and lun the
1032                          * user passed in.
1033                          */
1034                         if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1035                                             inccb->ccb_h.path_id,
1036                                             inccb->ccb_h.target_id,
1037                                             inccb->ccb_h.target_lun) !=
1038                                             CAM_REQ_CMP){
1039                                 error = EINVAL;
1040                                 break;
1041                         }
1042                         /* Ensure all of our fields are correct */
1043                         xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1044                                       inccb->ccb_h.pinfo.priority);
1045                         xpt_merge_ccb(&ccb, inccb);
1046                         ccb.ccb_h.cbfcnp = xptdone;
1047                         xpt_action(&ccb);
1048                         bcopy(&ccb, inccb, sizeof(union ccb));
1049                         xpt_free_path(ccb.ccb_h.path);
1050                         break;
1051
1052                 }
1053                 case XPT_DEV_MATCH: {
1054                         struct cam_periph_map_info mapinfo;
1055                         struct cam_path *old_path;
1056
1057                         /*
1058                          * We can't deal with physical addresses for this
1059                          * type of transaction.
1060                          */
1061                         if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1062                                 error = EINVAL;
1063                                 break;
1064                         }
1065
1066                         /*
1067                          * Save this in case the caller had it set to
1068                          * something in particular.
1069                          */
1070                         old_path = inccb->ccb_h.path;
1071
1072                         /*
1073                          * We really don't need a path for the matching
1074                          * code.  The path is needed because of the
1075                          * debugging statements in xpt_action().  They
1076                          * assume that the CCB has a valid path.
1077                          */
1078                         inccb->ccb_h.path = xpt_periph->path;
1079
1080                         bzero(&mapinfo, sizeof(mapinfo));
1081
1082                         /*
1083                          * Map the pattern and match buffers into kernel
1084                          * virtual address space.
1085                          */
1086                         error = cam_periph_mapmem(inccb, &mapinfo);
1087
1088                         if (error) {
1089                                 inccb->ccb_h.path = old_path;
1090                                 break;
1091                         }
1092
1093                         /*
1094                          * This is an immediate CCB, we can send it on directly.
1095                          */
1096                         xpt_action(inccb);
1097
1098                         /*
1099                          * Map the buffers back into user space.
1100                          */
1101                         cam_periph_unmapmem(inccb, &mapinfo);
1102
1103                         inccb->ccb_h.path = old_path;
1104
1105                         error = 0;
1106                         break;
1107                 }
1108                 default:
1109                         error = ENOTSUP;
1110                         break;
1111                 }
1112                 break;
1113         }
1114         /*
1115          * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1116          * with the periphal driver name and unit name filled in.  The other
1117          * fields don't really matter as input.  The passthrough driver name
1118          * ("pass"), and unit number are passed back in the ccb.  The current
1119          * device generation number, and the index into the device peripheral
1120          * driver list, and the status are also passed back.  Note that
1121          * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1122          * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
1123          * (or rather should be) impossible for the device peripheral driver
1124          * list to change since we look at the whole thing in one pass, and
1125          * we do it with splcam protection.
1126          * 
1127          */
1128         case CAMGETPASSTHRU: {
1129                 union ccb *ccb;
1130                 struct cam_periph *periph;
1131                 struct periph_driver **p_drv;
1132                 char   *name;
1133                 int unit;
1134                 int cur_generation;
1135                 int base_periph_found;
1136                 int splbreaknum;
1137                 int s;
1138
1139                 ccb = (union ccb *)addr;
1140                 unit = ccb->cgdl.unit_number;
1141                 name = ccb->cgdl.periph_name;
1142                 /*
1143                  * Every 100 devices, we want to drop our spl protection to
1144                  * give the software interrupt handler a chance to run.
1145                  * Most systems won't run into this check, but this should
1146                  * avoid starvation in the software interrupt handler in
1147                  * large systems.
1148                  */
1149                 splbreaknum = 100;
1150
1151                 ccb = (union ccb *)addr;
1152
1153                 base_periph_found = 0;
1154
1155                 /*
1156                  * Sanity check -- make sure we don't get a null peripheral
1157                  * driver name.
1158                  */
1159                 if (*ccb->cgdl.periph_name == '\0') {
1160                         error = EINVAL;
1161                         break;
1162                 }
1163
1164                 /* Keep the list from changing while we traverse it */
1165                 s = splcam();
1166 ptstartover:
1167                 cur_generation = xsoftc.generation;
1168
1169                 /* first find our driver in the list of drivers */
1170                 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1171                         if (strcmp((*p_drv)->driver_name, name) == 0)
1172                                 break;
1173
1174                 if (*p_drv == NULL) {
1175                         splx(s);
1176                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1177                         ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1178                         *ccb->cgdl.periph_name = '\0';
1179                         ccb->cgdl.unit_number = 0;
1180                         error = ENOENT;
1181                         break;
1182                 }       
1183
1184                 /*
1185                  * Run through every peripheral instance of this driver
1186                  * and check to see whether it matches the unit passed
1187                  * in by the user.  If it does, get out of the loops and
1188                  * find the passthrough driver associated with that
1189                  * peripheral driver.
1190                  */
1191                 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1192                      periph = TAILQ_NEXT(periph, unit_links)) {
1193
1194                         if (periph->unit_number == unit) {
1195                                 break;
1196                         } else if (--splbreaknum == 0) {
1197                                 splx(s);
1198                                 s = splcam();
1199                                 splbreaknum = 100;
1200                                 if (cur_generation != xsoftc.generation)
1201                                        goto ptstartover;
1202                         }
1203                 }
1204                 /*
1205                  * If we found the peripheral driver that the user passed
1206                  * in, go through all of the peripheral drivers for that
1207                  * particular device and look for a passthrough driver.
1208                  */
1209                 if (periph != NULL) {
1210                         struct cam_ed *device;
1211                         int i;
1212
1213                         base_periph_found = 1;
1214                         device = periph->path->device;
1215                         for (i = 0, periph = SLIST_FIRST(&device->periphs);
1216                              periph != NULL;
1217                              periph = SLIST_NEXT(periph, periph_links), i++) {
1218                                 /*
1219                                  * Check to see whether we have a
1220                                  * passthrough device or not. 
1221                                  */
1222                                 if (strcmp(periph->periph_name, "pass") == 0) {
1223                                         /*
1224                                          * Fill in the getdevlist fields.
1225                                          */
1226                                         strcpy(ccb->cgdl.periph_name,
1227                                                periph->periph_name);
1228                                         ccb->cgdl.unit_number =
1229                                                 periph->unit_number;
1230                                         if (SLIST_NEXT(periph, periph_links))
1231                                                 ccb->cgdl.status =
1232                                                         CAM_GDEVLIST_MORE_DEVS;
1233                                         else
1234                                                 ccb->cgdl.status =
1235                                                        CAM_GDEVLIST_LAST_DEVICE;
1236                                         ccb->cgdl.generation =
1237                                                 device->generation;
1238                                         ccb->cgdl.index = i;
1239                                         /*
1240                                          * Fill in some CCB header fields
1241                                          * that the user may want.
1242                                          */
1243                                         ccb->ccb_h.path_id =
1244                                                 periph->path->bus->path_id;
1245                                         ccb->ccb_h.target_id =
1246                                                 periph->path->target->target_id;
1247                                         ccb->ccb_h.target_lun =
1248                                                 periph->path->device->lun_id;
1249                                         ccb->ccb_h.status = CAM_REQ_CMP;
1250                                         break;
1251                                 }
1252                         }
1253                 }
1254
1255                 /*
1256                  * If the periph is null here, one of two things has
1257                  * happened.  The first possibility is that we couldn't
1258                  * find the unit number of the particular peripheral driver
1259                  * that the user is asking about.  e.g. the user asks for
1260                  * the passthrough driver for "da11".  We find the list of
1261                  * "da" peripherals all right, but there is no unit 11.
1262                  * The other possibility is that we went through the list
1263                  * of peripheral drivers attached to the device structure,
1264                  * but didn't find one with the name "pass".  Either way,
1265                  * we return ENOENT, since we couldn't find something.
1266                  */
1267                 if (periph == NULL) {
1268                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1269                         ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1270                         *ccb->cgdl.periph_name = '\0';
1271                         ccb->cgdl.unit_number = 0;
1272                         error = ENOENT;
1273                         /*
1274                          * It is unfortunate that this is even necessary,
1275                          * but there are many, many clueless users out there.
1276                          * If this is true, the user is looking for the
1277                          * passthrough driver, but doesn't have one in his
1278                          * kernel.
1279                          */
1280                         if (base_periph_found == 1) {
1281                                 printf("xptioctl: pass driver is not in the "
1282                                        "kernel\n");
1283                                 printf("xptioctl: put \"device pass0\" in "
1284                                        "your kernel config file\n");
1285                         }
1286                 }
1287                 splx(s);
1288                 break;
1289                 }
1290         default:
1291                 error = ENOTTY;
1292                 break;
1293         }
1294
1295         return(error);
1296 }
1297
1298 static int
1299 cam_module_event_handler(module_t mod, int what, void *arg)
1300 {
1301         if (what == MOD_LOAD) {
1302                 xpt_init(NULL);
1303         } else if (what == MOD_UNLOAD) {
1304                 return EBUSY;
1305         }
1306
1307         return 0;
1308 }
1309
1310 /* Functions accessed by the peripheral drivers */
1311 static void
1312 xpt_init(dummy)
1313         void *dummy;
1314 {
1315         struct cam_sim *xpt_sim;
1316         struct cam_path *path;
1317         struct cam_devq *devq;
1318         cam_status status;
1319
1320         TAILQ_INIT(&xpt_busses);
1321         TAILQ_INIT(&cam_bioq);
1322         TAILQ_INIT(&cam_netq);
1323         SLIST_INIT(&ccb_freeq);
1324         STAILQ_INIT(&highpowerq);
1325
1326         /*
1327          * The xpt layer is, itself, the equivelent of a SIM.
1328          * Allow 16 ccbs in the ccb pool for it.  This should
1329          * give decent parallelism when we probe busses and
1330          * perform other XPT functions.
1331          */
1332         devq = cam_simq_alloc(16);
1333         xpt_sim = cam_sim_alloc(xptaction,
1334                                 xptpoll,
1335                                 "xpt",
1336                                 /*softc*/NULL,
1337                                 /*unit*/0,
1338                                 /*max_dev_transactions*/0,
1339                                 /*max_tagged_dev_transactions*/0,
1340                                 devq);
1341         xpt_max_ccbs = 16;
1342                                 
1343         xpt_bus_register(xpt_sim, /*bus #*/0);
1344
1345         /*
1346          * Looking at the XPT from the SIM layer, the XPT is
1347          * the equivelent of a peripheral driver.  Allocate
1348          * a peripheral driver entry for us.
1349          */
1350         if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1351                                       CAM_TARGET_WILDCARD,
1352                                       CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1353                 printf("xpt_init: xpt_create_path failed with status %#x,"
1354                        " failing attach\n", status);
1355                 return;
1356         }
1357
1358         cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1359                          path, NULL, 0, NULL);
1360         xpt_free_path(path);
1361
1362         xpt_sim->softc = xpt_periph;
1363
1364         /*
1365          * Register a callback for when interrupts are enabled.
1366          */
1367         xpt_config_hook =
1368             (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1369                                               M_TEMP, M_NOWAIT | M_ZERO);
1370         if (xpt_config_hook == NULL) {
1371                 printf("xpt_init: Cannot malloc config hook "
1372                        "- failing attach\n");
1373                 return;
1374         }
1375
1376         xpt_config_hook->ich_func = xpt_config;
1377         if (config_intrhook_establish(xpt_config_hook) != 0) {
1378                 free (xpt_config_hook, M_TEMP);
1379                 printf("xpt_init: config_intrhook_establish failed "
1380                        "- failing attach\n");
1381         }
1382
1383         /* Install our software interrupt handlers */
1384         swi_add(NULL, "camnet", camisr, &cam_netq, SWI_CAMNET, 0, &camnet_ih);
1385         swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1386 }
1387
1388 static cam_status
1389 xptregister(struct cam_periph *periph, void *arg)
1390 {
1391         if (periph == NULL) {
1392                 printf("xptregister: periph was NULL!!\n");
1393                 return(CAM_REQ_CMP_ERR);
1394         }
1395
1396         periph->softc = NULL;
1397
1398         xpt_periph = periph;
1399
1400         return(CAM_REQ_CMP);
1401 }
1402
1403 int32_t
1404 xpt_add_periph(struct cam_periph *periph)
1405 {
1406         struct cam_ed *device;
1407         int32_t  status;
1408         struct periph_list *periph_head;
1409
1410         device = periph->path->device;
1411
1412         periph_head = &device->periphs;
1413
1414         status = CAM_REQ_CMP;
1415
1416         if (device != NULL) {
1417                 int s;
1418
1419                 /*
1420                  * Make room for this peripheral
1421                  * so it will fit in the queue
1422                  * when it's scheduled to run
1423                  */
1424                 s = splsoftcam();
1425                 status = camq_resize(&device->drvq,
1426                                      device->drvq.array_size + 1);
1427
1428                 device->generation++;
1429
1430                 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1431
1432                 splx(s);
1433         }
1434
1435         xsoftc.generation++;
1436
1437         return (status);
1438 }
1439
1440 void
1441 xpt_remove_periph(struct cam_periph *periph)
1442 {
1443         struct cam_ed *device;
1444
1445         device = periph->path->device;
1446
1447         if (device != NULL) {
1448                 int s;
1449                 struct periph_list *periph_head;
1450
1451                 periph_head = &device->periphs;
1452                 
1453                 /* Release the slot for this peripheral */
1454                 s = splsoftcam();
1455                 camq_resize(&device->drvq, device->drvq.array_size - 1);
1456
1457                 device->generation++;
1458
1459                 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1460
1461                 splx(s);
1462         }
1463
1464         xsoftc.generation++;
1465
1466 }
1467
1468 void
1469 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1470 {
1471         int s;
1472         u_int mb;
1473         struct cam_path *path;
1474         struct ccb_trans_settings cts;
1475
1476         path = periph->path;
1477         /*
1478          * To ensure that this is printed in one piece,
1479          * mask out CAM interrupts.
1480          */
1481         s = splsoftcam();
1482         printf("%s%d at %s%d bus %d target %d lun %d\n",
1483                periph->periph_name, periph->unit_number,
1484                path->bus->sim->sim_name,
1485                path->bus->sim->unit_number,
1486                path->bus->sim->bus_id,
1487                path->target->target_id,
1488                path->device->lun_id);
1489         printf("%s%d: ", periph->periph_name, periph->unit_number);
1490         scsi_print_inquiry(&path->device->inq_data);
1491         if ((bootverbose)
1492          && (path->device->serial_num_len > 0)) {
1493                 /* Don't wrap the screen  - print only the first 60 chars */
1494                 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1495                        periph->unit_number, path->device->serial_num);
1496         }
1497         xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1498         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1499         cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1500         xpt_action((union ccb*)&cts);
1501         if (cts.ccb_h.status == CAM_REQ_CMP) {
1502                 u_int speed;
1503                 u_int freq;
1504
1505                 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1506                   && cts.sync_offset != 0) {
1507                         freq = scsi_calc_syncsrate(cts.sync_period);
1508                         speed = freq;
1509                 } else {
1510                         struct ccb_pathinq cpi;
1511
1512                         /* Ask the SIM for its base transfer speed */
1513                         xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1514                         cpi.ccb_h.func_code = XPT_PATH_INQ;
1515                         xpt_action((union ccb *)&cpi);
1516
1517                         speed = cpi.base_transfer_speed;
1518                         freq = 0;
1519                 }
1520                 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1521                         speed *= (0x01 << cts.bus_width);
1522                 mb = speed / 1000;
1523                 if (mb > 0)
1524                         printf("%s%d: %d.%03dMB/s transfers",
1525                                periph->periph_name, periph->unit_number,
1526                                mb, speed % 1000);
1527                 else
1528                         printf("%s%d: %dKB/s transfers", periph->periph_name,
1529                                periph->unit_number, speed);
1530                 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1531                  && cts.sync_offset != 0) {
1532                         printf(" (%d.%03dMHz, offset %d", freq / 1000,
1533                                freq % 1000, cts.sync_offset);
1534                 }
1535                 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1536                  && cts.bus_width > 0) {
1537                         if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1538                          && cts.sync_offset != 0) {
1539                                 printf(", ");
1540                         } else {
1541                                 printf(" (");
1542                         }
1543                         printf("%dbit)", 8 * (0x01 << cts.bus_width));
1544                 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1545                         && cts.sync_offset != 0) {
1546                         printf(")");
1547                 }
1548
1549                 if (path->device->inq_flags & SID_CmdQue
1550                  || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1551                         printf(", Tagged Queueing Enabled");
1552                 }
1553
1554                 printf("\n");
1555         } else if (path->device->inq_flags & SID_CmdQue
1556                 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1557                 printf("%s%d: Tagged Queueing Enabled\n",
1558                        periph->periph_name, periph->unit_number);
1559         }
1560
1561         /*
1562          * We only want to print the caller's announce string if they've
1563          * passed one in..
1564          */
1565         if (announce_string != NULL)
1566                 printf("%s%d: %s\n", periph->periph_name,
1567                        periph->unit_number, announce_string);
1568         splx(s);
1569 }
1570
1571
1572 static dev_match_ret
1573 xptbusmatch(struct dev_match_pattern *patterns, int num_patterns,
1574             struct cam_eb *bus)
1575 {
1576         dev_match_ret retval;
1577         int i;
1578
1579         retval = DM_RET_NONE;
1580
1581         /*
1582          * If we aren't given something to match against, that's an error.
1583          */
1584         if (bus == NULL)
1585                 return(DM_RET_ERROR);
1586
1587         /*
1588          * If there are no match entries, then this bus matches no
1589          * matter what.
1590          */
1591         if ((patterns == NULL) || (num_patterns == 0))
1592                 return(DM_RET_DESCEND | DM_RET_COPY);
1593
1594         for (i = 0; i < num_patterns; i++) {
1595                 struct bus_match_pattern *cur_pattern;
1596
1597                 /*
1598                  * If the pattern in question isn't for a bus node, we
1599                  * aren't interested.  However, we do indicate to the
1600                  * calling routine that we should continue descending the
1601                  * tree, since the user wants to match against lower-level
1602                  * EDT elements.
1603                  */
1604                 if (patterns[i].type != DEV_MATCH_BUS) {
1605                         if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1606                                 retval |= DM_RET_DESCEND;
1607                         continue;
1608                 }
1609
1610                 cur_pattern = &patterns[i].pattern.bus_pattern;
1611
1612                 /*
1613                  * If they want to match any bus node, we give them any
1614                  * device node.
1615                  */
1616                 if (cur_pattern->flags == BUS_MATCH_ANY) {
1617                         /* set the copy flag */
1618                         retval |= DM_RET_COPY;
1619
1620                         /*
1621                          * If we've already decided on an action, go ahead
1622                          * and return.
1623                          */
1624                         if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1625                                 return(retval);
1626                 }
1627
1628                 /*
1629                  * Not sure why someone would do this...
1630                  */
1631                 if (cur_pattern->flags == BUS_MATCH_NONE)
1632                         continue;
1633
1634                 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1635                  && (cur_pattern->path_id != bus->path_id))
1636                         continue;
1637
1638                 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1639                  && (cur_pattern->bus_id != bus->sim->bus_id))
1640                         continue;
1641
1642                 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1643                  && (cur_pattern->unit_number != bus->sim->unit_number))
1644                         continue;
1645
1646                 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1647                  && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1648                              DEV_IDLEN) != 0))
1649                         continue;
1650
1651                 /*
1652                  * If we get to this point, the user definitely wants 
1653                  * information on this bus.  So tell the caller to copy the
1654                  * data out.
1655                  */
1656                 retval |= DM_RET_COPY;
1657
1658                 /*
1659                  * If the return action has been set to descend, then we
1660                  * know that we've already seen a non-bus matching
1661                  * expression, therefore we need to further descend the tree.
1662                  * This won't change by continuing around the loop, so we
1663                  * go ahead and return.  If we haven't seen a non-bus
1664                  * matching expression, we keep going around the loop until
1665                  * we exhaust the matching expressions.  We'll set the stop
1666                  * flag once we fall out of the loop.
1667                  */
1668                 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1669                         return(retval);
1670         }
1671
1672         /*
1673          * If the return action hasn't been set to descend yet, that means
1674          * we haven't seen anything other than bus matching patterns.  So
1675          * tell the caller to stop descending the tree -- the user doesn't
1676          * want to match against lower level tree elements.
1677          */
1678         if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1679                 retval |= DM_RET_STOP;
1680
1681         return(retval);
1682 }
1683
1684 static dev_match_ret
1685 xptdevicematch(struct dev_match_pattern *patterns, int num_patterns,
1686                struct cam_ed *device)
1687 {
1688         dev_match_ret retval;
1689         int i;
1690
1691         retval = DM_RET_NONE;
1692
1693         /*
1694          * If we aren't given something to match against, that's an error.
1695          */
1696         if (device == NULL)
1697                 return(DM_RET_ERROR);
1698
1699         /*
1700          * If there are no match entries, then this device matches no
1701          * matter what.
1702          */
1703         if ((patterns == NULL) || (patterns == 0))
1704                 return(DM_RET_DESCEND | DM_RET_COPY);
1705
1706         for (i = 0; i < num_patterns; i++) {
1707                 struct device_match_pattern *cur_pattern;
1708
1709                 /*
1710                  * If the pattern in question isn't for a device node, we
1711                  * aren't interested.
1712                  */
1713                 if (patterns[i].type != DEV_MATCH_DEVICE) {
1714                         if ((patterns[i].type == DEV_MATCH_PERIPH)
1715                          && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1716                                 retval |= DM_RET_DESCEND;
1717                         continue;
1718                 }
1719
1720                 cur_pattern = &patterns[i].pattern.device_pattern;
1721
1722                 /*
1723                  * If they want to match any device node, we give them any
1724                  * device node.
1725                  */
1726                 if (cur_pattern->flags == DEV_MATCH_ANY) {
1727                         /* set the copy flag */
1728                         retval |= DM_RET_COPY;
1729
1730                         
1731                         /*
1732                          * If we've already decided on an action, go ahead
1733                          * and return.
1734                          */
1735                         if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1736                                 return(retval);
1737                 }
1738
1739                 /*
1740                  * Not sure why someone would do this...
1741                  */
1742                 if (cur_pattern->flags == DEV_MATCH_NONE)
1743                         continue;
1744
1745                 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1746                  && (cur_pattern->path_id != device->target->bus->path_id))
1747                         continue;
1748
1749                 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1750                  && (cur_pattern->target_id != device->target->target_id))
1751                         continue;
1752
1753                 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1754                  && (cur_pattern->target_lun != device->lun_id))
1755                         continue;
1756
1757                 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1758                  && (cam_quirkmatch((caddr_t)&device->inq_data,
1759                                     (caddr_t)&cur_pattern->inq_pat,
1760                                     1, sizeof(cur_pattern->inq_pat),
1761                                     scsi_static_inquiry_match) == NULL))
1762                         continue;
1763
1764                 /*
1765                  * If we get to this point, the user definitely wants 
1766                  * information on this device.  So tell the caller to copy
1767                  * the data out.
1768                  */
1769                 retval |= DM_RET_COPY;
1770
1771                 /*
1772                  * If the return action has been set to descend, then we
1773                  * know that we've already seen a peripheral matching
1774                  * expression, therefore we need to further descend the tree.
1775                  * This won't change by continuing around the loop, so we
1776                  * go ahead and return.  If we haven't seen a peripheral
1777                  * matching expression, we keep going around the loop until
1778                  * we exhaust the matching expressions.  We'll set the stop
1779                  * flag once we fall out of the loop.
1780                  */
1781                 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1782                         return(retval);
1783         }
1784
1785         /*
1786          * If the return action hasn't been set to descend yet, that means
1787          * we haven't seen any peripheral matching patterns.  So tell the
1788          * caller to stop descending the tree -- the user doesn't want to
1789          * match against lower level tree elements.
1790          */
1791         if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1792                 retval |= DM_RET_STOP;
1793
1794         return(retval);
1795 }
1796
1797 /*
1798  * Match a single peripheral against any number of match patterns.
1799  */
1800 static dev_match_ret
1801 xptperiphmatch(struct dev_match_pattern *patterns, int num_patterns,
1802                struct cam_periph *periph)
1803 {
1804         dev_match_ret retval;
1805         int i;
1806
1807         /*
1808          * If we aren't given something to match against, that's an error.
1809          */
1810         if (periph == NULL)
1811                 return(DM_RET_ERROR);
1812
1813         /*
1814          * If there are no match entries, then this peripheral matches no
1815          * matter what.
1816          */
1817         if ((patterns == NULL) || (num_patterns == 0))
1818                 return(DM_RET_STOP | DM_RET_COPY);
1819
1820         /*
1821          * There aren't any nodes below a peripheral node, so there's no
1822          * reason to descend the tree any further.
1823          */
1824         retval = DM_RET_STOP;
1825
1826         for (i = 0; i < num_patterns; i++) {
1827                 struct periph_match_pattern *cur_pattern;
1828
1829                 /*
1830                  * If the pattern in question isn't for a peripheral, we
1831                  * aren't interested.
1832                  */
1833                 if (patterns[i].type != DEV_MATCH_PERIPH)
1834                         continue;
1835
1836                 cur_pattern = &patterns[i].pattern.periph_pattern;
1837
1838                 /*
1839                  * If they want to match on anything, then we will do so.
1840                  */
1841                 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1842                         /* set the copy flag */
1843                         retval |= DM_RET_COPY;
1844
1845                         /*
1846                          * We've already set the return action to stop,
1847                          * since there are no nodes below peripherals in
1848                          * the tree.
1849                          */
1850                         return(retval);
1851                 }
1852
1853                 /*
1854                  * Not sure why someone would do this...
1855                  */
1856                 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1857                         continue;
1858
1859                 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1860                  && (cur_pattern->path_id != periph->path->bus->path_id))
1861                         continue;
1862
1863                 /*
1864                  * For the target and lun id's, we have to make sure the
1865                  * target and lun pointers aren't NULL.  The xpt peripheral
1866                  * has a wildcard target and device.
1867                  */
1868                 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1869                  && ((periph->path->target == NULL)
1870                  ||(cur_pattern->target_id != periph->path->target->target_id)))
1871                         continue;
1872
1873                 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1874                  && ((periph->path->device == NULL)
1875                  || (cur_pattern->target_lun != periph->path->device->lun_id)))
1876                         continue;
1877
1878                 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1879                  && (cur_pattern->unit_number != periph->unit_number))
1880                         continue;
1881
1882                 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1883                  && (strncmp(cur_pattern->periph_name, periph->periph_name,
1884                              DEV_IDLEN) != 0))
1885                         continue;
1886
1887                 /*
1888                  * If we get to this point, the user definitely wants 
1889                  * information on this peripheral.  So tell the caller to
1890                  * copy the data out.
1891                  */
1892                 retval |= DM_RET_COPY;
1893
1894                 /*
1895                  * The return action has already been set to stop, since
1896                  * peripherals don't have any nodes below them in the EDT.
1897                  */
1898                 return(retval);
1899         }
1900
1901         /*
1902          * If we get to this point, the peripheral that was passed in
1903          * doesn't match any of the patterns.
1904          */
1905         return(retval);
1906 }
1907
1908 static int
1909 xptedtbusfunc(struct cam_eb *bus, void *arg)
1910 {
1911         struct ccb_dev_match *cdm;
1912         dev_match_ret retval;
1913
1914         cdm = (struct ccb_dev_match *)arg;
1915
1916         /*
1917          * If our position is for something deeper in the tree, that means
1918          * that we've already seen this node.  So, we keep going down.
1919          */
1920         if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1921          && (cdm->pos.cookie.bus == bus)
1922          && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1923          && (cdm->pos.cookie.target != NULL))
1924                 retval = DM_RET_DESCEND;
1925         else
1926                 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1927
1928         /*
1929          * If we got an error, bail out of the search.
1930          */
1931         if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1932                 cdm->status = CAM_DEV_MATCH_ERROR;
1933                 return(0);
1934         }
1935
1936         /*
1937          * If the copy flag is set, copy this bus out.
1938          */
1939         if (retval & DM_RET_COPY) {
1940                 int spaceleft, j;
1941
1942                 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1943                         sizeof(struct dev_match_result));
1944
1945                 /*
1946                  * If we don't have enough space to put in another
1947                  * match result, save our position and tell the
1948                  * user there are more devices to check.
1949                  */
1950                 if (spaceleft < sizeof(struct dev_match_result)) {
1951                         bzero(&cdm->pos, sizeof(cdm->pos));
1952                         cdm->pos.position_type = 
1953                                 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1954
1955                         cdm->pos.cookie.bus = bus;
1956                         cdm->pos.generations[CAM_BUS_GENERATION]=
1957                                 bus_generation;
1958                         cdm->status = CAM_DEV_MATCH_MORE;
1959                         return(0);
1960                 }
1961                 j = cdm->num_matches;
1962                 cdm->num_matches++;
1963                 cdm->matches[j].type = DEV_MATCH_BUS;
1964                 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1965                 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1966                 cdm->matches[j].result.bus_result.unit_number =
1967                         bus->sim->unit_number;
1968                 strncpy(cdm->matches[j].result.bus_result.dev_name,
1969                         bus->sim->sim_name, DEV_IDLEN);
1970         }
1971
1972         /*
1973          * If the user is only interested in busses, there's no
1974          * reason to descend to the next level in the tree.
1975          */
1976         if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1977                 return(1);
1978
1979         /*
1980          * If there is a target generation recorded, check it to
1981          * make sure the target list hasn't changed.
1982          */
1983         if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1984          && (bus == cdm->pos.cookie.bus)
1985          && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1986          && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1987          && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1988              bus->generation)) {
1989                 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1990                 return(0);
1991         }
1992
1993         if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1994          && (cdm->pos.cookie.bus == bus)
1995          && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1996          && (cdm->pos.cookie.target != NULL))
1997                 return(xpttargettraverse(bus,
1998                                         (struct cam_et *)cdm->pos.cookie.target,
1999                                          xptedttargetfunc, arg));
2000         else
2001                 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2002 }
2003
2004 static int
2005 xptedttargetfunc(struct cam_et *target, void *arg)
2006 {
2007         struct ccb_dev_match *cdm;
2008
2009         cdm = (struct ccb_dev_match *)arg;
2010
2011         /*
2012          * If there is a device list generation recorded, check it to
2013          * make sure the device list hasn't changed.
2014          */
2015         if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2016          && (cdm->pos.cookie.bus == target->bus)
2017          && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2018          && (cdm->pos.cookie.target == target)
2019          && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2020          && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2021          && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2022              target->generation)) {
2023                 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2024                 return(0);
2025         }
2026
2027         if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2028          && (cdm->pos.cookie.bus == target->bus)
2029          && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2030          && (cdm->pos.cookie.target == target)
2031          && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2032          && (cdm->pos.cookie.device != NULL))
2033                 return(xptdevicetraverse(target,
2034                                         (struct cam_ed *)cdm->pos.cookie.device,
2035                                          xptedtdevicefunc, arg));
2036         else
2037                 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2038 }
2039
2040 static int
2041 xptedtdevicefunc(struct cam_ed *device, void *arg)
2042 {
2043
2044         struct ccb_dev_match *cdm;
2045         dev_match_ret retval;
2046
2047         cdm = (struct ccb_dev_match *)arg;
2048
2049         /*
2050          * If our position is for something deeper in the tree, that means
2051          * that we've already seen this node.  So, we keep going down.
2052          */
2053         if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2054          && (cdm->pos.cookie.device == device)
2055          && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2056          && (cdm->pos.cookie.periph != NULL))
2057                 retval = DM_RET_DESCEND;
2058         else
2059                 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2060                                         device);
2061
2062         if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2063                 cdm->status = CAM_DEV_MATCH_ERROR;
2064                 return(0);
2065         }
2066
2067         /*
2068          * If the copy flag is set, copy this device out.
2069          */
2070         if (retval & DM_RET_COPY) {
2071                 int spaceleft, j;
2072
2073                 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2074                         sizeof(struct dev_match_result));
2075
2076                 /*
2077                  * If we don't have enough space to put in another
2078                  * match result, save our position and tell the
2079                  * user there are more devices to check.
2080                  */
2081                 if (spaceleft < sizeof(struct dev_match_result)) {
2082                         bzero(&cdm->pos, sizeof(cdm->pos));
2083                         cdm->pos.position_type = 
2084                                 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2085                                 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2086
2087                         cdm->pos.cookie.bus = device->target->bus;
2088                         cdm->pos.generations[CAM_BUS_GENERATION]=
2089                                 bus_generation;
2090                         cdm->pos.cookie.target = device->target;
2091                         cdm->pos.generations[CAM_TARGET_GENERATION] =
2092                                 device->target->bus->generation;
2093                         cdm->pos.cookie.device = device;
2094                         cdm->pos.generations[CAM_DEV_GENERATION] = 
2095                                 device->target->generation;
2096                         cdm->status = CAM_DEV_MATCH_MORE;
2097                         return(0);
2098                 }
2099                 j = cdm->num_matches;
2100                 cdm->num_matches++;
2101                 cdm->matches[j].type = DEV_MATCH_DEVICE;
2102                 cdm->matches[j].result.device_result.path_id =
2103                         device->target->bus->path_id;
2104                 cdm->matches[j].result.device_result.target_id =
2105                         device->target->target_id;
2106                 cdm->matches[j].result.device_result.target_lun =
2107                         device->lun_id;
2108                 bcopy(&device->inq_data,
2109                       &cdm->matches[j].result.device_result.inq_data,
2110                       sizeof(struct scsi_inquiry_data));
2111
2112                 /* Let the user know whether this device is unconfigured */
2113                 if (device->flags & CAM_DEV_UNCONFIGURED)
2114                         cdm->matches[j].result.device_result.flags =
2115                                 DEV_RESULT_UNCONFIGURED;
2116                 else
2117                         cdm->matches[j].result.device_result.flags =
2118                                 DEV_RESULT_NOFLAG;
2119         }
2120
2121         /*
2122          * If the user isn't interested in peripherals, don't descend
2123          * the tree any further.
2124          */
2125         if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2126                 return(1);
2127
2128         /*
2129          * If there is a peripheral list generation recorded, make sure
2130          * it hasn't changed.
2131          */
2132         if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2133          && (device->target->bus == cdm->pos.cookie.bus)
2134          && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2135          && (device->target == cdm->pos.cookie.target)
2136          && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2137          && (device == cdm->pos.cookie.device)
2138          && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2139          && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2140          && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2141              device->generation)){
2142                 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2143                 return(0);
2144         }
2145
2146         if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2147          && (cdm->pos.cookie.bus == device->target->bus)
2148          && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2149          && (cdm->pos.cookie.target == device->target)
2150          && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2151          && (cdm->pos.cookie.device == device)
2152          && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2153          && (cdm->pos.cookie.periph != NULL))
2154                 return(xptperiphtraverse(device,
2155                                 (struct cam_periph *)cdm->pos.cookie.periph,
2156                                 xptedtperiphfunc, arg));
2157         else
2158                 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2159 }
2160
2161 static int
2162 xptedtperiphfunc(struct cam_periph *periph, void *arg)
2163 {
2164         struct ccb_dev_match *cdm;
2165         dev_match_ret retval;
2166
2167         cdm = (struct ccb_dev_match *)arg;
2168
2169         retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2170
2171         if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2172                 cdm->status = CAM_DEV_MATCH_ERROR;
2173                 return(0);
2174         }
2175
2176         /*
2177          * If the copy flag is set, copy this peripheral out.
2178          */
2179         if (retval & DM_RET_COPY) {
2180                 int spaceleft, j;
2181
2182                 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2183                         sizeof(struct dev_match_result));
2184
2185                 /*
2186                  * If we don't have enough space to put in another
2187                  * match result, save our position and tell the
2188                  * user there are more devices to check.
2189                  */
2190                 if (spaceleft < sizeof(struct dev_match_result)) {
2191                         bzero(&cdm->pos, sizeof(cdm->pos));
2192                         cdm->pos.position_type = 
2193                                 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2194                                 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2195                                 CAM_DEV_POS_PERIPH;
2196
2197                         cdm->pos.cookie.bus = periph->path->bus;
2198                         cdm->pos.generations[CAM_BUS_GENERATION]=
2199                                 bus_generation;
2200                         cdm->pos.cookie.target = periph->path->target;
2201                         cdm->pos.generations[CAM_TARGET_GENERATION] =
2202                                 periph->path->bus->generation;
2203                         cdm->pos.cookie.device = periph->path->device;
2204                         cdm->pos.generations[CAM_DEV_GENERATION] = 
2205                                 periph->path->target->generation;
2206                         cdm->pos.cookie.periph = periph;
2207                         cdm->pos.generations[CAM_PERIPH_GENERATION] =
2208                                 periph->path->device->generation;
2209                         cdm->status = CAM_DEV_MATCH_MORE;
2210                         return(0);
2211                 }
2212
2213                 j = cdm->num_matches;
2214                 cdm->num_matches++;
2215                 cdm->matches[j].type = DEV_MATCH_PERIPH;
2216                 cdm->matches[j].result.periph_result.path_id =
2217                         periph->path->bus->path_id;
2218                 cdm->matches[j].result.periph_result.target_id =
2219                         periph->path->target->target_id;
2220                 cdm->matches[j].result.periph_result.target_lun =
2221                         periph->path->device->lun_id;
2222                 cdm->matches[j].result.periph_result.unit_number =
2223                         periph->unit_number;
2224                 strncpy(cdm->matches[j].result.periph_result.periph_name,
2225                         periph->periph_name, DEV_IDLEN);
2226         }
2227
2228         return(1);
2229 }
2230
2231 static int
2232 xptedtmatch(struct ccb_dev_match *cdm)
2233 {
2234         int ret;
2235
2236         cdm->num_matches = 0;
2237
2238         /*
2239          * Check the bus list generation.  If it has changed, the user
2240          * needs to reset everything and start over.
2241          */
2242         if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2243          && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2244          && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2245                 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2246                 return(0);
2247         }
2248
2249         if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2250          && (cdm->pos.cookie.bus != NULL))
2251                 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2252                                      xptedtbusfunc, cdm);
2253         else
2254                 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2255
2256         /*
2257          * If we get back 0, that means that we had to stop before fully
2258          * traversing the EDT.  It also means that one of the subroutines
2259          * has set the status field to the proper value.  If we get back 1,
2260          * we've fully traversed the EDT and copied out any matching entries.
2261          */
2262         if (ret == 1)
2263                 cdm->status = CAM_DEV_MATCH_LAST;
2264
2265         return(ret);
2266 }
2267
2268 static int
2269 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2270 {
2271         struct ccb_dev_match *cdm;
2272
2273         cdm = (struct ccb_dev_match *)arg;
2274
2275         if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2276          && (cdm->pos.cookie.pdrv == pdrv)
2277          && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2278          && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2279          && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2280              (*pdrv)->generation)) {
2281                 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2282                 return(0);
2283         }
2284
2285         if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2286          && (cdm->pos.cookie.pdrv == pdrv)
2287          && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2288          && (cdm->pos.cookie.periph != NULL))
2289                 return(xptpdperiphtraverse(pdrv,
2290                                 (struct cam_periph *)cdm->pos.cookie.periph,
2291                                 xptplistperiphfunc, arg));
2292         else
2293                 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2294 }
2295
2296 static int
2297 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2298 {
2299         struct ccb_dev_match *cdm;
2300         dev_match_ret retval;
2301
2302         cdm = (struct ccb_dev_match *)arg;
2303
2304         retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2305
2306         if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2307                 cdm->status = CAM_DEV_MATCH_ERROR;
2308                 return(0);
2309         }
2310
2311         /*
2312          * If the copy flag is set, copy this peripheral out.
2313          */
2314         if (retval & DM_RET_COPY) {
2315                 int spaceleft, j;
2316
2317                 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2318                         sizeof(struct dev_match_result));
2319
2320                 /*
2321                  * If we don't have enough space to put in another
2322                  * match result, save our position and tell the
2323                  * user there are more devices to check.
2324                  */
2325                 if (spaceleft < sizeof(struct dev_match_result)) {
2326                         struct periph_driver **pdrv;
2327
2328                         pdrv = NULL;
2329                         bzero(&cdm->pos, sizeof(cdm->pos));
2330                         cdm->pos.position_type = 
2331                                 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2332                                 CAM_DEV_POS_PERIPH;
2333
2334                         /*
2335                          * This may look a bit non-sensical, but it is
2336                          * actually quite logical.  There are very few
2337                          * peripheral drivers, and bloating every peripheral
2338                          * structure with a pointer back to its parent
2339                          * peripheral driver linker set entry would cost
2340                          * more in the long run than doing this quick lookup.
2341                          */
2342                         for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2343                                 if (strcmp((*pdrv)->driver_name,
2344                                     periph->periph_name) == 0)
2345                                         break;
2346                         }
2347
2348                         if (pdrv == NULL) {
2349                                 cdm->status = CAM_DEV_MATCH_ERROR;
2350                                 return(0);
2351                         }
2352
2353                         cdm->pos.cookie.pdrv = pdrv;
2354                         /*
2355                          * The periph generation slot does double duty, as
2356                          * does the periph pointer slot.  They are used for
2357                          * both edt and pdrv lookups and positioning.
2358                          */
2359                         cdm->pos.cookie.periph = periph;
2360                         cdm->pos.generations[CAM_PERIPH_GENERATION] =
2361                                 (*pdrv)->generation;
2362                         cdm->status = CAM_DEV_MATCH_MORE;
2363                         return(0);
2364                 }
2365
2366                 j = cdm->num_matches;
2367                 cdm->num_matches++;
2368                 cdm->matches[j].type = DEV_MATCH_PERIPH;
2369                 cdm->matches[j].result.periph_result.path_id =
2370                         periph->path->bus->path_id;
2371
2372                 /*
2373                  * The transport layer peripheral doesn't have a target or
2374                  * lun.
2375                  */
2376                 if (periph->path->target)
2377                         cdm->matches[j].result.periph_result.target_id =
2378                                 periph->path->target->target_id;
2379                 else
2380                         cdm->matches[j].result.periph_result.target_id = -1;
2381
2382                 if (periph->path->device)
2383                         cdm->matches[j].result.periph_result.target_lun =
2384                                 periph->path->device->lun_id;
2385                 else
2386                         cdm->matches[j].result.periph_result.target_lun = -1;
2387
2388                 cdm->matches[j].result.periph_result.unit_number =
2389                         periph->unit_number;
2390                 strncpy(cdm->matches[j].result.periph_result.periph_name,
2391                         periph->periph_name, DEV_IDLEN);
2392         }
2393
2394         return(1);
2395 }
2396
2397 static int
2398 xptperiphlistmatch(struct ccb_dev_match *cdm)
2399 {
2400         int ret;
2401
2402         cdm->num_matches = 0;
2403
2404         /*
2405          * At this point in the edt traversal function, we check the bus
2406          * list generation to make sure that no busses have been added or
2407          * removed since the user last sent a XPT_DEV_MATCH ccb through.
2408          * For the peripheral driver list traversal function, however, we
2409          * don't have to worry about new peripheral driver types coming or
2410          * going; they're in a linker set, and therefore can't change
2411          * without a recompile.
2412          */
2413
2414         if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2415          && (cdm->pos.cookie.pdrv != NULL))
2416                 ret = xptpdrvtraverse(
2417                                 (struct periph_driver **)cdm->pos.cookie.pdrv,
2418                                 xptplistpdrvfunc, cdm);
2419         else
2420                 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2421
2422         /*
2423          * If we get back 0, that means that we had to stop before fully
2424          * traversing the peripheral driver tree.  It also means that one of
2425          * the subroutines has set the status field to the proper value.  If
2426          * we get back 1, we've fully traversed the EDT and copied out any
2427          * matching entries.
2428          */
2429         if (ret == 1)
2430                 cdm->status = CAM_DEV_MATCH_LAST;
2431
2432         return(ret);
2433 }
2434
2435 static int
2436 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2437 {
2438         struct cam_eb *bus, *next_bus;
2439         int retval;
2440
2441         retval = 1;
2442
2443         for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2444              bus != NULL;
2445              bus = next_bus) {
2446                 next_bus = TAILQ_NEXT(bus, links);
2447
2448                 retval = tr_func(bus, arg);
2449                 if (retval == 0)
2450                         return(retval);
2451         }
2452
2453         return(retval);
2454 }
2455
2456 static int
2457 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2458                   xpt_targetfunc_t *tr_func, void *arg)
2459 {
2460         struct cam_et *target, *next_target;
2461         int retval;
2462
2463         retval = 1;
2464         for (target = (start_target ? start_target :
2465                        TAILQ_FIRST(&bus->et_entries));
2466              target != NULL; target = next_target) {
2467
2468                 next_target = TAILQ_NEXT(target, links);
2469
2470                 retval = tr_func(target, arg);
2471
2472                 if (retval == 0)
2473                         return(retval);
2474         }
2475
2476         return(retval);
2477 }
2478
2479 static int
2480 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2481                   xpt_devicefunc_t *tr_func, void *arg)
2482 {
2483         struct cam_ed *device, *next_device;
2484         int retval;
2485
2486         retval = 1;
2487         for (device = (start_device ? start_device :
2488                        TAILQ_FIRST(&target->ed_entries));
2489              device != NULL;
2490              device = next_device) {
2491
2492                 next_device = TAILQ_NEXT(device, links);
2493
2494                 retval = tr_func(device, arg);
2495
2496                 if (retval == 0)
2497                         return(retval);
2498         }
2499
2500         return(retval);
2501 }
2502
2503 static int
2504 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2505                   xpt_periphfunc_t *tr_func, void *arg)
2506 {
2507         struct cam_periph *periph, *next_periph;
2508         int retval;
2509
2510         retval = 1;
2511
2512         for (periph = (start_periph ? start_periph :
2513                        SLIST_FIRST(&device->periphs));
2514              periph != NULL;
2515              periph = next_periph) {
2516
2517                 next_periph = SLIST_NEXT(periph, periph_links);
2518
2519                 retval = tr_func(periph, arg);
2520                 if (retval == 0)
2521                         return(retval);
2522         }
2523
2524         return(retval);
2525 }
2526
2527 static int
2528 xptpdrvtraverse(struct periph_driver **start_pdrv,
2529                 xpt_pdrvfunc_t *tr_func, void *arg)
2530 {
2531         struct periph_driver **pdrv;
2532         int retval;
2533
2534         retval = 1;
2535
2536         /*
2537          * We don't traverse the peripheral driver list like we do the
2538          * other lists, because it is a linker set, and therefore cannot be
2539          * changed during runtime.  If the peripheral driver list is ever
2540          * re-done to be something other than a linker set (i.e. it can
2541          * change while the system is running), the list traversal should
2542          * be modified to work like the other traversal functions.
2543          */
2544         for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2545              *pdrv != NULL; pdrv++) {
2546                 retval = tr_func(pdrv, arg);
2547
2548                 if (retval == 0)
2549                         return(retval);
2550         }
2551
2552         return(retval);
2553 }
2554
2555 static int
2556 xptpdperiphtraverse(struct periph_driver **pdrv,
2557                     struct cam_periph *start_periph,
2558                     xpt_periphfunc_t *tr_func, void *arg)
2559 {
2560         struct cam_periph *periph, *next_periph;
2561         int retval;
2562
2563         retval = 1;
2564
2565         for (periph = (start_periph ? start_periph :
2566              TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2567              periph = next_periph) {
2568
2569                 next_periph = TAILQ_NEXT(periph, unit_links);
2570
2571                 retval = tr_func(periph, arg);
2572                 if (retval == 0)
2573                         return(retval);
2574         }
2575         return(retval);
2576 }
2577
2578 static int
2579 xptdefbusfunc(struct cam_eb *bus, void *arg)
2580 {
2581         struct xpt_traverse_config *tr_config;
2582
2583         tr_config = (struct xpt_traverse_config *)arg;
2584
2585         if (tr_config->depth == XPT_DEPTH_BUS) {
2586                 xpt_busfunc_t *tr_func;
2587
2588                 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2589
2590                 return(tr_func(bus, tr_config->tr_arg));
2591         } else
2592                 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2593 }
2594
2595 static int
2596 xptdeftargetfunc(struct cam_et *target, void *arg)
2597 {
2598         struct xpt_traverse_config *tr_config;
2599
2600         tr_config = (struct xpt_traverse_config *)arg;
2601
2602         if (tr_config->depth == XPT_DEPTH_TARGET) {
2603                 xpt_targetfunc_t *tr_func;
2604
2605                 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2606
2607                 return(tr_func(target, tr_config->tr_arg));
2608         } else
2609                 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2610 }
2611
2612 static int
2613 xptdefdevicefunc(struct cam_ed *device, void *arg)
2614 {
2615         struct xpt_traverse_config *tr_config;
2616
2617         tr_config = (struct xpt_traverse_config *)arg;
2618
2619         if (tr_config->depth == XPT_DEPTH_DEVICE) {
2620                 xpt_devicefunc_t *tr_func;
2621
2622                 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2623
2624                 return(tr_func(device, tr_config->tr_arg));
2625         } else
2626                 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2627 }
2628
2629 static int
2630 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2631 {
2632         struct xpt_traverse_config *tr_config;
2633         xpt_periphfunc_t *tr_func;
2634
2635         tr_config = (struct xpt_traverse_config *)arg;
2636
2637         tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2638
2639         /*
2640          * Unlike the other default functions, we don't check for depth
2641          * here.  The peripheral driver level is the last level in the EDT,
2642          * so if we're here, we should execute the function in question.
2643          */
2644         return(tr_func(periph, tr_config->tr_arg));
2645 }
2646
2647 /*
2648  * Execute the given function for every bus in the EDT.
2649  */
2650 static int
2651 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2652 {
2653         struct xpt_traverse_config tr_config;
2654
2655         tr_config.depth = XPT_DEPTH_BUS;
2656         tr_config.tr_func = tr_func;
2657         tr_config.tr_arg = arg;
2658
2659         return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2660 }
2661
2662 #ifdef notusedyet
2663 /*
2664  * Execute the given function for every target in the EDT.
2665  */
2666 static int
2667 xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2668 {
2669         struct xpt_traverse_config tr_config;
2670
2671         tr_config.depth = XPT_DEPTH_TARGET;
2672         tr_config.tr_func = tr_func;
2673         tr_config.tr_arg = arg;
2674
2675         return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2676 }
2677 #endif /* notusedyet */
2678
2679 /*
2680  * Execute the given function for every device in the EDT.
2681  */
2682 static int
2683 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2684 {
2685         struct xpt_traverse_config tr_config;
2686
2687         tr_config.depth = XPT_DEPTH_DEVICE;
2688         tr_config.tr_func = tr_func;
2689         tr_config.tr_arg = arg;
2690
2691         return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2692 }
2693
2694 #ifdef notusedyet
2695 /*
2696  * Execute the given function for every peripheral in the EDT.
2697  */
2698 static int
2699 xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2700 {
2701         struct xpt_traverse_config tr_config;
2702
2703         tr_config.depth = XPT_DEPTH_PERIPH;
2704         tr_config.tr_func = tr_func;
2705         tr_config.tr_arg = arg;
2706
2707         return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2708 }
2709 #endif /* notusedyet */
2710
2711 static int
2712 xptsetasyncfunc(struct cam_ed *device, void *arg)
2713 {
2714         struct cam_path path;
2715         struct ccb_getdev cgd;
2716         struct async_node *cur_entry;
2717
2718         cur_entry = (struct async_node *)arg;
2719
2720         /*
2721          * Don't report unconfigured devices (Wildcard devs,
2722          * devices only for target mode, device instances
2723          * that have been invalidated but are waiting for
2724          * their last reference count to be released).
2725          */
2726         if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2727                 return (1);
2728
2729         xpt_compile_path(&path,
2730                          NULL,
2731                          device->target->bus->path_id,
2732                          device->target->target_id,
2733                          device->lun_id);
2734         xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2735         cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2736         xpt_action((union ccb *)&cgd);
2737         cur_entry->callback(cur_entry->callback_arg,
2738                             AC_FOUND_DEVICE,
2739                             &path, &cgd);
2740         xpt_release_path(&path);
2741
2742         return(1);
2743 }
2744
2745 static int
2746 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2747 {
2748         struct cam_path path;
2749         struct ccb_pathinq cpi;
2750         struct async_node *cur_entry;
2751
2752         cur_entry = (struct async_node *)arg;
2753
2754         xpt_compile_path(&path, /*periph*/NULL,
2755                          bus->sim->path_id,
2756                          CAM_TARGET_WILDCARD,
2757                          CAM_LUN_WILDCARD);
2758         xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2759         cpi.ccb_h.func_code = XPT_PATH_INQ;
2760         xpt_action((union ccb *)&cpi);
2761         cur_entry->callback(cur_entry->callback_arg,
2762                             AC_PATH_REGISTERED,
2763                             &path, &cpi);
2764         xpt_release_path(&path);
2765
2766         return(1);
2767 }
2768
2769 void
2770 xpt_action(union ccb *start_ccb)
2771 {
2772         int iopl;
2773
2774         CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2775
2776         start_ccb->ccb_h.status = CAM_REQ_INPROG;
2777
2778         iopl = splsoftcam();
2779         switch (start_ccb->ccb_h.func_code) {
2780         case XPT_SCSI_IO:
2781         {
2782 #ifdef CAMDEBUG
2783                 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2784                 struct cam_path *path;
2785
2786                 path = start_ccb->ccb_h.path;
2787 #endif
2788
2789                 /*
2790                  * For the sake of compatibility with SCSI-1
2791                  * devices that may not understand the identify
2792                  * message, we include lun information in the
2793                  * second byte of all commands.  SCSI-1 specifies
2794                  * that luns are a 3 bit value and reserves only 3
2795                  * bits for lun information in the CDB.  Later
2796                  * revisions of the SCSI spec allow for more than 8
2797                  * luns, but have deprecated lun information in the
2798                  * CDB.  So, if the lun won't fit, we must omit.
2799                  *
2800                  * Also be aware that during initial probing for devices,
2801                  * the inquiry information is unknown but initialized to 0.
2802                  * This means that this code will be exercised while probing
2803                  * devices with an ANSI revision greater than 2.
2804                  */
2805                 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2806                  && start_ccb->ccb_h.target_lun < 8
2807                  && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2808
2809                         start_ccb->csio.cdb_io.cdb_bytes[1] |=
2810                             start_ccb->ccb_h.target_lun << 5;
2811                 }
2812                 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2813                 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2814                           scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2815                                        &path->device->inq_data),
2816                           scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2817                                           cdb_str, sizeof(cdb_str))));
2818                 /* FALLTHROUGH */
2819         }
2820         case XPT_TARGET_IO:
2821         case XPT_CONT_TARGET_IO:
2822                 start_ccb->csio.sense_resid = 0;
2823                 start_ccb->csio.resid = 0;
2824                 /* FALLTHROUGH */
2825         case XPT_RESET_DEV:
2826         case XPT_ENG_EXEC:
2827         {
2828                 struct cam_path *path;
2829                 int s;
2830                 int runq;
2831
2832                 path = start_ccb->ccb_h.path;
2833                 s = splsoftcam();
2834
2835                 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2836                 if (path->device->qfrozen_cnt == 0)
2837                         runq = xpt_schedule_dev_sendq(path->bus, path->device);
2838                 else
2839                         runq = 0;
2840                 splx(s);
2841                 if (runq != 0)
2842                         xpt_run_dev_sendq(path->bus);
2843                 break;
2844         }
2845         case XPT_SET_TRAN_SETTINGS:
2846         {
2847                 xpt_set_transfer_settings(&start_ccb->cts,
2848                                           start_ccb->ccb_h.path->device,
2849                                           /*async_update*/FALSE);
2850                 break;
2851         }
2852         case XPT_CALC_GEOMETRY:
2853         {
2854                 struct cam_sim *sim;
2855
2856                 /* Filter out garbage */
2857                 if (start_ccb->ccg.block_size == 0
2858                  || start_ccb->ccg.volume_size == 0) {
2859                         start_ccb->ccg.cylinders = 0;
2860                         start_ccb->ccg.heads = 0;
2861                         start_ccb->ccg.secs_per_track = 0;
2862                         start_ccb->ccb_h.status = CAM_REQ_CMP;
2863                         break;
2864                 }
2865 #ifdef PC98
2866                 /*
2867                  * In a PC-98 system, geometry translation depens on
2868                  * the "real" device geometry obtained from mode page 4.
2869                  * SCSI geometry translation is performed in the
2870                  * initialization routine of the SCSI BIOS and the result
2871                  * stored in host memory.  If the translation is available
2872                  * in host memory, use it.  If not, rely on the default
2873                  * translation the device driver performs.
2874                  */
2875                 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2876                         start_ccb->ccb_h.status = CAM_REQ_CMP;
2877                         break;
2878                 }
2879 #endif
2880                 sim = start_ccb->ccb_h.path->bus->sim;
2881                 (*(sim->sim_action))(sim, start_ccb);
2882                 break;
2883         }
2884         case XPT_ABORT:
2885         {
2886                 union ccb* abort_ccb;
2887                 int s;                          
2888
2889                 abort_ccb = start_ccb->cab.abort_ccb;
2890                 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2891
2892                         if (abort_ccb->ccb_h.pinfo.index >= 0) {
2893                                 struct cam_ccbq *ccbq;
2894
2895                                 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
2896                                 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2897                                 abort_ccb->ccb_h.status =
2898                                     CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2899                                 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2900                                 s = splcam();
2901                                 xpt_done(abort_ccb);
2902                                 splx(s);
2903                                 start_ccb->ccb_h.status = CAM_REQ_CMP;
2904                                 break;
2905                         }
2906                         if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2907                          && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2908                                 /*
2909                                  * We've caught this ccb en route to
2910                                  * the SIM.  Flag it for abort and the
2911                                  * SIM will do so just before starting
2912                                  * real work on the CCB.
2913                                  */
2914                                 abort_ccb->ccb_h.status =
2915                                     CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2916                                 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2917                                 start_ccb->ccb_h.status = CAM_REQ_CMP;
2918                                 break;
2919                         }
2920                 } 
2921                 if (XPT_FC_IS_QUEUED(abort_ccb)
2922                  && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2923                         /*
2924                          * It's already completed but waiting
2925                          * for our SWI to get to it.
2926                          */
2927                         start_ccb->ccb_h.status = CAM_UA_ABORT;
2928                         break;
2929                 }
2930                 /*
2931                  * If we weren't able to take care of the abort request
2932                  * in the XPT, pass the request down to the SIM for processing.
2933                  */
2934                 /* FALLTHROUGH */
2935         }
2936         case XPT_ACCEPT_TARGET_IO:
2937         case XPT_EN_LUN:
2938         case XPT_IMMED_NOTIFY:
2939         case XPT_NOTIFY_ACK:
2940         case XPT_GET_TRAN_SETTINGS:
2941         case XPT_RESET_BUS:
2942         {
2943                 struct cam_sim *sim;
2944
2945                 sim = start_ccb->ccb_h.path->bus->sim;
2946                 (*(sim->sim_action))(sim, start_ccb);
2947                 break;
2948         }
2949         case XPT_PATH_INQ:
2950         {
2951                 struct cam_sim *sim;
2952
2953                 sim = start_ccb->ccb_h.path->bus->sim;
2954                 (*(sim->sim_action))(sim, start_ccb);
2955                 break;
2956         }
2957         case XPT_PATH_STATS:
2958                 start_ccb->cpis.last_reset =
2959                         start_ccb->ccb_h.path->bus->last_reset;
2960                 start_ccb->ccb_h.status = CAM_REQ_CMP;
2961                 break;
2962         case XPT_GDEV_TYPE:
2963         {
2964                 struct cam_ed *dev;
2965                 int s;
2966
2967                 dev = start_ccb->ccb_h.path->device;
2968                 s = splcam();
2969                 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2970                         start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2971                 } else {
2972                         struct ccb_getdev *cgd;
2973                         struct cam_eb *bus;
2974                         struct cam_et *tar;
2975
2976                         cgd = &start_ccb->cgd;
2977                         bus = cgd->ccb_h.path->bus;
2978                         tar = cgd->ccb_h.path->target;
2979                         cgd->inq_data = dev->inq_data;
2980                         cgd->ccb_h.status = CAM_REQ_CMP;
2981                         cgd->serial_num_len = dev->serial_num_len;
2982                         if ((dev->serial_num_len > 0)
2983                          && (dev->serial_num != NULL))
2984                                 bcopy(dev->serial_num, cgd->serial_num,
2985                                       dev->serial_num_len);
2986                 }
2987                 splx(s);
2988                 break; 
2989         }
2990         case XPT_GDEV_STATS:
2991         {
2992                 struct cam_ed *dev;
2993                 int s;
2994
2995                 dev = start_ccb->ccb_h.path->device;
2996                 s = splcam();
2997                 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2998                         start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2999                 } else {
3000                         struct ccb_getdevstats *cgds;
3001                         struct cam_eb *bus;
3002                         struct cam_et *tar;
3003
3004                         cgds = &start_ccb->cgds;
3005                         bus = cgds->ccb_h.path->bus;
3006                         tar = cgds->ccb_h.path->target;
3007                         cgds->dev_openings = dev->ccbq.dev_openings;
3008                         cgds->dev_active = dev->ccbq.dev_active;
3009                         cgds->devq_openings = dev->ccbq.devq_openings;
3010                         cgds->devq_queued = dev->ccbq.queue.entries;
3011                         cgds->held = dev->ccbq.held;
3012                         cgds->last_reset = tar->last_reset;
3013                         cgds->maxtags = dev->quirk->maxtags;
3014                         cgds->mintags = dev->quirk->mintags;
3015                         if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3016                                 cgds->last_reset = bus->last_reset;
3017                         cgds->ccb_h.status = CAM_REQ_CMP;
3018                 }
3019                 splx(s);
3020                 break;
3021         }
3022         case XPT_GDEVLIST:
3023         {
3024                 struct cam_periph       *nperiph;
3025                 struct periph_list      *periph_head;
3026                 struct ccb_getdevlist   *cgdl;
3027                 int                     i;
3028                 int                     s;
3029                 struct cam_ed           *device;
3030                 int                     found;
3031
3032
3033                 found = 0;
3034
3035                 /*
3036                  * Don't want anyone mucking with our data.
3037                  */
3038                 s = splcam();
3039                 device = start_ccb->ccb_h.path->device;
3040                 periph_head = &device->periphs;
3041                 cgdl = &start_ccb->cgdl;
3042
3043                 /*
3044                  * Check and see if the list has changed since the user
3045                  * last requested a list member.  If so, tell them that the
3046                  * list has changed, and therefore they need to start over 
3047                  * from the beginning.
3048                  */
3049                 if ((cgdl->index != 0) && 
3050                     (cgdl->generation != device->generation)) {
3051                         cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3052                         splx(s);
3053                         break;
3054                 }
3055
3056                 /*
3057                  * Traverse the list of peripherals and attempt to find 
3058                  * the requested peripheral.
3059                  */
3060                 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3061                      (nperiph != NULL) && (i <= cgdl->index);
3062                      nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3063                         if (i == cgdl->index) {
3064                                 strncpy(cgdl->periph_name,
3065                                         nperiph->periph_name,
3066                                         DEV_IDLEN);
3067                                 cgdl->unit_number = nperiph->unit_number;
3068                                 found = 1;
3069                         }
3070                 }
3071                 if (found == 0) {
3072                         cgdl->status = CAM_GDEVLIST_ERROR;
3073                         splx(s);
3074                         break;
3075                 }
3076
3077                 if (nperiph == NULL)
3078                         cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3079                 else
3080                         cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3081
3082                 cgdl->index++;
3083                 cgdl->generation = device->generation;
3084
3085                 splx(s);
3086                 cgdl->ccb_h.status = CAM_REQ_CMP;
3087                 break;
3088         }
3089         case XPT_DEV_MATCH:
3090         {
3091                 int s;
3092                 dev_pos_type position_type;
3093                 struct ccb_dev_match *cdm;
3094                 int ret;
3095
3096                 cdm = &start_ccb->cdm;
3097
3098                 /*
3099                  * Prevent EDT changes while we traverse it.
3100                  */
3101                 s = splcam();
3102                 /*
3103                  * There are two ways of getting at information in the EDT.
3104                  * The first way is via the primary EDT tree.  It starts
3105                  * with a list of busses, then a list of targets on a bus,
3106                  * then devices/luns on a target, and then peripherals on a
3107                  * device/lun.  The "other" way is by the peripheral driver
3108                  * lists.  The peripheral driver lists are organized by
3109                  * peripheral driver.  (obviously)  So it makes sense to
3110                  * use the peripheral driver list if the user is looking
3111                  * for something like "da1", or all "da" devices.  If the
3112                  * user is looking for something on a particular bus/target
3113                  * or lun, it's generally better to go through the EDT tree.
3114                  */
3115
3116                 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3117                         position_type = cdm->pos.position_type;
3118                 else {
3119                         int i;
3120
3121                         position_type = CAM_DEV_POS_NONE;
3122
3123                         for (i = 0; i < cdm->num_patterns; i++) {
3124                                 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3125                                  ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3126                                         position_type = CAM_DEV_POS_EDT;
3127                                         break;
3128                                 }
3129                         }
3130
3131                         if (cdm->num_patterns == 0)
3132                                 position_type = CAM_DEV_POS_EDT;
3133                         else if (position_type == CAM_DEV_POS_NONE)
3134                                 position_type = CAM_DEV_POS_PDRV;
3135                 }
3136
3137                 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3138                 case CAM_DEV_POS_EDT:
3139                         ret = xptedtmatch(cdm);
3140                         break;
3141                 case CAM_DEV_POS_PDRV:
3142                         ret = xptperiphlistmatch(cdm);
3143                         break;
3144                 default:
3145                         cdm->status = CAM_DEV_MATCH_ERROR;
3146                         break;
3147                 }
3148
3149                 splx(s);
3150
3151                 if (cdm->status == CAM_DEV_MATCH_ERROR)
3152                         start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3153                 else
3154                         start_ccb->ccb_h.status = CAM_REQ_CMP;
3155
3156                 break;
3157         }
3158         case XPT_SASYNC_CB:
3159         {
3160                 struct ccb_setasync *csa;
3161                 struct async_node *cur_entry;
3162                 struct async_list *async_head;
3163                 u_int32_t added;
3164                 int s;
3165
3166                 csa = &start_ccb->csa;
3167                 added = csa->event_enable;
3168                 async_head = &csa->ccb_h.path->device->asyncs;
3169
3170                 /*
3171                  * If there is already an entry for us, simply
3172                  * update it.
3173                  */
3174                 s = splcam();
3175                 cur_entry = SLIST_FIRST(async_head);
3176                 while (cur_entry != NULL) {
3177                         if ((cur_entry->callback_arg == csa->callback_arg)
3178                          && (cur_entry->callback == csa->callback))
3179                                 break;
3180                         cur_entry = SLIST_NEXT(cur_entry, links);
3181                 }
3182
3183                 if (cur_entry != NULL) {
3184                         /*
3185                          * If the request has no flags set,
3186                          * remove the entry.
3187                          */
3188                         added &= ~cur_entry->event_enable;
3189                         if (csa->event_enable == 0) {
3190                                 SLIST_REMOVE(async_head, cur_entry,
3191                                              async_node, links);
3192                                 csa->ccb_h.path->device->refcount--;
3193                                 free(cur_entry, M_DEVBUF);
3194                         } else {
3195                                 cur_entry->event_enable = csa->event_enable;
3196                         }
3197                 } else {
3198                         cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
3199                                            M_NOWAIT);
3200                         if (cur_entry == NULL) {
3201                                 splx(s);
3202                                 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3203                                 break;
3204                         }
3205                         cur_entry->event_enable = csa->event_enable;
3206                         cur_entry->callback_arg = csa->callback_arg;
3207                         cur_entry->callback = csa->callback;
3208                         SLIST_INSERT_HEAD(async_head, cur_entry, links);
3209                         csa->ccb_h.path->device->refcount++;
3210                 }
3211
3212                 if ((added & AC_FOUND_DEVICE) != 0) {
3213                         /*
3214                          * Get this peripheral up to date with all
3215                          * the currently existing devices.
3216                          */
3217                         xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3218                 }
3219                 if ((added & AC_PATH_REGISTERED) != 0) {
3220                         /*
3221                          * Get this peripheral up to date with all
3222                          * the currently existing busses.
3223                          */
3224                         xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3225                 }
3226                 splx(s);
3227                 start_ccb->ccb_h.status = CAM_REQ_CMP;
3228                 break;
3229         }
3230         case XPT_REL_SIMQ:
3231         {
3232                 struct ccb_relsim *crs;
3233                 struct cam_ed *dev;
3234                 int s;
3235
3236                 crs = &start_ccb->crs;
3237                 dev = crs->ccb_h.path->device;
3238                 if (dev == NULL) {
3239
3240                         crs->ccb_h.status = CAM_DEV_NOT_THERE;
3241                         break;
3242                 }
3243
3244                 s = splcam();
3245
3246                 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3247
3248                         if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3249
3250                                 /* Don't ever go below one opening */
3251                                 if (crs->openings > 0) {
3252                                         xpt_dev_ccbq_resize(crs->ccb_h.path,
3253                                                             crs->openings);
3254
3255                                         if (bootverbose) {
3256                                                 xpt_print_path(crs->ccb_h.path);
3257                                                 printf("tagged openings "
3258                                                        "now %d\n",
3259                                                        crs->openings);
3260                                         }
3261                                 }
3262                         }
3263                 }
3264
3265                 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3266
3267                         if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3268
3269                                 /*
3270                                  * Just extend the old timeout and decrement
3271                                  * the freeze count so that a single timeout
3272                                  * is sufficient for releasing the queue.
3273                                  */
3274                                 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3275                                 untimeout(xpt_release_devq_timeout,
3276                                           dev, dev->c_handle);
3277                         } else {
3278
3279                                 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3280                         }
3281
3282                         dev->c_handle =
3283                                 timeout(xpt_release_devq_timeout,
3284                                         dev,
3285                                         (crs->release_timeout * hz) / 1000);
3286
3287                         dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3288
3289                 }
3290
3291                 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3292
3293                         if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3294                                 /*
3295                                  * Decrement the freeze count so that a single
3296                                  * completion is still sufficient to unfreeze
3297                                  * the queue.
3298                                  */
3299                                 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3300                         } else {
3301                                 
3302                                 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3303                                 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3304                         }
3305                 }
3306
3307                 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3308
3309                         if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3310                          || (dev->ccbq.dev_active == 0)) {
3311
3312                                 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3313                         } else {
3314                                 
3315                                 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3316                                 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3317                         }
3318                 }
3319                 splx(s);
3320                 
3321                 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3322
3323                         xpt_release_devq(crs->ccb_h.path, /*count*/1,
3324                                          /*run_queue*/TRUE);
3325                 }
3326                 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3327                 start_ccb->ccb_h.status = CAM_REQ_CMP;
3328                 break;
3329         }
3330         case XPT_SCAN_BUS:
3331                 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3332                 break;
3333         case XPT_SCAN_LUN:
3334                 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3335                              start_ccb->ccb_h.path, start_ccb->crcn.flags,
3336                              start_ccb);
3337                 break;
3338         case XPT_DEBUG: {
3339 #ifdef CAMDEBUG
3340                 int s;
3341                 
3342                 s = splcam();
3343 #ifdef CAM_DEBUG_DELAY
3344                 cam_debug_delay = CAM_DEBUG_DELAY;
3345 #endif
3346                 cam_dflags = start_ccb->cdbg.flags;
3347                 if (cam_dpath != NULL) {
3348                         xpt_free_path(cam_dpath);
3349                         cam_dpath = NULL;
3350                 }
3351
3352                 if (cam_dflags != CAM_DEBUG_NONE) {
3353                         if (xpt_create_path(&cam_dpath, xpt_periph,
3354                                             start_ccb->ccb_h.path_id,
3355                                             start_ccb->ccb_h.target_id,
3356                                             start_ccb->ccb_h.target_lun) !=
3357                                             CAM_REQ_CMP) {
3358                                 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3359                                 cam_dflags = CAM_DEBUG_NONE;
3360                         } else {
3361                                 start_ccb->ccb_h.status = CAM_REQ_CMP;
3362                                 xpt_print_path(cam_dpath);
3363                                 printf("debugging flags now %x\n", cam_dflags);
3364                         }
3365                 } else {
3366                         cam_dpath = NULL;
3367                         start_ccb->ccb_h.status = CAM_REQ_CMP;
3368                 }
3369                 splx(s);
3370 #else /* !CAMDEBUG */
3371                 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3372 #endif /* CAMDEBUG */
3373                 break;
3374         }
3375         case XPT_NOOP:
3376                 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3377                         xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3378                 start_ccb->ccb_h.status = CAM_REQ_CMP;
3379                 break;
3380         default:
3381         case XPT_SDEV_TYPE:
3382         case XPT_TERM_IO:
3383         case XPT_ENG_INQ:
3384                 /* XXX Implement */
3385                 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3386                 break;
3387         }
3388         splx(iopl);
3389 }
3390
3391 void
3392 xpt_polled_action(union ccb *start_ccb)
3393 {
3394         int       s;
3395         u_int32_t timeout;
3396         struct    cam_sim *sim; 
3397         struct    cam_devq *devq;
3398         struct    cam_ed *dev;
3399
3400         timeout = start_ccb->ccb_h.timeout;
3401         sim = start_ccb->ccb_h.path->bus->sim;
3402         devq = sim->devq;
3403         dev = start_ccb->ccb_h.path->device;
3404
3405         s = splcam();
3406
3407         /*
3408          * Steal an opening so that no other queued requests
3409          * can get it before us while we simulate interrupts.
3410          */
3411         dev->ccbq.devq_openings--;
3412         dev->ccbq.dev_openings--;       
3413         
3414         while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3415            && (--timeout > 0)) {
3416                 DELAY(1000);
3417                 (*(sim->sim_poll))(sim);
3418                 camisr(&cam_netq);
3419                 camisr(&cam_bioq);
3420         }
3421         
3422         dev->ccbq.devq_openings++;
3423         dev->ccbq.dev_openings++;
3424         
3425         if (timeout != 0) {
3426                 xpt_action(start_ccb);
3427                 while(--timeout > 0) {
3428                         (*(sim->sim_poll))(sim);
3429                         camisr(&cam_netq);
3430                         camisr(&cam_bioq);
3431                         if ((start_ccb->ccb_h.status  & CAM_STATUS_MASK)
3432                             != CAM_REQ_INPROG)
3433                                 break;
3434                         DELAY(1000);
3435                 }
3436                 if (timeout == 0) {
3437                         /*
3438                          * XXX Is it worth adding a sim_timeout entry
3439                          * point so we can attempt recovery?  If
3440                          * this is only used for dumps, I don't think
3441                          * it is.
3442                          */
3443                         start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3444                 }
3445         } else {
3446                 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3447         }
3448         splx(s);
3449 }
3450         
3451 /*
3452  * Schedule a peripheral driver to receive a ccb when it's
3453  * target device has space for more transactions.
3454  */
3455 void
3456 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3457 {
3458         struct cam_ed *device;
3459         int s;
3460         int runq;
3461
3462         CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3463         device = perph->path->device;
3464         s = splsoftcam();
3465         if (periph_is_queued(perph)) {
3466                 /* Simply reorder based on new priority */
3467                 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3468                           ("   change priority to %d\n", new_priority));
3469                 if (new_priority < perph->pinfo.priority) {
3470                         camq_change_priority(&device->drvq,
3471                                              perph->pinfo.index,
3472                                              new_priority);
3473                 }
3474                 runq = 0;
3475         } else {
3476                 /* New entry on the queue */
3477                 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3478                           ("   added periph to queue\n"));
3479                 perph->pinfo.priority = new_priority;
3480                 perph->pinfo.generation = ++device->drvq.generation;
3481                 camq_insert(&device->drvq, &perph->pinfo);
3482                 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3483         }
3484         splx(s);
3485         if (runq != 0) {
3486                 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3487                           ("   calling xpt_run_devq\n"));
3488                 xpt_run_dev_allocq(perph->path->bus);
3489         }
3490 }
3491
3492
3493 /*
3494  * Schedule a device to run on a given queue.
3495  * If the device was inserted as a new entry on the queue,
3496  * return 1 meaning the device queue should be run. If we
3497  * were already queued, implying someone else has already
3498  * started the queue, return 0 so the caller doesn't attempt
3499  * to run the queue.  Must be run at either splsoftcam
3500  * (or splcam since that encompases splsoftcam).
3501  */
3502 static int
3503 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3504                  u_int32_t new_priority)
3505 {
3506         int retval;
3507         u_int32_t old_priority;
3508
3509         CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3510
3511         old_priority = pinfo->priority;
3512
3513         /*
3514          * Are we already queued?
3515          */
3516         if (pinfo->index != CAM_UNQUEUED_INDEX) {
3517                 /* Simply reorder based on new priority */
3518                 if (new_priority < old_priority) {
3519                         camq_change_priority(queue, pinfo->index,
3520                                              new_priority);
3521                         CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3522                                         ("changed priority to %d\n",
3523                                          new_priority));
3524                 }
3525                 retval = 0;
3526         } else {
3527                 /* New entry on the queue */
3528                 if (new_priority < old_priority)
3529                         pinfo->priority = new_priority;
3530
3531                 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3532                                 ("Inserting onto queue\n"));
3533                 pinfo->generation = ++queue->generation;
3534                 camq_insert(queue, pinfo);
3535                 retval = 1;
3536         }
3537         return (retval);
3538 }
3539
3540 static void
3541 xpt_run_dev_allocq(struct cam_eb *bus)
3542 {
3543         struct  cam_devq *devq;
3544         int     s;
3545
3546         CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3547         devq = bus->sim->devq;
3548
3549         CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3550                         ("   qfrozen_cnt == 0x%x, entries == %d, "
3551                          "openings == %d, active == %d\n",
3552                          devq->alloc_queue.qfrozen_cnt,
3553                          devq->alloc_queue.entries,
3554                          devq->alloc_openings,
3555                          devq->alloc_active));
3556
3557         s = splsoftcam();
3558         devq->alloc_queue.qfrozen_cnt++;
3559         while ((devq->alloc_queue.entries > 0)
3560             && (devq->alloc_openings > 0)
3561             && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3562                 struct  cam_ed_qinfo *qinfo;
3563                 struct  cam_ed *device;
3564                 union   ccb *work_ccb;
3565                 struct  cam_periph *drv;
3566                 struct  camq *drvq;
3567                 
3568                 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3569                                                            CAMQ_HEAD);
3570                 device = qinfo->device;
3571
3572                 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3573                                 ("running device %p\n", device));
3574
3575                 drvq = &device->drvq;
3576
3577 #ifdef CAMDEBUG
3578                 if (drvq->entries <= 0) {
3579                         panic("xpt_run_dev_allocq: "
3580                               "Device on queue without any work to do");
3581                 }
3582 #endif
3583                 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3584                         devq->alloc_openings--;
3585                         devq->alloc_active++;
3586                         drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3587                         splx(s);
3588                         xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3589                                       drv->pinfo.priority);
3590                         CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3591                                         ("calling periph start\n"));
3592                         drv->periph_start(drv, work_ccb);
3593                 } else {
3594                         /*
3595                          * Malloc failure in alloc_ccb
3596                          */
3597                         /*
3598                          * XXX add us to a list to be run from free_ccb
3599                          * if we don't have any ccbs active on this
3600                          * device queue otherwise we may never get run
3601                          * again.
3602                          */
3603                         break;
3604                 }
3605         
3606                 /* Raise IPL for possible insertion and test at top of loop */
3607                 s = splsoftcam();
3608
3609                 if (drvq->entries > 0) {
3610                         /* We have more work.  Attempt to reschedule */
3611                         xpt_schedule_dev_allocq(bus, device);
3612                 }
3613         }
3614         devq->alloc_queue.qfrozen_cnt--;
3615         splx(s);
3616 }
3617
3618 static void
3619 xpt_run_dev_sendq(struct cam_eb *bus)
3620 {
3621         struct  cam_devq *devq;
3622         int     s;
3623
3624         CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3625         
3626         devq = bus->sim->devq;
3627
3628         s = splcam();
3629         devq->send_queue.qfrozen_cnt++;
3630         splx(s);
3631         s = splsoftcam();
3632         while ((devq->send_queue.entries > 0)
3633             && (devq->send_openings > 0)) {
3634                 struct  cam_ed_qinfo *qinfo;
3635                 struct  cam_ed *device;
3636                 union ccb *work_ccb;
3637                 struct  cam_sim *sim;
3638                 int     ospl;
3639
3640                 ospl = splcam();
3641                 if (devq->send_queue.qfrozen_cnt > 1) {
3642                         splx(ospl);
3643                         break;
3644                 }
3645
3646                 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3647                                                            CAMQ_HEAD);
3648                 device = qinfo->device;
3649
3650                 /*
3651                  * If the device has been "frozen", don't attempt
3652                  * to run it.
3653                  */
3654                 if (device->qfrozen_cnt > 0) {
3655                         splx(ospl);
3656                         continue;
3657                 }
3658
3659                 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3660                                 ("running device %p\n", device));
3661
3662                 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3663                 if (work_ccb == NULL) {
3664                         printf("device on run queue with no ccbs???");
3665                         splx(ospl);
3666                         continue;
3667                 }
3668
3669                 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3670
3671                         if (num_highpower <= 0) {
3672                                 /*
3673                                  * We got a high power command, but we
3674                                  * don't have any available slots.  Freeze
3675                                  * the device queue until we have a slot
3676                                  * available.
3677                                  */
3678                                 device->qfrozen_cnt++;
3679                                 STAILQ_INSERT_TAIL(&highpowerq, 
3680                                                    &work_ccb->ccb_h, 
3681                                                    xpt_links.stqe);
3682
3683                                 splx(ospl);
3684                                 continue;
3685                         } else {
3686                                 /*
3687                                  * Consume a high power slot while
3688                                  * this ccb runs.
3689                                  */
3690                                 num_highpower--;
3691                         }
3692                 }
3693                 devq->active_dev = device;
3694                 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3695
3696                 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3697                 splx(ospl);
3698
3699                 devq->send_openings--;
3700                 devq->send_active++;            
3701                 
3702                 if (device->ccbq.queue.entries > 0)
3703                         xpt_schedule_dev_sendq(bus, device);
3704
3705                 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3706                         /*
3707                          * The client wants to freeze the queue
3708                          * after this CCB is sent.
3709                          */
3710                         ospl = splcam();
3711                         device->qfrozen_cnt++;
3712                         splx(ospl);
3713                 }
3714                 
3715                 splx(s);
3716
3717                 /* In Target mode, the peripheral driver knows best... */
3718                 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3719                         if ((device->inq_flags & SID_CmdQue) != 0
3720                          && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3721                                 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3722                         else
3723                                 /*
3724                                  * Clear this in case of a retried CCB that
3725                                  * failed due to a rejected tag.
3726                                  */
3727                                 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3728                 }
3729
3730                 /*
3731                  * Device queues can be shared among multiple sim instances
3732                  * that reside on different busses.  Use the SIM in the queue
3733                  * CCB's path, rather than the one in the bus that was passed
3734                  * into this function.
3735                  */
3736                 sim = work_ccb->ccb_h.path->bus->sim;
3737                 (*(sim->sim_action))(sim, work_ccb);
3738
3739                 ospl = splcam();
3740                 devq->active_dev = NULL;
3741                 splx(ospl);
3742                 /* Raise IPL for possible insertion and test at top of loop */
3743                 s = splsoftcam();
3744         }
3745         splx(s);
3746         s = splcam();
3747         devq->send_queue.qfrozen_cnt--;
3748         splx(s);
3749 }
3750
3751 /*
3752  * This function merges stuff from the slave ccb into the master ccb, while
3753  * keeping important fields in the master ccb constant.
3754  */
3755 void
3756 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3757 {
3758         /*
3759          * Pull fields that are valid for peripheral drivers to set
3760          * into the master CCB along with the CCB "payload".
3761          */
3762         master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3763         master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3764         master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3765         master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3766         bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3767               sizeof(union ccb) - sizeof(struct ccb_hdr));
3768 }
3769
3770 void
3771 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3772 {
3773         CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3774         ccb_h->pinfo.priority = priority;
3775         ccb_h->path = path;
3776         ccb_h->path_id = path->bus->path_id;
3777         if (path->target)
3778                 ccb_h->target_id = path->target->target_id;
3779         else
3780                 ccb_h->target_id = CAM_TARGET_WILDCARD;
3781         if (path->device) {
3782                 ccb_h->target_lun = path->device->lun_id;
3783                 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3784         } else {
3785                 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3786         }
3787         ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3788         ccb_h->flags = 0;
3789 }
3790
3791 /* Path manipulation functions */
3792 cam_status
3793 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3794                 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3795 {
3796         struct     cam_path *path;
3797         cam_status status;
3798
3799         path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
3800
3801         if (path == NULL) {
3802                 status = CAM_RESRC_UNAVAIL;
3803                 return(status);
3804         }
3805         status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3806         if (status != CAM_REQ_CMP) {
3807                 free(path, M_DEVBUF);
3808                 path = NULL;
3809         }
3810         *new_path_ptr = path;
3811         return (status);
3812 }
3813
3814 static cam_status
3815 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3816                  path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3817 {
3818         struct       cam_eb *bus;
3819         struct       cam_et *target;
3820         struct       cam_ed *device;
3821         cam_status   status;
3822         int          s;
3823
3824         status = CAM_REQ_CMP;   /* Completed without error */
3825         target = NULL;          /* Wildcarded */
3826         device = NULL;          /* Wildcarded */
3827
3828         /*
3829          * We will potentially modify the EDT, so block interrupts
3830          * that may attempt to create cam paths.
3831          */
3832         s = splcam();
3833         bus = xpt_find_bus(path_id);
3834         if (bus == NULL) {
3835                 status = CAM_PATH_INVALID;
3836         } else {
3837                 target = xpt_find_target(bus, target_id);
3838                 if (target == NULL) {
3839                         /* Create one */
3840                         struct cam_et *new_target;
3841
3842                         new_target = xpt_alloc_target(bus, target_id);
3843                         if (new_target == NULL) {
3844                                 status = CAM_RESRC_UNAVAIL;
3845                         } else {
3846                                 target = new_target;
3847                         }
3848                 }
3849                 if (target != NULL) {
3850                         device = xpt_find_device(target, lun_id);
3851                         if (device == NULL) {
3852                                 /* Create one */
3853                                 struct cam_ed *new_device;
3854
3855                                 new_device = xpt_alloc_device(bus,
3856                                                               target,
3857                                                               lun_id);
3858                                 if (new_device == NULL) {
3859                                         status = CAM_RESRC_UNAVAIL;
3860                                 } else {
3861                                         device = new_device;
3862                                 }
3863                         }
3864                 }
3865         }
3866         splx(s);
3867
3868         /*
3869          * Only touch the user's data if we are successful.
3870          */
3871         if (status == CAM_REQ_CMP) {
3872                 new_path->periph = perph;
3873                 new_path->bus = bus;
3874                 new_path->target = target;
3875                 new_path->device = device;
3876                 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3877         } else {
3878                 if (device != NULL)
3879                         xpt_release_device(bus, target, device);
3880                 if (target != NULL)
3881                         xpt_release_target(bus, target);
3882                 if (bus != NULL)
3883                         xpt_release_bus(bus);
3884         }
3885         return (status);
3886 }
3887
3888 static void
3889 xpt_release_path(struct cam_path *path)
3890 {
3891         CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3892         if (path->device != NULL) {
3893                 xpt_release_device(path->bus, path->target, path->device);
3894                 path->device = NULL;
3895         }
3896         if (path->target != NULL) {
3897                 xpt_release_target(path->bus, path->target);
3898                 path->target = NULL;
3899         }
3900         if (path->bus != NULL) {
3901                 xpt_release_bus(path->bus);
3902                 path->bus = NULL;
3903         }
3904 }
3905
3906 void
3907 xpt_free_path(struct cam_path *path)
3908 {
3909         CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3910         xpt_release_path(path);
3911         free(path, M_DEVBUF);
3912 }
3913
3914
3915 /*
3916  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3917  * in path1, 2 for match with wildcards in path2.
3918  */
3919 int
3920 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3921 {
3922         int retval = 0;
3923
3924         if (path1->bus != path2->bus) {
3925                 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3926                         retval = 1;
3927                 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3928                         retval = 2;
3929                 else
3930                         return (-1);
3931         }
3932         if (path1->target != path2->target) {
3933                 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3934                         if (retval == 0)
3935                                 retval = 1;
3936                 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3937                         retval = 2;
3938                 else
3939                         return (-1);
3940         }
3941         if (path1->device != path2->device) {
3942                 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3943                         if (retval == 0)
3944                                 retval = 1;
3945                 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3946                         retval = 2;
3947                 else
3948                         return (-1);
3949         }
3950         return (retval);
3951 }
3952
3953 void
3954 xpt_print_path(struct cam_path *path)
3955 {
3956         if (path == NULL)
3957                 printf("(nopath): ");
3958         else {
3959                 if (path->periph != NULL)
3960                         printf("(%s%d:", path->periph->periph_name,
3961                                path->periph->unit_number);
3962                 else
3963                         printf("(noperiph:");
3964
3965                 if (path->bus != NULL)
3966                         printf("%s%d:%d:", path->bus->sim->sim_name,
3967                                path->bus->sim->unit_number,
3968                                path->bus->sim->bus_id);
3969                 else
3970                         printf("nobus:");
3971
3972                 if (path->target != NULL)
3973                         printf("%d:", path->target->target_id);
3974                 else
3975                         printf("X:");
3976
3977                 if (path->device != NULL)
3978                         printf("%d): ", path->device->lun_id);
3979                 else
3980                         printf("X): ");
3981         }
3982 }
3983
3984 path_id_t
3985 xpt_path_path_id(struct cam_path *path)
3986 {
3987         return(path->bus->path_id);
3988 }
3989
3990 target_id_t
3991 xpt_path_target_id(struct cam_path *path)
3992 {
3993         if (path->target != NULL)
3994                 return (path->target->target_id);
3995         else
3996                 return (CAM_TARGET_WILDCARD);
3997 }
3998
3999 lun_id_t
4000 xpt_path_lun_id(struct cam_path *path)
4001 {
4002         if (path->device != NULL)
4003                 return (path->device->lun_id);
4004         else
4005                 return (CAM_LUN_WILDCARD);
4006 }
4007
4008 struct cam_sim *
4009 xpt_path_sim(struct cam_path *path)
4010 {
4011         return (path->bus->sim);
4012 }
4013
4014 struct cam_periph*
4015 xpt_path_periph(struct cam_path *path)
4016 {
4017         return (path->periph);
4018 }
4019
4020 /*
4021  * Release a CAM control block for the caller.  Remit the cost of the structure
4022  * to the device referenced by the path.  If the this device had no 'credits'
4023  * and peripheral drivers have registered async callbacks for this notification
4024  * call them now.
4025  */
4026 void
4027 xpt_release_ccb(union ccb *free_ccb)
4028 {
4029         int      s;
4030         struct   cam_path *path;
4031         struct   cam_ed *device;
4032         struct   cam_eb *bus;
4033
4034         CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4035         path = free_ccb->ccb_h.path;
4036         device = path->device;
4037         bus = path->bus;
4038         s = splsoftcam();
4039         cam_ccbq_release_opening(&device->ccbq);
4040         if (xpt_ccb_count > xpt_max_ccbs) {
4041                 xpt_free_ccb(free_ccb);
4042                 xpt_ccb_count--;
4043         } else {
4044                 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4045         }
4046         bus->sim->devq->alloc_openings++;
4047         bus->sim->devq->alloc_active--;
4048         /* XXX Turn this into an inline function - xpt_run_device?? */
4049         if ((device_is_alloc_queued(device) == 0)
4050          && (device->drvq.entries > 0)) {
4051                 xpt_schedule_dev_allocq(bus, device);
4052         }
4053         splx(s);
4054         if (dev_allocq_is_runnable(bus->sim->devq))
4055                 xpt_run_dev_allocq(bus);
4056 }
4057
4058 /* Functions accessed by SIM drivers */
4059
4060 /*
4061  * A sim structure, listing the SIM entry points and instance
4062  * identification info is passed to xpt_bus_register to hook the SIM
4063  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
4064  * for this new bus and places it in the array of busses and assigns
4065  * it a path_id.  The path_id may be influenced by "hard wiring"
4066  * information specified by the user.  Once interrupt services are
4067  * availible, the bus will be probed.
4068  */
4069 int32_t
4070 xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4071 {
4072         struct cam_eb *new_bus;
4073         struct cam_eb *old_bus;
4074         struct ccb_pathinq cpi;
4075         int s;
4076
4077         sim->bus_id = bus;
4078         new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4079                                           M_DEVBUF, M_NOWAIT);
4080         if (new_bus == NULL) {
4081                 /* Couldn't satisfy request */
4082                 return (CAM_RESRC_UNAVAIL);
4083         }
4084
4085         if (strcmp(sim->sim_name, "xpt") != 0) {
4086
4087                 sim->path_id =
4088                     xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4089         }
4090
4091         TAILQ_INIT(&new_bus->et_entries);
4092         new_bus->path_id = sim->path_id;
4093         new_bus->sim = sim;
4094         timevalclear(&new_bus->last_reset);
4095         new_bus->flags = 0;
4096         new_bus->refcount = 1;  /* Held until a bus_deregister event */
4097         new_bus->generation = 0;
4098         s = splcam();
4099         old_bus = TAILQ_FIRST(&xpt_busses);
4100         while (old_bus != NULL
4101             && old_bus->path_id < new_bus->path_id)
4102                 old_bus = TAILQ_NEXT(old_bus, links);
4103         if (old_bus != NULL)
4104                 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4105         else
4106                 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4107         bus_generation++;
4108         splx(s);
4109
4110         /* Notify interested parties */
4111         if (sim->path_id != CAM_XPT_PATH_ID) {
4112                 struct cam_path path;
4113
4114                 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4115                                  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4116                 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4117                 cpi.ccb_h.func_code = XPT_PATH_INQ;
4118                 xpt_action((union ccb *)&cpi);
4119                 xpt_async(AC_PATH_REGISTERED, xpt_periph->path, &cpi);
4120                 xpt_release_path(&path);
4121         }
4122         return (CAM_SUCCESS);
4123 }
4124
4125 int32_t
4126 xpt_bus_deregister(path_id_t pathid)
4127 {
4128         struct cam_path bus_path;
4129         cam_status status;
4130
4131         status = xpt_compile_path(&bus_path, NULL, pathid,
4132                                   CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4133         if (status != CAM_REQ_CMP)
4134                 return (status);
4135
4136         xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4137         xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4138         
4139         /* Release the reference count held while registered. */
4140         xpt_release_bus(bus_path.bus);
4141         xpt_release_path(&bus_path);
4142
4143         return (CAM_REQ_CMP);
4144 }
4145
4146 static path_id_t
4147 xptnextfreepathid(void)
4148 {
4149         struct cam_eb *bus;
4150         path_id_t pathid;
4151         char *strval;
4152
4153         pathid = 0;
4154         bus = TAILQ_FIRST(&xpt_busses);
4155 retry:
4156         /* Find an unoccupied pathid */
4157         while (bus != NULL
4158             && bus->path_id <= pathid) {
4159                 if (bus->path_id == pathid)
4160                         pathid++;
4161                 bus = TAILQ_NEXT(bus, links);
4162         }
4163
4164         /*
4165          * Ensure that this pathid is not reserved for
4166          * a bus that may be registered in the future.
4167          */
4168         if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4169                 ++pathid;
4170                 /* Start the search over */
4171                 goto retry;
4172         }
4173         return (pathid);
4174 }
4175
4176 static path_id_t
4177 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4178 {
4179         path_id_t pathid;
4180         int i, dunit, val;
4181         char buf[32];
4182
4183         pathid = CAM_XPT_PATH_ID;
4184         snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4185         i = -1;
4186         while ((i = resource_query_string(i, "at", buf)) != -1) {
4187                 if (strcmp(resource_query_name(i), "scbus")) {
4188                         /* Avoid a bit of foot shooting. */
4189                         continue;
4190                 }
4191                 dunit = resource_query_unit(i);
4192                 if (dunit < 0)          /* unwired?! */
4193                         continue;
4194                 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4195                         if (sim_bus == val) {
4196                                 pathid = dunit;
4197                                 break;
4198                         }
4199                 } else if (sim_bus == 0) {
4200                         /* Unspecified matches bus 0 */
4201                         pathid = dunit;
4202                         break;
4203                 } else {
4204                         printf("Ambiguous scbus configuration for %s%d "
4205                                "bus %d, cannot wire down.  The kernel "
4206                                "config entry for scbus%d should "
4207                                "specify a controller bus.\n"
4208                                "Scbus will be assigned dynamically.\n",
4209                                sim_name, sim_unit, sim_bus, dunit);
4210                         break;
4211                 }
4212         }
4213
4214         if (pathid == CAM_XPT_PATH_ID)
4215                 pathid = xptnextfreepathid();
4216         return (pathid);
4217 }
4218
4219 void
4220 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4221 {
4222         struct cam_eb *bus;
4223         struct cam_et *target, *next_target;
4224         struct cam_ed *device, *next_device;
4225         int s;
4226
4227         CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4228
4229         /*
4230          * Most async events come from a CAM interrupt context.  In
4231          * a few cases, the error recovery code at the peripheral layer,
4232          * which may run from our SWI or a process context, may signal
4233          * deferred events with a call to xpt_async. Ensure async
4234          * notifications are serialized by blocking cam interrupts.
4235          */
4236         s = splcam();
4237
4238         bus = path->bus;
4239
4240         if (async_code == AC_BUS_RESET) { 
4241                 int s;
4242
4243                 s = splclock();
4244                 /* Update our notion of when the last reset occurred */
4245                 microtime(&bus->last_reset);
4246                 splx(s);
4247         }
4248
4249         for (target = TAILQ_FIRST(&bus->et_entries);
4250              target != NULL;
4251              target = next_target) {
4252
4253                 next_target = TAILQ_NEXT(target, links);
4254
4255                 if (path->target != target
4256                  && path->target->target_id != CAM_TARGET_WILDCARD
4257                  && target->target_id != CAM_TARGET_WILDCARD)
4258                         continue;
4259
4260                 if (async_code == AC_SENT_BDR) {
4261                         int s;
4262
4263                         /* Update our notion of when the last reset occurred */
4264                         s = splclock();
4265                         microtime(&path->target->last_reset);
4266                         splx(s);
4267                 }
4268
4269                 for (device = TAILQ_FIRST(&target->ed_entries);
4270                      device != NULL;
4271                      device = next_device) {
4272
4273                         next_device = TAILQ_NEXT(device, links);
4274
4275                         if (path->device != device 
4276                          && path->device->lun_id != CAM_LUN_WILDCARD
4277                          && device->lun_id != CAM_LUN_WILDCARD)
4278                                 continue;
4279
4280                         xpt_dev_async(async_code, bus, target,
4281                                       device, async_arg);
4282
4283                         xpt_async_bcast(&device->asyncs, async_code,
4284                                         path, async_arg);
4285                 }
4286         }
4287         
4288         /*
4289          * If this wasn't a fully wildcarded async, tell all
4290          * clients that want all async events.
4291          */
4292         if (bus != xpt_periph->path->bus)
4293                 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4294                                 path, async_arg);
4295         splx(s);
4296 }
4297
4298 static void
4299 xpt_async_bcast(struct async_list *async_head,
4300                 u_int32_t async_code,
4301                 struct cam_path *path, void *async_arg)
4302 {
4303         struct async_node *cur_entry;
4304
4305         cur_entry = SLIST_FIRST(async_head);
4306         while (cur_entry != NULL) {
4307                 struct async_node *next_entry;
4308                 /*
4309                  * Grab the next list entry before we call the current
4310                  * entry's callback.  This is because the callback function
4311                  * can delete its async callback entry.
4312                  */
4313                 next_entry = SLIST_NEXT(cur_entry, links);
4314                 if ((cur_entry->event_enable & async_code) != 0)
4315                         cur_entry->callback(cur_entry->callback_arg,
4316                                             async_code, path,
4317                                             async_arg);
4318                 cur_entry = next_entry;
4319         }
4320 }
4321
4322 /*
4323  * Handle any per-device event notifications that require action by the XPT.
4324  */
4325 static void
4326 xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4327               struct cam_ed *device, void *async_arg)
4328 {
4329         cam_status status;
4330         struct cam_path newpath;
4331
4332         /*
4333          * We only need to handle events for real devices.
4334          */
4335         if (target->target_id == CAM_TARGET_WILDCARD
4336          || device->lun_id == CAM_LUN_WILDCARD)
4337                 return;
4338
4339         /*
4340          * We need our own path with wildcards expanded to
4341          * handle certain types of events.
4342          */
4343         if ((async_code == AC_SENT_BDR)
4344          || (async_code == AC_BUS_RESET)
4345          || (async_code == AC_INQ_CHANGED))
4346                 status = xpt_compile_path(&newpath, NULL,
4347                                           bus->path_id,
4348                                           target->target_id,
4349                                           device->lun_id);
4350         else
4351                 status = CAM_REQ_CMP_ERR;
4352
4353         if (status == CAM_REQ_CMP) {
4354
4355                 /*
4356                  * Allow transfer negotiation to occur in a
4357                  * tag free environment.
4358                  */
4359                 if (async_code == AC_SENT_BDR
4360                  || async_code == AC_BUS_RESET)
4361                         xpt_toggle_tags(&newpath);
4362
4363                 if (async_code == AC_INQ_CHANGED) {
4364                         /*
4365                          * We've sent a start unit command, or
4366                          * something similar to a device that
4367                          * may have caused its inquiry data to
4368                          * change. So we re-scan the device to
4369                          * refresh the inquiry data for it.
4370                          */
4371                         xpt_scan_lun(newpath.periph, &newpath,
4372                                      CAM_EXPECT_INQ_CHANGE, NULL);
4373                 }
4374                 xpt_release_path(&newpath);
4375         } else if (async_code == AC_LOST_DEVICE) {
4376                 device->flags |= CAM_DEV_UNCONFIGURED;
4377         } else if (async_code == AC_TRANSFER_NEG) {
4378                 struct ccb_trans_settings *settings;
4379
4380                 settings = (struct ccb_trans_settings *)async_arg;
4381                 xpt_set_transfer_settings(settings, device,
4382                                           /*async_update*/TRUE);
4383         }
4384 }
4385
4386 u_int32_t
4387 xpt_freeze_devq(struct cam_path *path, u_int count)
4388 {
4389         int s;
4390         struct ccb_hdr *ccbh;
4391
4392         s = splcam();
4393         path->device->qfrozen_cnt += count;
4394
4395         /*
4396          * Mark the last CCB in the queue as needing
4397          * to be requeued if the driver hasn't
4398          * changed it's state yet.  This fixes a race
4399          * where a ccb is just about to be queued to
4400          * a controller driver when it's interrupt routine
4401          * freezes the queue.  To completly close the
4402          * hole, controller drives must check to see
4403          * if a ccb's status is still CAM_REQ_INPROG
4404          * under spl protection just before they queue
4405          * the CCB.  See ahc_action/ahc_freeze_devq for
4406          * an example.
4407          */
4408         ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4409         if (ccbh && ccbh->status == CAM_REQ_INPROG)
4410                 ccbh->status = CAM_REQUEUE_REQ;
4411         splx(s);
4412         return (path->device->qfrozen_cnt);
4413 }
4414
4415 u_int32_t
4416 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4417 {
4418         sim->devq->send_queue.qfrozen_cnt += count;
4419         if (sim->devq->active_dev != NULL) {
4420                 struct ccb_hdr *ccbh;
4421                 
4422                 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4423                                   ccb_hdr_tailq);
4424                 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4425                         ccbh->status = CAM_REQUEUE_REQ;
4426         }
4427         return (sim->devq->send_queue.qfrozen_cnt);
4428 }
4429
4430 static void
4431 xpt_release_devq_timeout(void *arg)
4432 {
4433         struct cam_ed *device;
4434
4435         device = (struct cam_ed *)arg;
4436
4437         xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4438 }
4439
4440 void
4441 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4442 {
4443         xpt_release_devq_device(path->device, count, run_queue);
4444 }
4445
4446 static void
4447 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4448 {
4449         int     rundevq;
4450         int     s0, s1;
4451
4452         rundevq = 0;
4453         s0 = splsoftcam();
4454         s1 = splcam();
4455         if (dev->qfrozen_cnt > 0) {
4456
4457                 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4458                 dev->qfrozen_cnt -= count;
4459                 if (dev->qfrozen_cnt == 0) {
4460
4461                         /*
4462                          * No longer need to wait for a successful
4463                          * command completion.
4464                          */
4465                         dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4466
4467                         /*
4468                          * Remove any timeouts that might be scheduled
4469                          * to release this queue.
4470                          */
4471                         if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4472                                 untimeout(xpt_release_devq_timeout, dev,
4473                                           dev->c_handle);
4474                                 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4475                         }
4476
4477                         /*
4478                          * Now that we are unfrozen schedule the
4479                          * device so any pending transactions are
4480                          * run.
4481                          */
4482                         if ((dev->ccbq.queue.entries > 0)
4483                          && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4484                          && (run_queue != 0)) {
4485                                 rundevq = 1;
4486                         }
4487                 }
4488         }
4489         splx(s1);
4490         if (rundevq != 0)
4491                 xpt_run_dev_sendq(dev->target->bus);
4492         splx(s0);
4493 }
4494
4495 void
4496 xpt_release_simq(struct cam_sim *sim, int run_queue)
4497 {
4498         int     s;
4499         struct  camq *sendq;
4500
4501         sendq = &(sim->devq->send_queue);
4502         s = splcam();
4503         if (sendq->qfrozen_cnt > 0) {
4504
4505                 sendq->qfrozen_cnt--;
4506                 if (sendq->qfrozen_cnt == 0) {
4507                         struct cam_eb *bus;
4508
4509                         /*
4510                          * If there is a timeout scheduled to release this
4511                          * sim queue, remove it.  The queue frozen count is
4512                          * already at 0.
4513                          */
4514                         if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4515                                 untimeout(xpt_release_simq_timeout, sim,
4516                                           sim->c_handle);
4517                                 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4518                         }
4519                         bus = xpt_find_bus(sim->path_id);
4520                         splx(s);
4521
4522                         if (run_queue) {
4523                                 /*
4524                                  * Now that we are unfrozen run the send queue.
4525                                  */
4526                                 xpt_run_dev_sendq(bus);
4527                         }
4528                         xpt_release_bus(bus);
4529                 } else
4530                         splx(s);
4531         } else
4532                 splx(s);
4533 }
4534
4535 static void
4536 xpt_release_simq_timeout(void *arg)
4537 {
4538         struct cam_sim *sim;
4539
4540         sim = (struct cam_sim *)arg;
4541         xpt_release_simq(sim, /* run_queue */ TRUE);
4542 }
4543
4544 void
4545 xpt_done(union ccb *done_ccb)
4546 {
4547         int s;
4548
4549         s = splcam();
4550
4551         CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4552         if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4553                 /*
4554                  * Queue up the request for handling by our SWI handler
4555                  * any of the "non-immediate" type of ccbs.
4556                  */
4557                 switch (done_ccb->ccb_h.path->periph->type) {
4558                 case CAM_PERIPH_BIO:
4559                         TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4560                                           sim_links.tqe);
4561                         done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4562                         swi_sched(cambio_ih, SWI_NOSWITCH);
4563                         break;
4564                 case CAM_PERIPH_NET:
4565                         TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4566                                           sim_links.tqe);
4567                         done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4568                         swi_sched(camnet_ih, SWI_NOSWITCH);
4569                         break;
4570                 }
4571         }
4572         splx(s);
4573 }
4574
4575 union ccb *
4576 xpt_alloc_ccb()
4577 {
4578         union ccb *new_ccb;
4579
4580         new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
4581         return (new_ccb);
4582 }
4583
4584 void
4585 xpt_free_ccb(union ccb *free_ccb)
4586 {
4587         free(free_ccb, M_DEVBUF);
4588 }
4589
4590
4591
4592 /* Private XPT functions */
4593
4594 /*
4595  * Get a CAM control block for the caller. Charge the structure to the device
4596  * referenced by the path.  If the this device has no 'credits' then the
4597  * device already has the maximum number of outstanding operations under way
4598  * and we return NULL. If we don't have sufficient resources to allocate more
4599  * ccbs, we also return NULL.
4600  */
4601 static union ccb *
4602 xpt_get_ccb(struct cam_ed *device)
4603 {
4604         union ccb *new_ccb;
4605         int s;
4606
4607         s = splsoftcam();
4608         if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4609                 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
4610                 if (new_ccb == NULL) {
4611                         splx(s);
4612                         return (NULL);
4613                 }
4614                 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4615                 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4616                                   xpt_links.sle);
4617                 xpt_ccb_count++;
4618         }
4619         cam_ccbq_take_opening(&device->ccbq);
4620         SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4621         splx(s);
4622         return (new_ccb);
4623 }
4624
4625 static void
4626 xpt_release_bus(struct cam_eb *bus)
4627 {
4628         int s;
4629
4630         s = splcam();
4631         if ((--bus->refcount == 0)
4632          && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4633                 TAILQ_REMOVE(&xpt_busses, bus, links);
4634                 bus_generation++;
4635                 splx(s);
4636                 free(bus, M_DEVBUF);
4637         } else
4638                 splx(s);
4639 }
4640
4641 static struct cam_et *
4642 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4643 {
4644         struct cam_et *target;
4645
4646         target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
4647         if (target != NULL) {
4648                 struct cam_et *cur_target;
4649
4650                 TAILQ_INIT(&target->ed_entries);
4651                 target->bus = bus;
4652                 target->target_id = target_id;
4653                 target->refcount = 1;
4654                 target->generation = 0;
4655                 timevalclear(&target->last_reset);
4656                 /*
4657                  * Hold a reference to our parent bus so it
4658                  * will not go away before we do.
4659                  */
4660                 bus->refcount++;
4661
4662                 /* Insertion sort into our bus's target list */
4663                 cur_target = TAILQ_FIRST(&bus->et_entries);
4664                 while (cur_target != NULL && cur_target->target_id < target_id)
4665                         cur_target = TAILQ_NEXT(cur_target, links);
4666
4667                 if (cur_target != NULL) {
4668                         TAILQ_INSERT_BEFORE(cur_target, target, links);
4669                 } else {
4670                         TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4671                 }
4672                 bus->generation++;
4673         }
4674         return (target);
4675 }
4676
4677 static void
4678 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4679 {
4680         int s;
4681
4682         s = splcam();
4683         if ((--target->refcount == 0)
4684          && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4685                 TAILQ_REMOVE(&bus->et_entries, target, links);
4686                 bus->generation++;
4687                 splx(s);
4688                 free(target, M_DEVBUF);
4689                 xpt_release_bus(bus);
4690         } else
4691                 splx(s);
4692 }
4693
4694 static struct cam_ed *
4695 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4696 {
4697         struct     cam_ed *device;
4698         struct     cam_devq *devq;
4699         cam_status status;
4700
4701         /* Make space for us in the device queue on our bus */
4702         devq = bus->sim->devq;
4703         status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4704
4705         if (status != CAM_REQ_CMP) {
4706                 device = NULL;
4707         } else {
4708                 device = (struct cam_ed *)malloc(sizeof(*device),
4709                                                  M_DEVBUF, M_NOWAIT);
4710         }
4711
4712         if (device != NULL) {
4713                 struct cam_ed *cur_device;
4714
4715                 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4716                 device->alloc_ccb_entry.device = device;
4717                 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4718                 device->send_ccb_entry.device = device;
4719                 device->target = target;
4720                 device->lun_id = lun_id;
4721                 /* Initialize our queues */
4722                 if (camq_init(&device->drvq, 0) != 0) {
4723                         free(device, M_DEVBUF);
4724                         return (NULL);
4725                 }
4726                 if (cam_ccbq_init(&device->ccbq,
4727                                   bus->sim->max_dev_openings) != 0) {
4728                         camq_fini(&device->drvq);
4729                         free(device, M_DEVBUF);
4730                         return (NULL);
4731                 }
4732                 SLIST_INIT(&device->asyncs);
4733                 SLIST_INIT(&device->periphs);
4734                 device->generation = 0;
4735                 device->owner = NULL;
4736                 /*
4737                  * Take the default quirk entry until we have inquiry
4738                  * data and can determine a better quirk to use.
4739                  */
4740                 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4741                 bzero(&device->inq_data, sizeof(device->inq_data));
4742                 device->inq_flags = 0;
4743                 device->queue_flags = 0;
4744                 device->serial_num = NULL;
4745                 device->serial_num_len = 0;
4746                 device->qfrozen_cnt = 0;
4747                 device->flags = CAM_DEV_UNCONFIGURED;
4748                 device->tag_delay_count = 0;
4749                 device->refcount = 1;
4750                 callout_handle_init(&device->c_handle);
4751
4752                 /*
4753                  * Hold a reference to our parent target so it
4754                  * will not go away before we do.
4755                  */
4756                 target->refcount++;
4757
4758                 /*
4759                  * XXX should be limited by number of CCBs this bus can
4760                  * do.
4761                  */
4762                 xpt_max_ccbs += device->ccbq.devq_openings;
4763                 /* Insertion sort into our target's device list */
4764                 cur_device = TAILQ_FIRST(&target->ed_entries);
4765                 while (cur_device != NULL && cur_device->lun_id < lun_id)
4766                         cur_device = TAILQ_NEXT(cur_device, links);
4767                 if (cur_device != NULL) {
4768                         TAILQ_INSERT_BEFORE(cur_device, device, links);
4769                 } else {
4770                         TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4771                 }
4772                 target->generation++;
4773         }
4774         return (device);
4775 }
4776
4777 static void
4778 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
4779                    struct cam_ed *device)
4780 {
4781         int s;
4782
4783         s = splcam();
4784         if ((--device->refcount == 0)
4785          && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
4786                 struct cam_devq *devq;
4787
4788                 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4789                  || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4790                         panic("Removing device while still queued for ccbs");
4791
4792                 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4793                                 untimeout(xpt_release_devq_timeout, device,
4794                                           device->c_handle);
4795
4796                 TAILQ_REMOVE(&target->ed_entries, device,links);
4797                 target->generation++;
4798                 xpt_max_ccbs -= device->ccbq.devq_openings;
4799                 /* Release our slot in the devq */
4800                 devq = bus->sim->devq;
4801                 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4802                 splx(s);
4803                 free(device, M_DEVBUF);
4804                 xpt_release_target(bus, target);
4805         } else
4806                 splx(s);
4807 }
4808
4809 static u_int32_t
4810 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4811 {
4812         int     s;
4813         int     diff;
4814         int     result;
4815         struct  cam_ed *dev;
4816
4817         dev = path->device;
4818         s = splsoftcam();
4819
4820         diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4821         result = cam_ccbq_resize(&dev->ccbq, newopenings);
4822         if (result == CAM_REQ_CMP && (diff < 0)) {
4823                 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4824         }
4825         /* Adjust the global limit */
4826         xpt_max_ccbs += diff;
4827         splx(s);
4828         return (result);
4829 }
4830
4831 static struct cam_eb *
4832 xpt_find_bus(path_id_t path_id)
4833 {
4834         struct cam_eb *bus;
4835
4836         for (bus = TAILQ_FIRST(&xpt_busses);
4837              bus != NULL;
4838              bus = TAILQ_NEXT(bus, links)) {
4839                 if (bus->path_id == path_id) {
4840                         bus->refcount++;
4841                         break;
4842                 }
4843         }
4844         return (bus);
4845 }
4846
4847 static struct cam_et *
4848 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4849 {
4850         struct cam_et *target;
4851
4852         for (target = TAILQ_FIRST(&bus->et_entries);
4853              target != NULL;
4854              target = TAILQ_NEXT(target, links)) {
4855                 if (target->target_id == target_id) {
4856                         target->refcount++;
4857                         break;
4858                 }
4859         }
4860         return (target);
4861 }
4862
4863 static struct cam_ed *
4864 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4865 {
4866         struct cam_ed *device;
4867
4868         for (device = TAILQ_FIRST(&target->ed_entries);
4869              device != NULL;
4870              device = TAILQ_NEXT(device, links)) {
4871                 if (device->lun_id == lun_id) {
4872                         device->refcount++;
4873                         break;
4874                 }
4875         }
4876         return (device);
4877 }
4878
4879 typedef struct {
4880         union   ccb *request_ccb;
4881         struct  ccb_pathinq *cpi;
4882         int     pending_count;
4883 } xpt_scan_bus_info;
4884
4885 /*
4886  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
4887  * As the scan progresses, xpt_scan_bus is used as the
4888  * callback on completion function.
4889  */
4890 static void
4891 xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
4892 {
4893         CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4894                   ("xpt_scan_bus\n"));
4895         switch (request_ccb->ccb_h.func_code) {
4896         case XPT_SCAN_BUS:
4897         {
4898                 xpt_scan_bus_info *scan_info;
4899                 union   ccb *work_ccb;
4900                 struct  cam_path *path;
4901                 u_int   i;
4902                 u_int   max_target;
4903                 u_int   initiator_id;
4904
4905                 /* Find out the characteristics of the bus */
4906                 work_ccb = xpt_alloc_ccb();
4907                 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
4908                               request_ccb->ccb_h.pinfo.priority);
4909                 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
4910                 xpt_action(work_ccb);
4911                 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
4912                         request_ccb->ccb_h.status = work_ccb->ccb_h.status;
4913                         xpt_free_ccb(work_ccb);
4914                         xpt_done(request_ccb);
4915                         return;
4916                 }
4917
4918                 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
4919                         /*
4920                          * Can't scan the bus on an adapter that
4921                          * cannot perform the initiator role.
4922                          */
4923                         request_ccb->ccb_h.status = CAM_REQ_CMP;
4924                         xpt_free_ccb(work_ccb);
4925                         xpt_done(request_ccb);
4926                         return;
4927                 }
4928
4929                 /* Save some state for use while we probe for devices */
4930                 scan_info = (xpt_scan_bus_info *)
4931                     malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
4932                 scan_info->request_ccb = request_ccb;
4933                 scan_info->cpi = &work_ccb->cpi;
4934
4935                 /* Cache on our stack so we can work asynchronously */
4936                 max_target = scan_info->cpi->max_target;
4937                 initiator_id = scan_info->cpi->initiator_id;
4938
4939                 /*
4940                  * Don't count the initiator if the
4941                  * initiator is addressable.
4942                  */
4943                 scan_info->pending_count = max_target + 1;
4944                 if (initiator_id <= max_target)
4945                         scan_info->pending_count--;
4946
4947                 for (i = 0; i <= max_target; i++) {
4948                         cam_status status;
4949                         if (i == initiator_id)
4950                                 continue;
4951
4952                         status = xpt_create_path(&path, xpt_periph,
4953                                                  request_ccb->ccb_h.path_id,
4954                                                  i, 0);
4955                         if (status != CAM_REQ_CMP) {
4956                                 printf("xpt_scan_bus: xpt_create_path failed"
4957                                        " with status %#x, bus scan halted\n",
4958                                        status);
4959                                 break;
4960                         }
4961                         work_ccb = xpt_alloc_ccb();
4962                         xpt_setup_ccb(&work_ccb->ccb_h, path,
4963                                       request_ccb->ccb_h.pinfo.priority);
4964                         work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
4965                         work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
4966                         work_ccb->ccb_h.ppriv_ptr0 = scan_info;
4967                         work_ccb->crcn.flags = request_ccb->crcn.flags;
4968 #if 0
4969                         printf("xpt_scan_bus: probing %d:%d:%d\n",
4970                                 request_ccb->ccb_h.path_id, i, 0);
4971 #endif
4972                         xpt_action(work_ccb);
4973                 }
4974                 break;
4975         }
4976         case XPT_SCAN_LUN:
4977         {
4978                 xpt_scan_bus_info *scan_info;
4979                 path_id_t path_id;
4980                 target_id_t target_id;
4981                 lun_id_t lun_id;
4982
4983                 /* Reuse the same CCB to query if a device was really found */
4984                 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
4985                 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
4986                               request_ccb->ccb_h.pinfo.priority);
4987                 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
4988
4989                 path_id = request_ccb->ccb_h.path_id;
4990                 target_id = request_ccb->ccb_h.target_id;
4991                 lun_id = request_ccb->ccb_h.target_lun;
4992                 xpt_action(request_ccb);
4993
4994 #if 0
4995                 printf("xpt_scan_bus: got back probe from %d:%d:%d\n",
4996                         path_id, target_id, lun_id);
4997 #endif
4998
4999                 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5000                         struct cam_ed *device;
5001                         struct cam_et *target;
5002                         int s, phl;
5003
5004                         /*
5005                          * If we already probed lun 0 successfully, or
5006                          * we have additional configured luns on this
5007                          * target that might have "gone away", go onto
5008                          * the next lun.
5009                          */
5010                         target = request_ccb->ccb_h.path->target;
5011                         /*
5012                          * We may touch devices that we don't
5013                          * hold references too, so ensure they
5014                          * don't disappear out from under us.
5015                          * The target above is referenced by the
5016                          * path in the request ccb.
5017                          */
5018                         phl = 0;
5019                         s = splcam();
5020                         device = TAILQ_FIRST(&target->ed_entries);
5021                         if (device != NULL) {
5022                                 phl = device->quirk->quirks & CAM_QUIRK_HILUNS;
5023                                 if (device->lun_id == 0)
5024                                         device = TAILQ_NEXT(device, links);
5025                         }
5026                         splx(s);
5027                         if ((lun_id != 0) || (device != NULL)) {
5028                                 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5029                                         lun_id++;
5030                         }
5031                 } else {
5032                         struct cam_ed *device;
5033                         
5034                         device = request_ccb->ccb_h.path->device;
5035
5036                         if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5037                                 /* Try the next lun */
5038                                 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
5039                                     (device->quirk->quirks & CAM_QUIRK_HILUNS))
5040                                         lun_id++;
5041                         }
5042                 }
5043
5044                 xpt_free_path(request_ccb->ccb_h.path);
5045
5046                 /* Check Bounds */
5047                 if ((lun_id == request_ccb->ccb_h.target_lun)
5048                  || lun_id > scan_info->cpi->max_lun) {
5049                         /* We're done */
5050
5051                         xpt_free_ccb(request_ccb);
5052                         scan_info->pending_count--;
5053                         if (scan_info->pending_count == 0) {
5054                                 xpt_free_ccb((union ccb *)scan_info->cpi);
5055                                 request_ccb = scan_info->request_ccb;
5056                                 free(scan_info, M_TEMP);
5057                                 request_ccb->ccb_h.status = CAM_REQ_CMP;
5058                                 xpt_done(request_ccb);
5059                         }
5060                 } else {
5061                         /* Try the next device */
5062                         struct cam_path *path;
5063                         cam_status status;
5064
5065                         path = request_ccb->ccb_h.path;
5066                         status = xpt_create_path(&path, xpt_periph,
5067                                                  path_id, target_id, lun_id);
5068                         if (status != CAM_REQ_CMP) {
5069                                 printf("xpt_scan_bus: xpt_create_path failed "
5070                                        "with status %#x, halting LUN scan\n",
5071                                        status);
5072                                 xpt_free_ccb(request_ccb);
5073                                 scan_info->pending_count--;
5074                                 if (scan_info->pending_count == 0) {
5075                                         xpt_free_ccb(
5076                                                 (union ccb *)scan_info->cpi);
5077                                         request_ccb = scan_info->request_ccb;
5078                                         free(scan_info, M_TEMP);
5079                                         request_ccb->ccb_h.status = CAM_REQ_CMP;
5080                                         xpt_done(request_ccb);
5081                                         break;
5082                                 }
5083                         }
5084                         xpt_setup_ccb(&request_ccb->ccb_h, path,
5085                                       request_ccb->ccb_h.pinfo.priority);
5086                         request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5087                         request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5088                         request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5089                         request_ccb->crcn.flags =
5090                                 scan_info->request_ccb->crcn.flags;
5091 #if 0
5092                         xpt_print_path(path);
5093                         printf("xpt_scan bus probing\n");
5094 #endif
5095                         xpt_action(request_ccb);
5096                 }
5097                 break;
5098         }
5099         default:
5100                 break;
5101         }
5102 }
5103
5104 typedef enum {
5105         PROBE_TUR,
5106         PROBE_INQUIRY,
5107         PROBE_FULL_INQUIRY,
5108         PROBE_MODE_SENSE,
5109         PROBE_SERIAL_NUM,
5110         PROBE_TUR_FOR_NEGOTIATION
5111 } probe_action;
5112
5113 typedef enum {
5114         PROBE_INQUIRY_CKSUM     = 0x01,
5115         PROBE_SERIAL_CKSUM      = 0x02,
5116         PROBE_NO_ANNOUNCE       = 0x04
5117 } probe_flags;
5118
5119 typedef struct {
5120         TAILQ_HEAD(, ccb_hdr) request_ccbs;
5121         probe_action    action;
5122         union ccb       saved_ccb;
5123         probe_flags     flags;
5124         MD5_CTX         context;
5125         u_int8_t        digest[16];
5126 } probe_softc;
5127
5128 static void
5129 xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5130              cam_flags flags, union ccb *request_ccb)
5131 {
5132         struct ccb_pathinq cpi;
5133         cam_status status;
5134         struct cam_path *new_path;
5135         struct cam_periph *old_periph;
5136         int s;
5137         
5138         CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5139                   ("xpt_scan_lun\n"));
5140         
5141         xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5142         cpi.ccb_h.func_code = XPT_PATH_INQ;
5143         xpt_action((union ccb *)&cpi);
5144
5145         if (cpi.ccb_h.status != CAM_REQ_CMP) {
5146                 if (request_ccb != NULL) {
5147                         request_ccb->ccb_h.status = cpi.ccb_h.status;
5148                         xpt_done(request_ccb);
5149                 }
5150                 return;
5151         }
5152
5153         if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5154                 /*
5155                  * Can't scan the bus on an adapter that
5156                  * cannot perform the initiator role.
5157                  */
5158                 if (request_ccb != NULL) {
5159                         request_ccb->ccb_h.status = CAM_REQ_CMP;
5160                         xpt_done(request_ccb);
5161                 }
5162                 return;
5163         }
5164
5165         if (request_ccb == NULL) {
5166                 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5167                 if (request_ccb == NULL) {
5168                         xpt_print_path(path);
5169                         printf("xpt_scan_lun: can't allocate CCB, can't "
5170                                "continue\n");
5171                         return;
5172                 }
5173                 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5174                 if (new_path == NULL) {
5175                         xpt_print_path(path);
5176                         printf("xpt_scan_lun: can't allocate path, can't "
5177                                "continue\n");
5178                         free(request_ccb, M_TEMP);
5179                         return;
5180                 }
5181                 status = xpt_compile_path(new_path, xpt_periph,
5182                                           path->bus->path_id,
5183                                           path->target->target_id,
5184                                           path->device->lun_id);
5185
5186                 if (status != CAM_REQ_CMP) {
5187                         xpt_print_path(path);
5188                         printf("xpt_scan_lun: can't compile path, can't "
5189                                "continue\n");
5190                         free(request_ccb, M_TEMP);
5191                         free(new_path, M_TEMP);
5192                         return;
5193                 }
5194                 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5195                 request_ccb->ccb_h.cbfcnp = xptscandone;
5196                 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5197                 request_ccb->crcn.flags = flags;
5198         }
5199
5200         s = splsoftcam();
5201         if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5202                 probe_softc *softc;
5203
5204                 softc = (probe_softc *)old_periph->softc;
5205                 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5206                                   periph_links.tqe);
5207         } else {
5208                 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5209                                           probestart, "probe",
5210                                           CAM_PERIPH_BIO,
5211                                           request_ccb->ccb_h.path, NULL, 0,
5212                                           request_ccb);
5213
5214                 if (status != CAM_REQ_CMP) {
5215                         xpt_print_path(path);
5216                         printf("xpt_scan_lun: cam_alloc_periph returned an "
5217                                "error, can't continue probe\n");
5218                         request_ccb->ccb_h.status = status;
5219                         xpt_done(request_ccb);
5220                 }
5221         }
5222         splx(s);
5223 }
5224
5225 static void
5226 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5227 {
5228         xpt_release_path(done_ccb->ccb_h.path);
5229         free(done_ccb->ccb_h.path, M_TEMP);
5230         free(done_ccb, M_TEMP);
5231 }
5232
5233 static cam_status
5234 proberegister(struct cam_periph *periph, void *arg)
5235 {
5236         union ccb *request_ccb; /* CCB representing the probe request */
5237         probe_softc *softc;
5238
5239         request_ccb = (union ccb *)arg;
5240         if (periph == NULL) {
5241                 printf("proberegister: periph was NULL!!\n");
5242                 return(CAM_REQ_CMP_ERR);
5243         }
5244
5245         if (request_ccb == NULL) {
5246                 printf("proberegister: no probe CCB, "
5247                        "can't register device\n");
5248                 return(CAM_REQ_CMP_ERR);
5249         }
5250
5251         softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5252
5253         if (softc == NULL) {
5254                 printf("proberegister: Unable to probe new device. "
5255                        "Unable to allocate softc\n");                           
5256                 return(CAM_REQ_CMP_ERR);
5257         }
5258         TAILQ_INIT(&softc->request_ccbs);
5259         TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5260                           periph_links.tqe);
5261         softc->flags = 0;
5262         periph->softc = softc;
5263         cam_periph_acquire(periph);
5264         /*
5265          * Ensure we've waited at least a bus settle
5266          * delay before attempting to probe the device.
5267          * For HBAs that don't do bus resets, this won't make a difference.
5268          */
5269         cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5270                                       SCSI_DELAY);
5271         probeschedule(periph);
5272         return(CAM_REQ_CMP);
5273 }
5274
5275 static void
5276 probeschedule(struct cam_periph *periph)
5277 {
5278         struct ccb_pathinq cpi;
5279         union ccb *ccb;
5280         probe_softc *softc;
5281
5282         softc = (probe_softc *)periph->softc;
5283         ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5284
5285         xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5286         cpi.ccb_h.func_code = XPT_PATH_INQ;
5287         xpt_action((union ccb *)&cpi);
5288
5289         /*
5290          * If a device has gone away and another device, or the same one,
5291          * is back in the same place, it should have a unit attention
5292          * condition pending.  It will not report the unit attention in
5293          * response to an inquiry, which may leave invalid transfer
5294          * negotiations in effect.  The TUR will reveal the unit attention
5295          * condition.  Only send the TUR for lun 0, since some devices 
5296          * will get confused by commands other than inquiry to non-existent
5297          * luns.  If you think a device has gone away start your scan from
5298          * lun 0.  This will insure that any bogus transfer settings are
5299          * invalidated.
5300          *
5301          * If we haven't seen the device before and the controller supports
5302          * some kind of transfer negotiation, negotiate with the first
5303          * sent command if no bus reset was performed at startup.  This
5304          * ensures that the device is not confused by transfer negotiation
5305          * settings left over by loader or BIOS action.
5306          */
5307         if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5308          && (ccb->ccb_h.target_lun == 0)) {
5309                 softc->action = PROBE_TUR;
5310         } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5311               && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5312                 proberequestdefaultnegotiation(periph);
5313                 softc->action = PROBE_INQUIRY;
5314         } else {
5315                 softc->action = PROBE_INQUIRY;
5316         }
5317
5318         if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5319                 softc->flags |= PROBE_NO_ANNOUNCE;
5320         else
5321                 softc->flags &= ~PROBE_NO_ANNOUNCE;
5322
5323         xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5324 }
5325
5326 static void
5327 probestart(struct cam_periph *periph, union ccb *start_ccb)
5328 {
5329         /* Probe the device that our peripheral driver points to */
5330         struct ccb_scsiio *csio;
5331         probe_softc *softc;
5332
5333         CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5334
5335         softc = (probe_softc *)periph->softc;
5336         csio = &start_ccb->csio;
5337
5338         switch (softc->action) {
5339         case PROBE_TUR:
5340         case PROBE_TUR_FOR_NEGOTIATION:
5341         {
5342                 scsi_test_unit_ready(csio,
5343                                      /*retries*/4,
5344                                      probedone,
5345                                      MSG_SIMPLE_Q_TAG,
5346                                      SSD_FULL_SIZE,
5347                                      /*timeout*/60000);
5348                 break;
5349         }
5350         case PROBE_INQUIRY:
5351         case PROBE_FULL_INQUIRY:
5352         {
5353                 u_int inquiry_len;
5354                 struct scsi_inquiry_data *inq_buf;
5355
5356                 inq_buf = &periph->path->device->inq_data;
5357                 /*
5358                  * If the device is currently configured, we calculate an
5359                  * MD5 checksum of the inquiry data, and if the serial number
5360                  * length is greater than 0, add the serial number data
5361                  * into the checksum as well.  Once the inquiry and the
5362                  * serial number check finish, we attempt to figure out
5363                  * whether we still have the same device.
5364                  */
5365                 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5366                         
5367                         MD5Init(&softc->context);
5368                         MD5Update(&softc->context, (unsigned char *)inq_buf,
5369                                   sizeof(struct scsi_inquiry_data));
5370                         softc->flags |= PROBE_INQUIRY_CKSUM;
5371                         if (periph->path->device->serial_num_len > 0) {
5372                                 MD5Update(&softc->context,
5373                                           periph->path->device->serial_num,
5374                                           periph->path->device->serial_num_len);
5375                                 softc->flags |= PROBE_SERIAL_CKSUM;
5376                         }
5377                         MD5Final(softc->digest, &softc->context);
5378                 } 
5379
5380                 if (softc->action == PROBE_INQUIRY)
5381                         inquiry_len = SHORT_INQUIRY_LENGTH;
5382                 else
5383                         inquiry_len = inq_buf->additional_length + 4;
5384         
5385                 scsi_inquiry(csio,
5386                              /*retries*/4,
5387                              probedone,
5388                              MSG_SIMPLE_Q_TAG,
5389                              (u_int8_t *)inq_buf,
5390                              inquiry_len,
5391                              /*evpd*/FALSE,
5392                              /*page_code*/0,
5393                              SSD_MIN_SIZE,
5394                              /*timeout*/60 * 1000);
5395                 break;
5396         }
5397         case PROBE_MODE_SENSE:
5398         {
5399                 void  *mode_buf;
5400                 int    mode_buf_len;
5401
5402                 mode_buf_len = sizeof(struct scsi_mode_header_6)
5403                              + sizeof(struct scsi_mode_blk_desc)
5404                              + sizeof(struct scsi_control_page);
5405                 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5406                 if (mode_buf != NULL) {
5407                         scsi_mode_sense(csio,
5408                                         /*retries*/4,
5409                                         probedone,
5410                                         MSG_SIMPLE_Q_TAG,
5411                                         /*dbd*/FALSE,
5412                                         SMS_PAGE_CTRL_CURRENT,
5413                                         SMS_CONTROL_MODE_PAGE,
5414                                         mode_buf,
5415                                         mode_buf_len,
5416                                         SSD_FULL_SIZE,
5417                                         /*timeout*/60000);
5418                         break;
5419                 }
5420                 xpt_print_path(periph->path);
5421                 printf("Unable to mode sense control page - malloc failure\n");
5422                 softc->action = PROBE_SERIAL_NUM;
5423                 /* FALLTHROUGH */
5424         }
5425         case PROBE_SERIAL_NUM:
5426         {
5427                 struct scsi_vpd_unit_serial_number *serial_buf;
5428                 struct cam_ed* device;
5429
5430                 serial_buf = NULL;
5431                 device = periph->path->device;
5432                 device->serial_num = NULL;
5433                 device->serial_num_len = 0;
5434
5435                 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5436                         serial_buf = (struct scsi_vpd_unit_serial_number *)
5437                                 malloc(sizeof(*serial_buf), M_TEMP,
5438                                         M_NOWAIT | M_ZERO);
5439
5440                 if (serial_buf != NULL) {
5441                         scsi_inquiry(csio,
5442                                      /*retries*/4,
5443                                      probedone,
5444                                      MSG_SIMPLE_Q_TAG,
5445                                      (u_int8_t *)serial_buf,
5446                                      sizeof(*serial_buf),
5447                                      /*evpd*/TRUE,
5448                                      SVPD_UNIT_SERIAL_NUMBER,
5449                                      SSD_MIN_SIZE,
5450                                      /*timeout*/60 * 1000);
5451                         break;
5452                 }
5453                 /*
5454                  * We'll have to do without, let our probedone
5455                  * routine finish up for us.
5456                  */
5457                 start_ccb->csio.data_ptr = NULL;
5458                 probedone(periph, start_ccb);
5459                 return;
5460         }
5461         }
5462         xpt_action(start_ccb);
5463 }
5464
5465 static void
5466 proberequestdefaultnegotiation(struct cam_periph *periph)
5467 {
5468         struct ccb_trans_settings cts;
5469
5470         xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5471         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5472         cts.flags = CCB_TRANS_USER_SETTINGS;
5473         xpt_action((union ccb *)&cts);
5474         cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5475         cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5476         cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5477         xpt_action((union ccb *)&cts);
5478 }
5479
5480 static void
5481 probedone(struct cam_periph *periph, union ccb *done_ccb)
5482 {
5483         probe_softc *softc;
5484         struct cam_path *path;
5485         u_int32_t  priority;
5486
5487         CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5488
5489         softc = (probe_softc *)periph->softc;
5490         path = done_ccb->ccb_h.path;
5491         priority = done_ccb->ccb_h.pinfo.priority;
5492
5493         switch (softc->action) {
5494         case PROBE_TUR:
5495         {
5496                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5497
5498                         if (cam_periph_error(done_ccb, 0,
5499                                              SF_NO_PRINT, NULL) == ERESTART)
5500                                 return;
5501                         else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5502                                 /* Don't wedge the queue */
5503                                 xpt_release_devq(done_ccb->ccb_h.path,
5504                                                  /*count*/1,
5505                                                  /*run_queue*/TRUE);
5506                 }
5507                 softc->action = PROBE_INQUIRY;
5508                 xpt_release_ccb(done_ccb);
5509                 xpt_schedule(periph, priority);
5510                 return;
5511         }
5512         case PROBE_INQUIRY:
5513         case PROBE_FULL_INQUIRY:
5514         {
5515                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5516                         struct scsi_inquiry_data *inq_buf;
5517                         u_int8_t periph_qual;
5518                         u_int8_t periph_dtype;
5519
5520                         path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5521                         inq_buf = &path->device->inq_data;
5522
5523                         periph_qual = SID_QUAL(inq_buf);
5524                         periph_dtype = SID_TYPE(inq_buf);
5525                         
5526                         if (periph_dtype != T_NODEVICE) {
5527                                 switch(periph_qual) {
5528                                 case SID_QUAL_LU_CONNECTED:
5529                                 {
5530                                         u_int8_t alen;
5531
5532                                         /*
5533                                          * We conservatively request only
5534                                          * SHORT_INQUIRY_LEN bytes of inquiry
5535                                          * information during our first try
5536                                          * at sending an INQUIRY. If the device
5537                                          * has more information to give,
5538                                          * perform a second request specifying
5539                                          * the amount of information the device
5540                                          * is willing to give.
5541                                          */
5542                                         alen = inq_buf->additional_length;
5543                                         if (softc->action == PROBE_INQUIRY
5544                                          && alen > (SHORT_INQUIRY_LENGTH - 4)) {
5545                                                 softc->action =
5546                                                     PROBE_FULL_INQUIRY;
5547                                                 xpt_release_ccb(done_ccb);
5548                                                 xpt_schedule(periph, priority);
5549                                                 return;
5550                                         }
5551
5552                                         xpt_find_quirk(path->device);
5553
5554                                         if ((inq_buf->flags & SID_CmdQue) != 0)
5555                                                 softc->action =
5556                                                     PROBE_MODE_SENSE;
5557                                         else
5558                                                 softc->action =
5559                                                     PROBE_SERIAL_NUM;
5560
5561                                         path->device->flags &= 
5562                                                 ~CAM_DEV_UNCONFIGURED;
5563
5564                                         xpt_release_ccb(done_ccb);
5565                                         xpt_schedule(periph, priority);
5566                                         return;
5567                                 }
5568                                 default:
5569                                         break;
5570                                 }
5571                         }
5572                 } else if (cam_periph_error(done_ccb, 0,
5573                                             done_ccb->ccb_h.target_lun > 0
5574                                             ? SF_RETRY_UA|SF_QUIET_IR
5575                                             : SF_RETRY_UA,
5576                                             &softc->saved_ccb) == ERESTART) {
5577                         return;
5578                 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5579                         /* Don't wedge the queue */
5580                         xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5581                                          /*run_queue*/TRUE);
5582                 }
5583                 /*
5584                  * If we get to this point, we got an error status back
5585                  * from the inquiry and the error status doesn't require
5586                  * automatically retrying the command.  Therefore, the
5587                  * inquiry failed.  If we had inquiry information before
5588                  * for this device, but this latest inquiry command failed,
5589                  * the device has probably gone away.  If this device isn't
5590                  * already marked unconfigured, notify the peripheral
5591                  * drivers that this device is no more.
5592                  */
5593                 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5594                         /* Send the async notification. */
5595                         xpt_async(AC_LOST_DEVICE, path, NULL);
5596
5597                 xpt_release_ccb(done_ccb);
5598                 break;
5599         }
5600         case PROBE_MODE_SENSE:
5601         {
5602                 struct ccb_scsiio *csio;
5603                 struct scsi_mode_header_6 *mode_hdr;
5604
5605                 csio = &done_ccb->csio;
5606                 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5607                 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5608                         struct scsi_control_page *page;
5609                         u_int8_t *offset;
5610
5611                         offset = ((u_int8_t *)&mode_hdr[1])
5612                             + mode_hdr->blk_desc_len;
5613                         page = (struct scsi_control_page *)offset;
5614                         path->device->queue_flags = page->queue_flags;
5615                 } else if (cam_periph_error(done_ccb, 0,
5616                                             SF_RETRY_UA|SF_NO_PRINT,
5617                                             &softc->saved_ccb) == ERESTART) {
5618                         return;
5619                 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5620                         /* Don't wedge the queue */
5621                         xpt_release_devq(done_ccb->ccb_h.path,
5622                                          /*count*/1, /*run_queue*/TRUE);
5623                 }
5624                 xpt_release_ccb(done_ccb);
5625                 free(mode_hdr, M_TEMP);
5626                 softc->action = PROBE_SERIAL_NUM;
5627                 xpt_schedule(periph, priority);
5628                 return;
5629         }
5630         case PROBE_SERIAL_NUM:
5631         {
5632                 struct ccb_scsiio *csio;
5633                 struct scsi_vpd_unit_serial_number *serial_buf;
5634                 u_int32_t  priority;
5635                 int changed;
5636                 int have_serialnum;
5637
5638                 changed = 1;
5639                 have_serialnum = 0;
5640                 csio = &done_ccb->csio;
5641                 priority = done_ccb->ccb_h.pinfo.priority;
5642                 serial_buf =
5643                     (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5644
5645                 /* Clean up from previous instance of this device */
5646                 if (path->device->serial_num != NULL) {
5647                         free(path->device->serial_num, M_DEVBUF);
5648                         path->device->serial_num = NULL;
5649                         path->device->serial_num_len = 0;
5650                 }
5651
5652                 if (serial_buf == NULL) {
5653                         /*
5654                          * Don't process the command as it was never sent
5655                          */
5656                 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5657                         && (serial_buf->length > 0)) {
5658
5659                         have_serialnum = 1;
5660                         path->device->serial_num =
5661                                 (u_int8_t *)malloc((serial_buf->length + 1),
5662                                                    M_DEVBUF, M_NOWAIT);
5663                         if (path->device->serial_num != NULL) {
5664                                 bcopy(serial_buf->serial_num,
5665                                       path->device->serial_num,
5666                                       serial_buf->length);
5667                                 path->device->serial_num_len =
5668                                     serial_buf->length;
5669                                 path->device->serial_num[serial_buf->length]
5670                                     = '\0';
5671                         }
5672                 } else if (cam_periph_error(done_ccb, 0,
5673                                             SF_RETRY_UA|SF_NO_PRINT,
5674                                             &softc->saved_ccb) == ERESTART) {
5675                         return;
5676                 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5677                         /* Don't wedge the queue */
5678                         xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5679                                          /*run_queue*/TRUE);
5680                 }
5681                 
5682                 /*
5683                  * Let's see if we have seen this device before.
5684                  */
5685                 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5686                         MD5_CTX context;
5687                         u_int8_t digest[16];
5688
5689                         MD5Init(&context);
5690                         
5691                         MD5Update(&context,
5692                                   (unsigned char *)&path->device->inq_data,
5693                                   sizeof(struct scsi_inquiry_data));
5694
5695                         if (have_serialnum)
5696                                 MD5Update(&context, serial_buf->serial_num,
5697                                           serial_buf->length);
5698
5699                         MD5Final(digest, &context);
5700                         if (bcmp(softc->digest, digest, 16) == 0)
5701                                 changed = 0;
5702
5703                         /*
5704                          * XXX Do we need to do a TUR in order to ensure
5705                          *     that the device really hasn't changed???
5706                          */
5707                         if ((changed != 0)
5708                          && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5709                                 xpt_async(AC_LOST_DEVICE, path, NULL);
5710                 }
5711                 if (serial_buf != NULL)
5712                         free(serial_buf, M_TEMP);
5713
5714                 if (changed != 0) {
5715                         /*
5716                          * Now that we have all the necessary
5717                          * information to safely perform transfer
5718                          * negotiations... Controllers don't perform
5719                          * any negotiation or tagged queuing until
5720                          * after the first XPT_SET_TRAN_SETTINGS ccb is
5721                          * received.  So, on a new device, just retreive
5722                          * the user settings, and set them as the current
5723                          * settings to set the device up.
5724                          */
5725                         proberequestdefaultnegotiation(periph);
5726                         xpt_release_ccb(done_ccb);
5727
5728                         /*
5729                          * Perform a TUR to allow the controller to
5730                          * perform any necessary transfer negotiation.
5731                          */
5732                         softc->action = PROBE_TUR_FOR_NEGOTIATION;
5733                         xpt_schedule(periph, priority);
5734                         return;
5735                 }
5736                 xpt_release_ccb(done_ccb);
5737                 break;
5738         }
5739         case PROBE_TUR_FOR_NEGOTIATION:
5740                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5741                         /* Don't wedge the queue */
5742                         xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5743                                          /*run_queue*/TRUE);
5744                 }
5745
5746                 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5747
5748                 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5749                         /* Inform the XPT that a new device has been found */
5750                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5751                         xpt_action(done_ccb);
5752
5753                         xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5754                 }
5755                 xpt_release_ccb(done_ccb);
5756                 break;
5757         }
5758         done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5759         TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5760         done_ccb->ccb_h.status = CAM_REQ_CMP;
5761         xpt_done(done_ccb);
5762         if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5763                 cam_periph_invalidate(periph);
5764                 cam_periph_release(periph);
5765         } else {
5766                 probeschedule(periph);
5767         }
5768 }
5769
5770 static void
5771 probecleanup(struct cam_periph *periph)
5772 {
5773         free(periph->softc, M_TEMP);
5774 }
5775
5776 static void
5777 xpt_find_quirk(struct cam_ed *device)
5778 {
5779         caddr_t match;
5780
5781         match = cam_quirkmatch((caddr_t)&device->inq_data,
5782                                (caddr_t)xpt_quirk_table,
5783                                sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
5784                                sizeof(*xpt_quirk_table), scsi_inquiry_match);
5785
5786         if (match == NULL)
5787                 panic("xpt_find_quirk: device didn't match wildcard entry!!");
5788
5789         device->quirk = (struct xpt_quirk_entry *)match;
5790 }
5791
5792 static void
5793 xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
5794                           int async_update)
5795 {
5796         struct  cam_sim *sim;
5797         int     qfrozen;
5798
5799         sim = cts->ccb_h.path->bus->sim;
5800         if (async_update == FALSE) {
5801                 struct  scsi_inquiry_data *inq_data;
5802                 struct  ccb_pathinq cpi;
5803                 struct  ccb_trans_settings cur_cts;
5804
5805                 if (device == NULL) {
5806                         cts->ccb_h.status = CAM_PATH_INVALID;
5807                         xpt_done((union ccb *)cts);
5808                         return;
5809                 }
5810
5811                 /*
5812                  * Perform sanity checking against what the
5813                  * controller and device can do.
5814                  */
5815                 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
5816                 cpi.ccb_h.func_code = XPT_PATH_INQ;
5817                 xpt_action((union ccb *)&cpi);
5818                 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
5819                 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5820                 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
5821                 xpt_action((union ccb *)&cur_cts);
5822                 inq_data = &device->inq_data;
5823
5824                 /* Fill in any gaps in what the user gave us */
5825                 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
5826                         cts->sync_period = cur_cts.sync_period;
5827                 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
5828                         cts->sync_offset = cur_cts.sync_offset;
5829                 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
5830                         cts->bus_width = cur_cts.bus_width;
5831                 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
5832                         cts->flags &= ~CCB_TRANS_DISC_ENB;
5833                         cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
5834                 }
5835                 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
5836                         cts->flags &= ~CCB_TRANS_TAG_ENB;
5837                         cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
5838                 }
5839                 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
5840                   && (inq_data->flags & SID_Sync) == 0)
5841                  || (cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
5842                         /* Force async */
5843                         cts->sync_period = 0;
5844                         cts->sync_offset = 0;
5845                 }
5846
5847                 /*
5848                  * Don't allow DT transmission rates if the
5849                  * device does not support it.
5850                  */
5851                 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
5852                  && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
5853                  && cts->sync_period <= 0x9)
5854                         cts->sync_period = 0xa;
5855
5856                 switch (cts->bus_width) {
5857                 case MSG_EXT_WDTR_BUS_32_BIT:
5858                         if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5859                           || (inq_data->flags & SID_WBus32) != 0)
5860                          && (cpi.hba_inquiry & PI_WIDE_32) != 0)
5861                                 break;
5862                         /* Fall Through to 16-bit */
5863                 case MSG_EXT_WDTR_BUS_16_BIT:
5864                         if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
5865                           || (inq_data->flags & SID_WBus16) != 0)
5866                          && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
5867                                 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5868                                 break;
5869                         }
5870                         /* Fall Through to 8-bit */
5871                 default: /* New bus width?? */
5872                 case MSG_EXT_WDTR_BUS_8_BIT:
5873                         /* All targets can do this */
5874                         cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5875                         break;
5876                 }
5877
5878                 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
5879                         /*
5880                          * Can't tag queue without disconnection.
5881                          */
5882                         cts->flags &= ~CCB_TRANS_TAG_ENB;
5883                         cts->valid |= CCB_TRANS_TQ_VALID;
5884                 }
5885
5886                 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
5887                  || (inq_data->flags & SID_CmdQue) == 0
5888                  || (device->queue_flags & SCP_QUEUE_DQUE) != 0
5889                  || (device->quirk->mintags == 0)) {
5890                         /*
5891                          * Can't tag on hardware that doesn't support,
5892                          * doesn't have it enabled, or has broken tag support.
5893                          */
5894                         cts->flags &= ~CCB_TRANS_TAG_ENB;
5895                 }
5896         }
5897
5898         qfrozen = FALSE;
5899         if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
5900                 int device_tagenb;
5901
5902                 /*
5903                  * If we are transitioning from tags to no-tags or
5904                  * vice-versa, we need to carefully freeze and restart
5905                  * the queue so that we don't overlap tagged and non-tagged
5906                  * commands.  We also temporarily stop tags if there is
5907                  * a change in transfer negotiation settings to allow
5908                  * "tag-less" negotiation.
5909                  */
5910                 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5911                  || (device->inq_flags & SID_CmdQue) != 0)
5912                         device_tagenb = TRUE;
5913                 else
5914                         device_tagenb = FALSE;
5915
5916                 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
5917                   && device_tagenb == FALSE)
5918                  || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
5919                   && device_tagenb == TRUE)) {
5920
5921                         if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
5922                                 /*
5923                                  * Delay change to use tags until after a
5924                                  * few commands have gone to this device so
5925                                  * the controller has time to perform transfer
5926                                  * negotiations without tagged messages getting
5927                                  * in the way.
5928                                  */
5929                                 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
5930                                 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
5931                         } else {
5932                                 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
5933                                 qfrozen = TRUE;
5934                                 device->inq_flags &= ~SID_CmdQue;
5935                                 xpt_dev_ccbq_resize(cts->ccb_h.path,
5936                                                     sim->max_dev_openings);
5937                                 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5938                                 device->tag_delay_count = 0;
5939                         }
5940                 }
5941         }
5942
5943         if (async_update == FALSE) {
5944                 /*
5945                  * If we are currently performing tagged transactions to
5946                  * this device and want to change its negotiation parameters,
5947                  * go non-tagged for a bit to give the controller a chance to
5948                  * negotiate unhampered by tag messages.
5949                  */
5950                 if ((device->inq_flags & SID_CmdQue) != 0
5951                  && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
5952                                    CCB_TRANS_SYNC_OFFSET_VALID|
5953                                    CCB_TRANS_BUS_WIDTH_VALID)) != 0)
5954                         xpt_toggle_tags(cts->ccb_h.path);
5955
5956                 (*(sim->sim_action))(sim, (union ccb *)cts);
5957         }
5958
5959         if (qfrozen) {
5960                 struct ccb_relsim crs;
5961
5962                 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
5963                               /*priority*/1);
5964                 crs.ccb_h.func_code = XPT_REL_SIMQ;
5965                 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5966                 crs.openings
5967                     = crs.release_timeout 
5968                     = crs.qfrozen_cnt
5969                     = 0;
5970                 xpt_action((union ccb *)&crs);
5971         }
5972 }
5973
5974 static void
5975 xpt_toggle_tags(struct cam_path *path)
5976 {
5977         struct cam_ed *dev;
5978
5979         /*
5980          * Give controllers a chance to renegotiate
5981          * before starting tag operations.  We
5982          * "toggle" tagged queuing off then on
5983          * which causes the tag enable command delay
5984          * counter to come into effect.
5985          */
5986         dev = path->device;
5987         if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5988          || ((dev->inq_flags & SID_CmdQue) != 0
5989           && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
5990                 struct ccb_trans_settings cts;
5991
5992                 xpt_setup_ccb(&cts.ccb_h, path, 1);
5993                 cts.flags = 0;
5994                 cts.valid = CCB_TRANS_TQ_VALID;
5995                 xpt_set_transfer_settings(&cts, path->device,
5996                                           /*async_update*/TRUE);
5997                 cts.flags = CCB_TRANS_TAG_ENB;
5998                 xpt_set_transfer_settings(&cts, path->device,
5999                                           /*async_update*/TRUE);
6000         }
6001 }
6002
6003 static void
6004 xpt_start_tags(struct cam_path *path)
6005 {
6006         struct ccb_relsim crs;
6007         struct cam_ed *device;
6008         struct cam_sim *sim;
6009         int    newopenings;
6010
6011         device = path->device;
6012         sim = path->bus->sim;
6013         device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6014         xpt_freeze_devq(path, /*count*/1);
6015         device->inq_flags |= SID_CmdQue;
6016         newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
6017         xpt_dev_ccbq_resize(path, newopenings);
6018         xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6019         crs.ccb_h.func_code = XPT_REL_SIMQ;
6020         crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6021         crs.openings
6022             = crs.release_timeout 
6023             = crs.qfrozen_cnt
6024             = 0;
6025         xpt_action((union ccb *)&crs);
6026 }
6027
6028 static int busses_to_config;
6029 static int busses_to_reset;
6030
6031 static int
6032 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6033 {
6034         if (bus->path_id != CAM_XPT_PATH_ID) {
6035                 struct cam_path path;
6036                 struct ccb_pathinq cpi;
6037                 int can_negotiate;
6038
6039                 busses_to_config++;
6040                 xpt_compile_path(&path, NULL, bus->path_id,
6041                                  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6042                 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6043                 cpi.ccb_h.func_code = XPT_PATH_INQ;
6044                 xpt_action((union ccb *)&cpi);
6045                 can_negotiate = cpi.hba_inquiry;
6046                 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6047                 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6048                  && can_negotiate)
6049                         busses_to_reset++;
6050                 xpt_release_path(&path);
6051         }
6052
6053         return(1);
6054 }
6055
6056 static int
6057 xptconfigfunc(struct cam_eb *bus, void *arg)
6058 {
6059         struct  cam_path *path;
6060         union   ccb *work_ccb;
6061
6062         if (bus->path_id != CAM_XPT_PATH_ID) {
6063                 cam_status status;
6064                 int can_negotiate;
6065
6066                 work_ccb = xpt_alloc_ccb();
6067                 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6068                                               CAM_TARGET_WILDCARD,
6069                                               CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6070                         printf("xptconfigfunc: xpt_create_path failed with "
6071                                "status %#x for bus %d\n", status, bus->path_id);
6072                         printf("xptconfigfunc: halting bus configuration\n");
6073                         xpt_free_ccb(work_ccb);
6074                         busses_to_config--;
6075                         xpt_finishconfig(xpt_periph, NULL);
6076                         return(0);
6077                 }
6078                 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6079                 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6080                 xpt_action(work_ccb);
6081                 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6082                         printf("xptconfigfunc: CPI failed on bus %d "
6083                                "with status %d\n", bus->path_id,
6084                                work_ccb->ccb_h.status);
6085                         xpt_finishconfig(xpt_periph, work_ccb);
6086                         return(1);
6087                 }
6088
6089                 can_negotiate = work_ccb->cpi.hba_inquiry;
6090                 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6091                 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6092                  && (can_negotiate != 0)) {
6093                         xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6094                         work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6095                         work_ccb->ccb_h.cbfcnp = NULL;
6096                         CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6097                                   ("Resetting Bus\n"));
6098                         xpt_action(work_ccb);
6099                         xpt_finishconfig(xpt_periph, work_ccb);
6100                 } else {
6101                         /* Act as though we performed a successful BUS RESET */
6102                         work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6103                         xpt_finishconfig(xpt_periph, work_ccb);
6104                 }
6105         }
6106
6107         return(1);
6108 }
6109
6110 static void
6111 xpt_config(void *arg)
6112 {
6113         /* Now that interrupts are enabled, go find our devices */
6114
6115 #ifdef CAMDEBUG
6116         /* Setup debugging flags and path */
6117 #ifdef CAM_DEBUG_FLAGS
6118         cam_dflags = CAM_DEBUG_FLAGS;
6119 #else /* !CAM_DEBUG_FLAGS */
6120         cam_dflags = CAM_DEBUG_NONE;
6121 #endif /* CAM_DEBUG_FLAGS */
6122 #ifdef CAM_DEBUG_BUS
6123         if (cam_dflags != CAM_DEBUG_NONE) {
6124                 if (xpt_create_path(&cam_dpath, xpt_periph,
6125                                     CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6126                                     CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6127                         printf("xpt_config: xpt_create_path() failed for debug"
6128                                " target %d:%d:%d, debugging disabled\n",
6129                                CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6130                         cam_dflags = CAM_DEBUG_NONE;
6131                 }
6132         } else
6133                 cam_dpath = NULL;
6134 #else /* !CAM_DEBUG_BUS */
6135         cam_dpath = NULL;
6136 #endif /* CAM_DEBUG_BUS */
6137 #endif /* CAMDEBUG */
6138
6139         /*
6140          * Scan all installed busses.
6141          */
6142         xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6143
6144         if (busses_to_config == 0) {
6145                 /* Call manually because we don't have any busses */
6146                 xpt_finishconfig(xpt_periph, NULL);
6147         } else  {
6148                 if (busses_to_reset > 0 && SCSI_DELAY >= 2000) {
6149                         printf("Waiting %d seconds for SCSI "
6150                                "devices to settle\n", SCSI_DELAY/1000);
6151                 }
6152                 xpt_for_all_busses(xptconfigfunc, NULL);
6153         }
6154 }
6155
6156 /*
6157  * If the given device only has one peripheral attached to it, and if that
6158  * peripheral is the passthrough driver, announce it.  This insures that the
6159  * user sees some sort of announcement for every peripheral in their system.
6160  */
6161 static int
6162 xptpassannouncefunc(struct cam_ed *device, void *arg)
6163 {
6164         struct cam_periph *periph;
6165         int i;
6166
6167         for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6168              periph = SLIST_NEXT(periph, periph_links), i++);
6169
6170         periph = SLIST_FIRST(&device->periphs);
6171         if ((i == 1)
6172          && (strncmp(periph->periph_name, "pass", 4) == 0))
6173                 xpt_announce_periph(periph, NULL);
6174
6175         return(1);
6176 }
6177
6178 static void
6179 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6180 {
6181         struct  periph_driver **p_drv;
6182         int     i;
6183
6184         if (done_ccb != NULL) {
6185                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6186                           ("xpt_finishconfig\n"));
6187                 switch(done_ccb->ccb_h.func_code) {
6188                 case XPT_RESET_BUS:
6189                         if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6190                                 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6191                                 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6192                                 xpt_action(done_ccb);
6193                                 return;
6194                         }
6195                         /* FALLTHROUGH */
6196                 case XPT_SCAN_BUS:
6197                 default:
6198                         xpt_free_path(done_ccb->ccb_h.path);
6199                         busses_to_config--;
6200                         break;
6201                 }
6202         }
6203
6204         if (busses_to_config == 0) {
6205                 /* Register all the peripheral drivers */
6206                 /* XXX This will have to change when we have loadable modules */
6207                 p_drv = periph_drivers;
6208                 for (i = 0; p_drv[i] != NULL; i++) {
6209                         (*p_drv[i]->init)();
6210                 }
6211
6212                 /*
6213                  * Check for devices with no "standard" peripheral driver
6214                  * attached.  For any devices like that, announce the
6215                  * passthrough driver so the user will see something.
6216                  */
6217                 xpt_for_all_devices(xptpassannouncefunc, NULL);
6218
6219                 /* Release our hook so that the boot can continue. */
6220                 config_intrhook_disestablish(xpt_config_hook);
6221                 free(xpt_config_hook, M_TEMP);
6222                 xpt_config_hook = NULL;
6223         }
6224         if (done_ccb != NULL)
6225                 xpt_free_ccb(done_ccb);
6226 }
6227
6228 static void
6229 xptaction(struct cam_sim *sim, union ccb *work_ccb)
6230 {
6231         CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6232
6233         switch (work_ccb->ccb_h.func_code) {
6234         /* Common cases first */
6235         case XPT_PATH_INQ:              /* Path routing inquiry */
6236         {
6237                 struct ccb_pathinq *cpi;
6238
6239                 cpi = &work_ccb->cpi;
6240                 cpi->version_num = 1; /* XXX??? */
6241                 cpi->hba_inquiry = 0;
6242                 cpi->target_sprt = 0;
6243                 cpi->hba_misc = 0;
6244                 cpi->hba_eng_cnt = 0;
6245                 cpi->max_target = 0;
6246                 cpi->max_lun = 0;
6247                 cpi->initiator_id = 0;
6248                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6249                 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6250                 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6251                 cpi->unit_number = sim->unit_number;
6252                 cpi->bus_id = sim->bus_id;
6253                 cpi->base_transfer_speed = 0;
6254                 cpi->ccb_h.status = CAM_REQ_CMP;
6255                 xpt_done(work_ccb);
6256                 break;
6257         }
6258         default:
6259                 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6260                 xpt_done(work_ccb);
6261                 break;
6262         }
6263 }
6264
6265 /*
6266  * The xpt as a "controller" has no interrupt sources, so polling
6267  * is a no-op.
6268  */
6269 static void
6270 xptpoll(struct cam_sim *sim)
6271 {
6272 }
6273
6274 static void
6275 camisr(void *V_queue)
6276 {
6277         cam_isrq_t *queue = V_queue;
6278         int     s;
6279         struct  ccb_hdr *ccb_h;
6280
6281         s = splcam();
6282         while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6283                 int     runq;
6284
6285                 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6286                 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6287                 splx(s);
6288
6289                 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6290                           ("camisr"));
6291
6292                 runq = FALSE;
6293
6294                 if (ccb_h->flags & CAM_HIGH_POWER) {
6295                         struct highpowerlist    *hphead;
6296                         struct cam_ed           *device;
6297                         union ccb               *send_ccb;
6298
6299                         hphead = &highpowerq;
6300
6301                         send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6302
6303                         /*
6304                          * Increment the count since this command is done.
6305                          */
6306                         num_highpower++;
6307
6308                         /* 
6309                          * Any high powered commands queued up?
6310                          */
6311                         if (send_ccb != NULL) {
6312                                 device = send_ccb->ccb_h.path->device;
6313
6314                                 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6315
6316                                 xpt_release_devq(send_ccb->ccb_h.path,
6317                                                  /*count*/1, /*runqueue*/TRUE);
6318                         }
6319                 }
6320                 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6321                         struct cam_ed *dev;
6322
6323                         dev = ccb_h->path->device;
6324
6325                         s = splcam();
6326                         cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6327
6328                         ccb_h->path->bus->sim->devq->send_active--;
6329                         ccb_h->path->bus->sim->devq->send_openings++;
6330                         splx(s);
6331                         
6332                         if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6333                          || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6334                           && (dev->ccbq.dev_active == 0))) {
6335                                 
6336                                 xpt_release_devq(ccb_h->path, /*count*/1,
6337                                                  /*run_queue*/TRUE);
6338                         }
6339
6340                         if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6341                          && (--dev->tag_delay_count == 0))
6342                                 xpt_start_tags(ccb_h->path);
6343
6344                         if ((dev->ccbq.queue.entries > 0)
6345                          && (dev->qfrozen_cnt == 0)
6346                          && (device_is_send_queued(dev) == 0)) {
6347                                 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6348                                                               dev);
6349                         }
6350                 }
6351
6352                 if (ccb_h->status & CAM_RELEASE_SIMQ) {
6353                         xpt_release_simq(ccb_h->path->bus->sim,
6354                                          /*run_queue*/TRUE);
6355                         ccb_h->status &= ~CAM_RELEASE_SIMQ;
6356                         runq = FALSE;
6357                 } 
6358
6359                 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6360                  && (ccb_h->status & CAM_DEV_QFRZN)) {
6361                         xpt_release_devq(ccb_h->path, /*count*/1,
6362                                          /*run_queue*/TRUE);
6363                         ccb_h->status &= ~CAM_DEV_QFRZN;
6364                 } else if (runq) {
6365                         xpt_run_dev_sendq(ccb_h->path->bus);
6366                 }
6367
6368                 /* Call the peripheral driver's callback */
6369                 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
6370
6371                 /* Raise IPL for while test */
6372                 s = splcam();
6373         }
6374         splx(s);
6375 }