2 * Implementation of the SCSI Transport
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
7 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/types.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
43 #include <sys/fcntl.h>
45 #include <sys/interrupt.h>
49 #include <sys/mutex.h>
50 #include <sys/sysctl.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_xpt_internal.h>
61 #include <cam/cam_debug.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_pass.h>
66 #include <machine/stdarg.h> /* for xpt_print below */
69 struct scsi_quirk_entry {
70 struct scsi_inquiry_pattern inq_pat;
72 #define CAM_QUIRK_NOLUNS 0x01
73 #define CAM_QUIRK_NOVPDS 0x02
74 #define CAM_QUIRK_HILUNS 0x04
75 #define CAM_QUIRK_NOHILUNS 0x08
76 #define CAM_QUIRK_NORPTLUNS 0x10
80 #define SCSI_QUIRK(dev) ((struct scsi_quirk_entry *)((dev)->quirk))
82 static int cam_srch_hi = 0;
83 static int sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS);
84 SYSCTL_PROC(_kern_cam, OID_AUTO, cam_srch_hi, CTLTYPE_INT | CTLFLAG_RWTUN, 0, 0,
85 sysctl_cam_search_luns, "I",
86 "allow search above LUN 7 for SCSI3 and greater devices");
88 #define CAM_SCSI2_MAXLUN 8
89 #define CAM_CAN_GET_SIMPLE_LUN(x, i) \
90 ((((x)->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) == \
91 RPL_LUNDATA_ATYP_PERIPH) || \
92 (((x)->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) == \
93 RPL_LUNDATA_ATYP_FLAT))
94 #define CAM_GET_SIMPLE_LUN(lp, i, lval) \
95 if (((lp)->luns[(i)].lundata[0] & RPL_LUNDATA_ATYP_MASK) == \
96 RPL_LUNDATA_ATYP_PERIPH) { \
97 (lval) = (lp)->luns[(i)].lundata[1]; \
99 (lval) = (lp)->luns[(i)].lundata[0]; \
100 (lval) &= RPL_LUNDATA_FLAT_LUN_MASK; \
102 (lval) |= (lp)->luns[(i)].lundata[1]; \
104 #define CAM_GET_LUN(lp, i, lval) \
105 (lval) = scsi_8btou64((lp)->luns[(i)].lundata); \
106 (lval) = CAM_EXTLUN_BYTE_SWIZZLE(lval);
109 * If we're not quirked to search <= the first 8 luns
110 * and we are either quirked to search above lun 8,
111 * or we're > SCSI-2 and we've enabled hilun searching,
112 * or we're > SCSI-2 and the last lun was a success,
113 * we can look for luns above lun 8.
115 #define CAN_SRCH_HI_SPARSE(dv) \
116 (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0) \
117 && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS) \
118 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
120 #define CAN_SRCH_HI_DENSE(dv) \
121 (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0) \
122 && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS) \
123 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
125 static periph_init_t probe_periph_init;
127 static struct periph_driver probe_driver =
129 probe_periph_init, "probe",
130 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
134 PERIPHDRIVER_DECLARE(probe, probe_driver);
138 PROBE_INQUIRY, /* this counts as DV0 for Basic Domain Validation */
142 PROBE_SUPPORTED_VPD_LIST,
144 PROBE_EXTENDED_INQUIRY,
146 PROBE_TUR_FOR_NEGOTIATION,
147 PROBE_INQUIRY_BASIC_DV1,
148 PROBE_INQUIRY_BASIC_DV2,
154 static char *probe_action_text[] = {
157 "PROBE_FULL_INQUIRY",
160 "PROBE_SUPPORTED_VPD_LIST",
162 "PROBE_EXTENDED_INQUIRY",
164 "PROBE_TUR_FOR_NEGOTIATION",
165 "PROBE_INQUIRY_BASIC_DV1",
166 "PROBE_INQUIRY_BASIC_DV2",
172 #define PROBE_SET_ACTION(softc, newaction) \
175 text = probe_action_text; \
176 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \
177 ("Probe %s to %s\n", text[(softc)->action], \
178 text[(newaction)])); \
179 (softc)->action = (newaction); \
183 PROBE_INQUIRY_CKSUM = 0x01,
184 PROBE_SERIAL_CKSUM = 0x02,
185 PROBE_NO_ANNOUNCE = 0x04,
190 TAILQ_HEAD(, ccb_hdr) request_ccbs;
196 struct cam_periph *periph;
199 static const char quantum[] = "QUANTUM";
200 static const char sony[] = "SONY";
201 static const char west_digital[] = "WDIGTL";
202 static const char samsung[] = "SAMSUNG";
203 static const char seagate[] = "SEAGATE";
204 static const char microp[] = "MICROP";
206 static struct scsi_quirk_entry scsi_quirk_table[] =
209 /* Reports QUEUE FULL for temporary resource shortages */
210 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
211 /*quirks*/0, /*mintags*/24, /*maxtags*/32
214 /* Reports QUEUE FULL for temporary resource shortages */
215 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
216 /*quirks*/0, /*mintags*/24, /*maxtags*/32
219 /* Reports QUEUE FULL for temporary resource shortages */
220 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
221 /*quirks*/0, /*mintags*/24, /*maxtags*/32
224 /* Broken tagged queuing drive */
225 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
226 /*quirks*/0, /*mintags*/0, /*maxtags*/0
229 /* Broken tagged queuing drive */
230 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
231 /*quirks*/0, /*mintags*/0, /*maxtags*/0
234 /* Broken tagged queuing drive */
235 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
236 /*quirks*/0, /*mintags*/0, /*maxtags*/0
240 * Unfortunately, the Quantum Atlas III has the same
241 * problem as the Atlas II drives above.
242 * Reported by: "Johan Granlund" <johan@granlund.nu>
244 * For future reference, the drive with the problem was:
245 * QUANTUM QM39100TD-SW N1B0
247 * It's possible that Quantum will fix the problem in later
248 * firmware revisions. If that happens, the quirk entry
249 * will need to be made specific to the firmware revisions
253 /* Reports QUEUE FULL for temporary resource shortages */
254 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
255 /*quirks*/0, /*mintags*/24, /*maxtags*/32
259 * 18 Gig Atlas III, same problem as the 9G version.
260 * Reported by: Andre Albsmeier
261 * <andre.albsmeier@mchp.siemens.de>
263 * For future reference, the drive with the problem was:
264 * QUANTUM QM318000TD-S N491
266 /* Reports QUEUE FULL for temporary resource shortages */
267 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
268 /*quirks*/0, /*mintags*/24, /*maxtags*/32
272 * Broken tagged queuing drive
273 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
274 * and: Martin Renters <martin@tdc.on.ca>
276 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
277 /*quirks*/0, /*mintags*/0, /*maxtags*/0
280 * The Seagate Medalist Pro drives have very poor write
281 * performance with anything more than 2 tags.
283 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
284 * Drive: <SEAGATE ST36530N 1444>
286 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
287 * Drive: <SEAGATE ST34520W 1281>
289 * No one has actually reported that the 9G version
290 * (ST39140*) of the Medalist Pro has the same problem, but
291 * we're assuming that it does because the 4G and 6.5G
292 * versions of the drive are broken.
295 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
296 /*quirks*/0, /*mintags*/2, /*maxtags*/2
299 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
300 /*quirks*/0, /*mintags*/2, /*maxtags*/2
303 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
304 /*quirks*/0, /*mintags*/2, /*maxtags*/2
308 * Experiences command timeouts under load with a
309 * tag count higher than 55.
311 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST3146855LW", "*"},
312 /*quirks*/0, /*mintags*/2, /*maxtags*/55
316 * Slow when tagged queueing is enabled. Write performance
317 * steadily drops off with more and more concurrent
318 * transactions. Best sequential write performance with
319 * tagged queueing turned off and write caching turned on.
322 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
323 * Drive: DCAS-34330 w/ "S65A" firmware.
325 * The drive with the problem had the "S65A" firmware
326 * revision, and has also been reported (by Stephen J.
327 * Roznowski <sjr@home.net>) for a drive with the "S61A"
330 * Although no one has reported problems with the 2 gig
331 * version of the DCAS drive, the assumption is that it
332 * has the same problems as the 4 gig version. Therefore
333 * this quirk entries disables tagged queueing for all
336 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
337 /*quirks*/0, /*mintags*/0, /*maxtags*/0
340 /* Broken tagged queuing drive */
341 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
342 /*quirks*/0, /*mintags*/0, /*maxtags*/0
345 /* Broken tagged queuing drive */
346 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
347 /*quirks*/0, /*mintags*/0, /*maxtags*/0
350 /* This does not support other than LUN 0 */
351 { T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
352 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
356 * Broken tagged queuing drive.
358 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
361 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
362 /*quirks*/0, /*mintags*/0, /*maxtags*/0
366 * Slow when tagged queueing is enabled. (1.5MB/sec versus
368 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
369 * Best performance with these drives is achieved with
370 * tagged queueing turned off, and write caching turned on.
372 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
373 /*quirks*/0, /*mintags*/0, /*maxtags*/0
377 * Slow when tagged queueing is enabled. (1.5MB/sec versus
379 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
380 * Best performance with these drives is achieved with
381 * tagged queueing turned off, and write caching turned on.
383 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
384 /*quirks*/0, /*mintags*/0, /*maxtags*/0
388 * Doesn't handle queue full condition correctly,
389 * so we need to limit maxtags to what the device
390 * can handle instead of determining this automatically.
392 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
393 /*quirks*/0, /*mintags*/2, /*maxtags*/32
396 /* Really only one LUN */
397 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
398 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
401 /* I can't believe we need a quirk for DPT volumes. */
402 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
404 /*mintags*/0, /*maxtags*/255
408 * Many Sony CDROM drives don't like multi-LUN probing.
410 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
411 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
415 * This drive doesn't like multiple LUN probing.
416 * Submitted by: Parag Patel <parag@cgt.com>
418 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
419 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
422 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
423 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
427 * The 8200 doesn't like multi-lun probing, and probably
428 * don't like serial number requests either.
431 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
434 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
438 * Let's try the same as above, but for a drive that says
439 * it's an IPL-6860 but is actually an EXB 8200.
442 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
445 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
449 * These Hitachi drives don't like multi-lun probing.
450 * The PR submitter has a DK319H, but says that the Linux
451 * kernel has a similar work-around for the DK312 and DK314,
452 * so all DK31* drives are quirked here.
454 * Submitted by: Paul Haddad <paul@pth.com>
456 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
457 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
461 * The Hitachi CJ series with J8A8 firmware apparently has
462 * problems with tagged commands.
464 * Reported by: amagai@nue.org
466 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
467 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
471 * These are the large storage arrays.
472 * Submitted by: William Carrel <william.carrel@infospace.com>
474 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
475 CAM_QUIRK_HILUNS, 2, 1024
479 * This old revision of the TDC3600 is also SCSI-1, and
480 * hangs upon serial number probing.
483 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
486 CAM_QUIRK_NOVPDS, /*mintags*/0, /*maxtags*/0
490 * Would repond to all LUNs if asked for.
493 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
496 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
500 * Would repond to all LUNs if asked for.
503 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
506 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
509 /* Submitted by: Matthew Dodd <winter@jurai.net> */
510 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
511 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
514 /* Submitted by: Matthew Dodd <winter@jurai.net> */
515 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
516 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
519 /* TeraSolutions special settings for TRC-22 RAID */
520 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
521 /*quirks*/0, /*mintags*/55, /*maxtags*/255
524 /* Veritas Storage Appliance */
525 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
526 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
530 * Would respond to all LUNs. Device type and removable
531 * flag are jumper-selectable.
533 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
536 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
539 /* EasyRAID E5A aka. areca ARC-6010 */
540 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
541 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
544 { T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
545 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
548 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Garmin", "*", "*" },
549 CAM_QUIRK_NORPTLUNS, /*mintags*/2, /*maxtags*/255
552 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic", "STORAGE DEVICE*", "120?" },
553 CAM_QUIRK_NORPTLUNS, /*mintags*/2, /*maxtags*/255
556 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic", "MassStorageClass", "1533" },
557 CAM_QUIRK_NORPTLUNS, /*mintags*/2, /*maxtags*/255
560 /* Default tagged queuing parameters for all devices */
562 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
563 /*vendor*/"*", /*product*/"*", /*revision*/"*"
565 /*quirks*/0, /*mintags*/2, /*maxtags*/255
569 static cam_status proberegister(struct cam_periph *periph,
571 static void probeschedule(struct cam_periph *probe_periph);
572 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
573 static void proberequestdefaultnegotiation(struct cam_periph *periph);
574 static int proberequestbackoff(struct cam_periph *periph,
575 struct cam_ed *device);
576 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
577 static void probe_purge_old(struct cam_path *path,
578 struct scsi_report_luns_data *new,
580 static void probecleanup(struct cam_periph *periph);
581 static void scsi_find_quirk(struct cam_ed *device);
582 static void scsi_scan_bus(struct cam_periph *periph, union ccb *ccb);
583 static void scsi_scan_lun(struct cam_periph *periph,
584 struct cam_path *path, cam_flags flags,
586 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
587 static struct cam_ed *
588 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target,
590 static void scsi_devise_transport(struct cam_path *path);
591 static void scsi_set_transfer_settings(struct ccb_trans_settings *cts,
592 struct cam_path *path,
594 static void scsi_toggle_tags(struct cam_path *path);
595 static void scsi_dev_async(u_int32_t async_code,
597 struct cam_et *target,
598 struct cam_ed *device,
600 static void scsi_action(union ccb *start_ccb);
601 static void scsi_announce_periph(struct cam_periph *periph);
602 static void scsi_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb);
603 static void scsi_proto_announce(struct cam_ed *device);
604 static void scsi_proto_announce_sbuf(struct cam_ed *device,
606 static void scsi_proto_denounce(struct cam_ed *device);
607 static void scsi_proto_denounce_sbuf(struct cam_ed *device,
609 static void scsi_proto_debug_out(union ccb *ccb);
610 static void _scsi_announce_periph(struct cam_periph *, u_int *, u_int *, struct ccb_trans_settings *);
612 static struct xpt_xport_ops scsi_xport_ops = {
613 .alloc_device = scsi_alloc_device,
614 .action = scsi_action,
615 .async = scsi_dev_async,
616 .announce = scsi_announce_periph,
617 .announce_sbuf = scsi_announce_periph_sbuf,
619 #define SCSI_XPT_XPORT(x, X) \
620 static struct xpt_xport scsi_xport_ ## x = { \
621 .xport = XPORT_ ## X, \
623 .ops = &scsi_xport_ops, \
625 CAM_XPT_XPORT(scsi_xport_ ## x);
627 SCSI_XPT_XPORT(spi, SPI);
628 SCSI_XPT_XPORT(sas, SAS);
629 SCSI_XPT_XPORT(fc, FC);
630 SCSI_XPT_XPORT(usb, USB);
631 SCSI_XPT_XPORT(iscsi, ISCSI);
632 SCSI_XPT_XPORT(srp, SRP);
633 SCSI_XPT_XPORT(ppb, PPB);
635 #undef SCSI_XPORT_XPORT
637 static struct xpt_proto_ops scsi_proto_ops = {
638 .announce = scsi_proto_announce,
639 .announce_sbuf = scsi_proto_announce_sbuf,
640 .denounce = scsi_proto_denounce,
641 .denounce_sbuf = scsi_proto_denounce_sbuf,
642 .debug_out = scsi_proto_debug_out,
644 static struct xpt_proto scsi_proto = {
647 .ops = &scsi_proto_ops,
649 CAM_XPT_PROTO(scsi_proto);
657 proberegister(struct cam_periph *periph, void *arg)
659 union ccb *request_ccb; /* CCB representing the probe request */
662 request_ccb = (union ccb *)arg;
663 if (request_ccb == NULL) {
664 printf("proberegister: no probe CCB, "
665 "can't register device\n");
666 return(CAM_REQ_CMP_ERR);
669 softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
672 printf("proberegister: Unable to probe new device. "
673 "Unable to allocate softc\n");
674 return(CAM_REQ_CMP_ERR);
676 TAILQ_INIT(&softc->request_ccbs);
677 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
680 periph->softc = softc;
681 softc->periph = periph;
682 softc->action = PROBE_INVALID;
683 if (cam_periph_acquire(periph) != 0)
684 return (CAM_REQ_CMP_ERR);
686 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
687 scsi_devise_transport(periph->path);
690 * Ensure we've waited at least a bus settle
691 * delay before attempting to probe the device.
692 * For HBAs that don't do bus resets, this won't make a difference.
694 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
696 probeschedule(periph);
701 probeschedule(struct cam_periph *periph)
703 struct ccb_pathinq cpi;
707 softc = (probe_softc *)periph->softc;
708 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
710 xpt_path_inq(&cpi, periph->path);
713 * If a device has gone away and another device, or the same one,
714 * is back in the same place, it should have a unit attention
715 * condition pending. It will not report the unit attention in
716 * response to an inquiry, which may leave invalid transfer
717 * negotiations in effect. The TUR will reveal the unit attention
718 * condition. Only send the TUR for lun 0, since some devices
719 * will get confused by commands other than inquiry to non-existent
720 * luns. If you think a device has gone away start your scan from
721 * lun 0. This will insure that any bogus transfer settings are
724 * If we haven't seen the device before and the controller supports
725 * some kind of transfer negotiation, negotiate with the first
726 * sent command if no bus reset was performed at startup. This
727 * ensures that the device is not confused by transfer negotiation
728 * settings left over by loader or BIOS action.
730 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
731 && (ccb->ccb_h.target_lun == 0)) {
732 PROBE_SET_ACTION(softc, PROBE_TUR);
733 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
734 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
735 proberequestdefaultnegotiation(periph);
736 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
738 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
741 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
742 softc->flags |= PROBE_NO_ANNOUNCE;
744 softc->flags &= ~PROBE_NO_ANNOUNCE;
746 if (cpi.hba_misc & PIM_EXTLUNS)
747 softc->flags |= PROBE_EXTLUN;
749 softc->flags &= ~PROBE_EXTLUN;
751 xpt_schedule(periph, CAM_PRIORITY_XPT);
755 probestart(struct cam_periph *periph, union ccb *start_ccb)
757 /* Probe the device that our peripheral driver points to */
758 struct ccb_scsiio *csio;
761 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
763 softc = (probe_softc *)periph->softc;
764 csio = &start_ccb->csio;
767 switch (softc->action) {
769 case PROBE_TUR_FOR_NEGOTIATION:
772 scsi_test_unit_ready(csio,
781 case PROBE_FULL_INQUIRY:
782 case PROBE_INQUIRY_BASIC_DV1:
783 case PROBE_INQUIRY_BASIC_DV2:
786 struct scsi_inquiry_data *inq_buf;
788 inq_buf = &periph->path->device->inq_data;
791 * If the device is currently configured, we calculate an
792 * MD5 checksum of the inquiry data, and if the serial number
793 * length is greater than 0, add the serial number data
794 * into the checksum as well. Once the inquiry and the
795 * serial number check finish, we attempt to figure out
796 * whether we still have the same device.
798 if (((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
799 && ((softc->flags & PROBE_INQUIRY_CKSUM) == 0)) {
801 MD5Init(&softc->context);
802 MD5Update(&softc->context, (unsigned char *)inq_buf,
803 sizeof(struct scsi_inquiry_data));
804 softc->flags |= PROBE_INQUIRY_CKSUM;
805 if (periph->path->device->serial_num_len > 0) {
806 MD5Update(&softc->context,
807 periph->path->device->serial_num,
808 periph->path->device->serial_num_len);
809 softc->flags |= PROBE_SERIAL_CKSUM;
811 MD5Final(softc->digest, &softc->context);
814 if (softc->action == PROBE_INQUIRY)
815 inquiry_len = SHORT_INQUIRY_LENGTH;
817 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
820 * Some parallel SCSI devices fail to send an
821 * ignore wide residue message when dealing with
822 * odd length inquiry requests. Round up to be
825 inquiry_len = roundup2(inquiry_len, 2);
827 if (softc->action == PROBE_INQUIRY_BASIC_DV1
828 || softc->action == PROBE_INQUIRY_BASIC_DV2) {
829 inq_buf = malloc(inquiry_len, M_CAMXPT, M_NOWAIT);
831 if (inq_buf == NULL) {
832 xpt_print(periph->path, "malloc failure- skipping Basic"
833 "Domain Validation\n");
834 PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
835 scsi_test_unit_ready(csio,
852 /*timeout*/60 * 1000);
855 case PROBE_REPORT_LUNS:
859 rp = malloc(periph->path->target->rpl_size,
860 M_CAMXPT, M_NOWAIT | M_ZERO);
862 struct scsi_inquiry_data *inq_buf;
863 inq_buf = &periph->path->device->inq_data;
864 xpt_print(periph->path,
865 "Unable to alloc report luns storage\n");
866 if (INQ_DATA_TQ_ENABLED(inq_buf))
867 PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
869 PROBE_SET_ACTION(softc,
870 PROBE_SUPPORTED_VPD_LIST);
873 scsi_report_luns(csio, 5, probedone, MSG_SIMPLE_Q_TAG,
874 RPL_REPORT_DEFAULT, rp, periph->path->target->rpl_size,
875 SSD_FULL_SIZE, 60000); break;
878 case PROBE_MODE_SENSE:
883 mode_buf_len = sizeof(struct scsi_mode_header_6)
884 + sizeof(struct scsi_mode_blk_desc)
885 + sizeof(struct scsi_control_page);
886 mode_buf = malloc(mode_buf_len, M_CAMXPT, M_NOWAIT);
887 if (mode_buf != NULL) {
888 scsi_mode_sense(csio,
893 SMS_PAGE_CTRL_CURRENT,
894 SMS_CONTROL_MODE_PAGE,
901 xpt_print(periph->path, "Unable to mode sense control page - "
903 PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
906 case PROBE_SUPPORTED_VPD_LIST:
908 struct scsi_vpd_supported_page_list *vpd_list;
909 struct cam_ed *device;
912 device = periph->path->device;
914 if ((SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOVPDS) == 0)
915 vpd_list = malloc(sizeof(*vpd_list), M_CAMXPT,
918 if (vpd_list != NULL) {
923 (u_int8_t *)vpd_list,
926 SVPD_SUPPORTED_PAGE_LIST,
928 /*timeout*/60 * 1000);
933 * We'll have to do without, let our probedone
934 * routine finish up for us.
936 start_ccb->csio.data_ptr = NULL;
937 cam_freeze_devq(periph->path);
938 cam_periph_doacquire(periph);
939 probedone(periph, start_ccb);
942 case PROBE_DEVICE_ID:
944 struct scsi_vpd_device_id *devid;
947 if (scsi_vpd_supported_page(periph, SVPD_DEVICE_ID))
948 devid = malloc(SVPD_DEVICE_ID_MAX_SIZE, M_CAMXPT,
957 SVPD_DEVICE_ID_MAX_SIZE,
961 /*timeout*/60 * 1000);
966 case PROBE_EXTENDED_INQUIRY:
968 struct scsi_vpd_extended_inquiry_data *ext_inq;
971 if (scsi_vpd_supported_page(periph, SVPD_EXTENDED_INQUIRY_DATA))
972 ext_inq = malloc(sizeof(*ext_inq), M_CAMXPT,
975 if (ext_inq != NULL) {
983 SVPD_EXTENDED_INQUIRY_DATA,
985 /*timeout*/60 * 1000);
989 * We'll have to do without, let our probedone
990 * routine finish up for us.
994 case PROBE_SERIAL_NUM:
996 struct scsi_vpd_unit_serial_number *serial_buf;
997 struct cam_ed* device;
1000 device = periph->path->device;
1001 if (device->serial_num != NULL) {
1002 free(device->serial_num, M_CAMXPT);
1003 device->serial_num = NULL;
1004 device->serial_num_len = 0;
1007 if (scsi_vpd_supported_page(periph, SVPD_UNIT_SERIAL_NUMBER))
1008 serial_buf = (struct scsi_vpd_unit_serial_number *)
1009 malloc(sizeof(*serial_buf), M_CAMXPT,
1012 if (serial_buf != NULL) {
1017 (u_int8_t *)serial_buf,
1018 sizeof(*serial_buf),
1020 SVPD_UNIT_SERIAL_NUMBER,
1022 /*timeout*/60 * 1000);
1028 panic("probestart: invalid action state 0x%x\n", softc->action);
1030 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1031 cam_periph_doacquire(periph);
1032 xpt_action(start_ccb);
1036 proberequestdefaultnegotiation(struct cam_periph *periph)
1038 struct ccb_trans_settings cts;
1040 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1041 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1042 cts.type = CTS_TYPE_USER_SETTINGS;
1043 xpt_action((union ccb *)&cts);
1044 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1047 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1048 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1049 xpt_action((union ccb *)&cts);
1053 * Backoff Negotiation Code- only pertinent for SPI devices.
1056 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
1058 struct ccb_trans_settings cts;
1059 struct ccb_trans_settings_spi *spi;
1061 memset(&cts, 0, sizeof (cts));
1062 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1063 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1064 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1065 xpt_action((union ccb *)&cts);
1066 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1068 xpt_print(periph->path,
1069 "failed to get current device settings\n");
1073 if (cts.transport != XPORT_SPI) {
1075 xpt_print(periph->path, "not SPI transport\n");
1079 spi = &cts.xport_specific.spi;
1082 * We cannot renegotiate sync rate if we don't have one.
1084 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
1086 xpt_print(periph->path, "no sync rate known\n");
1092 * We'll assert that we don't have to touch PPR options- the
1093 * SIM will see what we do with period and offset and adjust
1094 * the PPR options as appropriate.
1098 * A sync rate with unknown or zero offset is nonsensical.
1099 * A sync period of zero means Async.
1101 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
1102 || spi->sync_offset == 0 || spi->sync_period == 0) {
1104 xpt_print(periph->path, "no sync rate available\n");
1109 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
1110 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1111 ("hit async: giving up on DV\n"));
1117 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
1118 * We don't try to remember 'last' settings to see if the SIM actually
1119 * gets into the speed we want to set. We check on the SIM telling
1120 * us that a requested speed is bad, but otherwise don't try and
1121 * check the speed due to the asynchronous and handshake nature
1124 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
1127 if (spi->sync_period >= 0xf) {
1128 spi->sync_period = 0;
1129 spi->sync_offset = 0;
1130 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1131 ("setting to async for DV\n"));
1133 * Once we hit async, we don't want to try
1134 * any more settings.
1136 device->flags |= CAM_DEV_DV_HIT_BOTTOM;
1137 } else if (bootverbose) {
1138 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1139 ("DV: period 0x%x\n", spi->sync_period));
1140 printf("setting period to 0x%x\n", spi->sync_period);
1142 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1143 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1144 xpt_action((union ccb *)&cts);
1145 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1148 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1149 ("DV: failed to set period 0x%x\n", spi->sync_period));
1150 if (spi->sync_period == 0) {
1157 #define CCB_COMPLETED_OK(ccb) (((ccb).status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1160 probedone(struct cam_periph *periph, union ccb *done_ccb)
1163 struct cam_path *path;
1164 struct scsi_inquiry_data *inq_buf;
1167 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
1169 softc = (probe_softc *)periph->softc;
1170 path = done_ccb->ccb_h.path;
1171 priority = done_ccb->ccb_h.pinfo.priority;
1172 cam_periph_assert(periph, MA_OWNED);
1174 switch (softc->action) {
1177 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1179 if (cam_periph_error(done_ccb, 0, SF_NO_PRINT) ==
1182 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1183 cam_release_devq(path, 0, 0, 0, FALSE);
1186 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1187 /* Don't wedge the queue */
1188 xpt_release_devq(done_ccb->ccb_h.path,
1192 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1193 xpt_release_ccb(done_ccb);
1194 xpt_schedule(periph, priority);
1196 /* Drop freeze taken due to CAM_DEV_QFREEZE and release. */
1197 cam_release_devq(path, 0, 0, 0, FALSE);
1198 cam_periph_release_locked(periph);
1202 case PROBE_FULL_INQUIRY:
1204 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1205 u_int8_t periph_qual;
1207 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1208 scsi_find_quirk(path->device);
1209 inq_buf = &path->device->inq_data;
1211 periph_qual = SID_QUAL(inq_buf);
1213 if (periph_qual == SID_QUAL_LU_CONNECTED ||
1214 periph_qual == SID_QUAL_LU_OFFLINE) {
1218 * We conservatively request only
1219 * SHORT_INQUIRY_LEN bytes of inquiry
1220 * information during our first try
1221 * at sending an INQUIRY. If the device
1222 * has more information to give,
1223 * perform a second request specifying
1224 * the amount of information the device
1225 * is willing to give.
1227 len = inq_buf->additional_length
1228 + offsetof(struct scsi_inquiry_data,
1229 additional_length) + 1;
1230 if (softc->action == PROBE_INQUIRY
1231 && len > SHORT_INQUIRY_LENGTH) {
1232 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1233 xpt_release_ccb(done_ccb);
1234 xpt_schedule(periph, priority);
1238 scsi_devise_transport(path);
1240 if (path->device->lun_id == 0 &&
1241 SID_ANSI_REV(inq_buf) > SCSI_REV_SPC2 &&
1242 (SCSI_QUIRK(path->device)->quirks &
1243 CAM_QUIRK_NORPTLUNS) == 0) {
1244 PROBE_SET_ACTION(softc,
1247 * Start with room for *one* lun.
1249 periph->path->target->rpl_size = 16;
1250 } else if (INQ_DATA_TQ_ENABLED(inq_buf))
1251 PROBE_SET_ACTION(softc,
1254 PROBE_SET_ACTION(softc,
1255 PROBE_SUPPORTED_VPD_LIST);
1257 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1258 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1259 xpt_acquire_device(path->device);
1261 xpt_release_ccb(done_ccb);
1262 xpt_schedule(periph, priority);
1264 } else if (path->device->lun_id == 0 &&
1265 SID_ANSI_REV(inq_buf) >= SCSI_REV_SPC2 &&
1266 (SCSI_QUIRK(path->device)->quirks &
1267 CAM_QUIRK_NORPTLUNS) == 0) {
1268 PROBE_SET_ACTION(softc, PROBE_REPORT_LUNS);
1269 periph->path->target->rpl_size = 16;
1270 xpt_release_ccb(done_ccb);
1271 xpt_schedule(periph, priority);
1274 } else if (cam_periph_error(done_ccb, 0,
1275 done_ccb->ccb_h.target_lun > 0
1276 ? SF_RETRY_UA|SF_QUIET_IR
1277 : SF_RETRY_UA) == ERESTART) {
1280 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1281 /* Don't wedge the queue */
1282 xpt_release_devq(done_ccb->ccb_h.path,
1283 /*count*/1, /*run_queue*/TRUE);
1285 path->device->flags &= ~CAM_DEV_INQUIRY_DATA_VALID;
1288 * If we get to this point, we got an error status back
1289 * from the inquiry and the error status doesn't require
1290 * automatically retrying the command. Therefore, the
1291 * inquiry failed. If we had inquiry information before
1292 * for this device, but this latest inquiry command failed,
1293 * the device has probably gone away. If this device isn't
1294 * already marked unconfigured, notify the peripheral
1295 * drivers that this device is no more.
1297 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
1298 /* Send the async notification. */
1299 xpt_async(AC_LOST_DEVICE, path, NULL);
1300 PROBE_SET_ACTION(softc, PROBE_INVALID);
1302 xpt_release_ccb(done_ccb);
1305 case PROBE_REPORT_LUNS:
1307 struct ccb_scsiio *csio;
1308 struct scsi_report_luns_data *lp;
1311 csio = &done_ccb->csio;
1313 lp = (struct scsi_report_luns_data *)csio->data_ptr;
1314 nlun = scsi_4btoul(lp->length) / 8;
1315 maxlun = (csio->dxfer_len / 8) - 1;
1317 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1318 if (cam_periph_error(done_ccb, 0,
1319 done_ccb->ccb_h.target_lun > 0 ?
1320 SF_RETRY_UA|SF_QUIET_IR : SF_RETRY_UA) ==
1324 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1325 xpt_release_devq(done_ccb->ccb_h.path, 1,
1330 } else if (nlun > maxlun) {
1332 * Reallocate and retry to cover all luns
1334 CAM_DEBUG(path, CAM_DEBUG_PROBE,
1335 ("Probe: reallocating REPORT_LUNS for %u luns\n",
1338 path->target->rpl_size = (nlun << 3) + 8;
1339 xpt_release_ccb(done_ccb);
1340 xpt_schedule(periph, priority);
1342 } else if (nlun == 0) {
1344 * If there don't appear to be any luns, bail.
1352 CAM_DEBUG(path, CAM_DEBUG_PROBE,
1353 ("Probe: %u lun(s) reported\n", nlun));
1355 CAM_GET_LUN(lp, 0, lun);
1357 * If the first lun is not lun 0, then either there
1358 * is no lun 0 in the list, or the list is unsorted.
1361 for (idx = 0; idx < nlun; idx++) {
1362 CAM_GET_LUN(lp, idx, lun);
1370 lp->luns[0].lundata, 8);
1371 memcpy(lp->luns[0].lundata,
1372 lp->luns[idx].lundata, 8);
1373 memcpy(lp->luns[idx].lundata,
1375 CAM_DEBUG(path, CAM_DEBUG_PROBE,
1376 ("lun 0 in position %u\n", idx));
1380 * If we have an old lun list, We can either
1381 * retest luns that appear to have been dropped,
1382 * or just nuke them. We'll opt for the latter.
1383 * This function will also install the new list
1384 * in the target structure.
1386 probe_purge_old(path, lp, softc->flags);
1389 inq_buf = &path->device->inq_data;
1390 if (path->device->flags & CAM_DEV_INQUIRY_DATA_VALID &&
1391 (SID_QUAL(inq_buf) == SID_QUAL_LU_CONNECTED ||
1392 SID_QUAL(inq_buf) == SID_QUAL_LU_OFFLINE)) {
1393 if (INQ_DATA_TQ_ENABLED(inq_buf))
1394 PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
1396 PROBE_SET_ACTION(softc,
1397 PROBE_SUPPORTED_VPD_LIST);
1398 xpt_release_ccb(done_ccb);
1399 xpt_schedule(periph, priority);
1405 PROBE_SET_ACTION(softc, PROBE_INVALID);
1406 xpt_release_ccb(done_ccb);
1409 case PROBE_MODE_SENSE:
1411 struct ccb_scsiio *csio;
1412 struct scsi_mode_header_6 *mode_hdr;
1414 csio = &done_ccb->csio;
1415 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
1416 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1417 struct scsi_control_page *page;
1420 offset = ((u_int8_t *)&mode_hdr[1])
1421 + mode_hdr->blk_desc_len;
1422 page = (struct scsi_control_page *)offset;
1423 path->device->queue_flags = page->queue_flags;
1424 } else if (cam_periph_error(done_ccb, 0,
1425 SF_RETRY_UA|SF_NO_PRINT) == ERESTART) {
1427 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1428 /* Don't wedge the queue */
1429 xpt_release_devq(done_ccb->ccb_h.path,
1430 /*count*/1, /*run_queue*/TRUE);
1432 xpt_release_ccb(done_ccb);
1433 free(mode_hdr, M_CAMXPT);
1434 PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
1435 xpt_schedule(periph, priority);
1438 case PROBE_SUPPORTED_VPD_LIST:
1440 struct ccb_scsiio *csio;
1441 struct scsi_vpd_supported_page_list *page_list;
1443 csio = &done_ccb->csio;
1445 (struct scsi_vpd_supported_page_list *)csio->data_ptr;
1447 if (path->device->supported_vpds != NULL) {
1448 free(path->device->supported_vpds, M_CAMXPT);
1449 path->device->supported_vpds = NULL;
1450 path->device->supported_vpds_len = 0;
1453 if (page_list == NULL) {
1455 * Don't process the command as it was never sent
1457 } else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1459 path->device->supported_vpds_len = page_list->length +
1460 SVPD_SUPPORTED_PAGES_HDR_LEN;
1461 path->device->supported_vpds = (uint8_t *)page_list;
1462 xpt_release_ccb(done_ccb);
1463 PROBE_SET_ACTION(softc, PROBE_DEVICE_ID);
1464 xpt_schedule(periph, priority);
1466 } else if (cam_periph_error(done_ccb, 0,
1467 SF_RETRY_UA|SF_NO_PRINT) == ERESTART) {
1469 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1470 /* Don't wedge the queue */
1471 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1476 free(page_list, M_CAMXPT);
1477 /* No VPDs available, skip to device check. */
1478 csio->data_ptr = NULL;
1479 goto probe_device_check;
1481 case PROBE_DEVICE_ID:
1483 struct scsi_vpd_device_id *devid;
1484 struct ccb_scsiio *csio;
1485 uint32_t length = 0;
1487 csio = &done_ccb->csio;
1488 devid = (struct scsi_vpd_device_id *)csio->data_ptr;
1490 /* Clean up from previous instance of this device */
1491 if (path->device->device_id != NULL) {
1492 path->device->device_id_len = 0;
1493 free(path->device->device_id, M_CAMXPT);
1494 path->device->device_id = NULL;
1497 if (devid == NULL) {
1498 /* Don't process the command as it was never sent */
1499 } else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1500 length = scsi_2btoul(devid->length);
1503 * NB: device_id_len is actual response
1504 * size, not buffer size.
1506 path->device->device_id_len = length +
1507 SVPD_DEVICE_ID_HDR_LEN;
1508 path->device->device_id = (uint8_t *)devid;
1510 } else if (cam_periph_error(done_ccb, 0,
1511 SF_RETRY_UA) == ERESTART) {
1513 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1514 /* Don't wedge the queue */
1515 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1519 /* Free the device id space if we don't use it */
1520 if (devid && length == 0)
1521 free(devid, M_CAMXPT);
1522 xpt_release_ccb(done_ccb);
1523 PROBE_SET_ACTION(softc, PROBE_EXTENDED_INQUIRY);
1524 xpt_schedule(periph, priority);
1527 case PROBE_EXTENDED_INQUIRY: {
1528 struct scsi_vpd_extended_inquiry_data *ext_inq;
1529 struct ccb_scsiio *csio;
1532 csio = &done_ccb->csio;
1533 ext_inq = (struct scsi_vpd_extended_inquiry_data *)
1535 if (path->device->ext_inq != NULL) {
1536 path->device->ext_inq_len = 0;
1537 free(path->device->ext_inq, M_CAMXPT);
1538 path->device->ext_inq = NULL;
1541 if (ext_inq == NULL) {
1542 /* Don't process the command as it was never sent */
1543 } else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1544 length = scsi_2btoul(ext_inq->page_length) +
1545 __offsetof(struct scsi_vpd_extended_inquiry_data,
1547 length = min(length, sizeof(*ext_inq));
1548 length -= csio->resid;
1550 path->device->ext_inq_len = length;
1551 path->device->ext_inq = (uint8_t *)ext_inq;
1553 } else if (cam_periph_error(done_ccb, 0, SF_RETRY_UA) ==
1556 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1557 /* Don't wedge the queue */
1558 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1562 /* Free the device id space if we don't use it */
1563 if (ext_inq && length <= 0)
1564 free(ext_inq, M_CAMXPT);
1565 xpt_release_ccb(done_ccb);
1566 PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM);
1567 xpt_schedule(periph, priority);
1572 case PROBE_SERIAL_NUM:
1574 struct ccb_scsiio *csio;
1575 struct scsi_vpd_unit_serial_number *serial_buf;
1582 csio = &done_ccb->csio;
1583 priority = done_ccb->ccb_h.pinfo.priority;
1585 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
1587 if (serial_buf == NULL) {
1589 * Don't process the command as it was never sent
1591 } else if (cam_ccb_status(done_ccb) == CAM_REQ_CMP
1592 && (serial_buf->length > 0)) {
1595 path->device->serial_num =
1596 (u_int8_t *)malloc((serial_buf->length + 1),
1597 M_CAMXPT, M_NOWAIT);
1598 if (path->device->serial_num != NULL) {
1601 start = strspn(serial_buf->serial_num, " ");
1602 slen = serial_buf->length - start;
1605 * SPC5r05 says that an all-space serial
1606 * number means no product serial number
1611 memcpy(path->device->serial_num,
1612 &serial_buf->serial_num[start], slen);
1613 path->device->serial_num_len = slen;
1614 path->device->serial_num[slen] = '\0';
1616 } else if (cam_periph_error(done_ccb, 0,
1617 SF_RETRY_UA|SF_NO_PRINT) == ERESTART) {
1619 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1620 /* Don't wedge the queue */
1621 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1626 * Let's see if we have seen this device before.
1628 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
1630 u_int8_t digest[16];
1635 (unsigned char *)&path->device->inq_data,
1636 sizeof(struct scsi_inquiry_data));
1639 MD5Update(&context, path->device->serial_num,
1640 path->device->serial_num_len);
1642 MD5Final(digest, &context);
1643 if (bcmp(softc->digest, digest, 16) == 0)
1647 * XXX Do we need to do a TUR in order to ensure
1648 * that the device really hasn't changed???
1651 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
1652 xpt_async(AC_LOST_DEVICE, path, NULL);
1654 if (serial_buf != NULL)
1655 free(serial_buf, M_CAMXPT);
1659 * Now that we have all the necessary
1660 * information to safely perform transfer
1661 * negotiations... Controllers don't perform
1662 * any negotiation or tagged queuing until
1663 * after the first XPT_SET_TRAN_SETTINGS ccb is
1664 * received. So, on a new device, just retrieve
1665 * the user settings, and set them as the current
1666 * settings to set the device up.
1668 proberequestdefaultnegotiation(periph);
1669 xpt_release_ccb(done_ccb);
1672 * Perform a TUR to allow the controller to
1673 * perform any necessary transfer negotiation.
1675 PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1676 xpt_schedule(periph, priority);
1679 xpt_release_ccb(done_ccb);
1682 case PROBE_TUR_FOR_NEGOTIATION:
1684 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1685 cam_periph_error(done_ccb, 0,
1686 SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY);
1688 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1689 /* Don't wedge the queue */
1690 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1694 * Do Domain Validation for lun 0 on devices that claim
1695 * to support Synchronous Transfer modes.
1697 if (softc->action == PROBE_TUR_FOR_NEGOTIATION
1698 && done_ccb->ccb_h.target_lun == 0
1699 && (path->device->inq_data.flags & SID_Sync) != 0
1700 && (path->device->flags & CAM_DEV_IN_DV) == 0) {
1701 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1702 ("Begin Domain Validation\n"));
1703 path->device->flags |= CAM_DEV_IN_DV;
1704 xpt_release_ccb(done_ccb);
1705 PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
1706 xpt_schedule(periph, priority);
1709 if (softc->action == PROBE_DV_EXIT) {
1710 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1711 ("Leave Domain Validation\n"));
1713 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1714 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1715 xpt_acquire_device(path->device);
1717 path->device->flags &=
1718 ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1719 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1720 /* Inform the XPT that a new device has been found */
1721 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1722 xpt_action(done_ccb);
1723 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1726 PROBE_SET_ACTION(softc, PROBE_DONE);
1727 xpt_release_ccb(done_ccb);
1729 case PROBE_INQUIRY_BASIC_DV1:
1730 case PROBE_INQUIRY_BASIC_DV2:
1732 struct scsi_inquiry_data *nbuf;
1733 struct ccb_scsiio *csio;
1735 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1736 cam_periph_error(done_ccb, 0,
1737 SF_NO_PRINT | SF_NO_RECOVERY | SF_NO_RETRY);
1739 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1740 /* Don't wedge the queue */
1741 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1744 csio = &done_ccb->csio;
1745 nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
1746 if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
1748 "inquiry data fails comparison at DV%d step\n",
1749 softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
1750 if (proberequestbackoff(periph, path->device)) {
1751 path->device->flags &= ~CAM_DEV_IN_DV;
1752 PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1755 PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1757 free(nbuf, M_CAMXPT);
1758 xpt_release_ccb(done_ccb);
1759 xpt_schedule(periph, priority);
1762 free(nbuf, M_CAMXPT);
1763 if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
1764 PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
1765 xpt_release_ccb(done_ccb);
1766 xpt_schedule(periph, priority);
1769 if (softc->action == PROBE_INQUIRY_BASIC_DV2) {
1770 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1771 ("Leave Domain Validation Successfully\n"));
1773 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1774 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1775 xpt_acquire_device(path->device);
1777 path->device->flags &=
1778 ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1779 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1780 /* Inform the XPT that a new device has been found */
1781 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1782 xpt_action(done_ccb);
1783 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1786 PROBE_SET_ACTION(softc, PROBE_DONE);
1787 xpt_release_ccb(done_ccb);
1791 panic("probedone: invalid action state 0x%x\n", softc->action);
1793 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
1794 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
1795 done_ccb->ccb_h.status = CAM_REQ_CMP;
1797 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
1798 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1799 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1800 cam_release_devq(path, 0, 0, 0, FALSE);
1801 cam_periph_release_locked(periph);
1802 cam_periph_invalidate(periph);
1803 cam_periph_release_locked(periph);
1805 probeschedule(periph);
1811 probe_purge_old(struct cam_path *path, struct scsi_report_luns_data *new,
1814 struct cam_path *tp;
1815 struct scsi_report_luns_data *old;
1816 u_int idx1, idx2, nlun_old, nlun_new;
1820 if (path->target == NULL) {
1823 mtx_lock(&path->target->luns_mtx);
1824 old = path->target->luns;
1825 path->target->luns = new;
1826 mtx_unlock(&path->target->luns_mtx);
1829 nlun_old = scsi_4btoul(old->length) / 8;
1830 nlun_new = scsi_4btoul(new->length) / 8;
1833 * We are not going to assume sorted lists. Deal.
1835 for (idx1 = 0; idx1 < nlun_old; idx1++) {
1836 ol = old->luns[idx1].lundata;
1837 for (idx2 = 0; idx2 < nlun_new; idx2++) {
1838 nl = new->luns[idx2].lundata;
1839 if (memcmp(nl, ol, 8) == 0) {
1843 if (idx2 < nlun_new) {
1847 * An 'old' item not in the 'new' list.
1848 * Nuke it. Except that if it is lun 0,
1849 * that would be what the probe state
1850 * machine is currently working on,
1851 * so we won't do that.
1853 CAM_GET_LUN(old, idx1, this_lun);
1854 if (this_lun == 0) {
1859 * We also cannot nuke it if it is
1860 * not in a lun format we understand
1861 * and replace the LUN with a "simple" LUN
1862 * if that is all the HBA supports.
1864 if (!(flags & PROBE_EXTLUN)) {
1865 if (!CAM_CAN_GET_SIMPLE_LUN(old, idx1))
1867 CAM_GET_SIMPLE_LUN(old, idx1, this_lun);
1870 if (xpt_create_path(&tp, NULL, xpt_path_path_id(path),
1871 xpt_path_target_id(path), this_lun) == CAM_REQ_CMP) {
1872 xpt_async(AC_LOST_DEVICE, tp, NULL);
1876 free(old, M_CAMXPT);
1880 probecleanup(struct cam_periph *periph)
1882 free(periph->softc, M_CAMXPT);
1886 scsi_find_quirk(struct cam_ed *device)
1888 struct scsi_quirk_entry *quirk;
1891 match = cam_quirkmatch((caddr_t)&device->inq_data,
1892 (caddr_t)scsi_quirk_table,
1893 nitems(scsi_quirk_table),
1894 sizeof(*scsi_quirk_table), scsi_inquiry_match);
1897 panic("xpt_find_quirk: device didn't match wildcard entry!!");
1899 quirk = (struct scsi_quirk_entry *)match;
1900 device->quirk = quirk;
1901 device->mintags = quirk->mintags;
1902 device->maxtags = quirk->maxtags;
1906 sysctl_cam_search_luns(SYSCTL_HANDLER_ARGS)
1911 error = sysctl_handle_int(oidp, &val, 0, req);
1912 if (error != 0 || req->newptr == NULL)
1914 if (val == 0 || val == 1) {
1923 union ccb *request_ccb;
1924 struct ccb_pathinq *cpi;
1927 } scsi_scan_bus_info;
1930 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1931 * As the scan progresses, scsi_scan_bus is used as the
1932 * callback on completion function.
1935 scsi_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1939 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1940 ("scsi_scan_bus\n"));
1941 switch (request_ccb->ccb_h.func_code) {
1945 scsi_scan_bus_info *scan_info;
1946 union ccb *work_ccb, *reset_ccb;
1947 struct cam_path *path;
1949 u_int low_target, max_target;
1952 /* Find out the characteristics of the bus */
1953 work_ccb = xpt_alloc_ccb_nowait();
1954 if (work_ccb == NULL) {
1955 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1956 xpt_done(request_ccb);
1959 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1960 request_ccb->ccb_h.pinfo.priority);
1961 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1962 xpt_action(work_ccb);
1963 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1964 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1965 xpt_free_ccb(work_ccb);
1966 xpt_done(request_ccb);
1970 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1972 * Can't scan the bus on an adapter that
1973 * cannot perform the initiator role.
1975 request_ccb->ccb_h.status = CAM_REQ_CMP;
1976 xpt_free_ccb(work_ccb);
1977 xpt_done(request_ccb);
1981 /* We may need to reset bus first, if we haven't done it yet. */
1982 if ((work_ccb->cpi.hba_inquiry &
1983 (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1984 !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1985 !timevalisset(&request_ccb->ccb_h.path->bus->last_reset) &&
1986 (reset_ccb = xpt_alloc_ccb_nowait()) != NULL) {
1987 xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1989 reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1990 xpt_action(reset_ccb);
1991 if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1992 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1993 xpt_free_ccb(reset_ccb);
1994 xpt_free_ccb(work_ccb);
1995 xpt_done(request_ccb);
1998 xpt_free_ccb(reset_ccb);
2001 /* Save some state for use while we probe for devices */
2002 scan_info = (scsi_scan_bus_info *) malloc(sizeof(scsi_scan_bus_info) +
2003 (work_ccb->cpi.max_target * sizeof (u_int)), M_CAMXPT, M_ZERO|M_NOWAIT);
2004 if (scan_info == NULL) {
2005 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2006 xpt_free_ccb(work_ccb);
2007 xpt_done(request_ccb);
2010 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2011 ("SCAN start for %p\n", scan_info));
2012 scan_info->request_ccb = request_ccb;
2013 scan_info->cpi = &work_ccb->cpi;
2015 /* Cache on our stack so we can work asynchronously */
2016 max_target = scan_info->cpi->max_target;
2018 initiator_id = scan_info->cpi->initiator_id;
2022 * We can scan all targets in parallel, or do it sequentially.
2025 if (request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2026 max_target = low_target = request_ccb->ccb_h.target_id;
2027 scan_info->counter = 0;
2028 } else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2030 scan_info->counter = 0;
2032 scan_info->counter = scan_info->cpi->max_target + 1;
2033 if (scan_info->cpi->initiator_id < scan_info->counter) {
2034 scan_info->counter--;
2037 mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2040 for (i = low_target; i <= max_target; i++) {
2042 if (i == initiator_id)
2045 status = xpt_create_path(&path, NULL,
2046 request_ccb->ccb_h.path_id,
2048 if (status != CAM_REQ_CMP) {
2049 printf("scsi_scan_bus: xpt_create_path failed"
2050 " with status %#x, bus scan halted\n",
2052 free(scan_info, M_CAMXPT);
2053 request_ccb->ccb_h.status = status;
2054 xpt_free_ccb(work_ccb);
2055 xpt_done(request_ccb);
2058 work_ccb = xpt_alloc_ccb_nowait();
2059 if (work_ccb == NULL) {
2060 xpt_free_ccb((union ccb *)scan_info->cpi);
2061 free(scan_info, M_CAMXPT);
2062 xpt_free_path(path);
2063 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2064 xpt_done(request_ccb);
2067 xpt_setup_ccb(&work_ccb->ccb_h, path,
2068 request_ccb->ccb_h.pinfo.priority);
2069 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2070 work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2071 work_ccb->ccb_h.flags |= CAM_UNLOCKED;
2072 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
2073 work_ccb->crcn.flags = request_ccb->crcn.flags;
2074 xpt_action(work_ccb);
2083 struct cam_path *path, *oldpath;
2084 scsi_scan_bus_info *scan_info;
2085 struct cam_et *target;
2086 struct cam_ed *device, *nextdev;
2089 target_id_t target_id;
2092 oldpath = request_ccb->ccb_h.path;
2094 status = cam_ccb_status(request_ccb);
2095 scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
2096 path_id = request_ccb->ccb_h.path_id;
2097 target_id = request_ccb->ccb_h.target_id;
2098 lun_id = request_ccb->ccb_h.target_lun;
2099 target = request_ccb->ccb_h.path->target;
2102 mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2104 mtx_lock(&target->luns_mtx);
2107 u_int nluns = scsi_4btoul(target->luns->length) / 8;
2110 * Make sure we skip over lun 0 if it's the first member
2111 * of the list as we've actually just finished probing
2114 CAM_GET_LUN(target->luns, 0, first);
2115 if (first == 0 && scan_info->lunindex[target_id] == 0) {
2116 scan_info->lunindex[target_id]++;
2120 * Skip any LUNs that the HBA can't deal with.
2122 while (scan_info->lunindex[target_id] < nluns) {
2123 if (scan_info->cpi->hba_misc & PIM_EXTLUNS) {
2124 CAM_GET_LUN(target->luns,
2125 scan_info->lunindex[target_id],
2130 if (CAM_CAN_GET_SIMPLE_LUN(target->luns,
2131 scan_info->lunindex[target_id])) {
2132 CAM_GET_SIMPLE_LUN(target->luns,
2133 scan_info->lunindex[target_id],
2138 scan_info->lunindex[target_id]++;
2141 if (scan_info->lunindex[target_id] < nluns) {
2142 mtx_unlock(&target->luns_mtx);
2144 CAM_DEBUG(request_ccb->ccb_h.path,
2146 ("next lun to try at index %u is %jx\n",
2147 scan_info->lunindex[target_id],
2148 (uintmax_t)lun_id));
2149 scan_info->lunindex[target_id]++;
2151 mtx_unlock(&target->luns_mtx);
2152 /* We're done with scanning all luns. */
2155 mtx_unlock(&target->luns_mtx);
2156 device = request_ccb->ccb_h.path->device;
2157 /* Continue sequential LUN scan if: */
2158 /* -- we have more LUNs that need recheck */
2159 mtx_lock(&target->bus->eb_mtx);
2161 while ((nextdev = TAILQ_NEXT(nextdev, links)) != NULL)
2162 if ((nextdev->flags & CAM_DEV_UNCONFIGURED) == 0)
2164 mtx_unlock(&target->bus->eb_mtx);
2165 if (nextdev != NULL) {
2167 /* -- stop if CAM_QUIRK_NOLUNS is set. */
2168 } else if (SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOLUNS) {
2170 /* -- this LUN is connected and its SCSI version
2171 * allows more LUNs. */
2172 } else if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2173 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2174 CAN_SRCH_HI_DENSE(device))
2176 /* -- this LUN is disconnected, its SCSI version
2177 * allows more LUNs and we guess they may be. */
2178 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) {
2179 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2180 CAN_SRCH_HI_SPARSE(device))
2183 if (next_target == 0) {
2185 if (lun_id > scan_info->cpi->max_lun)
2191 * Check to see if we scan any further luns.
2197 * Free the current request path- we're done with it.
2199 xpt_free_path(oldpath);
2202 if (scan_info->request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2204 } else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2205 scan_info->counter++;
2206 if (scan_info->counter ==
2207 scan_info->cpi->initiator_id) {
2208 scan_info->counter++;
2210 if (scan_info->counter >=
2211 scan_info->cpi->max_target+1) {
2215 scan_info->counter--;
2216 if (scan_info->counter == 0) {
2222 xpt_free_ccb(request_ccb);
2223 xpt_free_ccb((union ccb *)scan_info->cpi);
2224 request_ccb = scan_info->request_ccb;
2225 CAM_DEBUG(request_ccb->ccb_h.path,
2227 ("SCAN done for %p\n", scan_info));
2228 free(scan_info, M_CAMXPT);
2229 request_ccb->ccb_h.status = CAM_REQ_CMP;
2230 xpt_done(request_ccb);
2234 if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
2236 xpt_free_ccb(request_ccb);
2239 status = xpt_create_path(&path, NULL,
2240 scan_info->request_ccb->ccb_h.path_id,
2241 scan_info->counter, 0);
2242 if (status != CAM_REQ_CMP) {
2244 printf("scsi_scan_bus: xpt_create_path failed"
2245 " with status %#x, bus scan halted\n",
2247 xpt_free_ccb(request_ccb);
2248 xpt_free_ccb((union ccb *)scan_info->cpi);
2249 request_ccb = scan_info->request_ccb;
2250 free(scan_info, M_CAMXPT);
2251 request_ccb->ccb_h.status = status;
2252 xpt_done(request_ccb);
2255 xpt_setup_ccb(&request_ccb->ccb_h, path,
2256 request_ccb->ccb_h.pinfo.priority);
2257 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2258 request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2259 request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2260 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2261 request_ccb->crcn.flags =
2262 scan_info->request_ccb->crcn.flags;
2264 status = xpt_create_path(&path, NULL,
2265 path_id, target_id, lun_id);
2267 * Free the old request path- we're done with it. We
2268 * do this *after* creating the new path so that
2269 * we don't remove a target that has our lun list
2270 * in the case that lun 0 is not present.
2272 xpt_free_path(oldpath);
2273 if (status != CAM_REQ_CMP) {
2274 printf("scsi_scan_bus: xpt_create_path failed "
2275 "with status %#x, halting LUN scan\n",
2279 xpt_setup_ccb(&request_ccb->ccb_h, path,
2280 request_ccb->ccb_h.pinfo.priority);
2281 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2282 request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2283 request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2284 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2285 request_ccb->crcn.flags =
2286 scan_info->request_ccb->crcn.flags;
2289 xpt_action(request_ccb);
2298 scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
2299 cam_flags flags, union ccb *request_ccb)
2301 struct ccb_pathinq cpi;
2303 struct cam_path *new_path;
2304 struct cam_periph *old_periph;
2307 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
2309 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2310 cpi.ccb_h.func_code = XPT_PATH_INQ;
2311 xpt_action((union ccb *)&cpi);
2313 if (cpi.ccb_h.status != CAM_REQ_CMP) {
2314 if (request_ccb != NULL) {
2315 request_ccb->ccb_h.status = cpi.ccb_h.status;
2316 xpt_done(request_ccb);
2321 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
2323 * Can't scan the bus on an adapter that
2324 * cannot perform the initiator role.
2326 if (request_ccb != NULL) {
2327 request_ccb->ccb_h.status = CAM_REQ_CMP;
2328 xpt_done(request_ccb);
2333 if (request_ccb == NULL) {
2334 request_ccb = xpt_alloc_ccb_nowait();
2335 if (request_ccb == NULL) {
2336 xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
2337 "can't continue\n");
2340 status = xpt_create_path(&new_path, NULL,
2342 path->target->target_id,
2343 path->device->lun_id);
2344 if (status != CAM_REQ_CMP) {
2345 xpt_print(path, "scsi_scan_lun: can't create path, "
2346 "can't continue\n");
2347 xpt_free_ccb(request_ccb);
2350 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
2351 request_ccb->ccb_h.cbfcnp = xptscandone;
2352 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2353 request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2354 request_ccb->crcn.flags = flags;
2357 lock = (xpt_path_owned(path) == 0);
2359 xpt_path_lock(path);
2360 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
2361 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
2364 softc = (probe_softc *)old_periph->softc;
2365 TAILQ_INSERT_TAIL(&softc->request_ccbs,
2366 &request_ccb->ccb_h, periph_links.tqe);
2368 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2369 xpt_done(request_ccb);
2372 status = cam_periph_alloc(proberegister, NULL, probecleanup,
2373 probestart, "probe",
2375 request_ccb->ccb_h.path, NULL, 0,
2378 if (status != CAM_REQ_CMP) {
2379 xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
2380 "returned an error, can't continue probe\n");
2381 request_ccb->ccb_h.status = status;
2382 xpt_done(request_ccb);
2386 xpt_path_unlock(path);
2390 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
2393 xpt_free_path(done_ccb->ccb_h.path);
2394 xpt_free_ccb(done_ccb);
2397 static struct cam_ed *
2398 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
2400 struct scsi_quirk_entry *quirk;
2401 struct cam_ed *device;
2403 device = xpt_alloc_device(bus, target, lun_id);
2408 * Take the default quirk entry until we have inquiry
2409 * data and can determine a better quirk to use.
2411 quirk = &scsi_quirk_table[nitems(scsi_quirk_table) - 1];
2412 device->quirk = (void *)quirk;
2413 device->mintags = quirk->mintags;
2414 device->maxtags = quirk->maxtags;
2415 bzero(&device->inq_data, sizeof(device->inq_data));
2416 device->inq_flags = 0;
2417 device->queue_flags = 0;
2418 device->serial_num = NULL;
2419 device->serial_num_len = 0;
2420 device->device_id = NULL;
2421 device->device_id_len = 0;
2422 device->supported_vpds = NULL;
2423 device->supported_vpds_len = 0;
2428 scsi_devise_transport(struct cam_path *path)
2430 struct ccb_pathinq cpi;
2431 struct ccb_trans_settings cts;
2432 struct scsi_inquiry_data *inq_buf;
2434 /* Get transport information from the SIM */
2435 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2436 cpi.ccb_h.func_code = XPT_PATH_INQ;
2437 xpt_action((union ccb *)&cpi);
2440 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
2441 inq_buf = &path->device->inq_data;
2442 path->device->protocol = PROTO_SCSI;
2443 path->device->protocol_version =
2444 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
2445 path->device->transport = cpi.transport;
2446 path->device->transport_version = cpi.transport_version;
2449 * Any device not using SPI3 features should
2450 * be considered SPI2 or lower.
2452 if (inq_buf != NULL) {
2453 if (path->device->transport == XPORT_SPI
2454 && (inq_buf->spi3data & SID_SPI_MASK) == 0
2455 && path->device->transport_version > 2)
2456 path->device->transport_version = 2;
2458 struct cam_ed* otherdev;
2460 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
2462 otherdev = TAILQ_NEXT(otherdev, links)) {
2463 if (otherdev != path->device)
2467 if (otherdev != NULL) {
2469 * Initially assume the same versioning as
2470 * prior luns for this target.
2472 path->device->protocol_version =
2473 otherdev->protocol_version;
2474 path->device->transport_version =
2475 otherdev->transport_version;
2477 /* Until we know better, opt for safety */
2478 path->device->protocol_version = 2;
2479 if (path->device->transport == XPORT_SPI)
2480 path->device->transport_version = 2;
2482 path->device->transport_version = 0;
2488 * For a device compliant with SPC-2 we should be able
2489 * to determine the transport version supported by
2490 * scrutinizing the version descriptors in the
2494 /* Tell the controller what we think */
2495 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2496 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2497 cts.type = CTS_TYPE_CURRENT_SETTINGS;
2498 cts.transport = path->device->transport;
2499 cts.transport_version = path->device->transport_version;
2500 cts.protocol = path->device->protocol;
2501 cts.protocol_version = path->device->protocol_version;
2502 cts.proto_specific.valid = 0;
2503 cts.xport_specific.valid = 0;
2504 xpt_action((union ccb *)&cts);
2508 scsi_dev_advinfo(union ccb *start_ccb)
2510 struct cam_ed *device;
2511 struct ccb_dev_advinfo *cdai;
2514 start_ccb->ccb_h.status = CAM_REQ_INVALID;
2515 device = start_ccb->ccb_h.path->device;
2516 cdai = &start_ccb->cdai;
2517 switch(cdai->buftype) {
2518 case CDAI_TYPE_SCSI_DEVID:
2519 if (cdai->flags & CDAI_FLAG_STORE)
2521 cdai->provsiz = device->device_id_len;
2522 if (device->device_id_len == 0)
2524 amt = device->device_id_len;
2525 if (cdai->provsiz > cdai->bufsiz)
2527 memcpy(cdai->buf, device->device_id, amt);
2529 case CDAI_TYPE_SERIAL_NUM:
2530 if (cdai->flags & CDAI_FLAG_STORE)
2532 cdai->provsiz = device->serial_num_len;
2533 if (device->serial_num_len == 0)
2535 amt = device->serial_num_len;
2536 if (cdai->provsiz > cdai->bufsiz)
2538 memcpy(cdai->buf, device->serial_num, amt);
2540 case CDAI_TYPE_PHYS_PATH:
2541 if (cdai->flags & CDAI_FLAG_STORE) {
2542 if (device->physpath != NULL) {
2543 free(device->physpath, M_CAMXPT);
2544 device->physpath = NULL;
2545 device->physpath_len = 0;
2547 /* Clear existing buffer if zero length */
2548 if (cdai->bufsiz == 0)
2550 device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
2551 if (device->physpath == NULL) {
2552 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2555 device->physpath_len = cdai->bufsiz;
2556 memcpy(device->physpath, cdai->buf, cdai->bufsiz);
2558 cdai->provsiz = device->physpath_len;
2559 if (device->physpath_len == 0)
2561 amt = device->physpath_len;
2562 if (cdai->provsiz > cdai->bufsiz)
2564 memcpy(cdai->buf, device->physpath, amt);
2567 case CDAI_TYPE_RCAPLONG:
2568 if (cdai->flags & CDAI_FLAG_STORE) {
2569 if (device->rcap_buf != NULL) {
2570 free(device->rcap_buf, M_CAMXPT);
2571 device->rcap_buf = NULL;
2574 device->rcap_len = cdai->bufsiz;
2575 /* Clear existing buffer if zero length */
2576 if (cdai->bufsiz == 0)
2579 device->rcap_buf = malloc(cdai->bufsiz, M_CAMXPT,
2581 if (device->rcap_buf == NULL) {
2582 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2586 memcpy(device->rcap_buf, cdai->buf, cdai->bufsiz);
2588 cdai->provsiz = device->rcap_len;
2589 if (device->rcap_len == 0)
2591 amt = device->rcap_len;
2592 if (cdai->provsiz > cdai->bufsiz)
2594 memcpy(cdai->buf, device->rcap_buf, amt);
2597 case CDAI_TYPE_EXT_INQ:
2599 * We fetch extended inquiry data during probe, if
2600 * available. We don't allow changing it.
2602 if (cdai->flags & CDAI_FLAG_STORE)
2604 cdai->provsiz = device->ext_inq_len;
2605 if (device->ext_inq_len == 0)
2607 amt = device->ext_inq_len;
2608 if (cdai->provsiz > cdai->bufsiz)
2610 memcpy(cdai->buf, device->ext_inq, amt);
2615 start_ccb->ccb_h.status = CAM_REQ_CMP;
2617 if (cdai->flags & CDAI_FLAG_STORE) {
2618 xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
2619 (void *)(uintptr_t)cdai->buftype);
2624 scsi_action(union ccb *start_ccb)
2627 switch (start_ccb->ccb_h.func_code) {
2628 case XPT_SET_TRAN_SETTINGS:
2630 scsi_set_transfer_settings(&start_ccb->cts,
2631 start_ccb->ccb_h.path,
2632 /*async_update*/FALSE);
2637 scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2640 scsi_scan_lun(start_ccb->ccb_h.path->periph,
2641 start_ccb->ccb_h.path, start_ccb->crcn.flags,
2644 case XPT_DEV_ADVINFO:
2646 scsi_dev_advinfo(start_ccb);
2650 xpt_action_default(start_ccb);
2656 scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
2659 struct ccb_pathinq cpi;
2660 struct ccb_trans_settings cur_cts;
2661 struct ccb_trans_settings_scsi *scsi;
2662 struct ccb_trans_settings_scsi *cur_scsi;
2663 struct scsi_inquiry_data *inq_data;
2664 struct cam_ed *device;
2666 if (path == NULL || (device = path->device) == NULL) {
2667 cts->ccb_h.status = CAM_PATH_INVALID;
2668 xpt_done((union ccb *)cts);
2672 if (cts->protocol == PROTO_UNKNOWN
2673 || cts->protocol == PROTO_UNSPECIFIED) {
2674 cts->protocol = device->protocol;
2675 cts->protocol_version = device->protocol_version;
2678 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2679 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2680 cts->protocol_version = device->protocol_version;
2682 if (cts->protocol != device->protocol) {
2683 xpt_print(path, "Uninitialized Protocol %x:%x?\n",
2684 cts->protocol, device->protocol);
2685 cts->protocol = device->protocol;
2688 if (cts->protocol_version > device->protocol_version) {
2690 xpt_print(path, "Down reving Protocol "
2691 "Version from %d to %d?\n", cts->protocol_version,
2692 device->protocol_version);
2694 cts->protocol_version = device->protocol_version;
2697 if (cts->transport == XPORT_UNKNOWN
2698 || cts->transport == XPORT_UNSPECIFIED) {
2699 cts->transport = device->transport;
2700 cts->transport_version = device->transport_version;
2703 if (cts->transport_version == XPORT_VERSION_UNKNOWN
2704 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2705 cts->transport_version = device->transport_version;
2707 if (cts->transport != device->transport) {
2708 xpt_print(path, "Uninitialized Transport %x:%x?\n",
2709 cts->transport, device->transport);
2710 cts->transport = device->transport;
2713 if (cts->transport_version > device->transport_version) {
2715 xpt_print(path, "Down reving Transport "
2716 "Version from %d to %d?\n", cts->transport_version,
2717 device->transport_version);
2719 cts->transport_version = device->transport_version;
2723 * Nothing more of interest to do unless
2724 * this is a device connected via the
2727 if (cts->protocol != PROTO_SCSI) {
2728 if (async_update == FALSE)
2729 xpt_action_default((union ccb *)cts);
2733 inq_data = &device->inq_data;
2734 scsi = &cts->proto_specific.scsi;
2735 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2736 cpi.ccb_h.func_code = XPT_PATH_INQ;
2737 xpt_action((union ccb *)&cpi);
2739 /* SCSI specific sanity checking */
2740 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2741 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2742 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2743 || (device->mintags == 0)) {
2745 * Can't tag on hardware that doesn't support tags,
2746 * doesn't have it enabled, or has broken tag support.
2748 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2751 if (async_update == FALSE) {
2753 * Perform sanity checking against what the
2754 * controller and device can do.
2756 xpt_setup_ccb(&cur_cts.ccb_h, path, CAM_PRIORITY_NONE);
2757 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2758 cur_cts.type = cts->type;
2759 xpt_action((union ccb *)&cur_cts);
2760 if (cam_ccb_status((union ccb *)&cur_cts) != CAM_REQ_CMP) {
2763 cur_scsi = &cur_cts.proto_specific.scsi;
2764 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2765 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2766 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2768 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2769 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2772 /* SPI specific sanity checking */
2773 if (cts->transport == XPORT_SPI && async_update == FALSE) {
2775 struct ccb_trans_settings_spi *spi;
2776 struct ccb_trans_settings_spi *cur_spi;
2778 spi = &cts->xport_specific.spi;
2780 cur_spi = &cur_cts.xport_specific.spi;
2782 /* Fill in any gaps in what the user gave us */
2783 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2784 spi->sync_period = cur_spi->sync_period;
2785 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2786 spi->sync_period = 0;
2787 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2788 spi->sync_offset = cur_spi->sync_offset;
2789 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2790 spi->sync_offset = 0;
2791 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2792 spi->ppr_options = cur_spi->ppr_options;
2793 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2794 spi->ppr_options = 0;
2795 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2796 spi->bus_width = cur_spi->bus_width;
2797 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2799 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2800 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2801 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2803 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2804 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2805 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2806 && (inq_data->flags & SID_Sync) == 0
2807 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2808 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2810 spi->sync_period = 0;
2811 spi->sync_offset = 0;
2814 switch (spi->bus_width) {
2815 case MSG_EXT_WDTR_BUS_32_BIT:
2816 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2817 || (inq_data->flags & SID_WBus32) != 0
2818 || cts->type == CTS_TYPE_USER_SETTINGS)
2819 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2821 /* Fall Through to 16-bit */
2822 case MSG_EXT_WDTR_BUS_16_BIT:
2823 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2824 || (inq_data->flags & SID_WBus16) != 0
2825 || cts->type == CTS_TYPE_USER_SETTINGS)
2826 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2827 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2830 /* Fall Through to 8-bit */
2831 default: /* New bus width?? */
2832 case MSG_EXT_WDTR_BUS_8_BIT:
2833 /* All targets can do this */
2834 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2838 spi3caps = cpi.xport_specific.spi.ppr_options;
2839 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2840 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2841 spi3caps &= inq_data->spi3data;
2843 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2844 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2846 if ((spi3caps & SID_SPI_IUS) == 0)
2847 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2849 if ((spi3caps & SID_SPI_QAS) == 0)
2850 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2852 /* No SPI Transfer settings are allowed unless we are wide */
2853 if (spi->bus_width == 0)
2854 spi->ppr_options = 0;
2856 if ((spi->valid & CTS_SPI_VALID_DISC)
2857 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2859 * Can't tag queue without disconnection.
2861 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2862 scsi->valid |= CTS_SCSI_VALID_TQ;
2866 * If we are currently performing tagged transactions to
2867 * this device and want to change its negotiation parameters,
2868 * go non-tagged for a bit to give the controller a chance to
2869 * negotiate unhampered by tag messages.
2871 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2872 && (device->inq_flags & SID_CmdQue) != 0
2873 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2874 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2875 CTS_SPI_VALID_SYNC_OFFSET|
2876 CTS_SPI_VALID_BUS_WIDTH)) != 0)
2877 scsi_toggle_tags(path);
2880 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2881 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2885 * If we are transitioning from tags to no-tags or
2886 * vice-versa, we need to carefully freeze and restart
2887 * the queue so that we don't overlap tagged and non-tagged
2888 * commands. We also temporarily stop tags if there is
2889 * a change in transfer negotiation settings to allow
2890 * "tag-less" negotiation.
2892 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2893 || (device->inq_flags & SID_CmdQue) != 0)
2894 device_tagenb = TRUE;
2896 device_tagenb = FALSE;
2898 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2899 && device_tagenb == FALSE)
2900 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2901 && device_tagenb == TRUE)) {
2903 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2905 * Delay change to use tags until after a
2906 * few commands have gone to this device so
2907 * the controller has time to perform transfer
2908 * negotiations without tagged messages getting
2911 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2912 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2914 xpt_stop_tags(path);
2918 if (async_update == FALSE)
2919 xpt_action_default((union ccb *)cts);
2923 scsi_toggle_tags(struct cam_path *path)
2928 * Give controllers a chance to renegotiate
2929 * before starting tag operations. We
2930 * "toggle" tagged queuing off then on
2931 * which causes the tag enable command delay
2932 * counter to come into effect.
2935 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2936 || ((dev->inq_flags & SID_CmdQue) != 0
2937 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2938 struct ccb_trans_settings cts;
2940 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2941 cts.protocol = PROTO_SCSI;
2942 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2943 cts.transport = XPORT_UNSPECIFIED;
2944 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2945 cts.proto_specific.scsi.flags = 0;
2946 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2947 scsi_set_transfer_settings(&cts, path,
2948 /*async_update*/TRUE);
2949 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2950 scsi_set_transfer_settings(&cts, path,
2951 /*async_update*/TRUE);
2956 * Handle any per-device event notifications that require action by the XPT.
2959 scsi_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2960 struct cam_ed *device, void *async_arg)
2963 struct cam_path newpath;
2966 * We only need to handle events for real devices.
2968 if (target->target_id == CAM_TARGET_WILDCARD
2969 || device->lun_id == CAM_LUN_WILDCARD)
2973 * We need our own path with wildcards expanded to
2974 * handle certain types of events.
2976 if ((async_code == AC_SENT_BDR)
2977 || (async_code == AC_BUS_RESET)
2978 || (async_code == AC_INQ_CHANGED))
2979 status = xpt_compile_path(&newpath, NULL,
2984 status = CAM_REQ_CMP_ERR;
2986 if (status == CAM_REQ_CMP) {
2989 * Allow transfer negotiation to occur in a
2990 * tag free environment and after settle delay.
2992 if (async_code == AC_SENT_BDR
2993 || async_code == AC_BUS_RESET) {
2994 cam_freeze_devq(&newpath);
2995 cam_release_devq(&newpath,
2996 RELSIM_RELEASE_AFTER_TIMEOUT,
2998 /*timeout*/scsi_delay,
2999 /*getcount_only*/0);
3000 scsi_toggle_tags(&newpath);
3003 if (async_code == AC_INQ_CHANGED) {
3005 * We've sent a start unit command, or
3006 * something similar to a device that
3007 * may have caused its inquiry data to
3008 * change. So we re-scan the device to
3009 * refresh the inquiry data for it.
3011 scsi_scan_lun(newpath.periph, &newpath,
3012 CAM_EXPECT_INQ_CHANGE, NULL);
3014 xpt_release_path(&newpath);
3015 } else if (async_code == AC_LOST_DEVICE &&
3016 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
3017 device->flags |= CAM_DEV_UNCONFIGURED;
3018 xpt_release_device(device);
3019 } else if (async_code == AC_TRANSFER_NEG) {
3020 struct ccb_trans_settings *settings;
3021 struct cam_path path;
3023 settings = (struct ccb_trans_settings *)async_arg;
3024 xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
3026 scsi_set_transfer_settings(settings, &path,
3027 /*async_update*/TRUE);
3028 xpt_release_path(&path);
3033 _scsi_announce_periph(struct cam_periph *periph, u_int *speed, u_int *freq, struct ccb_trans_settings *cts)
3035 struct ccb_pathinq cpi;
3036 struct cam_path *path = periph->path;
3038 cam_periph_assert(periph, MA_OWNED);
3040 xpt_setup_ccb(&cts->ccb_h, path, CAM_PRIORITY_NORMAL);
3041 cts->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
3042 cts->type = CTS_TYPE_CURRENT_SETTINGS;
3043 xpt_action((union ccb*)cts);
3044 if (cam_ccb_status((union ccb *)cts) != CAM_REQ_CMP)
3047 /* Ask the SIM for its base transfer speed */
3048 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3049 cpi.ccb_h.func_code = XPT_PATH_INQ;
3050 xpt_action((union ccb *)&cpi);
3052 /* Report connection speed */
3053 *speed = cpi.base_transfer_speed;
3056 if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_SPI) {
3057 struct ccb_trans_settings_spi *spi =
3058 &cts->xport_specific.spi;
3060 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
3061 && spi->sync_offset != 0) {
3062 *freq = scsi_calc_syncsrate(spi->sync_period);
3065 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
3066 *speed *= (0x01 << spi->bus_width);
3068 if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_FC) {
3069 struct ccb_trans_settings_fc *fc =
3070 &cts->xport_specific.fc;
3072 if (fc->valid & CTS_FC_VALID_SPEED)
3073 *speed = fc->bitrate;
3075 if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_SAS) {
3076 struct ccb_trans_settings_sas *sas =
3077 &cts->xport_specific.sas;
3079 if (sas->valid & CTS_SAS_VALID_SPEED)
3080 *speed = sas->bitrate;
3085 scsi_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
3087 struct ccb_trans_settings cts;
3088 u_int speed, freq, mb;
3090 _scsi_announce_periph(periph, &speed, &freq, &cts);
3091 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP)
3096 sbuf_printf(sb, "%s%d: %d.%03dMB/s transfers",
3097 periph->periph_name, periph->unit_number,
3100 sbuf_printf(sb, "%s%d: %dKB/s transfers", periph->periph_name,
3101 periph->unit_number, speed);
3102 /* Report additional information about SPI connections */
3103 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3104 struct ccb_trans_settings_spi *spi;
3106 spi = &cts.xport_specific.spi;
3108 sbuf_printf(sb, " (%d.%03dMHz%s, offset %d", freq / 1000,
3110 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
3114 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
3115 && spi->bus_width > 0) {
3117 sbuf_printf(sb, ", ");
3119 sbuf_printf(sb, " (");
3121 sbuf_printf(sb, "%dbit)", 8 * (0x01 << spi->bus_width));
3122 } else if (freq != 0) {
3123 sbuf_printf(sb, ")");
3126 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3127 struct ccb_trans_settings_fc *fc;
3129 fc = &cts.xport_specific.fc;
3130 if (fc->valid & CTS_FC_VALID_WWNN)
3131 sbuf_printf(sb, " WWNN 0x%llx", (long long) fc->wwnn);
3132 if (fc->valid & CTS_FC_VALID_WWPN)
3133 sbuf_printf(sb, " WWPN 0x%llx", (long long) fc->wwpn);
3134 if (fc->valid & CTS_FC_VALID_PORT)
3135 sbuf_printf(sb, " PortID 0x%x", fc->port);
3137 sbuf_printf(sb, "\n");
3141 scsi_announce_periph(struct cam_periph *periph)
3143 struct ccb_trans_settings cts;
3144 u_int speed, freq, mb;
3146 _scsi_announce_periph(periph, &speed, &freq, &cts);
3147 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP)
3152 printf("%s%d: %d.%03dMB/s transfers",
3153 periph->periph_name, periph->unit_number,
3156 printf("%s%d: %dKB/s transfers", periph->periph_name,
3157 periph->unit_number, speed);
3158 /* Report additional information about SPI connections */
3159 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3160 struct ccb_trans_settings_spi *spi;
3162 spi = &cts.xport_specific.spi;
3164 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
3166 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
3170 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
3171 && spi->bus_width > 0) {
3177 printf("%dbit)", 8 * (0x01 << spi->bus_width));
3178 } else if (freq != 0) {
3182 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3183 struct ccb_trans_settings_fc *fc;
3185 fc = &cts.xport_specific.fc;
3186 if (fc->valid & CTS_FC_VALID_WWNN)
3187 printf(" WWNN 0x%llx", (long long) fc->wwnn);
3188 if (fc->valid & CTS_FC_VALID_WWPN)
3189 printf(" WWPN 0x%llx", (long long) fc->wwpn);
3190 if (fc->valid & CTS_FC_VALID_PORT)
3191 printf(" PortID 0x%x", fc->port);
3197 scsi_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
3199 scsi_print_inquiry_sbuf(sb, &device->inq_data);
3203 scsi_proto_announce(struct cam_ed *device)
3205 scsi_print_inquiry(&device->inq_data);
3209 scsi_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
3211 scsi_print_inquiry_short_sbuf(sb, &device->inq_data);
3215 scsi_proto_denounce(struct cam_ed *device)
3217 scsi_print_inquiry_short(&device->inq_data);
3221 scsi_proto_debug_out(union ccb *ccb)
3223 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3224 struct cam_ed *device;
3226 if (ccb->ccb_h.func_code != XPT_SCSI_IO)
3229 device = ccb->ccb_h.path->device;
3230 CAM_DEBUG(ccb->ccb_h.path,
3231 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3232 scsi_op_desc(scsiio_cdb_ptr(&ccb->csio)[0], &device->inq_data),
3233 scsi_cdb_string(scsiio_cdb_ptr(&ccb->csio), cdb_str, sizeof(cdb_str))));