]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cam/scsi/scsi_xpt.c
MFC r228442:
[FreeBSD/stable/8.git] / sys / cam / scsi / scsi_xpt.c
1 /*-
2  * Implementation of the SCSI Transport
3  *
4  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/time.h>
40 #include <sys/conf.h>
41 #include <sys/fcntl.h>
42 #include <sys/md5.h>
43 #include <sys/interrupt.h>
44 #include <sys/sbuf.h>
45
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49
50 #include <cam/cam.h>
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_xpt_internal.h>
59 #include <cam/cam_debug.h>
60
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/scsi/scsi_pass.h>
64 #include <machine/stdarg.h>     /* for xpt_print below */
65 #include "opt_cam.h"
66
67 struct scsi_quirk_entry {
68         struct scsi_inquiry_pattern inq_pat;
69         u_int8_t quirks;
70 #define CAM_QUIRK_NOLUNS        0x01
71 #define CAM_QUIRK_NOSERIAL      0x02
72 #define CAM_QUIRK_HILUNS        0x04
73 #define CAM_QUIRK_NOHILUNS      0x08
74         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, val;
1466
1467         val = cam_srch_hi;
1468         error = sysctl_handle_int(oidp, &val, 0, req);
1469         if (error != 0 || req->newptr == NULL)
1470                 return (error);
1471         if (val == 0 || val == 1) {
1472                 cam_srch_hi = val;
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         case XPT_SCAN_TGT:
1498         {
1499                 scsi_scan_bus_info *scan_info;
1500                 union   ccb *work_ccb, *reset_ccb;
1501                 struct  cam_path *path;
1502                 u_int   i;
1503                 u_int   low_target, max_target;
1504                 u_int   initiator_id;
1505
1506                 /* Find out the characteristics of the bus */
1507                 work_ccb = xpt_alloc_ccb_nowait();
1508                 if (work_ccb == NULL) {
1509                         request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1510                         xpt_done(request_ccb);
1511                         return;
1512                 }
1513                 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1514                               request_ccb->ccb_h.pinfo.priority);
1515                 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1516                 xpt_action(work_ccb);
1517                 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1518                         request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1519                         xpt_free_ccb(work_ccb);
1520                         xpt_done(request_ccb);
1521                         return;
1522                 }
1523
1524                 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1525                         /*
1526                          * Can't scan the bus on an adapter that
1527                          * cannot perform the initiator role.
1528                          */
1529                         request_ccb->ccb_h.status = CAM_REQ_CMP;
1530                         xpt_free_ccb(work_ccb);
1531                         xpt_done(request_ccb);
1532                         return;
1533                 }
1534
1535                 /* We may need to reset bus first, if we haven't done it yet. */
1536                 if ((work_ccb->cpi.hba_inquiry &
1537                     (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1538                     !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1539                     !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1540                         reset_ccb = xpt_alloc_ccb_nowait();
1541                         xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1542                               CAM_PRIORITY_NONE);
1543                         reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1544                         xpt_action(reset_ccb);
1545                         if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1546                                 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1547                                 xpt_free_ccb(reset_ccb);
1548                                 xpt_free_ccb(work_ccb);
1549                                 xpt_done(request_ccb);
1550                                 return;
1551                         }
1552                         xpt_free_ccb(reset_ccb);
1553                 }
1554
1555                 /* Save some state for use while we probe for devices */
1556                 scan_info = (scsi_scan_bus_info *)
1557                     malloc(sizeof(scsi_scan_bus_info), M_CAMXPT, M_NOWAIT);
1558                 if (scan_info == NULL) {
1559                         request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1560                         xpt_done(request_ccb);
1561                         return;
1562                 }
1563                 scan_info->request_ccb = request_ccb;
1564                 scan_info->cpi = &work_ccb->cpi;
1565
1566                 /* Cache on our stack so we can work asynchronously */
1567                 max_target = scan_info->cpi->max_target;
1568                 low_target = 0;
1569                 initiator_id = scan_info->cpi->initiator_id;
1570
1571
1572                 /*
1573                  * We can scan all targets in parallel, or do it sequentially.
1574                  */
1575
1576                 if (request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
1577                         max_target = low_target = request_ccb->ccb_h.target_id;
1578                         scan_info->counter = 0;
1579                 } else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
1580                         max_target = 0;
1581                         scan_info->counter = 0;
1582                 } else {
1583                         scan_info->counter = scan_info->cpi->max_target + 1;
1584                         if (scan_info->cpi->initiator_id < scan_info->counter) {
1585                                 scan_info->counter--;
1586                         }
1587                 }
1588
1589                 for (i = low_target; i <= max_target; i++) {
1590                         cam_status status;
1591                         if (i == initiator_id)
1592                                 continue;
1593
1594                         status = xpt_create_path(&path, xpt_periph,
1595                                                  request_ccb->ccb_h.path_id,
1596                                                  i, 0);
1597                         if (status != CAM_REQ_CMP) {
1598                                 printf("scsi_scan_bus: xpt_create_path failed"
1599                                        " with status %#x, bus scan halted\n",
1600                                        status);
1601                                 free(scan_info, M_CAMXPT);
1602                                 request_ccb->ccb_h.status = status;
1603                                 xpt_free_ccb(work_ccb);
1604                                 xpt_done(request_ccb);
1605                                 break;
1606                         }
1607                         work_ccb = xpt_alloc_ccb_nowait();
1608                         if (work_ccb == NULL) {
1609                                 xpt_free_ccb((union ccb *)scan_info->cpi);
1610                                 free(scan_info, M_CAMXPT);
1611                                 xpt_free_path(path);
1612                                 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1613                                 xpt_done(request_ccb);
1614                                 break;
1615                         }
1616                         xpt_setup_ccb(&work_ccb->ccb_h, path,
1617                                       request_ccb->ccb_h.pinfo.priority);
1618                         work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1619                         work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1620                         work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1621                         work_ccb->crcn.flags = request_ccb->crcn.flags;
1622                         xpt_action(work_ccb);
1623                 }
1624                 break;
1625         }
1626         case XPT_SCAN_LUN:
1627         {
1628                 cam_status status;
1629                 struct cam_path *path;
1630                 scsi_scan_bus_info *scan_info;
1631                 path_id_t path_id;
1632                 target_id_t target_id;
1633                 lun_id_t lun_id;
1634
1635                 /* Reuse the same CCB to query if a device was really found */
1636                 scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
1637                 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
1638                               request_ccb->ccb_h.pinfo.priority);
1639                 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1640
1641                 path_id = request_ccb->ccb_h.path_id;
1642                 target_id = request_ccb->ccb_h.target_id;
1643                 lun_id = request_ccb->ccb_h.target_lun;
1644                 xpt_action(request_ccb);
1645
1646                 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
1647                         struct cam_ed *device;
1648                         struct cam_et *target;
1649                         int phl;
1650
1651                         /*
1652                          * If we already probed lun 0 successfully, or
1653                          * we have additional configured luns on this
1654                          * target that might have "gone away", go onto
1655                          * the next lun.
1656                          */
1657                         target = request_ccb->ccb_h.path->target;
1658                         /*
1659                          * We may touch devices that we don't
1660                          * hold references too, so ensure they
1661                          * don't disappear out from under us.
1662                          * The target above is referenced by the
1663                          * path in the request ccb.
1664                          */
1665                         phl = 0;
1666                         device = TAILQ_FIRST(&target->ed_entries);
1667                         if (device != NULL) {
1668                                 phl = CAN_SRCH_HI_SPARSE(device);
1669                                 if (device->lun_id == 0)
1670                                         device = TAILQ_NEXT(device, links);
1671                         }
1672                         if ((lun_id != 0) || (device != NULL)) {
1673                                 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
1674                                         lun_id++;
1675                         }
1676                 } else {
1677                         struct cam_ed *device;
1678
1679                         device = request_ccb->ccb_h.path->device;
1680
1681                         if ((SCSI_QUIRK(device)->quirks &
1682                             CAM_QUIRK_NOLUNS) == 0) {
1683                                 /* Try the next lun */
1684                                 if (lun_id < (CAM_SCSI2_MAXLUN-1)
1685                                   || CAN_SRCH_HI_DENSE(device))
1686                                         lun_id++;
1687                         }
1688                 }
1689
1690                 /*
1691                  * Free the current request path- we're done with it.
1692                  */
1693                 xpt_free_path(request_ccb->ccb_h.path);
1694
1695                 /*
1696                  * Check to see if we scan any further luns.
1697                  */
1698                 if (lun_id == request_ccb->ccb_h.target_lun
1699                  || lun_id > scan_info->cpi->max_lun) {
1700                         int done;
1701
1702  hop_again:
1703                         done = 0;
1704                         if (scan_info->request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
1705                                 done = 1;
1706                         } else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
1707                                 scan_info->counter++;
1708                                 if (scan_info->counter ==
1709                                     scan_info->cpi->initiator_id) {
1710                                         scan_info->counter++;
1711                                 }
1712                                 if (scan_info->counter >=
1713                                     scan_info->cpi->max_target+1) {
1714                                         done = 1;
1715                                 }
1716                         } else {
1717                                 scan_info->counter--;
1718                                 if (scan_info->counter == 0) {
1719                                         done = 1;
1720                                 }
1721                         }
1722                         if (done) {
1723                                 xpt_free_ccb(request_ccb);
1724                                 xpt_free_ccb((union ccb *)scan_info->cpi);
1725                                 request_ccb = scan_info->request_ccb;
1726                                 free(scan_info, M_CAMXPT);
1727                                 request_ccb->ccb_h.status = CAM_REQ_CMP;
1728                                 xpt_done(request_ccb);
1729                                 break;
1730                         }
1731
1732                         if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
1733                                 xpt_free_ccb(request_ccb);
1734                                 break;
1735                         }
1736                         status = xpt_create_path(&path, xpt_periph,
1737                             scan_info->request_ccb->ccb_h.path_id,
1738                             scan_info->counter, 0);
1739                         if (status != CAM_REQ_CMP) {
1740                                 printf("scsi_scan_bus: xpt_create_path failed"
1741                                     " with status %#x, bus scan halted\n",
1742                                     status);
1743                                 xpt_free_ccb(request_ccb);
1744                                 xpt_free_ccb((union ccb *)scan_info->cpi);
1745                                 request_ccb = scan_info->request_ccb;
1746                                 free(scan_info, M_CAMXPT);
1747                                 request_ccb->ccb_h.status = status;
1748                                 xpt_done(request_ccb);
1749                                 break;
1750                         }
1751                         xpt_setup_ccb(&request_ccb->ccb_h, path,
1752                             request_ccb->ccb_h.pinfo.priority);
1753                         request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1754                         request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1755                         request_ccb->ccb_h.ppriv_ptr0 = scan_info;
1756                         request_ccb->crcn.flags =
1757                             scan_info->request_ccb->crcn.flags;
1758                 } else {
1759                         status = xpt_create_path(&path, xpt_periph,
1760                                                  path_id, target_id, lun_id);
1761                         if (status != CAM_REQ_CMP) {
1762                                 printf("scsi_scan_bus: xpt_create_path failed "
1763                                        "with status %#x, halting LUN scan\n",
1764                                        status);
1765                                 goto hop_again;
1766                         }
1767                         xpt_setup_ccb(&request_ccb->ccb_h, path,
1768                                       request_ccb->ccb_h.pinfo.priority);
1769                         request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1770                         request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
1771                         request_ccb->ccb_h.ppriv_ptr0 = scan_info;
1772                         request_ccb->crcn.flags =
1773                                 scan_info->request_ccb->crcn.flags;
1774                 }
1775                 xpt_action(request_ccb);
1776                 break;
1777         }
1778         default:
1779                 break;
1780         }
1781 }
1782
1783 static void
1784 scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
1785              cam_flags flags, union ccb *request_ccb)
1786 {
1787         struct ccb_pathinq cpi;
1788         cam_status status;
1789         struct cam_path *new_path;
1790         struct cam_periph *old_periph;
1791
1792         CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
1793
1794         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1795         cpi.ccb_h.func_code = XPT_PATH_INQ;
1796         xpt_action((union ccb *)&cpi);
1797
1798         if (cpi.ccb_h.status != CAM_REQ_CMP) {
1799                 if (request_ccb != NULL) {
1800                         request_ccb->ccb_h.status = cpi.ccb_h.status;
1801                         xpt_done(request_ccb);
1802                 }
1803                 return;
1804         }
1805
1806         if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1807                 /*
1808                  * Can't scan the bus on an adapter that
1809                  * cannot perform the initiator role.
1810                  */
1811                 if (request_ccb != NULL) {
1812                         request_ccb->ccb_h.status = CAM_REQ_CMP;
1813                         xpt_done(request_ccb);
1814                 }
1815                 return;
1816         }
1817
1818         if (request_ccb == NULL) {
1819                 request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1820                 if (request_ccb == NULL) {
1821                         xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
1822                             "can't continue\n");
1823                         return;
1824                 }
1825                 new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1826                 if (new_path == NULL) {
1827                         xpt_print(path, "scsi_scan_lun: can't allocate path, "
1828                             "can't continue\n");
1829                         free(request_ccb, M_CAMXPT);
1830                         return;
1831                 }
1832                 status = xpt_compile_path(new_path, xpt_periph,
1833                                           path->bus->path_id,
1834                                           path->target->target_id,
1835                                           path->device->lun_id);
1836
1837                 if (status != CAM_REQ_CMP) {
1838                         xpt_print(path, "scsi_scan_lun: can't compile path, "
1839                             "can't continue\n");
1840                         free(request_ccb, M_CAMXPT);
1841                         free(new_path, M_CAMXPT);
1842                         return;
1843                 }
1844                 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1845                 request_ccb->ccb_h.cbfcnp = xptscandone;
1846                 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1847                 request_ccb->crcn.flags = flags;
1848         }
1849
1850         if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
1851                 probe_softc *softc;
1852
1853                 softc = (probe_softc *)old_periph->softc;
1854                 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1855                                   periph_links.tqe);
1856         } else {
1857                 status = cam_periph_alloc(proberegister, NULL, probecleanup,
1858                                           probestart, "probe",
1859                                           CAM_PERIPH_BIO,
1860                                           request_ccb->ccb_h.path, NULL, 0,
1861                                           request_ccb);
1862
1863                 if (status != CAM_REQ_CMP) {
1864                         xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
1865                             "returned an error, can't continue probe\n");
1866                         request_ccb->ccb_h.status = status;
1867                         xpt_done(request_ccb);
1868                 }
1869         }
1870 }
1871
1872 static void
1873 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1874 {
1875         xpt_release_path(done_ccb->ccb_h.path);
1876         free(done_ccb->ccb_h.path, M_CAMXPT);
1877         free(done_ccb, M_CAMXPT);
1878 }
1879
1880 static struct cam_ed *
1881 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1882 {
1883         struct cam_path path;
1884         struct scsi_quirk_entry *quirk;
1885         struct cam_ed *device;
1886         struct cam_ed *cur_device;
1887
1888         device = xpt_alloc_device(bus, target, lun_id);
1889         if (device == NULL)
1890                 return (NULL);
1891
1892         /*
1893          * Take the default quirk entry until we have inquiry
1894          * data and can determine a better quirk to use.
1895          */
1896         quirk = &scsi_quirk_table[scsi_quirk_table_size - 1];
1897         device->quirk = (void *)quirk;
1898         device->mintags = quirk->mintags;
1899         device->maxtags = quirk->maxtags;
1900         bzero(&device->inq_data, sizeof(device->inq_data));
1901         device->inq_flags = 0;
1902         device->queue_flags = 0;
1903         device->serial_num = NULL;
1904         device->serial_num_len = 0;
1905
1906         /*
1907          * XXX should be limited by number of CCBs this bus can
1908          * do.
1909          */
1910         bus->sim->max_ccbs += device->ccbq.devq_openings;
1911         /* Insertion sort into our target's device list */
1912         cur_device = TAILQ_FIRST(&target->ed_entries);
1913         while (cur_device != NULL && cur_device->lun_id < lun_id)
1914                 cur_device = TAILQ_NEXT(cur_device, links);
1915         if (cur_device != NULL) {
1916                 TAILQ_INSERT_BEFORE(cur_device, device, links);
1917         } else {
1918                 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1919         }
1920         target->generation++;
1921         if (lun_id != CAM_LUN_WILDCARD) {
1922                 xpt_compile_path(&path,
1923                                  NULL,
1924                                  bus->path_id,
1925                                  target->target_id,
1926                                  lun_id);
1927                 scsi_devise_transport(&path);
1928                 xpt_release_path(&path);
1929         }
1930
1931         return (device);
1932 }
1933
1934 static void
1935 scsi_devise_transport(struct cam_path *path)
1936 {
1937         struct ccb_pathinq cpi;
1938         struct ccb_trans_settings cts;
1939         struct scsi_inquiry_data *inq_buf;
1940
1941         /* Get transport information from the SIM */
1942         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1943         cpi.ccb_h.func_code = XPT_PATH_INQ;
1944         xpt_action((union ccb *)&cpi);
1945
1946         inq_buf = NULL;
1947         if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1948                 inq_buf = &path->device->inq_data;
1949         path->device->protocol = PROTO_SCSI;
1950         path->device->protocol_version =
1951             inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1952         path->device->transport = cpi.transport;
1953         path->device->transport_version = cpi.transport_version;
1954
1955         /*
1956          * Any device not using SPI3 features should
1957          * be considered SPI2 or lower.
1958          */
1959         if (inq_buf != NULL) {
1960                 if (path->device->transport == XPORT_SPI
1961                  && (inq_buf->spi3data & SID_SPI_MASK) == 0
1962                  && path->device->transport_version > 2)
1963                         path->device->transport_version = 2;
1964         } else {
1965                 struct cam_ed* otherdev;
1966
1967                 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
1968                      otherdev != NULL;
1969                      otherdev = TAILQ_NEXT(otherdev, links)) {
1970                         if (otherdev != path->device)
1971                                 break;
1972                 }
1973
1974                 if (otherdev != NULL) {
1975                         /*
1976                          * Initially assume the same versioning as
1977                          * prior luns for this target.
1978                          */
1979                         path->device->protocol_version =
1980                             otherdev->protocol_version;
1981                         path->device->transport_version =
1982                             otherdev->transport_version;
1983                 } else {
1984                         /* Until we know better, opt for safty */
1985                         path->device->protocol_version = 2;
1986                         if (path->device->transport == XPORT_SPI)
1987                                 path->device->transport_version = 2;
1988                         else
1989                                 path->device->transport_version = 0;
1990                 }
1991         }
1992
1993         /*
1994          * XXX
1995          * For a device compliant with SPC-2 we should be able
1996          * to determine the transport version supported by
1997          * scrutinizing the version descriptors in the
1998          * inquiry buffer.
1999          */
2000
2001         /* Tell the controller what we think */
2002         xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2003         cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2004         cts.type = CTS_TYPE_CURRENT_SETTINGS;
2005         cts.transport = path->device->transport;
2006         cts.transport_version = path->device->transport_version;
2007         cts.protocol = path->device->protocol;
2008         cts.protocol_version = path->device->protocol_version;
2009         cts.proto_specific.valid = 0;
2010         cts.xport_specific.valid = 0;
2011         xpt_action((union ccb *)&cts);
2012 }
2013
2014 static void
2015 scsi_action(union ccb *start_ccb)
2016 {
2017
2018         switch (start_ccb->ccb_h.func_code) {
2019         case XPT_SET_TRAN_SETTINGS:
2020         {
2021                 scsi_set_transfer_settings(&start_ccb->cts,
2022                                            start_ccb->ccb_h.path->device,
2023                                            /*async_update*/FALSE);
2024                 break;
2025         }
2026         case XPT_SCAN_BUS:
2027         case XPT_SCAN_TGT:
2028                 scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2029                 break;
2030         case XPT_SCAN_LUN:
2031                 scsi_scan_lun(start_ccb->ccb_h.path->periph,
2032                               start_ccb->ccb_h.path, start_ccb->crcn.flags,
2033                               start_ccb);
2034                 break;
2035         case XPT_GET_TRAN_SETTINGS:
2036         {
2037                 struct cam_sim *sim;
2038
2039                 sim = start_ccb->ccb_h.path->bus->sim;
2040                 (*(sim->sim_action))(sim, start_ccb);
2041                 break;
2042         }
2043         default:
2044                 xpt_action_default(start_ccb);
2045                 break;
2046         }
2047 }
2048
2049 static void
2050 scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
2051                            int async_update)
2052 {
2053         struct  ccb_pathinq cpi;
2054         struct  ccb_trans_settings cur_cts;
2055         struct  ccb_trans_settings_scsi *scsi;
2056         struct  ccb_trans_settings_scsi *cur_scsi;
2057         struct  cam_sim *sim;
2058         struct  scsi_inquiry_data *inq_data;
2059
2060         if (device == NULL) {
2061                 cts->ccb_h.status = CAM_PATH_INVALID;
2062                 xpt_done((union ccb *)cts);
2063                 return;
2064         }
2065
2066         if (cts->protocol == PROTO_UNKNOWN
2067          || cts->protocol == PROTO_UNSPECIFIED) {
2068                 cts->protocol = device->protocol;
2069                 cts->protocol_version = device->protocol_version;
2070         }
2071
2072         if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2073          || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2074                 cts->protocol_version = device->protocol_version;
2075
2076         if (cts->protocol != device->protocol) {
2077                 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
2078                        cts->protocol, device->protocol);
2079                 cts->protocol = device->protocol;
2080         }
2081
2082         if (cts->protocol_version > device->protocol_version) {
2083                 if (bootverbose) {
2084                         xpt_print(cts->ccb_h.path, "Down reving Protocol "
2085                             "Version from %d to %d?\n", cts->protocol_version,
2086                             device->protocol_version);
2087                 }
2088                 cts->protocol_version = device->protocol_version;
2089         }
2090
2091         if (cts->transport == XPORT_UNKNOWN
2092          || cts->transport == XPORT_UNSPECIFIED) {
2093                 cts->transport = device->transport;
2094                 cts->transport_version = device->transport_version;
2095         }
2096
2097         if (cts->transport_version == XPORT_VERSION_UNKNOWN
2098          || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2099                 cts->transport_version = device->transport_version;
2100
2101         if (cts->transport != device->transport) {
2102                 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
2103                     cts->transport, device->transport);
2104                 cts->transport = device->transport;
2105         }
2106
2107         if (cts->transport_version > device->transport_version) {
2108                 if (bootverbose) {
2109                         xpt_print(cts->ccb_h.path, "Down reving Transport "
2110                             "Version from %d to %d?\n", cts->transport_version,
2111                             device->transport_version);
2112                 }
2113                 cts->transport_version = device->transport_version;
2114         }
2115
2116         sim = cts->ccb_h.path->bus->sim;
2117
2118         /*
2119          * Nothing more of interest to do unless
2120          * this is a device connected via the
2121          * SCSI protocol.
2122          */
2123         if (cts->protocol != PROTO_SCSI) {
2124                 if (async_update == FALSE)
2125                         (*(sim->sim_action))(sim, (union ccb *)cts);
2126                 return;
2127         }
2128
2129         inq_data = &device->inq_data;
2130         scsi = &cts->proto_specific.scsi;
2131         xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
2132         cpi.ccb_h.func_code = XPT_PATH_INQ;
2133         xpt_action((union ccb *)&cpi);
2134
2135         /* SCSI specific sanity checking */
2136         if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2137          || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2138          || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2139          || (device->mintags == 0)) {
2140                 /*
2141                  * Can't tag on hardware that doesn't support tags,
2142                  * doesn't have it enabled, or has broken tag support.
2143                  */
2144                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2145         }
2146
2147         if (async_update == FALSE) {
2148                 /*
2149                  * Perform sanity checking against what the
2150                  * controller and device can do.
2151                  */
2152                 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
2153                 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2154                 cur_cts.type = cts->type;
2155                 xpt_action((union ccb *)&cur_cts);
2156                 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2157                         return;
2158                 }
2159                 cur_scsi = &cur_cts.proto_specific.scsi;
2160                 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2161                         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2162                         scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2163                 }
2164                 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2165                         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2166         }
2167
2168         /* SPI specific sanity checking */
2169         if (cts->transport == XPORT_SPI && async_update == FALSE) {
2170                 u_int spi3caps;
2171                 struct ccb_trans_settings_spi *spi;
2172                 struct ccb_trans_settings_spi *cur_spi;
2173
2174                 spi = &cts->xport_specific.spi;
2175
2176                 cur_spi = &cur_cts.xport_specific.spi;
2177
2178                 /* Fill in any gaps in what the user gave us */
2179                 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2180                         spi->sync_period = cur_spi->sync_period;
2181                 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2182                         spi->sync_period = 0;
2183                 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2184                         spi->sync_offset = cur_spi->sync_offset;
2185                 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2186                         spi->sync_offset = 0;
2187                 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2188                         spi->ppr_options = cur_spi->ppr_options;
2189                 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2190                         spi->ppr_options = 0;
2191                 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2192                         spi->bus_width = cur_spi->bus_width;
2193                 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2194                         spi->bus_width = 0;
2195                 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2196                         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2197                         spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2198                 }
2199                 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2200                         spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2201                 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2202                   && (inq_data->flags & SID_Sync) == 0
2203                   && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2204                  || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2205                         /* Force async */
2206                         spi->sync_period = 0;
2207                         spi->sync_offset = 0;
2208                 }
2209
2210                 switch (spi->bus_width) {
2211                 case MSG_EXT_WDTR_BUS_32_BIT:
2212                         if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2213                           || (inq_data->flags & SID_WBus32) != 0
2214                           || cts->type == CTS_TYPE_USER_SETTINGS)
2215                          && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2216                                 break;
2217                         /* Fall Through to 16-bit */
2218                 case MSG_EXT_WDTR_BUS_16_BIT:
2219                         if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2220                           || (inq_data->flags & SID_WBus16) != 0
2221                           || cts->type == CTS_TYPE_USER_SETTINGS)
2222                          && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2223                                 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2224                                 break;
2225                         }
2226                         /* Fall Through to 8-bit */
2227                 default: /* New bus width?? */
2228                 case MSG_EXT_WDTR_BUS_8_BIT:
2229                         /* All targets can do this */
2230                         spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2231                         break;
2232                 }
2233
2234                 spi3caps = cpi.xport_specific.spi.ppr_options;
2235                 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2236                  && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2237                         spi3caps &= inq_data->spi3data;
2238
2239                 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2240                         spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2241
2242                 if ((spi3caps & SID_SPI_IUS) == 0)
2243                         spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2244
2245                 if ((spi3caps & SID_SPI_QAS) == 0)
2246                         spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2247
2248                 /* No SPI Transfer settings are allowed unless we are wide */
2249                 if (spi->bus_width == 0)
2250                         spi->ppr_options = 0;
2251
2252                 if ((spi->valid & CTS_SPI_VALID_DISC)
2253                  && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2254                         /*
2255                          * Can't tag queue without disconnection.
2256                          */
2257                         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2258                         scsi->valid |= CTS_SCSI_VALID_TQ;
2259                 }
2260
2261                 /*
2262                  * If we are currently performing tagged transactions to
2263                  * this device and want to change its negotiation parameters,
2264                  * go non-tagged for a bit to give the controller a chance to
2265                  * negotiate unhampered by tag messages.
2266                  */
2267                 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2268                  && (device->inq_flags & SID_CmdQue) != 0
2269                  && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2270                  && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2271                                    CTS_SPI_VALID_SYNC_OFFSET|
2272                                    CTS_SPI_VALID_BUS_WIDTH)) != 0)
2273                         scsi_toggle_tags(cts->ccb_h.path);
2274         }
2275
2276         if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2277          && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2278                 int device_tagenb;
2279
2280                 /*
2281                  * If we are transitioning from tags to no-tags or
2282                  * vice-versa, we need to carefully freeze and restart
2283                  * the queue so that we don't overlap tagged and non-tagged
2284                  * commands.  We also temporarily stop tags if there is
2285                  * a change in transfer negotiation settings to allow
2286                  * "tag-less" negotiation.
2287                  */
2288                 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2289                  || (device->inq_flags & SID_CmdQue) != 0)
2290                         device_tagenb = TRUE;
2291                 else
2292                         device_tagenb = FALSE;
2293
2294                 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2295                   && device_tagenb == FALSE)
2296                  || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2297                   && device_tagenb == TRUE)) {
2298
2299                         if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2300                                 /*
2301                                  * Delay change to use tags until after a
2302                                  * few commands have gone to this device so
2303                                  * the controller has time to perform transfer
2304                                  * negotiations without tagged messages getting
2305                                  * in the way.
2306                                  */
2307                                 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2308                                 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2309                         } else {
2310                                 xpt_stop_tags(cts->ccb_h.path);
2311                         }
2312                 }
2313         }
2314         if (async_update == FALSE)
2315                 (*(sim->sim_action))(sim, (union ccb *)cts);
2316 }
2317
2318 static void
2319 scsi_toggle_tags(struct cam_path *path)
2320 {
2321         struct cam_ed *dev;
2322
2323         /*
2324          * Give controllers a chance to renegotiate
2325          * before starting tag operations.  We
2326          * "toggle" tagged queuing off then on
2327          * which causes the tag enable command delay
2328          * counter to come into effect.
2329          */
2330         dev = path->device;
2331         if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2332          || ((dev->inq_flags & SID_CmdQue) != 0
2333           && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2334                 struct ccb_trans_settings cts;
2335
2336                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2337                 cts.protocol = PROTO_SCSI;
2338                 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2339                 cts.transport = XPORT_UNSPECIFIED;
2340                 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2341                 cts.proto_specific.scsi.flags = 0;
2342                 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2343                 scsi_set_transfer_settings(&cts, path->device,
2344                                           /*async_update*/TRUE);
2345                 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2346                 scsi_set_transfer_settings(&cts, path->device,
2347                                           /*async_update*/TRUE);
2348         }
2349 }
2350
2351 /*
2352  * Handle any per-device event notifications that require action by the XPT.
2353  */
2354 static void
2355 scsi_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2356               struct cam_ed *device, void *async_arg)
2357 {
2358         cam_status status;
2359         struct cam_path newpath;
2360
2361         /*
2362          * We only need to handle events for real devices.
2363          */
2364         if (target->target_id == CAM_TARGET_WILDCARD
2365          || device->lun_id == CAM_LUN_WILDCARD)
2366                 return;
2367
2368         /*
2369          * We need our own path with wildcards expanded to
2370          * handle certain types of events.
2371          */
2372         if ((async_code == AC_SENT_BDR)
2373          || (async_code == AC_BUS_RESET)
2374          || (async_code == AC_INQ_CHANGED))
2375                 status = xpt_compile_path(&newpath, NULL,
2376                                           bus->path_id,
2377                                           target->target_id,
2378                                           device->lun_id);
2379         else
2380                 status = CAM_REQ_CMP_ERR;
2381
2382         if (status == CAM_REQ_CMP) {
2383
2384                 /*
2385                  * Allow transfer negotiation to occur in a
2386                  * tag free environment and after settle delay.
2387                  */
2388                 if (async_code == AC_SENT_BDR
2389                  || async_code == AC_BUS_RESET) {
2390                         cam_freeze_devq(&newpath); 
2391                         cam_release_devq(&newpath,
2392                                 RELSIM_RELEASE_AFTER_TIMEOUT,
2393                                 /*reduction*/0,
2394                                 /*timeout*/scsi_delay,
2395                                 /*getcount_only*/0);
2396                         scsi_toggle_tags(&newpath);
2397                 }
2398
2399                 if (async_code == AC_INQ_CHANGED) {
2400                         /*
2401                          * We've sent a start unit command, or
2402                          * something similar to a device that
2403                          * may have caused its inquiry data to
2404                          * change. So we re-scan the device to
2405                          * refresh the inquiry data for it.
2406                          */
2407                         scsi_scan_lun(newpath.periph, &newpath,
2408                                      CAM_EXPECT_INQ_CHANGE, NULL);
2409                 }
2410                 xpt_release_path(&newpath);
2411         } else if (async_code == AC_LOST_DEVICE &&
2412             (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2413                 device->flags |= CAM_DEV_UNCONFIGURED;
2414                 xpt_release_device(device);
2415         } else if (async_code == AC_TRANSFER_NEG) {
2416                 struct ccb_trans_settings *settings;
2417
2418                 settings = (struct ccb_trans_settings *)async_arg;
2419                 scsi_set_transfer_settings(settings, device,
2420                                           /*async_update*/TRUE);
2421         }
2422 }
2423
2424 static void
2425 scsi_announce_periph(struct cam_periph *periph)
2426 {
2427         struct  ccb_pathinq cpi;
2428         struct  ccb_trans_settings cts;
2429         struct  cam_path *path = periph->path;
2430         u_int   speed;
2431         u_int   freq;
2432         u_int   mb;
2433
2434         mtx_assert(periph->sim->mtx, MA_OWNED);
2435
2436         xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
2437         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2438         cts.type = CTS_TYPE_CURRENT_SETTINGS;
2439         xpt_action((union ccb*)&cts);
2440         if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2441                 return;
2442         /* Ask the SIM for its base transfer speed */
2443         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
2444         cpi.ccb_h.func_code = XPT_PATH_INQ;
2445         xpt_action((union ccb *)&cpi);
2446         /* Report connection speed */ 
2447         speed = cpi.base_transfer_speed;
2448         freq = 0;
2449         if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
2450                 struct  ccb_trans_settings_spi *spi =
2451                     &cts.xport_specific.spi;
2452
2453                 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
2454                   && spi->sync_offset != 0) {
2455                         freq = scsi_calc_syncsrate(spi->sync_period);
2456                         speed = freq;
2457                 }
2458                 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
2459                         speed *= (0x01 << spi->bus_width);
2460         }
2461         if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
2462                 struct  ccb_trans_settings_fc *fc =
2463                     &cts.xport_specific.fc;
2464
2465                 if (fc->valid & CTS_FC_VALID_SPEED)
2466                         speed = fc->bitrate;
2467         }
2468         if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
2469                 struct  ccb_trans_settings_sas *sas =
2470                     &cts.xport_specific.sas;
2471
2472                 if (sas->valid & CTS_SAS_VALID_SPEED)
2473                         speed = sas->bitrate;
2474         }
2475         mb = speed / 1000;
2476         if (mb > 0)
2477                 printf("%s%d: %d.%03dMB/s transfers",
2478                        periph->periph_name, periph->unit_number,
2479                        mb, speed % 1000);
2480         else
2481                 printf("%s%d: %dKB/s transfers", periph->periph_name,
2482                        periph->unit_number, speed);
2483         /* Report additional information about SPI connections */
2484         if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
2485                 struct  ccb_trans_settings_spi *spi;
2486
2487                 spi = &cts.xport_specific.spi;
2488                 if (freq != 0) {
2489                         printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
2490                                freq % 1000,
2491                                (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
2492                              ? " DT" : "",
2493                                spi->sync_offset);
2494                 }
2495                 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
2496                  && spi->bus_width > 0) {
2497                         if (freq != 0) {
2498                                 printf(", ");
2499                         } else {
2500                                 printf(" (");
2501                         }
2502                         printf("%dbit)", 8 * (0x01 << spi->bus_width));
2503                 } else if (freq != 0) {
2504                         printf(")");
2505                 }
2506         }
2507         if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
2508                 struct  ccb_trans_settings_fc *fc;
2509
2510                 fc = &cts.xport_specific.fc;
2511                 if (fc->valid & CTS_FC_VALID_WWNN)
2512                         printf(" WWNN 0x%llx", (long long) fc->wwnn);
2513                 if (fc->valid & CTS_FC_VALID_WWPN)
2514                         printf(" WWPN 0x%llx", (long long) fc->wwpn);
2515                 if (fc->valid & CTS_FC_VALID_PORT)
2516                         printf(" PortID 0x%x", fc->port);
2517         }
2518         printf("\n");
2519 }
2520