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