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