2 * Implementation of the SCSI Transport
4 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
46 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_queue.h>
52 #include <cam/cam_periph.h>
53 #include <cam/cam_sim.h>
54 #include <cam/cam_xpt.h>
55 #include <cam/cam_xpt_sim.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_xpt_internal.h>
58 #include <cam/cam_debug.h>
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/scsi/scsi_message.h>
62 #include <cam/scsi/scsi_pass.h>
63 #include <machine/stdarg.h> /* for xpt_print below */
66 struct scsi_quirk_entry {
67 struct scsi_inquiry_pattern inq_pat;
69 #define CAM_QUIRK_NOLUNS 0x01
70 #define CAM_QUIRK_NOVPDS 0x02
71 #define CAM_QUIRK_HILUNS 0x04
72 #define CAM_QUIRK_NOHILUNS 0x08
73 #define CAM_QUIRK_NORPTLUNS 0x10
77 #define SCSI_QUIRK(dev) ((struct scsi_quirk_entry *)((dev)->quirk))
79 static int cam_srch_hi = 0;
80 SYSCTL_INT(_kern_cam, OID_AUTO, cam_srch_hi, CTLFLAG_RWTUN,
81 &cam_srch_hi, 0, "Search above LUN 7 for SCSI3 and greater devices");
83 #define CAM_SCSI2_MAXLUN 8
84 #define CAM_CAN_GET_SIMPLE_LUN(x, i) \
85 ((((x)->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) == \
86 RPL_LUNDATA_ATYP_PERIPH) || \
87 (((x)->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) == \
88 RPL_LUNDATA_ATYP_FLAT))
89 #define CAM_GET_SIMPLE_LUN(lp, i, lval) \
90 if (((lp)->luns[(i)].lundata[0] & RPL_LUNDATA_ATYP_MASK) == \
91 RPL_LUNDATA_ATYP_PERIPH) { \
92 (lval) = (lp)->luns[(i)].lundata[1]; \
94 (lval) = (lp)->luns[(i)].lundata[0]; \
95 (lval) &= RPL_LUNDATA_FLAT_LUN_MASK; \
97 (lval) |= (lp)->luns[(i)].lundata[1]; \
99 #define CAM_GET_LUN(lp, i, lval) \
100 (lval) = scsi_8btou64((lp)->luns[(i)].lundata); \
101 (lval) = CAM_EXTLUN_BYTE_SWIZZLE(lval);
104 * If we're not quirked to search <= the first 8 luns
105 * and we are either quirked to search above lun 8,
106 * or we're > SCSI-2 and we've enabled hilun searching,
107 * or we're > SCSI-2 and the last lun was a success,
108 * we can look for luns above lun 8.
110 #define CAN_SRCH_HI_SPARSE(dv) \
111 (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0) \
112 && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS) \
113 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
115 #define CAN_SRCH_HI_DENSE(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)))
120 static periph_init_t probe_periph_init;
122 static struct periph_driver probe_driver =
124 probe_periph_init, "probe",
125 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
129 PERIPHDRIVER_DECLARE(probe, probe_driver);
133 PROBE_INQUIRY, /* this counts as DV0 for Basic Domain Validation */
137 PROBE_SUPPORTED_VPD_LIST,
139 PROBE_EXTENDED_INQUIRY,
141 PROBE_TUR_FOR_NEGOTIATION,
142 PROBE_INQUIRY_BASIC_DV1,
143 PROBE_INQUIRY_BASIC_DV2,
149 static char *probe_action_text[] = {
152 "PROBE_FULL_INQUIRY",
155 "PROBE_SUPPORTED_VPD_LIST",
157 "PROBE_EXTENDED_INQUIRY",
159 "PROBE_TUR_FOR_NEGOTIATION",
160 "PROBE_INQUIRY_BASIC_DV1",
161 "PROBE_INQUIRY_BASIC_DV2",
167 #define PROBE_SET_ACTION(softc, newaction) \
170 text = probe_action_text; \
171 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \
172 ("Probe %s to %s\n", text[(softc)->action], \
173 text[(newaction)])); \
174 (softc)->action = (newaction); \
178 PROBE_INQUIRY_CKSUM = 0x01,
179 PROBE_NO_ANNOUNCE = 0x04,
184 TAILQ_HEAD(, ccb_hdr) request_ccbs;
189 struct cam_periph *periph;
192 static const char quantum[] = "QUANTUM";
193 static const char sony[] = "SONY";
194 static const char west_digital[] = "WDIGTL";
195 static const char samsung[] = "SAMSUNG";
196 static const char seagate[] = "SEAGATE";
197 static const char microp[] = "MICROP";
199 static struct scsi_quirk_entry scsi_quirk_table[] =
202 /* Reports QUEUE FULL for temporary resource shortages */
203 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
204 /*quirks*/0, /*mintags*/24, /*maxtags*/32
207 /* Reports QUEUE FULL for temporary resource shortages */
208 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
209 /*quirks*/0, /*mintags*/24, /*maxtags*/32
212 /* Reports QUEUE FULL for temporary resource shortages */
213 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
214 /*quirks*/0, /*mintags*/24, /*maxtags*/32
217 /* Broken tagged queuing drive */
218 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
219 /*quirks*/0, /*mintags*/0, /*maxtags*/0
222 /* Broken tagged queuing drive */
223 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
224 /*quirks*/0, /*mintags*/0, /*maxtags*/0
227 /* Broken tagged queuing drive */
228 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
229 /*quirks*/0, /*mintags*/0, /*maxtags*/0
233 * Unfortunately, the Quantum Atlas III has the same
234 * problem as the Atlas II drives above.
235 * Reported by: "Johan Granlund" <johan@granlund.nu>
237 * For future reference, the drive with the problem was:
238 * QUANTUM QM39100TD-SW N1B0
240 * It's possible that Quantum will fix the problem in later
241 * firmware revisions. If that happens, the quirk entry
242 * will need to be made specific to the firmware revisions
246 /* Reports QUEUE FULL for temporary resource shortages */
247 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
248 /*quirks*/0, /*mintags*/24, /*maxtags*/32
252 * 18 Gig Atlas III, same problem as the 9G version.
253 * Reported by: Andre Albsmeier
254 * <andre.albsmeier@mchp.siemens.de>
256 * For future reference, the drive with the problem was:
257 * QUANTUM QM318000TD-S N491
259 /* Reports QUEUE FULL for temporary resource shortages */
260 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
261 /*quirks*/0, /*mintags*/24, /*maxtags*/32
265 * Broken tagged queuing drive
266 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
267 * and: Martin Renters <martin@tdc.on.ca>
269 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
270 /*quirks*/0, /*mintags*/0, /*maxtags*/0
273 * The Seagate Medalist Pro drives have very poor write
274 * performance with anything more than 2 tags.
276 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
277 * Drive: <SEAGATE ST36530N 1444>
279 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
280 * Drive: <SEAGATE ST34520W 1281>
282 * No one has actually reported that the 9G version
283 * (ST39140*) of the Medalist Pro has the same problem, but
284 * we're assuming that it does because the 4G and 6.5G
285 * versions of the drive are broken.
288 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
289 /*quirks*/0, /*mintags*/2, /*maxtags*/2
292 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
293 /*quirks*/0, /*mintags*/2, /*maxtags*/2
296 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
297 /*quirks*/0, /*mintags*/2, /*maxtags*/2
301 * Experiences command timeouts under load with a
302 * tag count higher than 55.
304 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST3146855LW", "*"},
305 /*quirks*/0, /*mintags*/2, /*maxtags*/55
309 * Slow when tagged queueing is enabled. Write performance
310 * steadily drops off with more and more concurrent
311 * transactions. Best sequential write performance with
312 * tagged queueing turned off and write caching turned on.
315 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
316 * Drive: DCAS-34330 w/ "S65A" firmware.
318 * The drive with the problem had the "S65A" firmware
319 * revision, and has also been reported (by Stephen J.
320 * Roznowski <sjr@home.net>) for a drive with the "S61A"
323 * Although no one has reported problems with the 2 gig
324 * version of the DCAS drive, the assumption is that it
325 * has the same problems as the 4 gig version. Therefore
326 * this quirk entries disables tagged queueing for all
329 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
330 /*quirks*/0, /*mintags*/0, /*maxtags*/0
333 /* Broken tagged queuing drive */
334 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
335 /*quirks*/0, /*mintags*/0, /*maxtags*/0
338 /* Broken tagged queuing drive */
339 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
340 /*quirks*/0, /*mintags*/0, /*maxtags*/0
343 /* This does not support other than LUN 0 */
344 { T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
345 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
349 * Broken tagged queuing drive.
351 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
354 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
355 /*quirks*/0, /*mintags*/0, /*maxtags*/0
359 * Slow when tagged queueing is enabled. (1.5MB/sec versus
361 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
362 * Best performance with these drives is achieved with
363 * tagged queueing turned off, and write caching turned on.
365 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
366 /*quirks*/0, /*mintags*/0, /*maxtags*/0
370 * Slow when tagged queueing is enabled. (1.5MB/sec versus
372 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
373 * Best performance with these drives is achieved with
374 * tagged queueing turned off, and write caching turned on.
376 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
377 /*quirks*/0, /*mintags*/0, /*maxtags*/0
381 * Doesn't handle queue full condition correctly,
382 * so we need to limit maxtags to what the device
383 * can handle instead of determining this automatically.
385 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
386 /*quirks*/0, /*mintags*/2, /*maxtags*/32
389 /* Really only one LUN */
390 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
391 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
394 /* I can't believe we need a quirk for DPT volumes. */
395 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
397 /*mintags*/0, /*maxtags*/255
401 * Many Sony CDROM drives don't like multi-LUN probing.
403 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
404 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
408 * This drive doesn't like multiple LUN probing.
409 * Submitted by: Parag Patel <parag@cgt.com>
411 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
412 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
415 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
416 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
420 * The 8200 doesn't like multi-lun probing, and probably
421 * don't like serial number requests either.
424 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
427 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
431 * Let's try the same as above, but for a drive that says
432 * it's an IPL-6860 but is actually an EXB 8200.
435 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
438 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
442 * These Hitachi drives don't like multi-lun probing.
443 * The PR submitter has a DK319H, but says that the Linux
444 * kernel has a similar work-around for the DK312 and DK314,
445 * so all DK31* drives are quirked here.
447 * Submitted by: Paul Haddad <paul@pth.com>
449 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
450 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
454 * The Hitachi CJ series with J8A8 firmware apparently has
455 * problems with tagged commands.
457 * Reported by: amagai@nue.org
459 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
460 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
464 * These are the large storage arrays.
465 * Submitted by: William Carrel <william.carrel@infospace.com>
467 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
468 CAM_QUIRK_HILUNS, 2, 1024
472 * This old revision of the TDC3600 is also SCSI-1, and
473 * hangs upon serial number probing.
476 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
479 CAM_QUIRK_NOVPDS, /*mintags*/0, /*maxtags*/0
483 * Would repond to all LUNs if asked for.
486 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
489 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
493 * Would repond to all LUNs if asked for.
496 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
499 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
502 /* Submitted by: Matthew Dodd <winter@jurai.net> */
503 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
504 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
507 /* Submitted by: Matthew Dodd <winter@jurai.net> */
508 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
509 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
512 /* TeraSolutions special settings for TRC-22 RAID */
513 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
514 /*quirks*/0, /*mintags*/55, /*maxtags*/255
517 /* Veritas Storage Appliance */
518 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
519 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
523 * Would respond to all LUNs. Device type and removable
524 * flag are jumper-selectable.
526 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
529 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
532 /* EasyRAID E5A aka. areca ARC-6010 */
533 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
534 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
537 { T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
538 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
541 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Garmin", "*", "*" },
542 CAM_QUIRK_NORPTLUNS, /*mintags*/2, /*maxtags*/255
545 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic", "STORAGE DEVICE*", "120?" },
546 CAM_QUIRK_NORPTLUNS, /*mintags*/2, /*maxtags*/255
549 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic", "MassStorageClass", "1533" },
550 CAM_QUIRK_NORPTLUNS, /*mintags*/2, /*maxtags*/255
553 /* Default tagged queuing parameters for all devices */
555 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
556 /*vendor*/"*", /*product*/"*", /*revision*/"*"
558 /*quirks*/0, /*mintags*/2, /*maxtags*/255
562 static cam_status proberegister(struct cam_periph *periph,
564 static void probeschedule(struct cam_periph *probe_periph);
565 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
566 static void proberequestdefaultnegotiation(struct cam_periph *periph);
567 static int proberequestbackoff(struct cam_periph *periph,
568 struct cam_ed *device);
569 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
570 static void probe_purge_old(struct cam_path *path,
571 struct scsi_report_luns_data *new,
573 static void probecleanup(struct cam_periph *periph);
574 static void scsi_find_quirk(struct cam_ed *device);
575 static void scsi_scan_bus(struct cam_periph *periph, union ccb *ccb);
576 static void scsi_scan_lun(struct cam_periph *periph,
577 struct cam_path *path, cam_flags flags,
579 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
580 static struct cam_ed *
581 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target,
583 static void scsi_devise_transport(struct cam_path *path);
584 static void scsi_set_transfer_settings(struct ccb_trans_settings *cts,
585 struct cam_path *path,
587 static void scsi_toggle_tags(struct cam_path *path);
588 static void scsi_dev_async(uint32_t async_code,
590 struct cam_et *target,
591 struct cam_ed *device,
593 static void scsi_action(union ccb *start_ccb);
594 static void scsi_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb);
595 static void scsi_proto_announce_sbuf(struct cam_ed *device,
597 static void scsi_proto_denounce_sbuf(struct cam_ed *device,
599 static void scsi_proto_debug_out(union ccb *ccb);
600 static void _scsi_announce_periph(struct cam_periph *, u_int *, u_int *, struct ccb_trans_settings *);
602 static struct xpt_xport_ops scsi_xport_ops = {
603 .alloc_device = scsi_alloc_device,
604 .action = scsi_action,
605 .async = scsi_dev_async,
606 .announce_sbuf = scsi_announce_periph_sbuf,
608 #define SCSI_XPT_XPORT(x, X) \
609 static struct xpt_xport scsi_xport_ ## x = { \
610 .xport = XPORT_ ## X, \
612 .ops = &scsi_xport_ops, \
614 CAM_XPT_XPORT(scsi_xport_ ## x);
616 SCSI_XPT_XPORT(spi, SPI);
617 SCSI_XPT_XPORT(sas, SAS);
618 SCSI_XPT_XPORT(fc, FC);
619 SCSI_XPT_XPORT(usb, USB);
620 SCSI_XPT_XPORT(iscsi, ISCSI);
621 SCSI_XPT_XPORT(srp, SRP);
622 SCSI_XPT_XPORT(ppb, PPB);
624 #undef SCSI_XPORT_XPORT
626 static struct xpt_proto_ops scsi_proto_ops = {
627 .announce_sbuf = scsi_proto_announce_sbuf,
628 .denounce_sbuf = scsi_proto_denounce_sbuf,
629 .debug_out = scsi_proto_debug_out,
631 static struct xpt_proto scsi_proto = {
634 .ops = &scsi_proto_ops,
636 CAM_XPT_PROTO(scsi_proto);
639 probe_periph_init(void)
644 proberegister(struct cam_periph *periph, void *arg)
646 union ccb *request_ccb; /* CCB representing the probe request */
649 request_ccb = (union ccb *)arg;
650 if (request_ccb == NULL) {
651 printf("proberegister: no probe CCB, "
652 "can't register device\n");
653 return(CAM_REQ_CMP_ERR);
656 softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
659 printf("proberegister: Unable to probe new device. "
660 "Unable to allocate softc\n");
661 return(CAM_REQ_CMP_ERR);
663 TAILQ_INIT(&softc->request_ccbs);
664 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
667 periph->softc = softc;
668 softc->periph = periph;
669 softc->action = PROBE_INVALID;
670 if (cam_periph_acquire(periph) != 0)
671 return (CAM_REQ_CMP_ERR);
673 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
674 scsi_devise_transport(periph->path);
677 * Ensure we've waited at least a bus settle
678 * delay before attempting to probe the device.
679 * For HBAs that don't do bus resets, this won't make a difference.
681 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
683 probeschedule(periph);
688 probeschedule(struct cam_periph *periph)
690 struct ccb_pathinq cpi;
694 softc = (probe_softc *)periph->softc;
695 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
697 xpt_path_inq(&cpi, periph->path);
700 * If a device has gone away and another device, or the same one,
701 * is back in the same place, it should have a unit attention
702 * condition pending. It will not report the unit attention in
703 * response to an inquiry, which may leave invalid transfer
704 * negotiations in effect. The TUR will reveal the unit attention
705 * condition. Only send the TUR for lun 0, since some devices
706 * will get confused by commands other than inquiry to non-existent
707 * luns. If you think a device has gone away start your scan from
708 * lun 0. This will insure that any bogus transfer settings are
711 * If we haven't seen the device before and the controller supports
712 * some kind of transfer negotiation, negotiate with the first
713 * sent command if no bus reset was performed at startup. This
714 * ensures that the device is not confused by transfer negotiation
715 * settings left over by loader or BIOS action.
717 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
718 && (ccb->ccb_h.target_lun == 0)) {
719 PROBE_SET_ACTION(softc, PROBE_TUR);
720 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
721 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
722 proberequestdefaultnegotiation(periph);
723 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
725 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
728 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
729 softc->flags |= PROBE_NO_ANNOUNCE;
731 softc->flags &= ~PROBE_NO_ANNOUNCE;
733 if (cpi.hba_misc & PIM_EXTLUNS)
734 softc->flags |= PROBE_EXTLUN;
736 softc->flags &= ~PROBE_EXTLUN;
738 xpt_schedule(periph, CAM_PRIORITY_XPT);
742 probestart(struct cam_periph *periph, union ccb *start_ccb)
744 /* Probe the device that our peripheral driver points to */
745 struct ccb_scsiio *csio;
748 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
750 softc = (probe_softc *)periph->softc;
751 csio = &start_ccb->csio;
754 switch (softc->action) {
756 case PROBE_TUR_FOR_NEGOTIATION:
759 scsi_test_unit_ready(csio,
768 case PROBE_FULL_INQUIRY:
771 struct scsi_inquiry_data *inq_buf;
773 inq_buf = &periph->path->device->inq_data;
776 * If the device is currently configured, we calculate an
777 * MD5 checksum of the inquiry data, and if the serial number
778 * length is greater than 0, add the serial number data
779 * into the checksum as well. Once the inquiry and the
780 * serial number check finish, we attempt to figure out
781 * whether we still have the same device.
783 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
784 softc->flags &= ~PROBE_INQUIRY_CKSUM;
785 } else if ((softc->flags & PROBE_INQUIRY_CKSUM) == 0) {
786 MD5Init(&softc->context);
787 MD5Update(&softc->context, (unsigned char *)inq_buf,
788 sizeof(struct scsi_inquiry_data));
789 if (periph->path->device->serial_num_len > 0) {
790 MD5Update(&softc->context,
791 periph->path->device->serial_num,
792 periph->path->device->serial_num_len);
794 MD5Final(softc->digest, &softc->context);
795 softc->flags |= PROBE_INQUIRY_CKSUM;
798 if (softc->action == PROBE_INQUIRY)
799 inquiry_len = SHORT_INQUIRY_LENGTH;
801 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
804 * Some parallel SCSI devices fail to send an
805 * ignore wide residue message when dealing with
806 * odd length inquiry requests. Round up to be
809 inquiry_len = roundup2(inquiry_len, 2);
820 /*timeout*/60 * 1000);
823 case PROBE_REPORT_LUNS:
827 rp = malloc(periph->path->target->rpl_size,
828 M_CAMXPT, M_NOWAIT | M_ZERO);
830 struct scsi_inquiry_data *inq_buf;
831 inq_buf = &periph->path->device->inq_data;
832 xpt_print(periph->path,
833 "Unable to alloc report luns storage\n");
834 if (INQ_DATA_TQ_ENABLED(inq_buf))
835 PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
837 PROBE_SET_ACTION(softc,
838 PROBE_SUPPORTED_VPD_LIST);
841 scsi_report_luns(csio, 5, probedone, MSG_SIMPLE_Q_TAG,
842 RPL_REPORT_DEFAULT, rp, periph->path->target->rpl_size,
843 SSD_FULL_SIZE, 60000);
846 case PROBE_MODE_SENSE:
851 mode_buf_len = sizeof(struct scsi_mode_header_6)
852 + sizeof(struct scsi_mode_blk_desc)
853 + sizeof(struct scsi_control_page);
854 mode_buf = malloc(mode_buf_len, M_CAMXPT, M_NOWAIT);
855 if (mode_buf != NULL) {
856 scsi_mode_sense(csio,
861 SMS_PAGE_CTRL_CURRENT,
862 SMS_CONTROL_MODE_PAGE,
869 xpt_print(periph->path, "Unable to mode sense control page - "
871 PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
874 case PROBE_SUPPORTED_VPD_LIST:
876 struct scsi_vpd_supported_page_list *vpd_list;
877 struct cam_ed *device;
880 device = periph->path->device;
882 if ((SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOVPDS) == 0)
883 vpd_list = malloc(sizeof(*vpd_list), M_CAMXPT,
886 if (vpd_list != NULL) {
894 SVPD_SUPPORTED_PAGE_LIST,
896 /*timeout*/60 * 1000);
901 * We'll have to do without, let our probedone
902 * routine finish up for us.
904 start_ccb->csio.data_ptr = NULL;
905 cam_freeze_devq(periph->path);
906 cam_periph_doacquire(periph);
907 probedone(periph, start_ccb);
910 case PROBE_DEVICE_ID:
912 struct scsi_vpd_device_id *devid;
915 if (scsi_vpd_supported_page(periph, SVPD_DEVICE_ID))
916 devid = malloc(SVPD_DEVICE_ID_MAX_SIZE, M_CAMXPT,
925 SVPD_DEVICE_ID_MAX_SIZE,
929 /*timeout*/60 * 1000);
934 case PROBE_EXTENDED_INQUIRY:
936 struct scsi_vpd_extended_inquiry_data *ext_inq;
939 if (scsi_vpd_supported_page(periph, SVPD_EXTENDED_INQUIRY_DATA))
940 ext_inq = malloc(sizeof(*ext_inq), M_CAMXPT,
943 if (ext_inq != NULL) {
951 SVPD_EXTENDED_INQUIRY_DATA,
953 /*timeout*/60 * 1000);
957 * We'll have to do without, let our probedone
958 * routine finish up for us.
962 case PROBE_SERIAL_NUM:
964 struct scsi_vpd_unit_serial_number *serial_buf;
965 struct cam_ed* device;
968 device = periph->path->device;
969 if (device->serial_num != NULL) {
970 free(device->serial_num, M_CAMXPT);
971 device->serial_num = NULL;
972 device->serial_num_len = 0;
975 if (scsi_vpd_supported_page(periph, SVPD_UNIT_SERIAL_NUMBER))
976 serial_buf = (struct scsi_vpd_unit_serial_number *)
977 malloc(sizeof(*serial_buf), M_CAMXPT,
980 if (serial_buf != NULL) {
985 (uint8_t *)serial_buf,
988 SVPD_UNIT_SERIAL_NUMBER,
990 /*timeout*/60 * 1000);
995 case PROBE_INQUIRY_BASIC_DV1:
996 case PROBE_INQUIRY_BASIC_DV2:
999 struct scsi_inquiry_data *inq_buf;
1001 inq_buf = &periph->path->device->inq_data;
1002 inquiry_len = roundup2(SID_ADDITIONAL_LENGTH(inq_buf), 2);
1003 inq_buf = malloc(inquiry_len, M_CAMXPT, M_NOWAIT);
1004 if (inq_buf == NULL) {
1005 xpt_print(periph->path, "malloc failure- skipping Basic"
1006 "Domain Validation\n");
1007 PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1008 scsi_test_unit_ready(csio,
1026 /*timeout*/60 * 1000);
1030 panic("probestart: invalid action state 0x%x\n", softc->action);
1032 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1033 cam_periph_doacquire(periph);
1034 xpt_action(start_ccb);
1038 proberequestdefaultnegotiation(struct cam_periph *periph)
1040 struct ccb_trans_settings cts;
1042 memset(&cts, 0, sizeof(cts));
1043 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1044 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1045 cts.type = CTS_TYPE_USER_SETTINGS;
1046 xpt_action((union ccb *)&cts);
1047 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1050 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1051 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1052 xpt_action((union ccb *)&cts);
1056 * Backoff Negotiation Code- only pertinent for SPI devices.
1059 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
1061 struct ccb_trans_settings cts;
1062 struct ccb_trans_settings_spi *spi;
1064 memset(&cts, 0, sizeof (cts));
1065 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1066 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1067 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1068 xpt_action((union ccb *)&cts);
1069 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1071 xpt_print(periph->path,
1072 "failed to get current device settings\n");
1076 if (cts.transport != XPORT_SPI) {
1078 xpt_print(periph->path, "not SPI transport\n");
1082 spi = &cts.xport_specific.spi;
1085 * We cannot renegotiate sync rate if we don't have one.
1087 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
1089 xpt_print(periph->path, "no sync rate known\n");
1095 * We'll assert that we don't have to touch PPR options- the
1096 * SIM will see what we do with period and offset and adjust
1097 * the PPR options as appropriate.
1101 * A sync rate with unknown or zero offset is nonsensical.
1102 * A sync period of zero means Async.
1104 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
1105 || spi->sync_offset == 0 || spi->sync_period == 0) {
1107 xpt_print(periph->path, "no sync rate available\n");
1112 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
1113 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1114 ("hit async: giving up on DV\n"));
1119 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
1120 * We don't try to remember 'last' settings to see if the SIM actually
1121 * gets into the speed we want to set. We check on the SIM telling
1122 * us that a requested speed is bad, but otherwise don't try and
1123 * check the speed due to the asynchronous and handshake nature
1126 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
1129 if (spi->sync_period >= 0xf) {
1130 spi->sync_period = 0;
1131 spi->sync_offset = 0;
1132 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1133 ("setting to async for DV\n"));
1135 * Once we hit async, we don't want to try
1136 * any more settings.
1138 device->flags |= CAM_DEV_DV_HIT_BOTTOM;
1139 } else if (bootverbose) {
1140 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1141 ("DV: period 0x%x\n", spi->sync_period));
1142 printf("setting period to 0x%x\n", spi->sync_period);
1144 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1145 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1146 xpt_action((union ccb *)&cts);
1147 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1150 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1151 ("DV: failed to set period 0x%x\n", spi->sync_period));
1152 if (spi->sync_period == 0) {
1159 #define CCB_COMPLETED_OK(ccb) (((ccb).status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1162 probedone(struct cam_periph *periph, union ccb *done_ccb)
1165 struct cam_path *path;
1166 struct scsi_inquiry_data *inq_buf;
1169 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
1171 softc = (probe_softc *)periph->softc;
1172 path = done_ccb->ccb_h.path;
1173 priority = done_ccb->ccb_h.pinfo.priority;
1174 cam_periph_assert(periph, MA_OWNED);
1176 switch (softc->action) {
1179 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1180 if (cam_periph_error(done_ccb, 0, SF_NO_PRINT) ==
1183 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1184 cam_release_devq(path, 0, 0, 0, FALSE);
1187 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1188 /* Don't wedge the queue */
1189 xpt_release_devq(done_ccb->ccb_h.path,
1193 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1194 xpt_release_ccb(done_ccb);
1195 xpt_schedule(periph, priority);
1197 /* Drop freeze taken due to CAM_DEV_QFREEZE and release. */
1198 cam_release_devq(path, 0, 0, 0, FALSE);
1199 cam_periph_release_locked(periph);
1203 case PROBE_FULL_INQUIRY:
1205 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1206 uint8_t periph_qual;
1208 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1209 scsi_find_quirk(path->device);
1210 inq_buf = &path->device->inq_data;
1212 periph_qual = SID_QUAL(inq_buf);
1214 if (periph_qual == SID_QUAL_LU_CONNECTED ||
1215 periph_qual == SID_QUAL_LU_OFFLINE) {
1217 * We conservatively request only
1218 * SHORT_INQUIRY_LEN bytes of inquiry
1219 * information during our first try
1220 * at sending an INQUIRY. If the device
1221 * has more information to give,
1222 * perform a second request specifying
1223 * the amount of information the device
1224 * is willing to give.
1226 if (softc->action == PROBE_INQUIRY
1227 && SID_ADDITIONAL_LENGTH(inq_buf)
1228 > SHORT_INQUIRY_LENGTH) {
1229 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1230 xpt_release_ccb(done_ccb);
1231 xpt_schedule(periph, priority);
1235 scsi_devise_transport(path);
1237 if (path->device->lun_id == 0 &&
1238 SID_ANSI_REV(inq_buf) > SCSI_REV_SPC2 &&
1239 (SCSI_QUIRK(path->device)->quirks &
1240 CAM_QUIRK_NORPTLUNS) == 0) {
1241 PROBE_SET_ACTION(softc,
1244 * Start with room for *one* lun.
1246 periph->path->target->rpl_size = 16;
1247 } else if (INQ_DATA_TQ_ENABLED(inq_buf))
1248 PROBE_SET_ACTION(softc,
1251 PROBE_SET_ACTION(softc,
1252 PROBE_SUPPORTED_VPD_LIST);
1254 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1255 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1256 xpt_acquire_device(path->device);
1258 xpt_release_ccb(done_ccb);
1259 xpt_schedule(periph, priority);
1261 } else if (path->device->lun_id == 0 &&
1262 SID_ANSI_REV(inq_buf) >= SCSI_REV_SPC2 &&
1263 (SCSI_QUIRK(path->device)->quirks &
1264 CAM_QUIRK_NORPTLUNS) == 0) {
1265 PROBE_SET_ACTION(softc, PROBE_REPORT_LUNS);
1266 periph->path->target->rpl_size = 16;
1267 xpt_release_ccb(done_ccb);
1268 xpt_schedule(periph, priority);
1271 } else if (cam_periph_error(done_ccb, 0,
1272 done_ccb->ccb_h.target_lun > 0
1273 ? SF_RETRY_UA|SF_QUIET_IR
1274 : SF_RETRY_UA) == ERESTART) {
1277 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1278 /* Don't wedge the queue */
1279 xpt_release_devq(done_ccb->ccb_h.path,
1280 /*count*/1, /*run_queue*/TRUE);
1282 path->device->flags &= ~CAM_DEV_INQUIRY_DATA_VALID;
1285 * If we get to this point, we got an error status back
1286 * from the inquiry and the error status doesn't require
1287 * automatically retrying the command. Therefore, the
1288 * inquiry failed. If we had inquiry information before
1289 * for this device, but this latest inquiry command failed,
1290 * the device has probably gone away. If this device isn't
1291 * already marked unconfigured, notify the peripheral
1292 * drivers that this device is no more.
1294 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
1295 /* Send the async notification. */
1296 xpt_async(AC_LOST_DEVICE, path, NULL);
1297 PROBE_SET_ACTION(softc, PROBE_INVALID);
1299 xpt_release_ccb(done_ccb);
1302 case PROBE_REPORT_LUNS:
1304 struct ccb_scsiio *csio;
1305 struct scsi_report_luns_data *lp;
1308 csio = &done_ccb->csio;
1310 lp = (struct scsi_report_luns_data *)csio->data_ptr;
1311 nlun = scsi_4btoul(lp->length) / 8;
1312 maxlun = (csio->dxfer_len / 8) - 1;
1314 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1315 if (cam_periph_error(done_ccb, 0,
1316 done_ccb->ccb_h.target_lun > 0 ?
1317 SF_RETRY_UA|SF_QUIET_IR : SF_RETRY_UA) ==
1321 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1322 xpt_release_devq(done_ccb->ccb_h.path, 1,
1327 } else if (nlun > maxlun) {
1329 * Reallocate and retry to cover all luns
1331 CAM_DEBUG(path, CAM_DEBUG_PROBE,
1332 ("Probe: reallocating REPORT_LUNS for %u luns\n",
1335 path->target->rpl_size = (nlun << 3) + 8;
1336 xpt_release_ccb(done_ccb);
1337 xpt_schedule(periph, priority);
1339 } else if (nlun == 0) {
1341 * If there don't appear to be any luns, bail.
1349 CAM_DEBUG(path, CAM_DEBUG_PROBE,
1350 ("Probe: %u lun(s) reported\n", nlun));
1352 CAM_GET_LUN(lp, 0, lun);
1354 * If the first lun is not lun 0, then either there
1355 * is no lun 0 in the list, or the list is unsorted.
1358 for (idx = 0; idx < nlun; idx++) {
1359 CAM_GET_LUN(lp, idx, lun);
1367 lp->luns[0].lundata, 8);
1368 memcpy(lp->luns[0].lundata,
1369 lp->luns[idx].lundata, 8);
1370 memcpy(lp->luns[idx].lundata,
1372 CAM_DEBUG(path, CAM_DEBUG_PROBE,
1373 ("lun 0 in position %u\n", idx));
1377 * If we have an old lun list, We can either
1378 * retest luns that appear to have been dropped,
1379 * or just nuke them. We'll opt for the latter.
1380 * This function will also install the new list
1381 * in the target structure.
1383 probe_purge_old(path, lp, softc->flags);
1386 /* The processing above should either exit via a `goto
1387 * out` or leave the `lp` variable `NULL` and (if
1388 * applicable) `free()` the storage to which it had
1389 * pointed. Assert here that is the case.
1391 KASSERT(lp == NULL, ("%s: lp is not NULL", __func__));
1392 inq_buf = &path->device->inq_data;
1393 if (path->device->flags & CAM_DEV_INQUIRY_DATA_VALID &&
1394 (SID_QUAL(inq_buf) == SID_QUAL_LU_CONNECTED ||
1395 SID_QUAL(inq_buf) == SID_QUAL_LU_OFFLINE)) {
1396 if (INQ_DATA_TQ_ENABLED(inq_buf))
1397 PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
1399 PROBE_SET_ACTION(softc,
1400 PROBE_SUPPORTED_VPD_LIST);
1401 xpt_release_ccb(done_ccb);
1402 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 = ((uint8_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)) {
1594 path->device->serial_num =
1595 (uint8_t *)malloc((serial_buf->length + 1),
1596 M_CAMXPT, M_NOWAIT);
1597 if (path->device->serial_num != NULL) {
1600 start = strspn(serial_buf->serial_num, " ");
1601 slen = serial_buf->length - start;
1604 * SPC5r05 says that an all-space serial
1605 * number means no product serial number
1611 * In apparent violation of the spec, some
1612 * devices pad their serial numbers with
1613 * trailing spaces. Remove them.
1616 serial_buf->serial_num[start + slen - 1] == ' ')
1618 memcpy(path->device->serial_num,
1619 &serial_buf->serial_num[start], slen);
1620 path->device->serial_num_len = slen;
1621 path->device->serial_num[slen] = '\0';
1623 } else if (cam_periph_error(done_ccb, 0,
1624 SF_RETRY_UA|SF_NO_PRINT) == ERESTART) {
1626 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1627 /* Don't wedge the queue */
1628 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1633 * Let's see if we have seen this device before.
1635 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
1642 (unsigned char *)&path->device->inq_data,
1643 sizeof(struct scsi_inquiry_data));
1646 MD5Update(&context, path->device->serial_num,
1647 path->device->serial_num_len);
1649 MD5Final(digest, &context);
1650 if (bcmp(softc->digest, digest, 16) == 0)
1654 * XXX Do we need to do a TUR in order to ensure
1655 * that the device really hasn't changed???
1658 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
1659 xpt_async(AC_LOST_DEVICE, path, NULL);
1661 if (serial_buf != NULL)
1662 free(serial_buf, M_CAMXPT);
1666 * Now that we have all the necessary
1667 * information to safely perform transfer
1668 * negotiations... Controllers don't perform
1669 * any negotiation or tagged queuing until
1670 * after the first XPT_SET_TRAN_SETTINGS ccb is
1671 * received. So, on a new device, just retrieve
1672 * the user settings, and set them as the current
1673 * settings to set the device up.
1675 proberequestdefaultnegotiation(periph);
1676 xpt_release_ccb(done_ccb);
1679 * Perform a TUR to allow the controller to
1680 * perform any necessary transfer negotiation.
1682 PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1683 xpt_schedule(periph, priority);
1686 xpt_release_ccb(done_ccb);
1689 case PROBE_TUR_FOR_NEGOTIATION:
1691 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1692 if (cam_periph_error(done_ccb, 0, SF_NO_PRINT |
1693 SF_NO_RECOVERY | SF_NO_RETRY) == ERESTART)
1696 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1697 /* Don't wedge the queue */
1698 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1702 * Do Domain Validation for lun 0 on devices that claim
1703 * to support Synchronous Transfer modes.
1705 if (softc->action == PROBE_TUR_FOR_NEGOTIATION
1706 && done_ccb->ccb_h.target_lun == 0
1707 && (path->device->inq_data.flags & SID_Sync) != 0
1708 && (path->device->flags & CAM_DEV_IN_DV) == 0) {
1709 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1710 ("Begin Domain Validation\n"));
1711 path->device->flags |= CAM_DEV_IN_DV;
1712 xpt_release_ccb(done_ccb);
1713 PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
1714 xpt_schedule(periph, priority);
1717 if (softc->action == PROBE_DV_EXIT) {
1718 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1719 ("Leave Domain Validation\n"));
1721 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1722 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1723 xpt_acquire_device(path->device);
1725 path->device->flags &=
1726 ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1727 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1728 /* Inform the XPT that a new device has been found */
1729 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1730 xpt_action(done_ccb);
1731 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1734 PROBE_SET_ACTION(softc, PROBE_DONE);
1735 xpt_release_ccb(done_ccb);
1737 case PROBE_INQUIRY_BASIC_DV1:
1738 case PROBE_INQUIRY_BASIC_DV2:
1740 struct scsi_inquiry_data *nbuf;
1741 struct ccb_scsiio *csio;
1743 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1744 if (cam_periph_error(done_ccb, 0, SF_NO_PRINT |
1745 SF_NO_RECOVERY | SF_NO_RETRY) == ERESTART)
1748 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1749 /* Don't wedge the queue */
1750 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1753 csio = &done_ccb->csio;
1754 nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
1755 if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
1757 "inquiry data fails comparison at DV%d step\n",
1758 softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
1759 if (proberequestbackoff(periph, path->device)) {
1760 path->device->flags &= ~CAM_DEV_IN_DV;
1761 PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1764 PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1766 free(nbuf, M_CAMXPT);
1767 xpt_release_ccb(done_ccb);
1768 xpt_schedule(periph, priority);
1771 free(nbuf, M_CAMXPT);
1772 if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
1773 PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
1774 xpt_release_ccb(done_ccb);
1775 xpt_schedule(periph, priority);
1778 if (softc->action == PROBE_INQUIRY_BASIC_DV2) {
1779 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1780 ("Leave Domain Validation Successfully\n"));
1782 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1783 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1784 xpt_acquire_device(path->device);
1786 path->device->flags &=
1787 ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1788 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1789 /* Inform the XPT that a new device has been found */
1790 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1791 xpt_action(done_ccb);
1792 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1795 PROBE_SET_ACTION(softc, PROBE_DONE);
1796 xpt_release_ccb(done_ccb);
1800 panic("probedone: invalid action state 0x%x\n", softc->action);
1802 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
1803 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
1804 done_ccb->ccb_h.status = CAM_REQ_CMP;
1806 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
1807 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1808 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1809 cam_release_devq(path, 0, 0, 0, FALSE);
1810 cam_periph_release_locked(periph);
1811 cam_periph_invalidate(periph);
1812 cam_periph_release_locked(periph);
1814 probeschedule(periph);
1820 probe_purge_old(struct cam_path *path, struct scsi_report_luns_data *new,
1823 struct cam_path *tp;
1824 struct scsi_report_luns_data *old;
1825 u_int idx1, idx2, nlun_old, nlun_new;
1829 if (path->target == NULL) {
1832 mtx_lock(&path->target->luns_mtx);
1833 old = path->target->luns;
1834 path->target->luns = new;
1835 mtx_unlock(&path->target->luns_mtx);
1838 nlun_old = scsi_4btoul(old->length) / 8;
1839 nlun_new = scsi_4btoul(new->length) / 8;
1842 * We are not going to assume sorted lists. Deal.
1844 for (idx1 = 0; idx1 < nlun_old; idx1++) {
1845 ol = old->luns[idx1].lundata;
1846 for (idx2 = 0; idx2 < nlun_new; idx2++) {
1847 nl = new->luns[idx2].lundata;
1848 if (memcmp(nl, ol, 8) == 0) {
1852 if (idx2 < nlun_new) {
1856 * An 'old' item not in the 'new' list.
1857 * Nuke it. Except that if it is lun 0,
1858 * that would be what the probe state
1859 * machine is currently working on,
1860 * so we won't do that.
1862 CAM_GET_LUN(old, idx1, this_lun);
1863 if (this_lun == 0) {
1868 * We also cannot nuke it if it is
1869 * not in a lun format we understand
1870 * and replace the LUN with a "simple" LUN
1871 * if that is all the HBA supports.
1873 if (!(flags & PROBE_EXTLUN)) {
1874 if (!CAM_CAN_GET_SIMPLE_LUN(old, idx1))
1876 CAM_GET_SIMPLE_LUN(old, idx1, this_lun);
1879 if (xpt_create_path(&tp, NULL, xpt_path_path_id(path),
1880 xpt_path_target_id(path), this_lun) == CAM_REQ_CMP) {
1881 xpt_async(AC_LOST_DEVICE, tp, NULL);
1885 free(old, M_CAMXPT);
1889 probecleanup(struct cam_periph *periph)
1891 free(periph->softc, M_CAMXPT);
1895 scsi_find_quirk(struct cam_ed *device)
1897 struct scsi_quirk_entry *quirk;
1900 match = cam_quirkmatch((caddr_t)&device->inq_data,
1901 (caddr_t)scsi_quirk_table,
1902 nitems(scsi_quirk_table),
1903 sizeof(*scsi_quirk_table), scsi_inquiry_match);
1906 panic("xpt_find_quirk: device didn't match wildcard entry!!");
1908 quirk = (struct scsi_quirk_entry *)match;
1909 device->quirk = quirk;
1910 device->mintags = quirk->mintags;
1911 device->maxtags = quirk->maxtags;
1915 union ccb *request_ccb;
1916 struct ccb_pathinq *cpi;
1919 } scsi_scan_bus_info;
1922 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1923 * As the scan progresses, scsi_scan_bus is used as the
1924 * callback on completion function.
1927 scsi_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1931 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1932 ("scsi_scan_bus\n"));
1933 switch (request_ccb->ccb_h.func_code) {
1937 scsi_scan_bus_info *scan_info;
1938 union ccb *work_ccb, *reset_ccb;
1939 struct cam_path *path;
1941 u_int low_target, max_target;
1944 /* Find out the characteristics of the bus */
1945 work_ccb = xpt_alloc_ccb_nowait();
1946 if (work_ccb == NULL) {
1947 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1948 xpt_done(request_ccb);
1951 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1952 request_ccb->ccb_h.pinfo.priority);
1953 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1954 xpt_action(work_ccb);
1955 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1956 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1957 xpt_free_ccb(work_ccb);
1958 xpt_done(request_ccb);
1962 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1964 * Can't scan the bus on an adapter that
1965 * cannot perform the initiator role.
1967 request_ccb->ccb_h.status = CAM_REQ_CMP;
1968 xpt_free_ccb(work_ccb);
1969 xpt_done(request_ccb);
1973 /* We may need to reset bus first, if we haven't done it yet. */
1974 if ((work_ccb->cpi.hba_inquiry &
1975 (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1976 !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1977 !timevalisset(&request_ccb->ccb_h.path->bus->last_reset) &&
1978 (reset_ccb = xpt_alloc_ccb_nowait()) != NULL) {
1979 xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1981 reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1982 xpt_action(reset_ccb);
1983 if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1984 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1985 xpt_free_ccb(reset_ccb);
1986 xpt_free_ccb(work_ccb);
1987 xpt_done(request_ccb);
1990 xpt_free_ccb(reset_ccb);
1993 /* Save some state for use while we probe for devices */
1994 scan_info = (scsi_scan_bus_info *) malloc(sizeof(scsi_scan_bus_info) +
1995 (work_ccb->cpi.max_target * sizeof (u_int)), M_CAMXPT, M_ZERO|M_NOWAIT);
1996 if (scan_info == NULL) {
1997 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1998 xpt_free_ccb(work_ccb);
1999 xpt_done(request_ccb);
2002 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2003 ("SCAN start for %p\n", scan_info));
2004 scan_info->request_ccb = request_ccb;
2005 scan_info->cpi = &work_ccb->cpi;
2007 /* Cache on our stack so we can work asynchronously */
2008 max_target = scan_info->cpi->max_target;
2010 initiator_id = scan_info->cpi->initiator_id;
2013 * We can scan all targets in parallel, or do it sequentially.
2016 if (request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2017 max_target = low_target = request_ccb->ccb_h.target_id;
2018 scan_info->counter = 0;
2019 } else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2021 scan_info->counter = 0;
2023 scan_info->counter = scan_info->cpi->max_target + 1;
2024 if (scan_info->cpi->initiator_id < scan_info->counter) {
2025 scan_info->counter--;
2028 mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2031 for (i = low_target; i <= max_target; i++) {
2033 if (i == initiator_id)
2036 status = xpt_create_path(&path, NULL,
2037 request_ccb->ccb_h.path_id,
2039 if (status != CAM_REQ_CMP) {
2040 printf("scsi_scan_bus: xpt_create_path failed"
2041 " with status %#x, bus scan halted\n",
2043 free(scan_info, M_CAMXPT);
2044 request_ccb->ccb_h.status = status;
2045 xpt_free_ccb(work_ccb);
2046 xpt_done(request_ccb);
2049 work_ccb = xpt_alloc_ccb_nowait();
2050 if (work_ccb == NULL) {
2051 xpt_free_ccb((union ccb *)scan_info->cpi);
2052 free(scan_info, M_CAMXPT);
2053 xpt_free_path(path);
2054 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2055 xpt_done(request_ccb);
2058 xpt_setup_ccb(&work_ccb->ccb_h, path,
2059 request_ccb->ccb_h.pinfo.priority);
2060 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2061 work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2062 work_ccb->ccb_h.flags |= CAM_UNLOCKED;
2063 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
2064 work_ccb->crcn.flags = request_ccb->crcn.flags;
2065 xpt_action(work_ccb);
2074 struct cam_path *path, *oldpath;
2075 scsi_scan_bus_info *scan_info;
2076 struct cam_et *target;
2077 struct cam_ed *device, *nextdev;
2080 target_id_t target_id;
2083 oldpath = request_ccb->ccb_h.path;
2085 status = cam_ccb_status(request_ccb);
2086 scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
2087 path_id = request_ccb->ccb_h.path_id;
2088 target_id = request_ccb->ccb_h.target_id;
2089 lun_id = request_ccb->ccb_h.target_lun;
2090 target = request_ccb->ccb_h.path->target;
2093 mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2095 mtx_lock(&target->luns_mtx);
2098 u_int nluns = scsi_4btoul(target->luns->length) / 8;
2101 * Make sure we skip over lun 0 if it's the first member
2102 * of the list as we've actually just finished probing
2105 CAM_GET_LUN(target->luns, 0, first);
2106 if (first == 0 && scan_info->lunindex[target_id] == 0) {
2107 scan_info->lunindex[target_id]++;
2111 * Skip any LUNs that the HBA can't deal with.
2113 while (scan_info->lunindex[target_id] < nluns) {
2114 if (scan_info->cpi->hba_misc & PIM_EXTLUNS) {
2115 CAM_GET_LUN(target->luns,
2116 scan_info->lunindex[target_id],
2121 if (CAM_CAN_GET_SIMPLE_LUN(target->luns,
2122 scan_info->lunindex[target_id])) {
2123 CAM_GET_SIMPLE_LUN(target->luns,
2124 scan_info->lunindex[target_id],
2129 scan_info->lunindex[target_id]++;
2132 if (scan_info->lunindex[target_id] < nluns) {
2133 mtx_unlock(&target->luns_mtx);
2135 CAM_DEBUG(request_ccb->ccb_h.path,
2137 ("next lun to try at index %u is %jx\n",
2138 scan_info->lunindex[target_id],
2139 (uintmax_t)lun_id));
2140 scan_info->lunindex[target_id]++;
2142 mtx_unlock(&target->luns_mtx);
2143 /* We're done with scanning all luns. */
2146 mtx_unlock(&target->luns_mtx);
2147 device = request_ccb->ccb_h.path->device;
2148 /* Continue sequential LUN scan if: */
2149 /* -- we have more LUNs that need recheck */
2150 mtx_lock(&target->bus->eb_mtx);
2152 while ((nextdev = TAILQ_NEXT(nextdev, links)) != NULL)
2153 if ((nextdev->flags & CAM_DEV_UNCONFIGURED) == 0)
2155 mtx_unlock(&target->bus->eb_mtx);
2156 if (nextdev != NULL) {
2158 /* -- stop if CAM_QUIRK_NOLUNS is set. */
2159 } else if (SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOLUNS) {
2161 /* -- this LUN is connected and its SCSI version
2162 * allows more LUNs. */
2163 } else if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2164 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2165 CAN_SRCH_HI_DENSE(device))
2167 /* -- this LUN is disconnected, its SCSI version
2168 * allows more LUNs and we guess they may be. */
2169 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) {
2170 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2171 CAN_SRCH_HI_SPARSE(device))
2174 if (next_target == 0) {
2176 if (lun_id > scan_info->cpi->max_lun)
2182 * Check to see if we scan any further luns.
2188 * Free the current request path- we're done with it.
2190 xpt_free_path(oldpath);
2193 if (scan_info->request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2195 } else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2196 scan_info->counter++;
2197 if (scan_info->counter ==
2198 scan_info->cpi->initiator_id) {
2199 scan_info->counter++;
2201 if (scan_info->counter >=
2202 scan_info->cpi->max_target+1) {
2206 scan_info->counter--;
2207 if (scan_info->counter == 0) {
2213 xpt_free_ccb(request_ccb);
2214 xpt_free_ccb((union ccb *)scan_info->cpi);
2215 request_ccb = scan_info->request_ccb;
2216 CAM_DEBUG(request_ccb->ccb_h.path,
2218 ("SCAN done for %p\n", scan_info));
2219 free(scan_info, M_CAMXPT);
2220 request_ccb->ccb_h.status = CAM_REQ_CMP;
2221 xpt_done(request_ccb);
2225 if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
2227 xpt_free_ccb(request_ccb);
2230 status = xpt_create_path(&path, NULL,
2231 scan_info->request_ccb->ccb_h.path_id,
2232 scan_info->counter, 0);
2233 if (status != CAM_REQ_CMP) {
2235 printf("scsi_scan_bus: xpt_create_path failed"
2236 " with status %#x, bus scan halted\n",
2238 xpt_free_ccb(request_ccb);
2239 xpt_free_ccb((union ccb *)scan_info->cpi);
2240 request_ccb = scan_info->request_ccb;
2241 free(scan_info, M_CAMXPT);
2242 request_ccb->ccb_h.status = status;
2243 xpt_done(request_ccb);
2246 xpt_setup_ccb(&request_ccb->ccb_h, path,
2247 request_ccb->ccb_h.pinfo.priority);
2248 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2249 request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2250 request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2251 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2252 request_ccb->crcn.flags =
2253 scan_info->request_ccb->crcn.flags;
2255 status = xpt_create_path(&path, NULL,
2256 path_id, target_id, lun_id);
2258 * Free the old request path- we're done with it. We
2259 * do this *after* creating the new path so that
2260 * we don't remove a target that has our lun list
2261 * in the case that lun 0 is not present.
2263 xpt_free_path(oldpath);
2264 if (status != CAM_REQ_CMP) {
2265 printf("scsi_scan_bus: xpt_create_path failed "
2266 "with status %#x, halting LUN scan\n",
2270 xpt_setup_ccb(&request_ccb->ccb_h, path,
2271 request_ccb->ccb_h.pinfo.priority);
2272 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2273 request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2274 request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2275 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2276 request_ccb->crcn.flags =
2277 scan_info->request_ccb->crcn.flags;
2280 xpt_action(request_ccb);
2289 scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
2290 cam_flags flags, union ccb *request_ccb)
2292 struct ccb_pathinq cpi;
2294 struct cam_path *new_path;
2295 struct cam_periph *old_periph;
2298 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
2300 memset(&cpi, 0, sizeof(cpi));
2301 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2302 cpi.ccb_h.func_code = XPT_PATH_INQ;
2303 xpt_action((union ccb *)&cpi);
2305 if (cpi.ccb_h.status != CAM_REQ_CMP) {
2306 if (request_ccb != NULL) {
2307 request_ccb->ccb_h.status = cpi.ccb_h.status;
2308 xpt_done(request_ccb);
2313 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
2315 * Can't scan the bus on an adapter that
2316 * cannot perform the initiator role.
2318 if (request_ccb != NULL) {
2319 request_ccb->ccb_h.status = CAM_REQ_CMP;
2320 xpt_done(request_ccb);
2325 if (request_ccb == NULL) {
2326 request_ccb = xpt_alloc_ccb_nowait();
2327 if (request_ccb == NULL) {
2328 xpt_print(path, "scsi_scan_lun: can't allocate CCB, "
2329 "can't continue\n");
2332 status = xpt_create_path(&new_path, NULL,
2334 path->target->target_id,
2335 path->device->lun_id);
2336 if (status != CAM_REQ_CMP) {
2337 xpt_print(path, "scsi_scan_lun: can't create path, "
2338 "can't continue\n");
2339 xpt_free_ccb(request_ccb);
2342 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
2343 request_ccb->ccb_h.cbfcnp = xptscandone;
2344 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2345 request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2346 request_ccb->crcn.flags = flags;
2349 lock = (xpt_path_owned(path) == 0);
2351 xpt_path_lock(path);
2352 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
2353 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
2356 softc = (probe_softc *)old_periph->softc;
2357 TAILQ_INSERT_TAIL(&softc->request_ccbs,
2358 &request_ccb->ccb_h, periph_links.tqe);
2360 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2361 xpt_done(request_ccb);
2364 status = cam_periph_alloc(proberegister, NULL, probecleanup,
2365 probestart, "probe",
2367 request_ccb->ccb_h.path, NULL, 0,
2370 if (status != CAM_REQ_CMP) {
2371 xpt_print(path, "scsi_scan_lun: cam_alloc_periph "
2372 "returned an error, can't continue probe\n");
2373 request_ccb->ccb_h.status = status;
2374 xpt_done(request_ccb);
2378 xpt_path_unlock(path);
2382 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
2385 xpt_free_path(done_ccb->ccb_h.path);
2386 xpt_free_ccb(done_ccb);
2389 static struct cam_ed *
2390 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
2392 struct scsi_quirk_entry *quirk;
2393 struct cam_ed *device;
2395 device = xpt_alloc_device(bus, target, lun_id);
2400 * Take the default quirk entry until we have inquiry
2401 * data and can determine a better quirk to use.
2403 quirk = &scsi_quirk_table[nitems(scsi_quirk_table) - 1];
2404 device->quirk = (void *)quirk;
2405 device->mintags = quirk->mintags;
2406 device->maxtags = quirk->maxtags;
2407 bzero(&device->inq_data, sizeof(device->inq_data));
2408 device->inq_flags = 0;
2409 device->queue_flags = 0;
2410 device->serial_num = NULL;
2411 device->serial_num_len = 0;
2412 device->device_id = NULL;
2413 device->device_id_len = 0;
2414 device->supported_vpds = NULL;
2415 device->supported_vpds_len = 0;
2420 scsi_devise_transport(struct cam_path *path)
2422 struct ccb_pathinq cpi;
2423 struct ccb_trans_settings cts;
2424 struct scsi_inquiry_data *inq_buf;
2426 /* Get transport information from the SIM */
2427 memset(&cpi, 0, sizeof(cpi));
2428 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2429 cpi.ccb_h.func_code = XPT_PATH_INQ;
2430 xpt_action((union ccb *)&cpi);
2433 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
2434 inq_buf = &path->device->inq_data;
2435 path->device->protocol = PROTO_SCSI;
2436 path->device->protocol_version =
2437 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
2438 path->device->transport = cpi.transport;
2439 path->device->transport_version = cpi.transport_version;
2442 * Any device not using SPI3 features should
2443 * be considered SPI2 or lower.
2445 if (inq_buf != NULL) {
2446 if (path->device->transport == XPORT_SPI
2447 && (inq_buf->spi3data & SID_SPI_MASK) == 0
2448 && path->device->transport_version > 2)
2449 path->device->transport_version = 2;
2451 struct cam_ed* otherdev;
2453 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
2455 otherdev = TAILQ_NEXT(otherdev, links)) {
2456 if (otherdev != path->device)
2460 if (otherdev != NULL) {
2462 * Initially assume the same versioning as
2463 * prior luns for this target.
2465 path->device->protocol_version =
2466 otherdev->protocol_version;
2467 path->device->transport_version =
2468 otherdev->transport_version;
2470 /* Until we know better, opt for safety */
2471 path->device->protocol_version = 2;
2472 if (path->device->transport == XPORT_SPI)
2473 path->device->transport_version = 2;
2475 path->device->transport_version = 0;
2481 * For a device compliant with SPC-2 we should be able
2482 * to determine the transport version supported by
2483 * scrutinizing the version descriptors in the
2487 /* Tell the controller what we think */
2488 memset(&cts, 0, sizeof(cts));
2489 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2490 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2491 cts.type = CTS_TYPE_CURRENT_SETTINGS;
2492 cts.transport = path->device->transport;
2493 cts.transport_version = path->device->transport_version;
2494 cts.protocol = path->device->protocol;
2495 cts.protocol_version = path->device->protocol_version;
2496 cts.proto_specific.valid = 0;
2497 cts.xport_specific.valid = 0;
2498 xpt_action((union ccb *)&cts);
2502 scsi_dev_advinfo(union ccb *start_ccb)
2504 struct cam_ed *device;
2505 struct ccb_dev_advinfo *cdai;
2508 xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
2509 start_ccb->ccb_h.status = CAM_REQ_INVALID;
2510 device = start_ccb->ccb_h.path->device;
2511 cdai = &start_ccb->cdai;
2512 switch(cdai->buftype) {
2513 case CDAI_TYPE_SCSI_DEVID:
2514 if (cdai->flags & CDAI_FLAG_STORE)
2516 cdai->provsiz = device->device_id_len;
2517 if (device->device_id_len == 0)
2519 amt = device->device_id_len;
2520 if (cdai->provsiz > cdai->bufsiz)
2522 memcpy(cdai->buf, device->device_id, amt);
2524 case CDAI_TYPE_SERIAL_NUM:
2525 if (cdai->flags & CDAI_FLAG_STORE)
2527 cdai->provsiz = device->serial_num_len;
2528 if (device->serial_num_len == 0)
2530 amt = device->serial_num_len;
2531 if (cdai->provsiz > cdai->bufsiz)
2533 memcpy(cdai->buf, device->serial_num, amt);
2535 case CDAI_TYPE_PHYS_PATH:
2536 if (cdai->flags & CDAI_FLAG_STORE) {
2537 if (device->physpath != NULL) {
2538 free(device->physpath, M_CAMXPT);
2539 device->physpath = NULL;
2540 device->physpath_len = 0;
2542 /* Clear existing buffer if zero length */
2543 if (cdai->bufsiz == 0)
2545 device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
2546 if (device->physpath == NULL) {
2547 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2550 device->physpath_len = cdai->bufsiz;
2551 memcpy(device->physpath, cdai->buf, cdai->bufsiz);
2553 cdai->provsiz = device->physpath_len;
2554 if (device->physpath_len == 0)
2556 amt = device->physpath_len;
2557 if (cdai->provsiz > cdai->bufsiz)
2559 memcpy(cdai->buf, device->physpath, amt);
2562 case CDAI_TYPE_RCAPLONG:
2563 if (cdai->flags & CDAI_FLAG_STORE) {
2564 if (device->rcap_buf != NULL) {
2565 free(device->rcap_buf, M_CAMXPT);
2566 device->rcap_buf = NULL;
2569 device->rcap_len = cdai->bufsiz;
2570 /* Clear existing buffer if zero length */
2571 if (cdai->bufsiz == 0)
2574 device->rcap_buf = malloc(cdai->bufsiz, M_CAMXPT,
2576 if (device->rcap_buf == NULL) {
2577 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2581 memcpy(device->rcap_buf, cdai->buf, cdai->bufsiz);
2583 cdai->provsiz = device->rcap_len;
2584 if (device->rcap_len == 0)
2586 amt = device->rcap_len;
2587 if (cdai->provsiz > cdai->bufsiz)
2589 memcpy(cdai->buf, device->rcap_buf, amt);
2592 case CDAI_TYPE_EXT_INQ:
2594 * We fetch extended inquiry data during probe, if
2595 * available. We don't allow changing it.
2597 if (cdai->flags & CDAI_FLAG_STORE)
2599 cdai->provsiz = device->ext_inq_len;
2600 if (device->ext_inq_len == 0)
2602 amt = device->ext_inq_len;
2603 if (cdai->provsiz > cdai->bufsiz)
2605 memcpy(cdai->buf, device->ext_inq, amt);
2610 start_ccb->ccb_h.status = CAM_REQ_CMP;
2612 if (cdai->flags & CDAI_FLAG_STORE) {
2613 xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
2614 (void *)(uintptr_t)cdai->buftype);
2619 scsi_action(union ccb *start_ccb)
2622 if (start_ccb->ccb_h.func_code != XPT_SCSI_IO) {
2623 KASSERT((start_ccb->ccb_h.alloc_flags & CAM_CCB_FROM_UMA) == 0,
2624 ("%s: ccb %p, func_code %#x should not be allocated "
2626 __func__, start_ccb, start_ccb->ccb_h.func_code));
2629 switch (start_ccb->ccb_h.func_code) {
2630 case XPT_SET_TRAN_SETTINGS:
2632 scsi_set_transfer_settings(&start_ccb->cts,
2633 start_ccb->ccb_h.path,
2634 /*async_update*/FALSE);
2639 scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2642 scsi_scan_lun(start_ccb->ccb_h.path->periph,
2643 start_ccb->ccb_h.path, start_ccb->crcn.flags,
2646 case XPT_DEV_ADVINFO:
2648 scsi_dev_advinfo(start_ccb);
2652 xpt_action_default(start_ccb);
2658 scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
2661 struct ccb_pathinq cpi;
2662 struct ccb_trans_settings cur_cts;
2663 struct ccb_trans_settings_scsi *scsi;
2664 struct ccb_trans_settings_scsi *cur_scsi;
2665 struct scsi_inquiry_data *inq_data;
2666 struct cam_ed *device;
2668 if (path == NULL || (device = path->device) == NULL) {
2669 cts->ccb_h.status = CAM_PATH_INVALID;
2670 xpt_done((union ccb *)cts);
2674 if (cts->protocol == PROTO_UNKNOWN
2675 || cts->protocol == PROTO_UNSPECIFIED) {
2676 cts->protocol = device->protocol;
2677 cts->protocol_version = device->protocol_version;
2680 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2681 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2682 cts->protocol_version = device->protocol_version;
2684 if (cts->protocol != device->protocol) {
2685 xpt_print(path, "Uninitialized Protocol %x:%x?\n",
2686 cts->protocol, device->protocol);
2687 cts->protocol = device->protocol;
2690 if (cts->protocol_version > device->protocol_version) {
2692 xpt_print(path, "Down reving Protocol "
2693 "Version from %d to %d?\n", cts->protocol_version,
2694 device->protocol_version);
2696 cts->protocol_version = device->protocol_version;
2699 if (cts->transport == XPORT_UNKNOWN
2700 || cts->transport == XPORT_UNSPECIFIED) {
2701 cts->transport = device->transport;
2702 cts->transport_version = device->transport_version;
2705 if (cts->transport_version == XPORT_VERSION_UNKNOWN
2706 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2707 cts->transport_version = device->transport_version;
2709 if (cts->transport != device->transport) {
2710 xpt_print(path, "Uninitialized Transport %x:%x?\n",
2711 cts->transport, device->transport);
2712 cts->transport = device->transport;
2715 if (cts->transport_version > device->transport_version) {
2717 xpt_print(path, "Down reving Transport "
2718 "Version from %d to %d?\n", cts->transport_version,
2719 device->transport_version);
2721 cts->transport_version = device->transport_version;
2725 * Nothing more of interest to do unless
2726 * this is a device connected via the
2729 if (cts->protocol != PROTO_SCSI) {
2730 if (async_update == FALSE)
2731 xpt_action_default((union ccb *)cts);
2735 inq_data = &device->inq_data;
2736 scsi = &cts->proto_specific.scsi;
2737 memset(&cpi, 0, sizeof(cpi));
2738 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2739 cpi.ccb_h.func_code = XPT_PATH_INQ;
2740 xpt_action((union ccb *)&cpi);
2742 /* SCSI specific sanity checking */
2743 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2744 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2745 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2746 || (device->mintags == 0)) {
2748 * Can't tag on hardware that doesn't support tags,
2749 * doesn't have it enabled, or has broken tag support.
2751 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2754 if (async_update == FALSE) {
2756 * Perform sanity checking against what the
2757 * controller and device can do.
2759 memset(&cur_cts, 0, sizeof(cur_cts));
2760 xpt_setup_ccb(&cur_cts.ccb_h, path, CAM_PRIORITY_NONE);
2761 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2762 cur_cts.type = cts->type;
2763 xpt_action((union ccb *)&cur_cts);
2764 if (cam_ccb_status((union ccb *)&cur_cts) != CAM_REQ_CMP) {
2767 cur_scsi = &cur_cts.proto_specific.scsi;
2768 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2769 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2770 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2772 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2773 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2776 /* SPI specific sanity checking */
2777 if (cts->transport == XPORT_SPI && async_update == FALSE) {
2779 struct ccb_trans_settings_spi *spi;
2780 struct ccb_trans_settings_spi *cur_spi;
2782 spi = &cts->xport_specific.spi;
2784 cur_spi = &cur_cts.xport_specific.spi;
2786 /* Fill in any gaps in what the user gave us */
2787 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2788 spi->sync_period = cur_spi->sync_period;
2789 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2790 spi->sync_period = 0;
2791 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2792 spi->sync_offset = cur_spi->sync_offset;
2793 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2794 spi->sync_offset = 0;
2795 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2796 spi->ppr_options = cur_spi->ppr_options;
2797 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2798 spi->ppr_options = 0;
2799 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2800 spi->bus_width = cur_spi->bus_width;
2801 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2803 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2804 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2805 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2807 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2808 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2809 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2810 && (inq_data->flags & SID_Sync) == 0
2811 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2812 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2814 spi->sync_period = 0;
2815 spi->sync_offset = 0;
2818 switch (spi->bus_width) {
2819 case MSG_EXT_WDTR_BUS_32_BIT:
2820 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2821 || (inq_data->flags & SID_WBus32) != 0
2822 || cts->type == CTS_TYPE_USER_SETTINGS)
2823 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2825 /* Fall Through to 16-bit */
2826 case MSG_EXT_WDTR_BUS_16_BIT:
2827 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2828 || (inq_data->flags & SID_WBus16) != 0
2829 || cts->type == CTS_TYPE_USER_SETTINGS)
2830 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2831 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2834 /* Fall Through to 8-bit */
2835 default: /* New bus width?? */
2836 case MSG_EXT_WDTR_BUS_8_BIT:
2837 /* All targets can do this */
2838 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2842 spi3caps = cpi.xport_specific.spi.ppr_options;
2843 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2844 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2845 spi3caps &= inq_data->spi3data;
2847 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2848 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2850 if ((spi3caps & SID_SPI_IUS) == 0)
2851 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2853 if ((spi3caps & SID_SPI_QAS) == 0)
2854 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2856 /* No SPI Transfer settings are allowed unless we are wide */
2857 if (spi->bus_width == 0)
2858 spi->ppr_options = 0;
2860 if ((spi->valid & CTS_SPI_VALID_DISC)
2861 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2863 * Can't tag queue without disconnection.
2865 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2866 scsi->valid |= CTS_SCSI_VALID_TQ;
2870 * If we are currently performing tagged transactions to
2871 * this device and want to change its negotiation parameters,
2872 * go non-tagged for a bit to give the controller a chance to
2873 * negotiate unhampered by tag messages.
2875 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2876 && (device->inq_flags & SID_CmdQue) != 0
2877 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2878 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2879 CTS_SPI_VALID_SYNC_OFFSET|
2880 CTS_SPI_VALID_BUS_WIDTH)) != 0)
2881 scsi_toggle_tags(path);
2884 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2885 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2889 * If we are transitioning from tags to no-tags or
2890 * vice-versa, we need to carefully freeze and restart
2891 * the queue so that we don't overlap tagged and non-tagged
2892 * commands. We also temporarily stop tags if there is
2893 * a change in transfer negotiation settings to allow
2894 * "tag-less" negotiation.
2896 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2897 || (device->inq_flags & SID_CmdQue) != 0)
2898 device_tagenb = TRUE;
2900 device_tagenb = FALSE;
2902 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2903 && device_tagenb == FALSE)
2904 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2905 && device_tagenb == TRUE)) {
2906 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2908 * Delay change to use tags until after a
2909 * few commands have gone to this device so
2910 * the controller has time to perform transfer
2911 * negotiations without tagged messages getting
2914 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2915 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2917 xpt_stop_tags(path);
2921 if (async_update == FALSE)
2922 xpt_action_default((union ccb *)cts);
2926 scsi_toggle_tags(struct cam_path *path)
2931 * Give controllers a chance to renegotiate
2932 * before starting tag operations. We
2933 * "toggle" tagged queuing off then on
2934 * which causes the tag enable command delay
2935 * counter to come into effect.
2938 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2939 || ((dev->inq_flags & SID_CmdQue) != 0
2940 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2941 struct ccb_trans_settings cts;
2943 memset(&cts, 0, sizeof(cts));
2944 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2945 cts.protocol = PROTO_SCSI;
2946 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2947 cts.transport = XPORT_UNSPECIFIED;
2948 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2949 cts.proto_specific.scsi.flags = 0;
2950 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2951 scsi_set_transfer_settings(&cts, path,
2952 /*async_update*/TRUE);
2953 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2954 scsi_set_transfer_settings(&cts, path,
2955 /*async_update*/TRUE);
2960 * Handle any per-device event notifications that require action by the XPT.
2963 scsi_dev_async(uint32_t async_code, struct cam_eb *bus, struct cam_et *target,
2964 struct cam_ed *device, void *async_arg)
2967 struct cam_path newpath;
2970 * We only need to handle events for real devices.
2972 if (target->target_id == CAM_TARGET_WILDCARD
2973 || device->lun_id == CAM_LUN_WILDCARD)
2977 * We need our own path with wildcards expanded to
2978 * handle certain types of events.
2980 if ((async_code == AC_SENT_BDR)
2981 || (async_code == AC_BUS_RESET)
2982 || (async_code == AC_INQ_CHANGED))
2983 status = xpt_compile_path(&newpath, NULL,
2988 status = CAM_REQ_CMP_ERR;
2990 if (status == CAM_REQ_CMP) {
2992 * Allow transfer negotiation to occur in a
2993 * tag free environment and after settle delay.
2995 if (async_code == AC_SENT_BDR
2996 || async_code == AC_BUS_RESET) {
2997 cam_freeze_devq(&newpath);
2998 cam_release_devq(&newpath,
2999 RELSIM_RELEASE_AFTER_TIMEOUT,
3001 /*timeout*/scsi_delay,
3002 /*getcount_only*/0);
3003 scsi_toggle_tags(&newpath);
3006 if (async_code == AC_INQ_CHANGED) {
3008 * We've sent a start unit command, or
3009 * something similar to a device that
3010 * may have caused its inquiry data to
3011 * change. So we re-scan the device to
3012 * refresh the inquiry data for it.
3014 scsi_scan_lun(newpath.periph, &newpath,
3015 CAM_EXPECT_INQ_CHANGE, NULL);
3017 xpt_release_path(&newpath);
3018 } else if (async_code == AC_LOST_DEVICE &&
3019 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
3020 device->flags |= CAM_DEV_UNCONFIGURED;
3021 xpt_release_device(device);
3022 } else if (async_code == AC_TRANSFER_NEG) {
3023 struct ccb_trans_settings *settings;
3024 struct cam_path path;
3026 settings = (struct ccb_trans_settings *)async_arg;
3027 xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
3029 scsi_set_transfer_settings(settings, &path,
3030 /*async_update*/TRUE);
3031 xpt_release_path(&path);
3036 _scsi_announce_periph(struct cam_periph *periph, u_int *speed, u_int *freq, struct ccb_trans_settings *cts)
3038 struct ccb_pathinq cpi;
3039 struct cam_path *path = periph->path;
3041 cam_periph_assert(periph, MA_OWNED);
3043 xpt_setup_ccb(&cts->ccb_h, path, CAM_PRIORITY_NORMAL);
3044 cts->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
3045 cts->type = CTS_TYPE_CURRENT_SETTINGS;
3046 xpt_action((union ccb*)cts);
3047 if (cam_ccb_status((union ccb *)cts) != CAM_REQ_CMP)
3050 /* Ask the SIM for its base transfer speed */
3051 memset(&cpi, 0, sizeof(cpi));
3052 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3053 cpi.ccb_h.func_code = XPT_PATH_INQ;
3054 xpt_action((union ccb *)&cpi);
3056 /* Report connection speed */
3057 *speed = cpi.base_transfer_speed;
3060 if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_SPI) {
3061 struct ccb_trans_settings_spi *spi =
3062 &cts->xport_specific.spi;
3064 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
3065 && spi->sync_offset != 0) {
3066 *freq = scsi_calc_syncsrate(spi->sync_period);
3069 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
3070 *speed *= (0x01 << spi->bus_width);
3072 if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_FC) {
3073 struct ccb_trans_settings_fc *fc =
3074 &cts->xport_specific.fc;
3076 if (fc->valid & CTS_FC_VALID_SPEED)
3077 *speed = fc->bitrate;
3079 if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_SAS) {
3080 struct ccb_trans_settings_sas *sas =
3081 &cts->xport_specific.sas;
3083 if (sas->valid & CTS_SAS_VALID_SPEED)
3084 *speed = sas->bitrate;
3089 scsi_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
3091 struct ccb_trans_settings cts;
3092 u_int speed, freq, mb;
3094 memset(&cts, 0, sizeof(cts));
3095 _scsi_announce_periph(periph, &speed, &freq, &cts);
3096 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP)
3101 sbuf_printf(sb, "%s%d: %d.%03dMB/s transfers",
3102 periph->periph_name, periph->unit_number,
3105 sbuf_printf(sb, "%s%d: %dKB/s transfers", periph->periph_name,
3106 periph->unit_number, speed);
3107 /* Report additional information about SPI connections */
3108 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3109 struct ccb_trans_settings_spi *spi;
3111 spi = &cts.xport_specific.spi;
3113 sbuf_printf(sb, " (%d.%03dMHz%s, offset %d", freq / 1000,
3115 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
3119 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
3120 && spi->bus_width > 0) {
3122 sbuf_printf(sb, ", ");
3124 sbuf_printf(sb, " (");
3126 sbuf_printf(sb, "%dbit)", 8 * (0x01 << spi->bus_width));
3127 } else if (freq != 0) {
3128 sbuf_printf(sb, ")");
3131 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3132 struct ccb_trans_settings_fc *fc;
3134 fc = &cts.xport_specific.fc;
3135 if (fc->valid & CTS_FC_VALID_WWNN)
3136 sbuf_printf(sb, " WWNN 0x%llx", (long long) fc->wwnn);
3137 if (fc->valid & CTS_FC_VALID_WWPN)
3138 sbuf_printf(sb, " WWPN 0x%llx", (long long) fc->wwpn);
3139 if (fc->valid & CTS_FC_VALID_PORT)
3140 sbuf_printf(sb, " PortID 0x%x", fc->port);
3142 sbuf_printf(sb, "\n");
3146 scsi_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
3148 scsi_print_inquiry_sbuf(sb, &device->inq_data);
3152 scsi_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
3154 scsi_print_inquiry_short_sbuf(sb, &device->inq_data);
3158 scsi_proto_debug_out(union ccb *ccb)
3160 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3161 struct cam_ed *device;
3163 if (ccb->ccb_h.func_code != XPT_SCSI_IO)
3166 device = ccb->ccb_h.path->device;
3167 CAM_DEBUG(ccb->ccb_h.path,
3168 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3169 scsi_op_desc(scsiio_cdb_ptr(&ccb->csio)[0], &device->inq_data),
3170 scsi_cdb_string(scsiio_cdb_ptr(&ccb->csio), cdb_str, sizeof(cdb_str))));