The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/cam/scsi/scsi_xpt.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: cd7ab4892abbf427d938db7637dde07b


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.