]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/cam/scsi/scsi_xpt.c
MFC r344024:
[FreeBSD/stable/10.git] / sys / cam / scsi / scsi_xpt.c
1 /*-
2  * Implementation of the SCSI Transport
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/time.h>
40 #include <sys/conf.h>
41 #include <sys/fcntl.h>
42 #include <sys/md5.h>
43 #include <sys/interrupt.h>
44 #include <sys/sbuf.h>
45
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49
50 #include <cam/cam.h>
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_xpt_internal.h>
59 #include <cam/cam_debug.h>
60
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/scsi/scsi_pass.h>
64 #include <machine/stdarg.h>     /* for xpt_print below */
65 #include "opt_cam.h"
66
67 struct scsi_quirk_entry {
68         struct scsi_inquiry_pattern inq_pat;
69         u_int8_t quirks;
70 #define CAM_QUIRK_NOLUNS        0x01
71 #define CAM_QUIRK_NOVPDS        0x02
72 #define CAM_QUIRK_HILUNS        0x04
73 #define CAM_QUIRK_NOHILUNS      0x08
74 #define CAM_QUIRK_NORPTLUNS     0x10
75         u_int mintags;
76         u_int maxtags;
77 };
78 #define SCSI_QUIRK(dev) ((struct scsi_quirk_entry *)((dev)->quirk))
79
80 static int cam_srch_hi = 0;
81 TUNABLE_INT("kern.cam.cam_srch_hi", &cam_srch_hi);
82 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
83 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT|CTLFLAG_RW, 0, 0,
84     sysctl_cam_search_luns, "I",
85     "allow search above LUN 7 for SCSI3 and greater devices");
86
87 #define CAM_SCSI2_MAXLUN        8
88 #define CAM_CAN_GET_SIMPLE_LUN(x, i)                            \
89         ((((x)->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) ==  \
90         RPL_LUNDATA_ATYP_PERIPH) ||                             \
91         (((x)->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) ==   \
92         RPL_LUNDATA_ATYP_FLAT))
93 #define CAM_GET_SIMPLE_LUN(lp, i, lval)                                 \
94         if (((lp)->luns[(i)].lundata[0] & RPL_LUNDATA_ATYP_MASK) ==     \
95             RPL_LUNDATA_ATYP_PERIPH) {                                  \
96                 (lval) = (lp)->luns[(i)].lundata[1];                    \
97         } else {                                                        \
98                 (lval) = (lp)->luns[(i)].lundata[0];                    \
99                 (lval) &= RPL_LUNDATA_FLAT_LUN_MASK;                    \
100                 (lval) <<= 8;                                           \
101                 (lval) |=  (lp)->luns[(i)].lundata[1];                  \
102         }
103 #define CAM_GET_LUN(lp, i, lval)                                        \
104         (lval) = scsi_4btoul((lp)->luns[(i)].lundata);                  \
105         (lval) = ((lval) >> 16) | ((lval) << 16);
106 #define CAM_LUN_ONLY_32BITS(lp, i)                              \
107         (scsi_4btoul(&((lp)->luns[(i)].lundata[4])) == 0)
108
109 /*
110  * If we're not quirked to search <= the first 8 luns
111  * and we are either quirked to search above lun 8,
112  * or we're > SCSI-2 and we've enabled hilun searching,
113  * or we're > SCSI-2 and the last lun was a success,
114  * we can look for luns above lun 8.
115  */
116 #define CAN_SRCH_HI_SPARSE(dv)                                  \
117   (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0)         \
118   && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS)               \
119   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
120
121 #define CAN_SRCH_HI_DENSE(dv)                                   \
122   (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0)         \
123   && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS)               \
124   || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
125
126 static periph_init_t probe_periph_init;
127
128 static struct periph_driver probe_driver =
129 {
130         probe_periph_init, "probe",
131         TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
132         CAM_PERIPH_DRV_EARLY
133 };
134
135 PERIPHDRIVER_DECLARE(probe, probe_driver);
136
137 typedef enum {
138         PROBE_TUR,
139         PROBE_INQUIRY,  /* this counts as DV0 for Basic Domain Validation */
140         PROBE_FULL_INQUIRY,
141         PROBE_REPORT_LUNS,
142         PROBE_MODE_SENSE,
143         PROBE_SUPPORTED_VPD_LIST,
144         PROBE_DEVICE_ID,
145         PROBE_EXTENDED_INQUIRY,
146         PROBE_SERIAL_NUM,
147         PROBE_TUR_FOR_NEGOTIATION,
148         PROBE_INQUIRY_BASIC_DV1,
149         PROBE_INQUIRY_BASIC_DV2,
150         PROBE_DV_EXIT,
151         PROBE_DONE,
152         PROBE_INVALID
153 } probe_action;
154
155 static char *probe_action_text[] = {
156         "PROBE_TUR",
157         "PROBE_INQUIRY",
158         "PROBE_FULL_INQUIRY",
159         "PROBE_REPORT_LUNS",
160         "PROBE_MODE_SENSE",
161         "PROBE_SUPPORTED_VPD_LIST",
162         "PROBE_DEVICE_ID",
163         "PROBE_EXTENDED_INQUIRY",
164         "PROBE_SERIAL_NUM",
165         "PROBE_TUR_FOR_NEGOTIATION",
166         "PROBE_INQUIRY_BASIC_DV1",
167         "PROBE_INQUIRY_BASIC_DV2",
168         "PROBE_DV_EXIT",
169         "PROBE_DONE",
170         "PROBE_INVALID"
171 };
172
173 #define PROBE_SET_ACTION(softc, newaction)      \
174 do {                                                                    \
175         char **text;                                                    \
176         text = probe_action_text;                                       \
177         CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE,               \
178             ("Probe %s to %s\n", text[(softc)->action],                 \
179             text[(newaction)]));                                        \
180         (softc)->action = (newaction);                                  \
181 } while(0)
182
183 typedef enum {
184         PROBE_INQUIRY_CKSUM     = 0x01,
185         PROBE_SERIAL_CKSUM      = 0x02,
186         PROBE_NO_ANNOUNCE       = 0x04,
187         PROBE_EXTLUN            = 0x08
188 } probe_flags;
189
190 typedef struct {
191         TAILQ_HEAD(, ccb_hdr) request_ccbs;
192         probe_action    action;
193         union ccb       saved_ccb;
194         probe_flags     flags;
195         MD5_CTX         context;
196         u_int8_t        digest[16];
197         struct cam_periph *periph;
198 } probe_softc;
199
200 static const char quantum[] = "QUANTUM";
201 static const char sony[] = "SONY";
202 static const char west_digital[] = "WDIGTL";
203 static const char samsung[] = "SAMSUNG";
204 static const char seagate[] = "SEAGATE";
205 static const char microp[] = "MICROP";
206
207 static struct scsi_quirk_entry scsi_quirk_table[] =
208 {
209         {
210                 /* Reports QUEUE FULL for temporary resource shortages */
211                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
212                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
213         },
214         {
215                 /* Reports QUEUE FULL for temporary resource shortages */
216                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
217                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
218         },
219         {
220                 /* Reports QUEUE FULL for temporary resource shortages */
221                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
222                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
223         },
224         {
225                 /* Broken tagged queuing drive */
226                 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
227                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
228         },
229         {
230                 /* Broken tagged queuing drive */
231                 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
232                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
233         },
234         {
235                 /* Broken tagged queuing drive */
236                 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
237                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
238         },
239         {
240                 /*
241                  * Unfortunately, the Quantum Atlas III has the same
242                  * problem as the Atlas II drives above.
243                  * Reported by: "Johan Granlund" <johan@granlund.nu>
244                  *
245                  * For future reference, the drive with the problem was:
246                  * QUANTUM QM39100TD-SW N1B0
247                  *
248                  * It's possible that Quantum will fix the problem in later
249                  * firmware revisions.  If that happens, the quirk entry
250                  * will need to be made specific to the firmware revisions
251                  * with the problem.
252                  *
253                  */
254                 /* Reports QUEUE FULL for temporary resource shortages */
255                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
256                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
257         },
258         {
259                 /*
260                  * 18 Gig Atlas III, same problem as the 9G version.
261                  * Reported by: Andre Albsmeier
262                  *              <andre.albsmeier@mchp.siemens.de>
263                  *
264                  * For future reference, the drive with the problem was:
265                  * QUANTUM QM318000TD-S N491
266                  */
267                 /* Reports QUEUE FULL for temporary resource shortages */
268                 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
269                 /*quirks*/0, /*mintags*/24, /*maxtags*/32
270         },
271         {
272                 /*
273                  * Broken tagged queuing drive
274                  * Reported by: Bret Ford <bford@uop.cs.uop.edu>
275                  *         and: Martin Renters <martin@tdc.on.ca>
276                  */
277                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
278                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
279         },
280                 /*
281                  * The Seagate Medalist Pro drives have very poor write
282                  * performance with anything more than 2 tags.
283                  *
284                  * Reported by:  Paul van der Zwan <paulz@trantor.xs4all.nl>
285                  * Drive:  <SEAGATE ST36530N 1444>
286                  *
287                  * Reported by:  Jeremy Lea <reg@shale.csir.co.za>
288                  * Drive:  <SEAGATE ST34520W 1281>
289                  *
290                  * No one has actually reported that the 9G version
291                  * (ST39140*) of the Medalist Pro has the same problem, but
292                  * we're assuming that it does because the 4G and 6.5G
293                  * versions of the drive are broken.
294                  */
295         {
296                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
297                 /*quirks*/0, /*mintags*/2, /*maxtags*/2
298         },
299         {
300                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
301                 /*quirks*/0, /*mintags*/2, /*maxtags*/2
302         },
303         {
304                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
305                 /*quirks*/0, /*mintags*/2, /*maxtags*/2
306         },
307         {
308                 /*
309                  * Experiences command timeouts under load with a
310                  * tag count higher than 55.
311                  */
312                 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST3146855LW", "*"},
313                 /*quirks*/0, /*mintags*/2, /*maxtags*/55
314         },
315         {
316                 /*
317                  * Slow when tagged queueing is enabled.  Write performance
318                  * steadily drops off with more and more concurrent
319                  * transactions.  Best sequential write performance with
320                  * tagged queueing turned off and write caching turned on.
321                  *
322                  * PR:  kern/10398
323                  * Submitted by:  Hideaki Okada <hokada@isl.melco.co.jp>
324                  * Drive:  DCAS-34330 w/ "S65A" firmware.
325                  *
326                  * The drive with the problem had the "S65A" firmware
327                  * revision, and has also been reported (by Stephen J.
328                  * Roznowski <sjr@home.net>) for a drive with the "S61A"
329                  * firmware revision.
330                  *
331                  * Although no one has reported problems with the 2 gig
332                  * version of the DCAS drive, the assumption is that it
333                  * has the same problems as the 4 gig version.  Therefore
334                  * this quirk entries disables tagged queueing for all
335                  * DCAS drives.
336                  */
337                 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
338                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
339         },
340         {
341                 /* Broken tagged queuing drive */
342                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
343                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
344         },
345         {
346                 /* Broken tagged queuing drive */
347                 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
348                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
349         },
350         {
351                 /* This does not support other than LUN 0 */
352                 { T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
353                 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
354         },
355         {
356                 /*
357                  * Broken tagged queuing drive.
358                  * Submitted by:
359                  * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
360                  * in PR kern/9535
361                  */
362                 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
363                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
364         },
365         {
366                 /*
367                  * Slow when tagged queueing is enabled. (1.5MB/sec versus
368                  * 8MB/sec.)
369                  * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
370                  * Best performance with these drives is achieved with
371                  * tagged queueing turned off, and write caching turned on.
372                  */
373                 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
374                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
375         },
376         {
377                 /*
378                  * Slow when tagged queueing is enabled. (1.5MB/sec versus
379                  * 8MB/sec.)
380                  * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
381                  * Best performance with these drives is achieved with
382                  * tagged queueing turned off, and write caching turned on.
383                  */
384                 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
385                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
386         },
387         {
388                 /*
389                  * Doesn't handle queue full condition correctly,
390                  * so we need to limit maxtags to what the device
391                  * can handle instead of determining this automatically.
392                  */
393                 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
394                 /*quirks*/0, /*mintags*/2, /*maxtags*/32
395         },
396         {
397                 /* Really only one LUN */
398                 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
399                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
400         },
401         {
402                 /* I can't believe we need a quirk for DPT volumes. */
403                 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
404                 CAM_QUIRK_NOLUNS,
405                 /*mintags*/0, /*maxtags*/255
406         },
407         {
408                 /*
409                  * Many Sony CDROM drives don't like multi-LUN probing.
410                  */
411                 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
412                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
413         },
414         {
415                 /*
416                  * This drive doesn't like multiple LUN probing.
417                  * Submitted by:  Parag Patel <parag@cgt.com>
418                  */
419                 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R   CDU9*", "*" },
420                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
421         },
422         {
423                 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
424                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
425         },
426         {
427                 /*
428                  * The 8200 doesn't like multi-lun probing, and probably
429                  * don't like serial number requests either.
430                  */
431                 {
432                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
433                         "EXB-8200*", "*"
434                 },
435                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
436         },
437         {
438                 /*
439                  * Let's try the same as above, but for a drive that says
440                  * it's an IPL-6860 but is actually an EXB 8200.
441                  */
442                 {
443                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
444                         "IPL-6860*", "*"
445                 },
446                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
447         },
448         {
449                 /*
450                  * These Hitachi drives don't like multi-lun probing.
451                  * The PR submitter has a DK319H, but says that the Linux
452                  * kernel has a similar work-around for the DK312 and DK314,
453                  * so all DK31* drives are quirked here.
454                  * PR:            misc/18793
455                  * Submitted by:  Paul Haddad <paul@pth.com>
456                  */
457                 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
458                 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
459         },
460         {
461                 /*
462                  * The Hitachi CJ series with J8A8 firmware apparently has
463                  * problems with tagged commands.
464                  * PR: 23536
465                  * Reported by: amagai@nue.org
466                  */
467                 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
468                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
469         },
470         {
471                 /*
472                  * These are the large storage arrays.
473                  * Submitted by:  William Carrel <william.carrel@infospace.com>
474                  */
475                 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
476                 CAM_QUIRK_HILUNS, 2, 1024
477         },
478         {
479                 /*
480                  * This old revision of the TDC3600 is also SCSI-1, and
481                  * hangs upon serial number probing.
482                  */
483                 {
484                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
485                         " TDC 3600", "U07:"
486                 },
487                 CAM_QUIRK_NOVPDS, /*mintags*/0, /*maxtags*/0
488         },
489         {
490                 /*
491                  * Would repond to all LUNs if asked for.
492                  */
493                 {
494                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
495                         "CP150", "*"
496                 },
497                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
498         },
499         {
500                 /*
501                  * Would repond to all LUNs if asked for.
502                  */
503                 {
504                         T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
505                         "96X2*", "*"
506                 },
507                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
508         },
509         {
510                 /* Submitted by: Matthew Dodd <winter@jurai.net> */
511                 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
512                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
513         },
514         {
515                 /* Submitted by: Matthew Dodd <winter@jurai.net> */
516                 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
517                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
518         },
519         {
520                 /* TeraSolutions special settings for TRC-22 RAID */
521                 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
522                   /*quirks*/0, /*mintags*/55, /*maxtags*/255
523         },
524         {
525                 /* Veritas Storage Appliance */
526                 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
527                   CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
528         },
529         {
530                 /*
531                  * Would respond to all LUNs.  Device type and removable
532                  * flag are jumper-selectable.
533                  */
534                 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
535                   "Tahiti 1", "*"
536                 },
537                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
538         },
539         {
540                 /* EasyRAID E5A aka. areca ARC-6010 */
541                 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
542                   CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
543         },
544         {
545                 { T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
546                 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
547         },
548         {
549                 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Garmin", "*", "*" },
550                 CAM_QUIRK_NORPTLUNS, /*mintags*/2, /*maxtags*/255
551         },
552         {
553                 /* Default tagged queuing parameters for all devices */
554                 {
555                   T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
556                   /*vendor*/"*", /*product*/"*", /*revision*/"*"
557                 },
558                 /*quirks*/0, /*mintags*/2, /*maxtags*/255
559         },
560 };
561
562 static const int scsi_quirk_table_size =
563         sizeof(scsi_quirk_table) / sizeof(*scsi_quirk_table);
564
565 static cam_status       proberegister(struct cam_periph *periph,
566                                       void *arg);
567 static void      probeschedule(struct cam_periph *probe_periph);
568 static void      probestart(struct cam_periph *periph, union ccb *start_ccb);
569 static void      proberequestdefaultnegotiation(struct cam_periph *periph);
570 static int       proberequestbackoff(struct cam_periph *periph,
571                                      struct cam_ed *device);
572 static void      probedone(struct cam_periph *periph, union ccb *done_ccb);
573 static void      probe_purge_old(struct cam_path *path,
574                                  struct scsi_report_luns_data *new,
575                                  probe_flags flags);
576 static void      probecleanup(struct cam_periph *periph);
577 static void      scsi_find_quirk(struct cam_ed *device);
578 static void      scsi_scan_bus(struct cam_periph *periph, union ccb *ccb);
579 static void      scsi_scan_lun(struct cam_periph *periph,
580                                struct cam_path *path, cam_flags flags,
581                                union ccb *ccb);
582 static void      xptscandone(struct cam_periph *periph, union ccb *done_ccb);
583 static struct cam_ed *
584                  scsi_alloc_device(struct cam_eb *bus, struct cam_et *target,
585                                    lun_id_t lun_id);
586 static void      scsi_devise_transport(struct cam_path *path);
587 static void      scsi_set_transfer_settings(struct ccb_trans_settings *cts,
588                                             struct cam_path *path,
589                                             int async_update);
590 static void      scsi_toggle_tags(struct cam_path *path);
591 static void      scsi_dev_async(u_int32_t async_code,
592                                 struct cam_eb *bus,
593                                 struct cam_et *target,
594                                 struct cam_ed *device,
595                                 void *async_arg);
596 static void      scsi_action(union ccb *start_ccb);
597 static void      scsi_announce_periph(struct cam_periph *periph);
598
599 static struct xpt_xport scsi_xport = {
600         .alloc_device = scsi_alloc_device,
601         .action = scsi_action,
602         .async = scsi_dev_async,
603         .announce = scsi_announce_periph,
604 };
605
606 struct xpt_xport *
607 scsi_get_xport(void)
608 {
609         return (&scsi_xport);
610 }
611
612 static void
613 probe_periph_init()
614 {
615 }
616
617 static cam_status
618 proberegister(struct cam_periph *periph, void *arg)
619 {
620         union ccb *request_ccb; /* CCB representing the probe request */
621         cam_status status;
622         probe_softc *softc;
623
624         request_ccb = (union ccb *)arg;
625         if (request_ccb == NULL) {
626                 printf("proberegister: no probe CCB, "
627                        "can't register device\n");
628                 return(CAM_REQ_CMP_ERR);
629         }
630
631         softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
632
633         if (softc == NULL) {
634                 printf("proberegister: Unable to probe new device. "
635                        "Unable to allocate softc\n");
636                 return(CAM_REQ_CMP_ERR);
637         }
638         TAILQ_INIT(&softc->request_ccbs);
639         TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
640                           periph_links.tqe);
641         softc->flags = 0;
642         periph->softc = softc;
643         softc->periph = periph;
644         softc->action = PROBE_INVALID;
645         status = cam_periph_acquire(periph);
646         if (status != CAM_REQ_CMP) {
647                 return (status);
648         }
649         CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
650         scsi_devise_transport(periph->path);
651
652         /*
653          * Ensure we've waited at least a bus settle
654          * delay before attempting to probe the device.
655          * For HBAs that don't do bus resets, this won't make a difference.
656          */
657         cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
658                                       scsi_delay);
659         probeschedule(periph);
660         return(CAM_REQ_CMP);
661 }
662
663 static void
664 probeschedule(struct cam_periph *periph)
665 {
666         struct ccb_pathinq cpi;
667         union ccb *ccb;
668         probe_softc *softc;
669
670         softc = (probe_softc *)periph->softc;
671         ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
672
673         xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
674         cpi.ccb_h.func_code = XPT_PATH_INQ;
675         xpt_action((union ccb *)&cpi);
676
677         /*
678          * If a device has gone away and another device, or the same one,
679          * is back in the same place, it should have a unit attention
680          * condition pending.  It will not report the unit attention in
681          * response to an inquiry, which may leave invalid transfer
682          * negotiations in effect.  The TUR will reveal the unit attention
683          * condition.  Only send the TUR for lun 0, since some devices
684          * will get confused by commands other than inquiry to non-existent
685          * luns.  If you think a device has gone away start your scan from
686          * lun 0.  This will insure that any bogus transfer settings are
687          * invalidated.
688          *
689          * If we haven't seen the device before and the controller supports
690          * some kind of transfer negotiation, negotiate with the first
691          * sent command if no bus reset was performed at startup.  This
692          * ensures that the device is not confused by transfer negotiation
693          * settings left over by loader or BIOS action.
694          */
695         if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
696          && (ccb->ccb_h.target_lun == 0)) {
697                 PROBE_SET_ACTION(softc, PROBE_TUR);
698         } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
699               && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
700                 proberequestdefaultnegotiation(periph);
701                 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
702         } else {
703                 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
704         }
705
706         if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
707                 softc->flags |= PROBE_NO_ANNOUNCE;
708         else
709                 softc->flags &= ~PROBE_NO_ANNOUNCE;
710
711         if (cpi.hba_misc & PIM_EXTLUNS)
712                 softc->flags |= PROBE_EXTLUN;
713         else
714                 softc->flags &= ~PROBE_EXTLUN;
715
716         xpt_schedule(periph, CAM_PRIORITY_XPT);
717 }
718
719 static void
720 probestart(struct cam_periph *periph, union ccb *start_ccb)
721 {
722         /* Probe the device that our peripheral driver points to */
723         struct ccb_scsiio *csio;
724         probe_softc *softc;
725
726         CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
727
728         softc = (probe_softc *)periph->softc;
729         csio = &start_ccb->csio;
730 again:
731
732         switch (softc->action) {
733         case PROBE_TUR:
734         case PROBE_TUR_FOR_NEGOTIATION:
735         case PROBE_DV_EXIT:
736         {
737                 scsi_test_unit_ready(csio,
738                                      /*retries*/4,
739                                      probedone,
740                                      MSG_SIMPLE_Q_TAG,
741                                      SSD_FULL_SIZE,
742                                      /*timeout*/60000);
743                 break;
744         }
745         case PROBE_INQUIRY:
746         case PROBE_FULL_INQUIRY:
747         case PROBE_INQUIRY_BASIC_DV1:
748         case PROBE_INQUIRY_BASIC_DV2:
749         {
750                 u_int inquiry_len;
751                 struct scsi_inquiry_data *inq_buf;
752
753                 inq_buf = &periph->path->device->inq_data;
754
755                 /*
756                  * If the device is currently configured, we calculate an
757                  * MD5 checksum of the inquiry data, and if the serial number
758                  * length is greater than 0, add the serial number data
759                  * into the checksum as well.  Once the inquiry and the
760                  * serial number check finish, we attempt to figure out
761                  * whether we still have the same device.
762                  */
763                 if (((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
764                  && ((softc->flags & PROBE_INQUIRY_CKSUM) == 0)) {
765
766                         MD5Init(&softc->context);
767                         MD5Update(&softc->context, (unsigned char *)inq_buf,
768                                   sizeof(struct scsi_inquiry_data));
769                         softc->flags |= PROBE_INQUIRY_CKSUM;
770                         if (periph->path->device->serial_num_len > 0) {
771                                 MD5Update(&softc->context,
772                                           periph->path->device->serial_num,
773                                           periph->path->device->serial_num_len);
774                                 softc->flags |= PROBE_SERIAL_CKSUM;
775                         }
776                         MD5Final(softc->digest, &softc->context);
777                 }
778
779                 if (softc->action == PROBE_INQUIRY)
780                         inquiry_len = SHORT_INQUIRY_LENGTH;
781                 else
782                         inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
783
784                 /*
785                  * Some parallel SCSI devices fail to send an
786                  * ignore wide residue message when dealing with
787                  * odd length inquiry requests.  Round up to be
788                  * safe.
789                  */
790                 inquiry_len = roundup2(inquiry_len, 2);
791
792                 if (softc->action == PROBE_INQUIRY_BASIC_DV1
793                  || softc->action == PROBE_INQUIRY_BASIC_DV2) {
794                         inq_buf = malloc(inquiry_len, M_CAMXPT, M_NOWAIT);
795                 }
796                 if (inq_buf == NULL) {
797                         xpt_print(periph->path, "malloc failure- skipping Basic"
798                             "Domain Validation\n");
799                         PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
800                         scsi_test_unit_ready(csio,
801                                              /*retries*/4,
802                                              probedone,
803                                              MSG_SIMPLE_Q_TAG,
804                                              SSD_FULL_SIZE,
805                                              /*timeout*/60000);
806                         break;
807                 }
808                 scsi_inquiry(csio,
809                              /*retries*/4,
810                              probedone,
811                              MSG_SIMPLE_Q_TAG,
812                              (u_int8_t *)inq_buf,
813                              inquiry_len,
814                              /*evpd*/FALSE,
815                              /*page_code*/0,
816                              SSD_MIN_SIZE,
817                              /*timeout*/60 * 1000);
818                 break;
819         }
820         case PROBE_REPORT_LUNS:
821         {
822                 void *rp;
823
824                 rp = malloc(periph->path->target->rpl_size,
825                     M_CAMXPT, M_NOWAIT | M_ZERO);
826                 if (rp == NULL) {
827                         struct scsi_inquiry_data *inq_buf;
828                         inq_buf = &periph->path->device->inq_data;
829                         xpt_print(periph->path,
830                             "Unable to alloc report luns storage\n");
831                         if (INQ_DATA_TQ_ENABLED(inq_buf))
832                                 PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
833                         else
834                                 PROBE_SET_ACTION(softc,
835                                     PROBE_SUPPORTED_VPD_LIST);
836                         goto again;
837                 }
838                 scsi_report_luns(csio, 5, probedone, MSG_SIMPLE_Q_TAG,
839                     RPL_REPORT_DEFAULT, rp, periph->path->target->rpl_size,
840                     SSD_FULL_SIZE, 60000); break;
841                 break;
842         }
843         case PROBE_MODE_SENSE:
844         {
845                 void  *mode_buf;
846                 int    mode_buf_len;
847
848                 mode_buf_len = sizeof(struct scsi_mode_header_6)
849                              + sizeof(struct scsi_mode_blk_desc)
850                              + sizeof(struct scsi_control_page);
851                 mode_buf = malloc(mode_buf_len, M_CAMXPT, M_NOWAIT);
852                 if (mode_buf != NULL) {
853                         scsi_mode_sense(csio,
854                                         /*retries*/4,
855                                         probedone,
856                                         MSG_SIMPLE_Q_TAG,
857                                         /*dbd*/FALSE,
858                                         SMS_PAGE_CTRL_CURRENT,
859                                         SMS_CONTROL_MODE_PAGE,
860                                         mode_buf,
861                                         mode_buf_len,
862                                         SSD_FULL_SIZE,
863                                         /*timeout*/60000);
864                         break;
865                 }
866                 xpt_print(periph->path, "Unable to mode sense control page - "
867                     "malloc failure\n");
868                 PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
869         }
870         /* FALLTHROUGH */
871         case PROBE_SUPPORTED_VPD_LIST:
872         {
873                 struct scsi_vpd_supported_page_list *vpd_list;
874                 struct cam_ed *device;
875
876                 vpd_list = NULL;
877                 device = periph->path->device;
878
879                 if ((SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOVPDS) == 0)
880                         vpd_list = malloc(sizeof(*vpd_list), M_CAMXPT,
881                             M_NOWAIT | M_ZERO);
882
883                 if (vpd_list != NULL) {
884                         scsi_inquiry(csio,
885                                      /*retries*/4,
886                                      probedone,
887                                      MSG_SIMPLE_Q_TAG,
888                                      (u_int8_t *)vpd_list,
889                                      sizeof(*vpd_list),
890                                      /*evpd*/TRUE,
891                                      SVPD_SUPPORTED_PAGE_LIST,
892                                      SSD_MIN_SIZE,
893                                      /*timeout*/60 * 1000);
894                         break;
895                 }
896 done:
897                 /*
898                  * We'll have to do without, let our probedone
899                  * routine finish up for us.
900                  */
901                 start_ccb->csio.data_ptr = NULL;
902                 cam_freeze_devq(periph->path);
903                 cam_periph_doacquire(periph);
904                 probedone(periph, start_ccb);
905                 return;
906         }
907         case PROBE_DEVICE_ID:
908         {
909                 struct scsi_vpd_device_id *devid;
910
911                 devid = NULL;
912                 if (scsi_vpd_supported_page(periph, SVPD_DEVICE_ID))
913                         devid = malloc(SVPD_DEVICE_ID_MAX_SIZE, M_CAMXPT,
914                             M_NOWAIT | M_ZERO);
915
916                 if (devid != NULL) {
917                         scsi_inquiry(csio,
918                                      /*retries*/4,
919                                      probedone,
920                                      MSG_SIMPLE_Q_TAG,
921                                      (uint8_t *)devid,
922                                      SVPD_DEVICE_ID_MAX_SIZE,
923                                      /*evpd*/TRUE,
924                                      SVPD_DEVICE_ID,
925                                      SSD_MIN_SIZE,
926                                      /*timeout*/60 * 1000);
927                         break;
928                 }
929                 goto done;
930         }
931         case PROBE_EXTENDED_INQUIRY:
932         {
933                 struct scsi_vpd_extended_inquiry_data *ext_inq;
934
935                 ext_inq = NULL;
936                 if (scsi_vpd_supported_page(periph, SVPD_EXTENDED_INQUIRY_DATA))
937                         ext_inq = malloc(sizeof(*ext_inq), M_CAMXPT,
938                             M_NOWAIT | M_ZERO);
939
940                 if (ext_inq != NULL) {
941                         scsi_inquiry(csio,
942                                      /*retries*/4,
943                                      probedone,
944                                      MSG_SIMPLE_Q_TAG,
945                                      (uint8_t *)ext_inq,
946                                      sizeof(*ext_inq),
947                                      /*evpd*/TRUE,
948                                      SVPD_EXTENDED_INQUIRY_DATA,
949                                      SSD_MIN_SIZE,
950                                      /*timeout*/60 * 1000);
951                         break;
952                 }
953                 /*
954                  * We'll have to do without, let our probedone
955                  * routine finish up for us.
956                  */
957                 goto done;
958         }
959         case PROBE_SERIAL_NUM:
960         {
961                 struct scsi_vpd_unit_serial_number *serial_buf;
962                 struct cam_ed* device;
963
964                 serial_buf = NULL;
965                 device = periph->path->device;
966                 if (device->serial_num != NULL) {
967                         free(device->serial_num, M_CAMXPT);
968                         device->serial_num = NULL;
969                         device->serial_num_len = 0;
970                 }
971
972                 if (scsi_vpd_supported_page(periph, SVPD_UNIT_SERIAL_NUMBER))
973                         serial_buf = (struct scsi_vpd_unit_serial_number *)
974                                 malloc(sizeof(*serial_buf), M_CAMXPT,
975                                     M_NOWAIT|M_ZERO);
976
977                 if (serial_buf != NULL) {
978                         scsi_inquiry(csio,
979                                      /*retries*/4,
980                                      probedone,
981                                      MSG_SIMPLE_Q_TAG,
982                                      (u_int8_t *)serial_buf,
983                                      sizeof(*serial_buf),
984                                      /*evpd*/TRUE,
985                                      SVPD_UNIT_SERIAL_NUMBER,
986                                      SSD_MIN_SIZE,
987                                      /*timeout*/60 * 1000);
988                         break;
989                 }
990                 goto done;
991         }
992         default:
993                 panic("probestart: invalid action state 0x%x\n", softc->action);
994         }
995         start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
996         cam_periph_doacquire(periph);
997         xpt_action(start_ccb);
998 }
999
1000 static void
1001 proberequestdefaultnegotiation(struct cam_periph *periph)
1002 {
1003         struct ccb_trans_settings cts;
1004
1005         xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1006         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1007         cts.type = CTS_TYPE_USER_SETTINGS;
1008         xpt_action((union ccb *)&cts);
1009         if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1010                 return;
1011         }
1012         cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1013         cts.type = CTS_TYPE_CURRENT_SETTINGS;
1014         xpt_action((union ccb *)&cts);
1015 }
1016
1017 /*
1018  * Backoff Negotiation Code- only pertinent for SPI devices.
1019  */
1020 static int
1021 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
1022 {
1023         struct ccb_trans_settings cts;
1024         struct ccb_trans_settings_spi *spi;
1025
1026         memset(&cts, 0, sizeof (cts));
1027         xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1028         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1029         cts.type = CTS_TYPE_CURRENT_SETTINGS;
1030         xpt_action((union ccb *)&cts);
1031         if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1032                 if (bootverbose) {
1033                         xpt_print(periph->path,
1034                             "failed to get current device settings\n");
1035                 }
1036                 return (0);
1037         }
1038         if (cts.transport != XPORT_SPI) {
1039                 if (bootverbose) {
1040                         xpt_print(periph->path, "not SPI transport\n");
1041                 }
1042                 return (0);
1043         }
1044         spi = &cts.xport_specific.spi;
1045
1046         /*
1047          * We cannot renegotiate sync rate if we don't have one.
1048          */
1049         if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
1050                 if (bootverbose) {
1051                         xpt_print(periph->path, "no sync rate known\n");
1052                 }
1053                 return (0);
1054         }
1055
1056         /*
1057          * We'll assert that we don't have to touch PPR options- the
1058          * SIM will see what we do with period and offset and adjust
1059          * the PPR options as appropriate.
1060          */
1061
1062         /*
1063          * A sync rate with unknown or zero offset is nonsensical.
1064          * A sync period of zero means Async.
1065          */
1066         if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
1067          || spi->sync_offset == 0 || spi->sync_period == 0) {
1068                 if (bootverbose) {
1069                         xpt_print(periph->path, "no sync rate available\n");
1070                 }
1071                 return (0);
1072         }
1073
1074         if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
1075                 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1076                     ("hit async: giving up on DV\n"));
1077                 return (0);
1078         }
1079
1080
1081         /*
1082          * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
1083          * We don't try to remember 'last' settings to see if the SIM actually
1084          * gets into the speed we want to set. We check on the SIM telling
1085          * us that a requested speed is bad, but otherwise don't try and
1086          * check the speed due to the asynchronous and handshake nature
1087          * of speed setting.
1088          */
1089         spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
1090         for (;;) {
1091                 spi->sync_period++;
1092                 if (spi->sync_period >= 0xf) {
1093                         spi->sync_period = 0;
1094                         spi->sync_offset = 0;
1095                         CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1096                             ("setting to async for DV\n"));
1097                         /*
1098                          * Once we hit async, we don't want to try
1099                          * any more settings.
1100                          */
1101                         device->flags |= CAM_DEV_DV_HIT_BOTTOM;
1102                 } else if (bootverbose) {
1103                         CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1104                             ("DV: period 0x%x\n", spi->sync_period));
1105                         printf("setting period to 0x%x\n", spi->sync_period);
1106                 }
1107                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1108                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1109                 xpt_action((union ccb *)&cts);
1110                 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1111                         break;
1112                 }
1113                 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1114                     ("DV: failed to set period 0x%x\n", spi->sync_period));
1115                 if (spi->sync_period == 0) {
1116                         return (0);
1117                 }
1118         }
1119         return (1);
1120 }
1121
1122 #define CCB_COMPLETED_OK(ccb) (((ccb).status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1123
1124 static void
1125 probedone(struct cam_periph *periph, union ccb *done_ccb)
1126 {
1127         probe_softc *softc;
1128         struct cam_path *path;
1129         struct scsi_inquiry_data *inq_buf;
1130         u_int32_t  priority;
1131
1132         CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
1133
1134         softc = (probe_softc *)periph->softc;
1135         path = done_ccb->ccb_h.path;
1136         priority = done_ccb->ccb_h.pinfo.priority;
1137
1138         switch (softc->action) {
1139         case PROBE_TUR:
1140         {
1141                 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1142
1143                         if (cam_periph_error(done_ccb, 0,
1144                                              SF_NO_PRINT, NULL) == ERESTART) {
1145 outr:
1146                                 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1147                                 cam_release_devq(path, 0, 0, 0, FALSE);
1148                                 return;
1149                         }
1150                         else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1151                                 /* Don't wedge the queue */
1152                                 xpt_release_devq(done_ccb->ccb_h.path,
1153                                                  /*count*/1,
1154                                                  /*run_queue*/TRUE);
1155                 }
1156                 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1157                 xpt_release_ccb(done_ccb);
1158                 xpt_schedule(periph, priority);
1159 out:
1160                 /* Drop freeze taken due to CAM_DEV_QFREEZE and release. */
1161                 cam_release_devq(path, 0, 0, 0, FALSE);
1162                 cam_periph_release_locked(periph);
1163                 return;
1164         }
1165         case PROBE_INQUIRY:
1166         case PROBE_FULL_INQUIRY:
1167         {
1168                 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1169                         u_int8_t periph_qual;
1170
1171                         path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1172                         scsi_find_quirk(path->device);
1173                         inq_buf = &path->device->inq_data;
1174
1175                         periph_qual = SID_QUAL(inq_buf);
1176
1177                         if (periph_qual == SID_QUAL_LU_CONNECTED ||
1178                             periph_qual == SID_QUAL_LU_OFFLINE) {
1179                                 u_int8_t len;
1180
1181                                 /*
1182                                  * We conservatively request only
1183                                  * SHORT_INQUIRY_LEN bytes of inquiry
1184                                  * information during our first try
1185                                  * at sending an INQUIRY. If the device
1186                                  * has more information to give,
1187                                  * perform a second request specifying
1188                                  * the amount of information the device
1189                                  * is willing to give.
1190                                  */
1191                                 len = inq_buf->additional_length
1192                                     + offsetof(struct scsi_inquiry_data,
1193                                                additional_length) + 1;
1194                                 if (softc->action == PROBE_INQUIRY
1195                                     && len > SHORT_INQUIRY_LENGTH) {
1196                                         PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1197                                         xpt_release_ccb(done_ccb);
1198                                         xpt_schedule(periph, priority);
1199                                         goto out;
1200                                 }
1201
1202                                 scsi_devise_transport(path);
1203
1204                                 if (path->device->lun_id == 0 &&
1205                                     SID_ANSI_REV(inq_buf) > SCSI_REV_SPC2 &&
1206                                     (SCSI_QUIRK(path->device)->quirks &
1207                                      CAM_QUIRK_NORPTLUNS) == 0) {
1208                                         PROBE_SET_ACTION(softc,
1209                                             PROBE_REPORT_LUNS);
1210                                         /*
1211                                          * Start with room for *one* lun.
1212                                          */
1213                                         periph->path->target->rpl_size = 16;
1214                                 } else if (INQ_DATA_TQ_ENABLED(inq_buf))
1215                                         PROBE_SET_ACTION(softc,
1216                                             PROBE_MODE_SENSE);
1217                                 else
1218                                         PROBE_SET_ACTION(softc,
1219                                             PROBE_SUPPORTED_VPD_LIST);
1220
1221                                 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1222                                         path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1223                                         xpt_acquire_device(path->device);
1224                                 }
1225                                 xpt_release_ccb(done_ccb);
1226                                 xpt_schedule(periph, priority);
1227                                 goto out;
1228                         } else if (path->device->lun_id == 0 &&
1229                             SID_ANSI_REV(inq_buf) >= SCSI_REV_SPC2 &&
1230                             (SCSI_QUIRK(path->device)->quirks &
1231                              CAM_QUIRK_NORPTLUNS) == 0) {
1232                                 PROBE_SET_ACTION(softc, PROBE_REPORT_LUNS);
1233                                 periph->path->target->rpl_size = 16;
1234                                 xpt_release_ccb(done_ccb);
1235                                 xpt_schedule(periph, priority);
1236                                 goto out;
1237                         }
1238                 } else if (cam_periph_error(done_ccb, 0,
1239                                             done_ccb->ccb_h.target_lun > 0
1240                                             ? SF_RETRY_UA|SF_QUIET_IR
1241                                             : SF_RETRY_UA,
1242                                             &softc->saved_ccb) == ERESTART) {
1243                         goto outr;
1244                 } else {
1245                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1246                                 /* Don't wedge the queue */
1247                                 xpt_release_devq(done_ccb->ccb_h.path,
1248                                     /*count*/1, /*run_queue*/TRUE);
1249                         }
1250                         path->device->flags &= ~CAM_DEV_INQUIRY_DATA_VALID;
1251                 }
1252                 /*
1253                  * If we get to this point, we got an error status back
1254                  * from the inquiry and the error status doesn't require
1255                  * automatically retrying the command.  Therefore, the
1256                  * inquiry failed.  If we had inquiry information before
1257                  * for this device, but this latest inquiry command failed,
1258                  * the device has probably gone away.  If this device isn't
1259                  * already marked unconfigured, notify the peripheral
1260                  * drivers that this device is no more.
1261                  */
1262                 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
1263                         /* Send the async notification. */
1264                         xpt_async(AC_LOST_DEVICE, path, NULL);
1265                 PROBE_SET_ACTION(softc, PROBE_INVALID);
1266
1267                 xpt_release_ccb(done_ccb);
1268                 break;
1269         }
1270         case PROBE_REPORT_LUNS:
1271         {
1272                 struct ccb_scsiio *csio;
1273                 struct scsi_report_luns_data *lp;
1274                 u_int nlun, maxlun;
1275
1276                 csio = &done_ccb->csio;
1277
1278                 lp = (struct scsi_report_luns_data *)csio->data_ptr;
1279                 nlun = scsi_4btoul(lp->length) / 8;
1280                 maxlun = (csio->dxfer_len / 8) - 1;
1281
1282                 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1283                         if (cam_periph_error(done_ccb, 0,
1284                             done_ccb->ccb_h.target_lun > 0 ?
1285                             SF_RETRY_UA|SF_QUIET_IR : SF_RETRY_UA,
1286                             &softc->saved_ccb) == ERESTART) {
1287                                 goto outr;
1288                         }
1289                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1290                                 xpt_release_devq(done_ccb->ccb_h.path, 1,
1291                                     TRUE);
1292                         }
1293                         free(lp, M_CAMXPT);
1294                         lp = NULL;
1295                 } else if (nlun > maxlun) {
1296                         /*
1297                          * Reallocate and retry to cover all luns
1298                          */
1299                         CAM_DEBUG(path, CAM_DEBUG_PROBE,
1300                             ("Probe: reallocating REPORT_LUNS for %u luns\n",
1301                              nlun));
1302                         free(lp, M_CAMXPT);
1303                         path->target->rpl_size = (nlun << 3) + 8;
1304                         xpt_release_ccb(done_ccb);
1305                         xpt_schedule(periph, priority);
1306                         goto out;
1307                 } else if (nlun == 0) {
1308                         /*
1309                          * If there don't appear to be any luns, bail.
1310                          */
1311                         free(lp, M_CAMXPT);
1312                         lp = NULL;
1313                 } else {
1314                         lun_id_t lun;
1315                         int idx;
1316
1317                         CAM_DEBUG(path, CAM_DEBUG_PROBE,
1318                            ("Probe: %u lun(s) reported\n", nlun));
1319
1320                         CAM_GET_LUN(lp, 0, lun);
1321                         /*
1322                          * If the first lun is not lun 0, then either there
1323                          * is no lun 0 in the list, or the list is unsorted.
1324                          */
1325                         if (lun != 0) {
1326                                 for (idx = 0; idx < nlun; idx++) {
1327                                         CAM_GET_LUN(lp, idx, lun);
1328                                         if (lun == 0) {
1329                                                 break;
1330                                         }
1331                                 }
1332                                 if (idx != nlun) {
1333                                         uint8_t tlun[8];
1334                                         memcpy(tlun,
1335                                             lp->luns[0].lundata, 8);
1336                                         memcpy(lp->luns[0].lundata,
1337                                             lp->luns[idx].lundata, 8);
1338                                         memcpy(lp->luns[idx].lundata,
1339                                             tlun, 8);
1340                                         CAM_DEBUG(path, CAM_DEBUG_PROBE,
1341                                             ("lun 0 in position %u\n", idx));
1342                                 }
1343                         }
1344                         /*
1345                          * If we have an old lun list, We can either
1346                          * retest luns that appear to have been dropped,
1347                          * or just nuke them.  We'll opt for the latter.
1348                          * This function will also install the new list
1349                          * in the target structure.
1350                          */
1351                         probe_purge_old(path, lp, softc->flags);
1352                         lp = NULL;
1353                 }
1354                 /* The processing above should either exit via a `goto
1355                  * out` or leave the `lp` variable `NULL` and (if
1356                  * applicable) `free()` the storage to which it had
1357                  * pointed. Assert here that is the case.
1358                  */
1359                 KASSERT(lp == NULL, ("%s: lp is not NULL", __func__));
1360                 inq_buf = &path->device->inq_data;
1361                 if (path->device->flags & CAM_DEV_INQUIRY_DATA_VALID &&
1362                     (SID_QUAL(inq_buf) == SID_QUAL_LU_CONNECTED ||
1363                     SID_QUAL(inq_buf) == SID_QUAL_LU_OFFLINE)) {
1364                         if (INQ_DATA_TQ_ENABLED(inq_buf))
1365                                 PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
1366                         else
1367                                 PROBE_SET_ACTION(softc,
1368                                     PROBE_SUPPORTED_VPD_LIST);
1369                         xpt_release_ccb(done_ccb);
1370                         xpt_schedule(periph, priority);
1371                         goto out;
1372                 }
1373                 PROBE_SET_ACTION(softc, PROBE_INVALID);
1374                 xpt_release_ccb(done_ccb);
1375                 break;
1376         }
1377         case PROBE_MODE_SENSE:
1378         {
1379                 struct ccb_scsiio *csio;
1380                 struct scsi_mode_header_6 *mode_hdr;
1381
1382                 csio = &done_ccb->csio;
1383                 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
1384                 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1385                         struct scsi_control_page *page;
1386                         u_int8_t *offset;
1387
1388                         offset = ((u_int8_t *)&mode_hdr[1])
1389                             + mode_hdr->blk_desc_len;
1390                         page = (struct scsi_control_page *)offset;
1391                         path->device->queue_flags = page->queue_flags;
1392                 } else if (cam_periph_error(done_ccb, 0,
1393                                             SF_RETRY_UA|SF_NO_PRINT,
1394                                             &softc->saved_ccb) == ERESTART) {
1395                         goto outr;
1396                 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1397                         /* Don't wedge the queue */
1398                         xpt_release_devq(done_ccb->ccb_h.path,
1399                                          /*count*/1, /*run_queue*/TRUE);
1400                 }
1401                 xpt_release_ccb(done_ccb);
1402                 free(mode_hdr, M_CAMXPT);
1403                 PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
1404                 xpt_schedule(periph, priority);
1405                 goto out;
1406         }
1407         case PROBE_SUPPORTED_VPD_LIST:
1408         {
1409                 struct ccb_scsiio *csio;
1410                 struct scsi_vpd_supported_page_list *page_list;
1411
1412                 csio = &done_ccb->csio;
1413                 page_list =
1414                     (struct scsi_vpd_supported_page_list *)csio->data_ptr;
1415
1416                 if (path->device->supported_vpds != NULL) {
1417                         free(path->device->supported_vpds, M_CAMXPT);
1418                         path->device->supported_vpds = NULL;
1419                         path->device->supported_vpds_len = 0;
1420                 }
1421
1422                 if (page_list == NULL) {
1423                         /*
1424                          * Don't process the command as it was never sent
1425                          */
1426                 } else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1427                         /* Got vpd list */
1428                         path->device->supported_vpds_len = page_list->length +
1429                             SVPD_SUPPORTED_PAGES_HDR_LEN;
1430                         path->device->supported_vpds = (uint8_t *)page_list;
1431                         xpt_release_ccb(done_ccb);
1432                         PROBE_SET_ACTION(softc, PROBE_DEVICE_ID);
1433                         xpt_schedule(periph, priority);
1434                         goto out;
1435                 } else if (cam_periph_error(done_ccb, 0,
1436                                             SF_RETRY_UA|SF_NO_PRINT,
1437                                             &softc->saved_ccb) == ERESTART) {
1438                         goto outr;
1439                 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1440                         /* Don't wedge the queue */
1441                         xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1442                                          /*run_queue*/TRUE);
1443                 }
1444
1445                 if (page_list)
1446                         free(page_list, M_CAMXPT);
1447                 /* No VPDs available, skip to device check. */
1448                 csio->data_ptr = NULL;
1449                 goto probe_device_check;
1450         }
1451         case PROBE_DEVICE_ID:
1452         {
1453                 struct scsi_vpd_device_id *devid;
1454                 struct ccb_scsiio *csio;
1455                 uint32_t length = 0;
1456
1457                 csio = &done_ccb->csio;
1458                 devid = (struct scsi_vpd_device_id *)csio->data_ptr;
1459
1460                 /* Clean up from previous instance of this device */
1461                 if (path->device->device_id != NULL) {
1462                         path->device->device_id_len = 0;
1463                         free(path->device->device_id, M_CAMXPT);
1464                         path->device->device_id = NULL;
1465                 }
1466
1467                 if (devid == NULL) {
1468                         /* Don't process the command as it was never sent */
1469                 } else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1470                         length = scsi_2btoul(devid->length);
1471                         if (length != 0) {
1472                                 /*
1473                                  * NB: device_id_len is actual response
1474                                  * size, not buffer size.
1475                                  */
1476                                 path->device->device_id_len = length +
1477                                     SVPD_DEVICE_ID_HDR_LEN;
1478                                 path->device->device_id = (uint8_t *)devid;
1479                         }
1480                 } else if (cam_periph_error(done_ccb, 0,
1481                                             SF_RETRY_UA,
1482                                             &softc->saved_ccb) == ERESTART) {
1483                         goto outr;
1484                 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1485                         /* Don't wedge the queue */
1486                         xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1487                                          /*run_queue*/TRUE);
1488                 }
1489
1490                 /* Free the device id space if we don't use it */
1491                 if (devid && length == 0)
1492                         free(devid, M_CAMXPT);
1493                 xpt_release_ccb(done_ccb);
1494                 PROBE_SET_ACTION(softc, PROBE_EXTENDED_INQUIRY);
1495                 xpt_schedule(periph, priority);
1496                 goto out;
1497         }
1498         case PROBE_EXTENDED_INQUIRY: {
1499                 struct scsi_vpd_extended_inquiry_data *ext_inq;
1500                 struct ccb_scsiio *csio;
1501                 int32_t length = 0;
1502
1503                 csio = &done_ccb->csio;
1504                 ext_inq = (struct scsi_vpd_extended_inquiry_data *)
1505                     csio->data_ptr;
1506                 if (path->device->ext_inq != NULL) {
1507                         path->device->ext_inq_len = 0;
1508                         free(path->device->ext_inq, M_CAMXPT);
1509                         path->device->ext_inq = NULL;
1510                 }
1511
1512                 if (ext_inq == NULL) {
1513                         /* Don't process the command as it was never sent */
1514                 } else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1515                         length = scsi_2btoul(ext_inq->page_length) +
1516                             __offsetof(struct scsi_vpd_extended_inquiry_data,
1517                             flags1);
1518                         length = min(length, sizeof(*ext_inq));
1519                         length -= csio->resid;
1520                         if (length > 0) {
1521                                 path->device->ext_inq_len = length;
1522                                 path->device->ext_inq = (uint8_t *)ext_inq;
1523                         }
1524                 } else if (cam_periph_error(done_ccb, 0,
1525                                             SF_RETRY_UA,
1526                                             &softc->saved_ccb) == ERESTART) {
1527                         goto outr;
1528                 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1529                         /* Don't wedge the queue */
1530                         xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1531                                          /*run_queue*/TRUE);
1532                 }
1533
1534                 /* Free the device id space if we don't use it */
1535                 if (ext_inq && length <= 0)
1536                         free(ext_inq, M_CAMXPT);
1537                 xpt_release_ccb(done_ccb);
1538                 PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM);
1539                 xpt_schedule(periph, priority);
1540                 goto out;
1541         }
1542
1543 probe_device_check:
1544         case PROBE_SERIAL_NUM:
1545         {
1546                 struct ccb_scsiio *csio;
1547                 struct scsi_vpd_unit_serial_number *serial_buf;
1548                 u_int32_t  priority;
1549                 int changed;
1550                 int have_serialnum;
1551
1552                 changed = 1;
1553                 have_serialnum = 0;
1554                 csio = &done_ccb->csio;
1555                 priority = done_ccb->ccb_h.pinfo.priority;
1556                 serial_buf =
1557                     (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
1558
1559                 if (serial_buf == NULL) {
1560                         /*
1561                          * Don't process the command as it was never sent
1562                          */
1563                 } else if (cam_ccb_status(done_ccb) == CAM_REQ_CMP
1564                         && (serial_buf->length > 0)) {
1565
1566                         have_serialnum = 1;
1567                         path->device->serial_num =
1568                                 (u_int8_t *)malloc((serial_buf->length + 1),
1569                                                    M_CAMXPT, M_NOWAIT);
1570                         if (path->device->serial_num != NULL) {
1571                                 memcpy(path->device->serial_num,
1572                                        serial_buf->serial_num,
1573                                        serial_buf->length);
1574                                 path->device->serial_num_len =
1575                                     serial_buf->length;
1576                                 path->device->serial_num[serial_buf->length]
1577                                     = '\0';
1578                         }
1579                 } else if (cam_periph_error(done_ccb, 0,
1580                                             SF_RETRY_UA|SF_NO_PRINT,
1581                                             &softc->saved_ccb) == ERESTART) {
1582                         goto outr;
1583                 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1584                         /* Don't wedge the queue */
1585                         xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1586                                          /*run_queue*/TRUE);
1587                 }
1588
1589                 /*
1590                  * Let's see if we have seen this device before.
1591                  */
1592                 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
1593                         MD5_CTX context;
1594                         u_int8_t digest[16];
1595
1596                         MD5Init(&context);
1597
1598                         MD5Update(&context,
1599                                   (unsigned char *)&path->device->inq_data,
1600                                   sizeof(struct scsi_inquiry_data));
1601
1602                         if (have_serialnum)
1603                                 MD5Update(&context, serial_buf->serial_num,
1604                                           serial_buf->length);
1605
1606                         MD5Final(digest, &context);
1607                         if (bcmp(softc->digest, digest, 16) == 0)
1608                                 changed = 0;
1609
1610                         /*
1611                          * XXX Do we need to do a TUR in order to ensure
1612                          *     that the device really hasn't changed???
1613                          */
1614                         if ((changed != 0)
1615                          && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
1616                                 xpt_async(AC_LOST_DEVICE, path, NULL);
1617                 }
1618                 if (serial_buf != NULL)
1619                         free(serial_buf, M_CAMXPT);
1620
1621                 if (changed != 0) {
1622                         /*
1623                          * Now that we have all the necessary
1624                          * information to safely perform transfer
1625                          * negotiations... Controllers don't perform
1626                          * any negotiation or tagged queuing until
1627                          * after the first XPT_SET_TRAN_SETTINGS ccb is
1628                          * received.  So, on a new device, just retrieve
1629                          * the user settings, and set them as the current
1630                          * settings to set the device up.
1631                          */
1632                         proberequestdefaultnegotiation(periph);
1633                         xpt_release_ccb(done_ccb);
1634
1635                         /*
1636                          * Perform a TUR to allow the controller to
1637                          * perform any necessary transfer negotiation.
1638                          */
1639                         PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1640                         xpt_schedule(periph, priority);
1641                         goto out;
1642                 }
1643                 xpt_release_ccb(done_ccb);
1644                 break;
1645         }
1646         case PROBE_TUR_FOR_NEGOTIATION:
1647         case PROBE_DV_EXIT:
1648                 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1649                         cam_periph_error(done_ccb, 0,
1650                             SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1651                 }
1652                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1653                         /* Don't wedge the queue */
1654                         xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1655                                          /*run_queue*/TRUE);
1656                 }
1657                 /*
1658                  * Do Domain Validation for lun 0 on devices that claim
1659                  * to support Synchronous Transfer modes.
1660                  */
1661                 if (softc->action == PROBE_TUR_FOR_NEGOTIATION
1662                  && done_ccb->ccb_h.target_lun == 0
1663                  && (path->device->inq_data.flags & SID_Sync) != 0
1664                  && (path->device->flags & CAM_DEV_IN_DV) == 0) {
1665                         CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1666                             ("Begin Domain Validation\n"));
1667                         path->device->flags |= CAM_DEV_IN_DV;
1668                         xpt_release_ccb(done_ccb);
1669                         PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
1670                         xpt_schedule(periph, priority);
1671                         goto out;
1672                 }
1673                 if (softc->action == PROBE_DV_EXIT) {
1674                         CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1675                             ("Leave Domain Validation\n"));
1676                 }
1677                 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1678                         path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1679                         xpt_acquire_device(path->device);
1680                 }
1681                 path->device->flags &=
1682                     ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1683                 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1684                         /* Inform the XPT that a new device has been found */
1685                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1686                         xpt_action(done_ccb);
1687                         xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1688                                   done_ccb);
1689                 }
1690                 PROBE_SET_ACTION(softc, PROBE_DONE);
1691                 xpt_release_ccb(done_ccb);
1692                 break;
1693         case PROBE_INQUIRY_BASIC_DV1:
1694         case PROBE_INQUIRY_BASIC_DV2:
1695         {
1696                 struct scsi_inquiry_data *nbuf;
1697                 struct ccb_scsiio *csio;
1698
1699                 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1700                         cam_periph_error(done_ccb, 0,
1701                             SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY, NULL);
1702                 }
1703                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1704                         /* Don't wedge the queue */
1705                         xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1706                                          /*run_queue*/TRUE);
1707                 }
1708                 csio = &done_ccb->csio;
1709                 nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
1710                 if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
1711                         xpt_print(path,
1712                             "inquiry data fails comparison at DV%d step\n",
1713                             softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
1714                         if (proberequestbackoff(periph, path->device)) {
1715                                 path->device->flags &= ~CAM_DEV_IN_DV;
1716                                 PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1717                         } else {
1718                                 /* give up */
1719                                 PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1720                         }
1721                         free(nbuf, M_CAMXPT);
1722                         xpt_release_ccb(done_ccb);
1723                         xpt_schedule(periph, priority);
1724                         goto out;
1725                 }
1726                 free(nbuf, M_CAMXPT);
1727                 if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
1728                         PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
1729                         xpt_release_ccb(done_ccb);
1730                         xpt_schedule(periph, priority);
1731                         goto out;
1732                 }
1733                 if (softc->action == PROBE_INQUIRY_BASIC_DV2) {
1734                         CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1735                             ("Leave Domain Validation Successfully\n"));
1736                 }
1737                 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1738                         path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1739                         xpt_acquire_device(path->device);
1740                 }
1741                 path->device->flags &=
1742                     ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1743                 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1744                         /* Inform the XPT that a new device has been found */
1745                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1746                         xpt_action(done_ccb);
1747                         xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1748                                   done_ccb);
1749                 }
1750                 PROBE_SET_ACTION(softc, PROBE_DONE);
1751                 xpt_release_ccb(done_ccb);
1752                 break;
1753         }
1754         default:
1755                 panic("probedone: invalid action state 0x%x\n", softc->action);
1756         }
1757         done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
1758         TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
1759         done_ccb->ccb_h.status = CAM_REQ_CMP;
1760         xpt_done(done_ccb);
1761         if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
1762                 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1763                 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1764                 cam_release_devq(path, 0, 0, 0, FALSE);
1765                 cam_periph_release_locked(periph);
1766                 cam_periph_invalidate(periph);
1767                 cam_periph_release_locked(periph);
1768         } else {
1769                 probeschedule(periph);
1770                 goto out;
1771         }
1772 }
1773
1774 static void
1775 probe_purge_old(struct cam_path *path, struct scsi_report_luns_data *new,
1776     probe_flags flags)
1777 {
1778         struct cam_path *tp;
1779         struct scsi_report_luns_data *old;
1780         u_int idx1, idx2, nlun_old, nlun_new;
1781         lun_id_t this_lun;
1782         u_int8_t *ol, *nl;
1783
1784         if (path->target == NULL) {
1785                 return;
1786         }
1787         mtx_lock(&path->target->luns_mtx);
1788         old = path->target->luns;
1789         path->target->luns = new;
1790         mtx_unlock(&path->target->luns_mtx);
1791         if (old == NULL)
1792                 return;
1793         nlun_old = scsi_4btoul(old->length) / 8;
1794         nlun_new = scsi_4btoul(new->length) / 8;
1795
1796         /*
1797          * We are not going to assume sorted lists. Deal.
1798          */
1799         for (idx1 = 0; idx1 < nlun_old; idx1++) {
1800                 ol = old->luns[idx1].lundata;
1801                 for (idx2 = 0; idx2 < nlun_new; idx2++) {
1802                         nl = new->luns[idx2].lundata;
1803                         if (memcmp(nl, ol, 8) == 0) {
1804                                 break;
1805                         }
1806                 }
1807                 if (idx2 < nlun_new) {
1808                         continue;
1809                 }
1810                 /*
1811                  * An 'old' item not in the 'new' list.
1812                  * Nuke it. Except that if it is lun 0,
1813                  * that would be what the probe state
1814                  * machine is currently working on,
1815                  * so we won't do that.
1816                  */
1817                 CAM_GET_LUN(old, idx1, this_lun);
1818                 if (this_lun == 0) {
1819                         continue;
1820                 }
1821
1822                 /*
1823                  * We also cannot nuke it if it is
1824                  * not in a lun format we understand
1825                  * and replace the LUN with a "simple" LUN
1826                  * if that is all the HBA supports.
1827                  */
1828                 if (!(flags & PROBE_EXTLUN)) {
1829                         if (!CAM_CAN_GET_SIMPLE_LUN(old, idx1))
1830                                 continue;
1831                         CAM_GET_SIMPLE_LUN(old, idx1, this_lun);
1832                 }
1833                 if (!CAM_LUN_ONLY_32BITS(old, idx1))
1834                         continue;
1835
1836                 if (xpt_create_path(&tp, NULL, xpt_path_path_id(path),
1837                     xpt_path_target_id(path), this_lun) == CAM_REQ_CMP) {
1838                         xpt_async(AC_LOST_DEVICE, tp, NULL);
1839                         xpt_free_path(tp);
1840                 }
1841         }
1842         free(old, M_CAMXPT);
1843 }
1844
1845 static void
1846 probecleanup(struct cam_periph *periph)
1847 {
1848         free(periph->softc, M_CAMXPT);
1849 }
1850
1851 static void
1852 scsi_find_quirk(struct cam_ed *device)
1853 {
1854         struct scsi_quirk_entry *quirk;
1855         caddr_t match;
1856
1857         match = cam_quirkmatch((caddr_t)&device->inq_data,
1858                                (caddr_t)scsi_quirk_table,
1859                                sizeof(scsi_quirk_table) /
1860                                sizeof(*scsi_quirk_table),
1861                                sizeof(*scsi_quirk_table), scsi_inquiry_match);
1862
1863         if (match == NULL)
1864                 panic("xpt_find_quirk: device didn't match wildcard entry!!");
1865
1866         quirk = (struct scsi_quirk_entry *)match;
1867         device->quirk = quirk;
1868         device->mintags = quirk->mintags;
1869         device->maxtags = quirk->maxtags;
1870 }
1871
1872 static int
1873 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
1874 {
1875         int error, val;
1876
1877         val = cam_srch_hi;
1878         error = sysctl_handle_int(oidp, &val, 0, req);
1879         if (error != 0 || req->newptr == NULL)
1880                 return (error);
1881         if (val == 0 || val == 1) {
1882                 cam_srch_hi = val;
1883                 return (0);
1884         } else {
1885                 return (EINVAL);
1886         }
1887 }
1888
1889 typedef struct {
1890         union   ccb *request_ccb;
1891         struct  ccb_pathinq *cpi;
1892         int     counter;
1893         int     lunindex[0];
1894 } scsi_scan_bus_info;
1895
1896 /*
1897  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1898  * As the scan progresses, scsi_scan_bus is used as the
1899  * callback on completion function.
1900  */
1901 static void
1902 scsi_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1903 {
1904         struct mtx *mtx;
1905
1906         CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1907                   ("scsi_scan_bus\n"));
1908         switch (request_ccb->ccb_h.func_code) {
1909         case XPT_SCAN_BUS:
1910         case XPT_SCAN_TGT:
1911         {
1912                 scsi_scan_bus_info *scan_info;
1913                 union   ccb *work_ccb, *reset_ccb;
1914                 struct  cam_path *path;
1915                 u_int   i;
1916                 u_int   low_target, max_target;
1917                 u_int   initiator_id;
1918
1919                 /* Find out the characteristics of the bus */
1920                 work_ccb = xpt_alloc_ccb_nowait();
1921                 if (work_ccb == NULL) {
1922                         request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1923                         xpt_done(request_ccb);
1924                         return;
1925                 }
1926                 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1927                               request_ccb->ccb_h.pinfo.priority);
1928                 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1929                 xpt_action(work_ccb);
1930                 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1931                         request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1932                         xpt_free_ccb(work_ccb);
1933                         xpt_done(request_ccb);
1934                         return;
1935                 }
1936
1937                 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1938                         /*
1939                          * Can't scan the bus on an adapter that
1940                          * cannot perform the initiator role.
1941                          */
1942                         request_ccb->ccb_h.status = CAM_REQ_CMP;
1943                         xpt_free_ccb(work_ccb);
1944                         xpt_done(request_ccb);
1945                         return;
1946                 }
1947
1948                 /* We may need to reset bus first, if we haven't done it yet. */
1949                 if ((work_ccb->cpi.hba_inquiry &
1950                     (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1951                     !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1952                     !timevalisset(&request_ccb->ccb_h.path->bus->last_reset) &&
1953                     (reset_ccb = xpt_alloc_ccb_nowait()) != NULL) {
1954                         xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1955                               CAM_PRIORITY_NONE);
1956                         reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1957                         xpt_action(reset_ccb);
1958                         if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1959                                 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1960                                 xpt_free_ccb(reset_ccb);
1961                                 xpt_free_ccb(work_ccb);
1962                                 xpt_done(request_ccb);
1963                                 return;
1964                         }
1965                         xpt_free_ccb(reset_ccb);
1966                 }
1967
1968                 /* Save some state for use while we probe for devices */
1969                 scan_info = (scsi_scan_bus_info *) malloc(sizeof(scsi_scan_bus_info) +
1970                     (work_ccb->cpi.max_target * sizeof (u_int)), M_CAMXPT, M_ZERO|M_NOWAIT);
1971                 if (scan_info == NULL) {
1972                         request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1973                         xpt_free_ccb(work_ccb);
1974                         xpt_done(request_ccb);
1975                         return;
1976                 }
1977                 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1978                    ("SCAN start for %p\n", scan_info));
1979                 scan_info->request_ccb = request_ccb;
1980                 scan_info->cpi = &work_ccb->cpi;
1981
1982                 /* Cache on our stack so we can work asynchronously */
1983                 max_target = scan_info->cpi->max_target;
1984                 low_target = 0;
1985                 initiator_id = scan_info->cpi->initiator_id;
1986
1987
1988                 /*
1989                  * We can scan all targets in parallel, or do it sequentially.
1990                  */
1991
1992                 if (request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
1993                         max_target = low_target = request_ccb->ccb_h.target_id;
1994                         scan_info->counter = 0;
1995                 } else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
1996                         max_target = 0;
1997                         scan_info->counter = 0;
1998                 } else {
1999                         scan_info->counter = scan_info->cpi->max_target + 1;
2000                         if (scan_info->cpi->initiator_id < scan_info->counter) {
2001                                 scan_info->counter--;
2002                         }
2003                 }
2004                 mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2005                 mtx_unlock(mtx);
2006
2007                 for (i = low_target; i <= max_target; i++) {
2008                         cam_status status;
2009                         if (i == initiator_id)
2010                                 continue;
2011
2012                         status = xpt_create_path(&path, NULL,
2013                                                  request_ccb->ccb_h.path_id,
2014                                                  i, 0);
2015                         if (status != CAM_REQ_CMP) {
2016                                 printf("scsi_scan_bus: xpt_create_path failed"
2017                                        " with status %#x, bus scan halted\n",
2018                                        status);
2019                                 free(scan_info, M_CAMXPT);
2020                                 request_ccb->ccb_h.status = status;
2021                                 xpt_free_ccb(work_ccb);
2022                                 xpt_done(request_ccb);
2023                                 break;
2024                         }
2025                         work_ccb = xpt_alloc_ccb_nowait();
2026                         if (work_ccb == NULL) {
2027                                 xpt_free_ccb((union ccb *)scan_info->cpi);
2028                                 free(scan_info, M_CAMXPT);
2029                                 xpt_free_path(path);
2030                                 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2031                                 xpt_done(request_ccb);
2032                                 break;
2033                         }
2034                         xpt_setup_ccb(&work_ccb->ccb_h, path,
2035                                       request_ccb->ccb_h.pinfo.priority);
2036                         work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2037                         work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2038                         work_ccb->ccb_h.flags |= CAM_UNLOCKED;
2039                         work_ccb->ccb_h.ppriv_ptr0 = scan_info;
2040                         work_ccb->crcn.flags = request_ccb->crcn.flags;
2041                         xpt_action(work_ccb);
2042                 }
2043
2044                 mtx_lock(mtx);
2045                 break;
2046         }
2047         case XPT_SCAN_LUN:
2048         {
2049                 cam_status status;
2050                 struct cam_path *path, *oldpath;
2051                 scsi_scan_bus_info *scan_info;
2052                 struct cam_et *target;
2053                 struct cam_ed *device, *nextdev;
2054                 int next_target;
2055                 path_id_t path_id;
2056                 target_id_t target_id;
2057                 lun_id_t lun_id;
2058
2059                 oldpath = request_ccb->ccb_h.path;
2060
2061                 status = cam_ccb_status(request_ccb);
2062                 scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
2063                 path_id = request_ccb->ccb_h.path_id;
2064                 target_id = request_ccb->ccb_h.target_id;
2065                 lun_id = request_ccb->ccb_h.target_lun;
2066                 target = request_ccb->ccb_h.path->target;
2067                 next_target = 1;
2068
2069                 mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2070                 mtx_lock(mtx);
2071                 mtx_lock(&target->luns_mtx);
2072                 if (target->luns) {
2073                         lun_id_t first;
2074                         u_int nluns = scsi_4btoul(target->luns->length) / 8;
2075
2076                         /*
2077                          * Make sure we skip over lun 0 if it's the first member
2078                          * of the list as we've actually just finished probing
2079                          * it.
2080                          */
2081                         CAM_GET_LUN(target->luns, 0, first);
2082                         if (first == 0 && scan_info->lunindex[target_id] == 0) {
2083                                 scan_info->lunindex[target_id]++;
2084                         } 
2085
2086                         /*
2087                          * Skip any LUNs that the HBA can't deal with.
2088                          */
2089                         while (scan_info->lunindex[target_id] < nluns) {
2090                                 if (scan_info->cpi->hba_misc & PIM_EXTLUNS) {
2091                                         CAM_GET_LUN(target->luns,
2092                                             scan_info->lunindex[target_id],
2093                                             lun_id);
2094                                         break;
2095                                 }
2096
2097                                 /* XXX print warning? */
2098                                 if (!CAM_LUN_ONLY_32BITS(target->luns,
2099                                     scan_info->lunindex[target_id]))
2100                                         continue;
2101                                 if (CAM_CAN_GET_SIMPLE_LUN(target->luns,
2102                                     scan_info->lunindex[target_id])) {
2103                                         CAM_GET_SIMPLE_LUN(target->luns,
2104                                             scan_info->lunindex[target_id],
2105                                             lun_id);
2106                                         break;
2107                                 }
2108                                         
2109                                 scan_info->lunindex[target_id]++;
2110                         }
2111
2112                         if (scan_info->lunindex[target_id] < nluns) {
2113                                 mtx_unlock(&target->luns_mtx);
2114                                 next_target = 0;
2115                                 CAM_DEBUG(request_ccb->ccb_h.path,
2116                                     CAM_DEBUG_PROBE,
2117                                    ("next lun to try at index %u is %jx\n",
2118                                    scan_info->lunindex[target_id],
2119                                    (uintmax_t)lun_id));
2120                                 scan_info->lunindex[target_id]++;
2121                         } else {
2122                                 mtx_unlock(&target->luns_mtx);
2123                                 /* We're done with scanning all luns. */
2124                         }
2125                 } else {
2126                         mtx_unlock(&target->luns_mtx);
2127                         device = request_ccb->ccb_h.path->device;
2128                         /* Continue sequential LUN scan if: */
2129                         /*  -- we have more LUNs that need recheck */
2130                         mtx_lock(&target->bus->eb_mtx);
2131                         nextdev = device;
2132                         while ((nextdev = TAILQ_NEXT(nextdev, links)) != NULL)
2133                                 if ((nextdev->flags & CAM_DEV_UNCONFIGURED) == 0)
2134                                         break;
2135                         mtx_unlock(&target->bus->eb_mtx);
2136                         if (nextdev != NULL) {
2137                                 next_target = 0;
2138                         /*  -- stop if CAM_QUIRK_NOLUNS is set. */
2139                         } else if (SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOLUNS) {
2140                                 next_target = 1;
2141                         /*  -- this LUN is connected and its SCSI version
2142                          *     allows more LUNs. */
2143                         } else if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2144                                 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2145                                     CAN_SRCH_HI_DENSE(device))
2146                                         next_target = 0;
2147                         /*  -- this LUN is disconnected, its SCSI version
2148                          *     allows more LUNs and we guess they may be. */
2149                         } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) {
2150                                 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2151                                     CAN_SRCH_HI_SPARSE(device))
2152                                         next_target = 0;
2153                         }
2154                         if (next_target == 0) {
2155                                 lun_id++;
2156                                 if (lun_id > scan_info->cpi->max_lun)
2157                                         next_target = 1;
2158                         }
2159                 }
2160
2161                 /*
2162                  * Check to see if we scan any further luns.
2163                  */
2164                 if (next_target) {
2165                         int done;
2166
2167                         /*
2168                          * Free the current request path- we're done with it.
2169                          */
2170                         xpt_free_path(oldpath);
2171  hop_again:
2172                         done = 0;
2173                         if (scan_info->request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2174                                 done = 1;
2175                         } else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2176                                 scan_info->counter++;
2177                                 if (scan_info->counter ==
2178                                     scan_info->cpi->initiator_id) {
2179                                         scan_info->counter++;
2180                                 }
2181                                 if (scan_info->counter >=
2182                                     scan_info->cpi->max_target+1) {
2183                                         done = 1;
2184                                 }
2185                         } else {
2186                                 scan_info->counter--;
2187                                 if (scan_info->counter == 0) {
2188                                         done = 1;
2189                                 }
2190                         }
2191                         if (done) {
2192                                 mtx_unlock(mtx);
2193                                 xpt_free_ccb(request_ccb);
2194                                 xpt_free_ccb((union ccb *)scan_info->cpi);
2195                                 request_ccb = scan_info->request_ccb;
2196                                 CAM_DEBUG(request_ccb->ccb_h.path,
2197                                     CAM_DEBUG_TRACE,
2198                                    ("SCAN done for %p\n", scan_info));
2199                                 free(scan_info, M_CAMXPT);
2200                                 request_ccb->ccb_h.status = CAM_REQ_CMP;
2201                                 xpt_done(request_ccb);
2202                                 break;
2203                         }
2204
2205                         if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
2206                                 mtx_unlock(mtx);
2207                                 xpt_free_ccb(request_ccb);
2208                                 break;
2209                         }
2210                         status = xpt_create_path(&path, NULL,
2211                             scan_info->request_ccb->ccb_h.path_id,
2212                             scan_info->counter, 0);
2213                         if (status != CAM_REQ_CMP) {
2214                                 mtx_unlock(mtx);
2215                                 printf("scsi_scan_bus: xpt_create_path failed"
2216                                     " with status %#x, bus scan halted\n",
2217                                     status);
2218                                 xpt_free_ccb(request_ccb);
2219                                 xpt_free_ccb((union ccb *)scan_info->cpi);
2220                                 request_ccb = scan_info->request_ccb;
2221                                 free(scan_info, M_CAMXPT);
2222                                 request_ccb->ccb_h.status = status;
2223                                 xpt_done(request_ccb);
2224                                 break;
2225                         }
2226                         xpt_setup_ccb(&request_ccb->ccb_h, path,
2227                             request_ccb->ccb_h.pinfo.priority);
2228                         request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2229                         request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2230                         request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2231                         request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2232                         request_ccb->crcn.flags =
2233                             scan_info->request_ccb->crcn.flags;
2234                 } else {
2235                         status = xpt_create_path(&path, NULL,
2236                                                  path_id, target_id, lun_id);
2237                         /*
2238                          * Free the old request path- we're done with it. We
2239                          * do this *after* creating the new path so that
2240                          * we don't remove a target that has our lun list
2241                          * in the case that lun 0 is not present.
2242                          */
2243                         xpt_free_path(oldpath);
2244                         if (status != CAM_REQ_CMP) {
2245                                 printf("scsi_scan_bus: xpt_create_path failed "
2246                                        "with status %#x, halting LUN scan\n",
2247                                        status);
2248                                 goto hop_again;
2249                         }
2250                         xpt_setup_ccb(&request_ccb->ccb_h, path,
2251                                       request_ccb->ccb_h.pinfo.priority);
2252                         request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2253                         request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2254                         request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2255                         request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2256                         request_ccb->crcn.flags =
2257                                 scan_info->request_ccb->crcn.flags;
2258                 }
2259                 mtx_unlock(mtx);
2260                 xpt_action(request_ccb);
2261                 break;
2262         }
2263         default:
2264                 break;
2265         }
2266 }
2267
2268 static void
2269 scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
2270              cam_flags flags, union ccb *request_ccb)
2271 {
2272         struct ccb_pathinq cpi;
2273         cam_status status;
2274         struct cam_path *new_path;
2275         struct cam_periph *old_periph;
2276         int lock;
2277
2278         CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
2279
2280         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2281         cpi.ccb_h.func_code = XPT_PATH_INQ;
2282         xpt_action((union ccb *)&cpi);
2283
2284         if (cpi.ccb_h.status != CAM_REQ_CMP) {
2285                 if (request_ccb != NULL) {
2286                         request_ccb->ccb_h.status = cpi.ccb_h.status;
2287                         xpt_done(request_ccb);
2288                 }
2289                 return;
2290         }
2291
2292         if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
2293                 /*
2294                  * Can't scan the bus on an adapter that
2295                  * cannot perform the initiator role.
2296                  */
2297                 if (request_ccb != NULL) {
2298                         request_ccb->ccb_h.status = CAM_REQ_CMP;
2299                         xpt_done(request_ccb);
2300                 }
2301                 return;
2302         }
2303
2304         if (request_ccb == NULL) {
2305                 request_ccb = xpt_alloc_ccb_nowait();
2306                 if (request_ccb == NULL) {
2307                         xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
2308                             "can't continue\n");
2309                         return;
2310                 }
2311                 status = xpt_create_path(&new_path, NULL,
2312                                           path->bus->path_id,
2313                                           path->target->target_id,
2314                                           path->device->lun_id);
2315                 if (status != CAM_REQ_CMP) {
2316                         xpt_print(path, "scsi_scan_lun: can't create path, "
2317                             "can't continue\n");
2318                         xpt_free_ccb(request_ccb);
2319                         return;
2320                 }
2321                 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
2322                 request_ccb->ccb_h.cbfcnp = xptscandone;
2323                 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2324                 request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2325                 request_ccb->crcn.flags = flags;
2326         }
2327
2328         lock = (xpt_path_owned(path) == 0);
2329         if (lock)
2330                 xpt_path_lock(path);
2331         if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
2332                 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
2333                         probe_softc *softc;
2334
2335                         softc = (probe_softc *)old_periph->softc;
2336                         TAILQ_INSERT_TAIL(&softc->request_ccbs,
2337                             &request_ccb->ccb_h, periph_links.tqe);
2338                 } else {
2339                         request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2340                         xpt_done(request_ccb);
2341                 }
2342         } else {
2343                 status = cam_periph_alloc(proberegister, NULL, probecleanup,
2344                                           probestart, "probe",
2345                                           CAM_PERIPH_BIO,
2346                                           request_ccb->ccb_h.path, NULL, 0,
2347                                           request_ccb);
2348
2349                 if (status != CAM_REQ_CMP) {
2350                         xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
2351                             "returned an error, can't continue probe\n");
2352                         request_ccb->ccb_h.status = status;
2353                         xpt_done(request_ccb);
2354                 }
2355         }
2356         if (lock)
2357                 xpt_path_unlock(path);
2358 }
2359
2360 static void
2361 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
2362 {
2363
2364         xpt_free_path(done_ccb->ccb_h.path);
2365         xpt_free_ccb(done_ccb);
2366 }
2367
2368 static struct cam_ed *
2369 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
2370 {
2371         struct scsi_quirk_entry *quirk;
2372         struct cam_ed *device;
2373
2374         device = xpt_alloc_device(bus, target, lun_id);
2375         if (device == NULL)
2376                 return (NULL);
2377
2378         /*
2379          * Take the default quirk entry until we have inquiry
2380          * data and can determine a better quirk to use.
2381          */
2382         quirk = &scsi_quirk_table[scsi_quirk_table_size - 1];
2383         device->quirk = (void *)quirk;
2384         device->mintags = quirk->mintags;
2385         device->maxtags = quirk->maxtags;
2386         bzero(&device->inq_data, sizeof(device->inq_data));
2387         device->inq_flags = 0;
2388         device->queue_flags = 0;
2389         device->serial_num = NULL;
2390         device->serial_num_len = 0;
2391         device->device_id = NULL;
2392         device->device_id_len = 0;
2393         device->supported_vpds = NULL;
2394         device->supported_vpds_len = 0;
2395         return (device);
2396 }
2397
2398 static void
2399 scsi_devise_transport(struct cam_path *path)
2400 {
2401         struct ccb_pathinq cpi;
2402         struct ccb_trans_settings cts;
2403         struct scsi_inquiry_data *inq_buf;
2404
2405         /* Get transport information from the SIM */
2406         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2407         cpi.ccb_h.func_code = XPT_PATH_INQ;
2408         xpt_action((union ccb *)&cpi);
2409
2410         inq_buf = NULL;
2411         if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
2412                 inq_buf = &path->device->inq_data;
2413         path->device->protocol = PROTO_SCSI;
2414         path->device->protocol_version =
2415             inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
2416         path->device->transport = cpi.transport;
2417         path->device->transport_version = cpi.transport_version;
2418
2419         /*
2420          * Any device not using SPI3 features should
2421          * be considered SPI2 or lower.
2422          */
2423         if (inq_buf != NULL) {
2424                 if (path->device->transport == XPORT_SPI
2425                  && (inq_buf->spi3data & SID_SPI_MASK) == 0
2426                  && path->device->transport_version > 2)
2427                         path->device->transport_version = 2;
2428         } else {
2429                 struct cam_ed* otherdev;
2430
2431                 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
2432                      otherdev != NULL;
2433                      otherdev = TAILQ_NEXT(otherdev, links)) {
2434                         if (otherdev != path->device)
2435                                 break;
2436                 }
2437
2438                 if (otherdev != NULL) {
2439                         /*
2440                          * Initially assume the same versioning as
2441                          * prior luns for this target.
2442                          */
2443                         path->device->protocol_version =
2444                             otherdev->protocol_version;
2445                         path->device->transport_version =
2446                             otherdev->transport_version;
2447                 } else {
2448                         /* Until we know better, opt for safety */
2449                         path->device->protocol_version = 2;
2450                         if (path->device->transport == XPORT_SPI)
2451                                 path->device->transport_version = 2;
2452                         else
2453                                 path->device->transport_version = 0;
2454                 }
2455         }
2456
2457         /*
2458          * XXX
2459          * For a device compliant with SPC-2 we should be able
2460          * to determine the transport version supported by
2461          * scrutinizing the version descriptors in the
2462          * inquiry buffer.
2463          */
2464
2465         /* Tell the controller what we think */
2466         xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2467         cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2468         cts.type = CTS_TYPE_CURRENT_SETTINGS;
2469         cts.transport = path->device->transport;
2470         cts.transport_version = path->device->transport_version;
2471         cts.protocol = path->device->protocol;
2472         cts.protocol_version = path->device->protocol_version;
2473         cts.proto_specific.valid = 0;
2474         cts.xport_specific.valid = 0;
2475         xpt_action((union ccb *)&cts);
2476 }
2477
2478 static void
2479 scsi_dev_advinfo(union ccb *start_ccb)
2480 {
2481         struct cam_ed *device;
2482         struct ccb_dev_advinfo *cdai;
2483         off_t amt;
2484
2485         start_ccb->ccb_h.status = CAM_REQ_INVALID;
2486         device = start_ccb->ccb_h.path->device;
2487         cdai = &start_ccb->cdai;
2488         switch(cdai->buftype) {
2489         case CDAI_TYPE_SCSI_DEVID:
2490                 if (cdai->flags & CDAI_FLAG_STORE)
2491                         return;
2492                 cdai->provsiz = device->device_id_len;
2493                 if (device->device_id_len == 0)
2494                         break;
2495                 amt = device->device_id_len;
2496                 if (cdai->provsiz > cdai->bufsiz)
2497                         amt = cdai->bufsiz;
2498                 memcpy(cdai->buf, device->device_id, amt);
2499                 break;
2500         case CDAI_TYPE_SERIAL_NUM:
2501                 if (cdai->flags & CDAI_FLAG_STORE)
2502                         return;
2503                 cdai->provsiz = device->serial_num_len;
2504                 if (device->serial_num_len == 0)
2505                         break;
2506                 amt = device->serial_num_len;
2507                 if (cdai->provsiz > cdai->bufsiz)
2508                         amt = cdai->bufsiz;
2509                 memcpy(cdai->buf, device->serial_num, amt);
2510                 break;
2511         case CDAI_TYPE_PHYS_PATH:
2512                 if (cdai->flags & CDAI_FLAG_STORE) {
2513                         if (device->physpath != NULL) {
2514                                 free(device->physpath, M_CAMXPT);
2515                                 device->physpath = NULL;
2516                         }
2517                         device->physpath_len = cdai->bufsiz;
2518                         /* Clear existing buffer if zero length */
2519                         if (cdai->bufsiz == 0)
2520                                 break;
2521                         device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
2522                         if (device->physpath == NULL) {
2523                                 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2524                                 return;
2525                         }
2526                         memcpy(device->physpath, cdai->buf, cdai->bufsiz);
2527                 } else {
2528                         cdai->provsiz = device->physpath_len;
2529                         if (device->physpath_len == 0)
2530                                 break;
2531                         amt = device->physpath_len;
2532                         if (cdai->provsiz > cdai->bufsiz)
2533                                 amt = cdai->bufsiz;
2534                         memcpy(cdai->buf, device->physpath, amt);
2535                 }
2536                 break;
2537         case CDAI_TYPE_RCAPLONG:
2538                 if (cdai->flags & CDAI_FLAG_STORE) {
2539                         if (device->rcap_buf != NULL) {
2540                                 free(device->rcap_buf, M_CAMXPT);
2541                                 device->rcap_buf = NULL;
2542                         }
2543
2544                         device->rcap_len = cdai->bufsiz;
2545                         /* Clear existing buffer if zero length */
2546                         if (cdai->bufsiz == 0)
2547                                 break;
2548
2549                         device->rcap_buf = malloc(cdai->bufsiz, M_CAMXPT,
2550                                                   M_NOWAIT);
2551                         if (device->rcap_buf == NULL) {
2552                                 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2553                                 return;
2554                         }
2555
2556                         memcpy(device->rcap_buf, cdai->buf, cdai->bufsiz);
2557                 } else {
2558                         cdai->provsiz = device->rcap_len;
2559                         if (device->rcap_len == 0)
2560                                 break;
2561                         amt = device->rcap_len;
2562                         if (cdai->provsiz > cdai->bufsiz)
2563                                 amt = cdai->bufsiz;
2564                         memcpy(cdai->buf, device->rcap_buf, amt);
2565                 }
2566                 break;
2567         case CDAI_TYPE_EXT_INQ:
2568                 /*
2569                  * We fetch extended inquiry data during probe, if
2570                  * available.  We don't allow changing it.
2571                  */
2572                 if (cdai->flags & CDAI_FLAG_STORE) 
2573                         return;
2574                 cdai->provsiz = device->ext_inq_len;
2575                 if (device->ext_inq_len == 0)
2576                         break;
2577                 amt = device->ext_inq_len;
2578                 if (cdai->provsiz > cdai->bufsiz)
2579                         amt = cdai->bufsiz;
2580                 memcpy(cdai->buf, device->ext_inq, amt);
2581                 break;
2582         default:
2583                 return;
2584         }
2585         start_ccb->ccb_h.status = CAM_REQ_CMP;
2586
2587         if (cdai->flags & CDAI_FLAG_STORE) {
2588                 xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
2589                           (void *)(uintptr_t)cdai->buftype);
2590         }
2591 }
2592
2593 static void
2594 scsi_action(union ccb *start_ccb)
2595 {
2596
2597         switch (start_ccb->ccb_h.func_code) {
2598         case XPT_SET_TRAN_SETTINGS:
2599         {
2600                 scsi_set_transfer_settings(&start_ccb->cts,
2601                                            start_ccb->ccb_h.path,
2602                                            /*async_update*/FALSE);
2603                 break;
2604         }
2605         case XPT_SCAN_BUS:
2606         case XPT_SCAN_TGT:
2607                 scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2608                 break;
2609         case XPT_SCAN_LUN:
2610                 scsi_scan_lun(start_ccb->ccb_h.path->periph,
2611                               start_ccb->ccb_h.path, start_ccb->crcn.flags,
2612                               start_ccb);
2613                 break;
2614         case XPT_DEV_ADVINFO:
2615         {
2616                 scsi_dev_advinfo(start_ccb);
2617                 break;
2618         }
2619         default:
2620                 xpt_action_default(start_ccb);
2621                 break;
2622         }
2623 }
2624
2625 static void
2626 scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
2627                            int async_update)
2628 {
2629         struct  ccb_pathinq cpi;
2630         struct  ccb_trans_settings cur_cts;
2631         struct  ccb_trans_settings_scsi *scsi;
2632         struct  ccb_trans_settings_scsi *cur_scsi;
2633         struct  scsi_inquiry_data *inq_data;
2634         struct  cam_ed *device;
2635
2636         if (path == NULL || (device = path->device) == NULL) {
2637                 cts->ccb_h.status = CAM_PATH_INVALID;
2638                 xpt_done((union ccb *)cts);
2639                 return;
2640         }
2641
2642         if (cts->protocol == PROTO_UNKNOWN
2643          || cts->protocol == PROTO_UNSPECIFIED) {
2644                 cts->protocol = device->protocol;
2645                 cts->protocol_version = device->protocol_version;
2646         }
2647
2648         if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2649          || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2650                 cts->protocol_version = device->protocol_version;
2651
2652         if (cts->protocol != device->protocol) {
2653                 xpt_print(path, "Uninitialized Protocol %x:%x?\n",
2654                        cts->protocol, device->protocol);
2655                 cts->protocol = device->protocol;
2656         }
2657
2658         if (cts->protocol_version > device->protocol_version) {
2659                 if (bootverbose) {
2660                         xpt_print(path, "Down reving Protocol "
2661                             "Version from %d to %d?\n", cts->protocol_version,
2662                             device->protocol_version);
2663                 }
2664                 cts->protocol_version = device->protocol_version;
2665         }
2666
2667         if (cts->transport == XPORT_UNKNOWN
2668          || cts->transport == XPORT_UNSPECIFIED) {
2669                 cts->transport = device->transport;
2670                 cts->transport_version = device->transport_version;
2671         }
2672
2673         if (cts->transport_version == XPORT_VERSION_UNKNOWN
2674          || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2675                 cts->transport_version = device->transport_version;
2676
2677         if (cts->transport != device->transport) {
2678                 xpt_print(path, "Uninitialized Transport %x:%x?\n",
2679                     cts->transport, device->transport);
2680                 cts->transport = device->transport;
2681         }
2682
2683         if (cts->transport_version > device->transport_version) {
2684                 if (bootverbose) {
2685                         xpt_print(path, "Down reving Transport "
2686                             "Version from %d to %d?\n", cts->transport_version,
2687                             device->transport_version);
2688                 }
2689                 cts->transport_version = device->transport_version;
2690         }
2691
2692         /*
2693          * Nothing more of interest to do unless
2694          * this is a device connected via the
2695          * SCSI protocol.
2696          */
2697         if (cts->protocol != PROTO_SCSI) {
2698                 if (async_update == FALSE)
2699                         xpt_action_default((union ccb *)cts);
2700                 return;
2701         }
2702
2703         inq_data = &device->inq_data;
2704         scsi = &cts->proto_specific.scsi;
2705         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2706         cpi.ccb_h.func_code = XPT_PATH_INQ;
2707         xpt_action((union ccb *)&cpi);
2708
2709         /* SCSI specific sanity checking */
2710         if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2711          || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2712          || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2713          || (device->mintags == 0)) {
2714                 /*
2715                  * Can't tag on hardware that doesn't support tags,
2716                  * doesn't have it enabled, or has broken tag support.
2717                  */
2718                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2719         }
2720
2721         if (async_update == FALSE) {
2722                 /*
2723                  * Perform sanity checking against what the
2724                  * controller and device can do.
2725                  */
2726                 xpt_setup_ccb(&cur_cts.ccb_h, path, CAM_PRIORITY_NONE);
2727                 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2728                 cur_cts.type = cts->type;
2729                 xpt_action((union ccb *)&cur_cts);
2730                 if (cam_ccb_status((union ccb *)&cur_cts) != CAM_REQ_CMP) {
2731                         return;
2732                 }
2733                 cur_scsi = &cur_cts.proto_specific.scsi;
2734                 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2735                         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2736                         scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2737                 }
2738                 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2739                         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2740         }
2741
2742         /* SPI specific sanity checking */
2743         if (cts->transport == XPORT_SPI && async_update == FALSE) {
2744                 u_int spi3caps;
2745                 struct ccb_trans_settings_spi *spi;
2746                 struct ccb_trans_settings_spi *cur_spi;
2747
2748                 spi = &cts->xport_specific.spi;
2749
2750                 cur_spi = &cur_cts.xport_specific.spi;
2751
2752                 /* Fill in any gaps in what the user gave us */
2753                 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2754                         spi->sync_period = cur_spi->sync_period;
2755                 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2756                         spi->sync_period = 0;
2757                 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2758                         spi->sync_offset = cur_spi->sync_offset;
2759                 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2760                         spi->sync_offset = 0;
2761                 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2762                         spi->ppr_options = cur_spi->ppr_options;
2763                 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2764                         spi->ppr_options = 0;
2765                 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2766                         spi->bus_width = cur_spi->bus_width;
2767                 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2768                         spi->bus_width = 0;
2769                 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2770                         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2771                         spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2772                 }
2773                 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2774                         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2775                 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2776                   && (inq_data->flags & SID_Sync) == 0
2777                   && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2778                  || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2779                         /* Force async */
2780                         spi->sync_period = 0;
2781                         spi->sync_offset = 0;
2782                 }
2783
2784                 switch (spi->bus_width) {
2785                 case MSG_EXT_WDTR_BUS_32_BIT:
2786                         if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2787                           || (inq_data->flags & SID_WBus32) != 0
2788                           || cts->type == CTS_TYPE_USER_SETTINGS)
2789                          && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2790                                 break;
2791                         /* Fall Through to 16-bit */
2792                 case MSG_EXT_WDTR_BUS_16_BIT:
2793                         if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2794                           || (inq_data->flags & SID_WBus16) != 0
2795                           || cts->type == CTS_TYPE_USER_SETTINGS)
2796                          && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2797                                 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2798                                 break;
2799                         }
2800                         /* Fall Through to 8-bit */
2801                 default: /* New bus width?? */
2802                 case MSG_EXT_WDTR_BUS_8_BIT:
2803                         /* All targets can do this */
2804                         spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2805                         break;
2806                 }
2807
2808                 spi3caps = cpi.xport_specific.spi.ppr_options;
2809                 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2810                  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2811                         spi3caps &= inq_data->spi3data;
2812
2813                 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2814                         spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2815
2816                 if ((spi3caps & SID_SPI_IUS) == 0)
2817                         spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2818
2819                 if ((spi3caps & SID_SPI_QAS) == 0)
2820                         spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2821
2822                 /* No SPI Transfer settings are allowed unless we are wide */
2823                 if (spi->bus_width == 0)
2824                         spi->ppr_options = 0;
2825
2826                 if ((spi->valid & CTS_SPI_VALID_DISC)
2827                  && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2828                         /*
2829                          * Can't tag queue without disconnection.
2830                          */
2831                         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2832                         scsi->valid |= CTS_SCSI_VALID_TQ;
2833                 }
2834
2835                 /*
2836                  * If we are currently performing tagged transactions to
2837                  * this device and want to change its negotiation parameters,
2838                  * go non-tagged for a bit to give the controller a chance to
2839                  * negotiate unhampered by tag messages.
2840                  */
2841                 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2842                  && (device->inq_flags & SID_CmdQue) != 0
2843                  && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2844                  && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2845                                    CTS_SPI_VALID_SYNC_OFFSET|
2846                                    CTS_SPI_VALID_BUS_WIDTH)) != 0)
2847                         scsi_toggle_tags(path);
2848         }
2849
2850         if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2851          && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2852                 int device_tagenb;
2853
2854                 /*
2855                  * If we are transitioning from tags to no-tags or
2856                  * vice-versa, we need to carefully freeze and restart
2857                  * the queue so that we don't overlap tagged and non-tagged
2858                  * commands.  We also temporarily stop tags if there is
2859                  * a change in transfer negotiation settings to allow
2860                  * "tag-less" negotiation.
2861                  */
2862                 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2863                  || (device->inq_flags & SID_CmdQue) != 0)
2864                         device_tagenb = TRUE;
2865                 else
2866                         device_tagenb = FALSE;
2867
2868                 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2869                   && device_tagenb == FALSE)
2870                  || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2871                   && device_tagenb == TRUE)) {
2872
2873                         if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2874                                 /*
2875                                  * Delay change to use tags until after a
2876                                  * few commands have gone to this device so
2877                                  * the controller has time to perform transfer
2878                                  * negotiations without tagged messages getting
2879                                  * in the way.
2880                                  */
2881                                 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2882                                 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2883                         } else {
2884                                 xpt_stop_tags(path);
2885                         }
2886                 }
2887         }
2888         if (async_update == FALSE)
2889                 xpt_action_default((union ccb *)cts);
2890 }
2891
2892 static void
2893 scsi_toggle_tags(struct cam_path *path)
2894 {
2895         struct cam_ed *dev;
2896
2897         /*
2898          * Give controllers a chance to renegotiate
2899          * before starting tag operations.  We
2900          * "toggle" tagged queuing off then on
2901          * which causes the tag enable command delay
2902          * counter to come into effect.
2903          */
2904         dev = path->device;
2905         if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2906          || ((dev->inq_flags & SID_CmdQue) != 0
2907           && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2908                 struct ccb_trans_settings cts;
2909
2910                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2911                 cts.protocol = PROTO_SCSI;
2912                 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2913                 cts.transport = XPORT_UNSPECIFIED;
2914                 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2915                 cts.proto_specific.scsi.flags = 0;
2916                 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2917                 scsi_set_transfer_settings(&cts, path,
2918                                           /*async_update*/TRUE);
2919                 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2920                 scsi_set_transfer_settings(&cts, path,
2921                                           /*async_update*/TRUE);
2922         }
2923 }
2924
2925 /*
2926  * Handle any per-device event notifications that require action by the XPT.
2927  */
2928 static void
2929 scsi_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2930               struct cam_ed *device, void *async_arg)
2931 {
2932         cam_status status;
2933         struct cam_path newpath;
2934
2935         /*
2936          * We only need to handle events for real devices.
2937          */
2938         if (target->target_id == CAM_TARGET_WILDCARD
2939          || device->lun_id == CAM_LUN_WILDCARD)
2940                 return;
2941
2942         /*
2943          * We need our own path with wildcards expanded to
2944          * handle certain types of events.
2945          */
2946         if ((async_code == AC_SENT_BDR)
2947          || (async_code == AC_BUS_RESET)
2948          || (async_code == AC_INQ_CHANGED))
2949                 status = xpt_compile_path(&newpath, NULL,
2950                                           bus->path_id,
2951                                           target->target_id,
2952                                           device->lun_id);
2953         else
2954                 status = CAM_REQ_CMP_ERR;
2955
2956         if (status == CAM_REQ_CMP) {
2957
2958                 /*
2959                  * Allow transfer negotiation to occur in a
2960                  * tag free environment and after settle delay.
2961                  */
2962                 if (async_code == AC_SENT_BDR
2963                  || async_code == AC_BUS_RESET) {
2964                         cam_freeze_devq(&newpath); 
2965                         cam_release_devq(&newpath,
2966                                 RELSIM_RELEASE_AFTER_TIMEOUT,
2967                                 /*reduction*/0,
2968                                 /*timeout*/scsi_delay,
2969                                 /*getcount_only*/0);
2970                         scsi_toggle_tags(&newpath);
2971                 }
2972
2973                 if (async_code == AC_INQ_CHANGED) {
2974                         /*
2975                          * We've sent a start unit command, or
2976                          * something similar to a device that
2977                          * may have caused its inquiry data to
2978                          * change. So we re-scan the device to
2979                          * refresh the inquiry data for it.
2980                          */
2981                         scsi_scan_lun(newpath.periph, &newpath,
2982                                      CAM_EXPECT_INQ_CHANGE, NULL);
2983                 }
2984                 xpt_release_path(&newpath);
2985         } else if (async_code == AC_LOST_DEVICE &&
2986             (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2987                 device->flags |= CAM_DEV_UNCONFIGURED;
2988                 xpt_release_device(device);
2989         } else if (async_code == AC_TRANSFER_NEG) {
2990                 struct ccb_trans_settings *settings;
2991                 struct cam_path path;
2992
2993                 settings = (struct ccb_trans_settings *)async_arg;
2994                 xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
2995                                  device->lun_id);
2996                 scsi_set_transfer_settings(settings, &path,
2997                                           /*async_update*/TRUE);
2998                 xpt_release_path(&path);
2999         }
3000 }
3001
3002 static void
3003 scsi_announce_periph(struct cam_periph *periph)
3004 {
3005         struct  ccb_pathinq cpi;
3006         struct  ccb_trans_settings cts;
3007         struct  cam_path *path = periph->path;
3008         u_int   speed;
3009         u_int   freq;
3010         u_int   mb;
3011
3012         cam_periph_assert(periph, MA_OWNED);
3013
3014         xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
3015         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
3016         cts.type = CTS_TYPE_CURRENT_SETTINGS;
3017         xpt_action((union ccb*)&cts);
3018         if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP)
3019                 return;
3020         /* Ask the SIM for its base transfer speed */
3021         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3022         cpi.ccb_h.func_code = XPT_PATH_INQ;
3023         xpt_action((union ccb *)&cpi);
3024         /* Report connection speed */ 
3025         speed = cpi.base_transfer_speed;
3026         freq = 0;
3027         if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3028                 struct  ccb_trans_settings_spi *spi =
3029                     &cts.xport_specific.spi;
3030
3031                 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
3032                   && spi->sync_offset != 0) {
3033                         freq = scsi_calc_syncsrate(spi->sync_period);
3034                         speed = freq;
3035                 }
3036                 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
3037                         speed *= (0x01 << spi->bus_width);
3038         }
3039         if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3040                 struct  ccb_trans_settings_fc *fc =
3041                     &cts.xport_specific.fc;
3042
3043                 if (fc->valid & CTS_FC_VALID_SPEED)
3044                         speed = fc->bitrate;
3045         }
3046         if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
3047                 struct  ccb_trans_settings_sas *sas =
3048                     &cts.xport_specific.sas;
3049
3050                 if (sas->valid & CTS_SAS_VALID_SPEED)
3051                         speed = sas->bitrate;
3052         }
3053         mb = speed / 1000;
3054         if (mb > 0)
3055                 printf("%s%d: %d.%03dMB/s transfers",
3056                        periph->periph_name, periph->unit_number,
3057                        mb, speed % 1000);
3058         else
3059                 printf("%s%d: %dKB/s transfers", periph->periph_name,
3060                        periph->unit_number, speed);
3061         /* Report additional information about SPI connections */
3062         if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3063                 struct  ccb_trans_settings_spi *spi;
3064
3065                 spi = &cts.xport_specific.spi;
3066                 if (freq != 0) {
3067                         printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
3068                                freq % 1000,
3069                                (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
3070                              ? " DT" : "",
3071                                spi->sync_offset);
3072                 }
3073                 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
3074                  && spi->bus_width > 0) {
3075                         if (freq != 0) {
3076                                 printf(", ");
3077                         } else {
3078                                 printf(" (");
3079                         }
3080                         printf("%dbit)", 8 * (0x01 << spi->bus_width));
3081                 } else if (freq != 0) {
3082                         printf(")");
3083                 }
3084         }
3085         if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3086                 struct  ccb_trans_settings_fc *fc;
3087
3088                 fc = &cts.xport_specific.fc;
3089                 if (fc->valid & CTS_FC_VALID_WWNN)
3090                         printf(" WWNN 0x%llx", (long long) fc->wwnn);
3091                 if (fc->valid & CTS_FC_VALID_WWPN)
3092                         printf(" WWPN 0x%llx", (long long) fc->wwpn);
3093                 if (fc->valid & CTS_FC_VALID_PORT)
3094                         printf(" PortID 0x%x", fc->port);
3095         }
3096         printf("\n");
3097 }
3098