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