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