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