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