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