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