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

Cache object: f69211d11ae13482a693535a6f08ea37


[ 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.