2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
40 #include <sys/interrupt.h>
44 #include <sys/mutex.h>
45 #include <sys/sysctl.h>
48 #include <cam/cam_ccb.h>
49 #include <cam/cam_queue.h>
50 #include <cam/cam_periph.h>
51 #include <cam/cam_sim.h>
52 #include <cam/cam_xpt.h>
53 #include <cam/cam_xpt_sim.h>
54 #include <cam/cam_xpt_periph.h>
55 #include <cam/cam_xpt_internal.h>
56 #include <cam/cam_debug.h>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_message.h>
60 #include <cam/ata/ata_all.h>
61 #include <machine/stdarg.h> /* for xpt_print below */
64 struct ata_quirk_entry {
65 struct scsi_inquiry_pattern inq_pat;
67 #define CAM_QUIRK_MAXTAGS 0x01
71 static periph_init_t probe_periph_init;
73 static struct periph_driver probe_driver =
75 probe_periph_init, "aprobe",
76 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
80 PERIPHDRIVER_DECLARE(aprobe, probe_driver);
99 static char *probe_action_text[] = {
110 "PROBE_FULL_INQUIRY",
116 #define PROBE_SET_ACTION(softc, newaction) \
119 text = probe_action_text; \
120 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO, \
121 ("Probe %s to %s\n", text[(softc)->action], \
122 text[(newaction)])); \
123 (softc)->action = (newaction); \
127 PROBE_NO_ANNOUNCE = 0x04
131 TAILQ_HEAD(, ccb_hdr) request_ccbs;
132 struct ata_params ident_data;
141 struct cam_periph *periph;
144 static struct ata_quirk_entry ata_quirk_table[] =
147 /* Default tagged queuing parameters for all devices */
149 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
150 /*vendor*/"*", /*product*/"*", /*revision*/"*"
152 /*quirks*/0, /*maxtags*/0
156 static const int ata_quirk_table_size =
157 sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
159 static cam_status proberegister(struct cam_periph *periph,
161 static void probeschedule(struct cam_periph *probe_periph);
162 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
163 //static void proberequestdefaultnegotiation(struct cam_periph *periph);
164 //static int proberequestbackoff(struct cam_periph *periph,
165 // struct cam_ed *device);
166 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
167 static void probecleanup(struct cam_periph *periph);
168 static void ata_find_quirk(struct cam_ed *device);
169 static void ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
170 static void ata_scan_lun(struct cam_periph *periph,
171 struct cam_path *path, cam_flags flags,
173 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
174 static struct cam_ed *
175 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
177 static void ata_device_transport(struct cam_path *path);
178 static void ata_set_transfer_settings(struct ccb_trans_settings *cts,
179 struct cam_ed *device,
181 static void ata_dev_async(u_int32_t async_code,
183 struct cam_et *target,
184 struct cam_ed *device,
186 static void ata_action(union ccb *start_ccb);
187 static void ata_announce_periph(struct cam_periph *periph);
189 static struct xpt_xport ata_xport = {
190 .alloc_device = ata_alloc_device,
191 .action = ata_action,
192 .async = ata_dev_async,
193 .announce = ata_announce_periph,
208 proberegister(struct cam_periph *periph, void *arg)
210 union ccb *request_ccb; /* CCB representing the probe request */
214 request_ccb = (union ccb *)arg;
215 if (periph == NULL) {
216 printf("proberegister: periph was NULL!!\n");
217 return(CAM_REQ_CMP_ERR);
220 if (request_ccb == NULL) {
221 printf("proberegister: no probe CCB, "
222 "can't register device\n");
223 return(CAM_REQ_CMP_ERR);
226 softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
229 printf("proberegister: Unable to probe new device. "
230 "Unable to allocate softc\n");
231 return(CAM_REQ_CMP_ERR);
233 TAILQ_INIT(&softc->request_ccbs);
234 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
237 periph->softc = softc;
238 softc->periph = periph;
239 softc->action = PROBE_INVALID;
240 status = cam_periph_acquire(periph);
241 if (status != CAM_REQ_CMP) {
245 * Ensure nobody slip in until probe finish.
247 cam_freeze_devq_arg(periph->path,
248 RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
249 probeschedule(periph);
254 probeschedule(struct cam_periph *periph)
259 softc = (probe_softc *)periph->softc;
260 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
262 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
263 periph->path->device->protocol == PROTO_SATAPM)
264 PROBE_SET_ACTION(softc, PROBE_RESET);
266 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
268 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
269 softc->flags |= PROBE_NO_ANNOUNCE;
271 softc->flags &= ~PROBE_NO_ANNOUNCE;
273 xpt_schedule(periph, CAM_PRIORITY_XPT);
277 probestart(struct cam_periph *periph, union ccb *start_ccb)
279 struct ccb_trans_settings cts;
280 struct ccb_ataio *ataio;
281 struct ccb_scsiio *csio;
283 struct cam_path *path;
284 struct ata_params *ident_buf;
286 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
288 softc = (probe_softc *)periph->softc;
289 path = start_ccb->ccb_h.path;
290 ataio = &start_ccb->ataio;
291 csio = &start_ccb->csio;
292 ident_buf = &periph->path->device->ident_data;
294 if (softc->restart) {
296 if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
297 path->device->protocol == PROTO_SATAPM)
298 softc->action = PROBE_RESET;
300 softc->action = PROBE_IDENTIFY;
302 switch (softc->action) {
304 cam_fill_ataio(ataio,
307 /*flags*/CAM_DIR_NONE,
312 ata_reset_cmd(ataio);
315 cam_fill_ataio(ataio,
320 /*data_ptr*/(u_int8_t *)&softc->ident_data,
321 /*dxfer_len*/sizeof(softc->ident_data),
323 if (periph->path->device->protocol == PROTO_ATA)
324 ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
326 ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
330 xpt_print(path, "Spinning up device\n");
331 cam_fill_ataio(ataio,
334 /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
339 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
346 /* Fetch user modes from SIM. */
347 bzero(&cts, sizeof(cts));
348 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
349 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
350 cts.type = CTS_TYPE_USER_SETTINGS;
351 xpt_action((union ccb *)&cts);
352 if (path->device->transport == XPORT_ATA) {
353 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
354 mode = cts.xport_specific.ata.mode;
356 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
357 mode = cts.xport_specific.sata.mode;
360 /* Honor device capabilities. */
361 wantmode = mode = ata_max_mode(ident_buf, mode);
362 /* Report modes to SIM. */
363 bzero(&cts, sizeof(cts));
364 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
365 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
366 cts.type = CTS_TYPE_CURRENT_SETTINGS;
367 if (path->device->transport == XPORT_ATA) {
368 cts.xport_specific.ata.mode = mode;
369 cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
371 cts.xport_specific.sata.mode = mode;
372 cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
374 xpt_action((union ccb *)&cts);
375 /* Fetch current modes from SIM. */
376 bzero(&cts, sizeof(cts));
377 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
378 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
379 cts.type = CTS_TYPE_CURRENT_SETTINGS;
380 xpt_action((union ccb *)&cts);
381 if (path->device->transport == XPORT_ATA) {
382 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
383 mode = cts.xport_specific.ata.mode;
385 if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
386 mode = cts.xport_specific.sata.mode;
388 /* If SIM disagree - renegotiate. */
389 if (mode != wantmode)
391 /* Remember what transport thinks about DMA. */
393 path->device->inq_flags &= ~SID_DMA;
395 path->device->inq_flags |= SID_DMA;
396 cam_fill_ataio(ataio,
399 /*flags*/CAM_DIR_NONE,
404 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
408 cam_fill_ataio(ataio,
416 ata_28bit_cmd(ataio, ATA_SETFEATURES,
417 (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
421 cam_fill_ataio(ataio,
429 ata_28bit_cmd(ataio, ATA_SETFEATURES,
430 (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
434 cam_fill_ataio(ataio,
442 ata_28bit_cmd(ataio, ATA_SETFEATURES,
443 (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
447 cam_fill_ataio(ataio,
455 ata_28bit_cmd(ataio, ATA_SETFEATURES,
456 (softc->caps & CTS_SATA_CAPS_H_AN) ? 0x10 : 0x90,
459 case PROBE_SET_MULTI:
461 u_int sectors, bytecount;
463 bytecount = 8192; /* SATA maximum */
464 /* Fetch user bytecount from SIM. */
465 bzero(&cts, sizeof(cts));
466 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
467 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
468 cts.type = CTS_TYPE_USER_SETTINGS;
469 xpt_action((union ccb *)&cts);
470 if (path->device->transport == XPORT_ATA) {
471 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
472 bytecount = cts.xport_specific.ata.bytecount;
474 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
475 bytecount = cts.xport_specific.sata.bytecount;
477 /* Honor device capabilities. */
478 sectors = max(1, min(ident_buf->sectors_intr & 0xff,
479 bytecount / ata_logical_sector_size(ident_buf)));
480 /* Report bytecount to SIM. */
481 bzero(&cts, sizeof(cts));
482 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
483 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
484 cts.type = CTS_TYPE_CURRENT_SETTINGS;
485 if (path->device->transport == XPORT_ATA) {
486 cts.xport_specific.ata.bytecount = sectors *
487 ata_logical_sector_size(ident_buf);
488 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
490 cts.xport_specific.sata.bytecount = sectors *
491 ata_logical_sector_size(ident_buf);
492 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
494 xpt_action((union ccb *)&cts);
495 /* Fetch current bytecount from SIM. */
496 bzero(&cts, sizeof(cts));
497 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
498 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
499 cts.type = CTS_TYPE_CURRENT_SETTINGS;
500 xpt_action((union ccb *)&cts);
501 if (path->device->transport == XPORT_ATA) {
502 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
503 bytecount = cts.xport_specific.ata.bytecount;
505 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
506 bytecount = cts.xport_specific.sata.bytecount;
508 sectors = bytecount / ata_logical_sector_size(ident_buf);
510 cam_fill_ataio(ataio,
518 ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
525 bytecount = 8192; /* SATA maximum */
526 /* Fetch user bytecount from SIM. */
527 bzero(&cts, sizeof(cts));
528 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
529 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
530 cts.type = CTS_TYPE_USER_SETTINGS;
531 xpt_action((union ccb *)&cts);
532 if (path->device->transport == XPORT_ATA) {
533 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
534 bytecount = cts.xport_specific.ata.bytecount;
536 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
537 bytecount = cts.xport_specific.sata.bytecount;
539 /* Honor device capabilities. */
541 bytecount = max(2, min(65534, bytecount));
542 if (ident_buf->satacapabilities != 0x0000 &&
543 ident_buf->satacapabilities != 0xffff) {
544 bytecount = min(8192, bytecount);
546 /* Report bytecount to SIM. */
547 bzero(&cts, sizeof(cts));
548 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
549 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
550 cts.type = CTS_TYPE_CURRENT_SETTINGS;
551 if (path->device->transport == XPORT_ATA) {
552 cts.xport_specific.ata.bytecount = bytecount;
553 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
555 cts.xport_specific.sata.bytecount = bytecount;
556 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
558 xpt_action((union ccb *)&cts);
561 case PROBE_FULL_INQUIRY:
564 struct scsi_inquiry_data *inq_buf =
565 &periph->path->device->inq_data;
567 if (softc->action == PROBE_INQUIRY)
568 inquiry_len = SHORT_INQUIRY_LENGTH;
570 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
572 * Some parallel SCSI devices fail to send an
573 * ignore wide residue message when dealing with
574 * odd length inquiry requests. Round up to be
577 inquiry_len = roundup2(inquiry_len, 2);
587 /*timeout*/60 * 1000);
591 cam_fill_ataio(ataio,
594 /*flags*/CAM_DIR_NONE,
599 ata_pm_read_cmd(ataio, 0, 15);
602 cam_fill_ataio(ataio,
605 /*flags*/CAM_DIR_NONE,
610 ata_pm_read_cmd(ataio, 1, 15);
613 CAM_DEBUG(path, CAM_DEBUG_INFO,
614 ("probestart: invalid action state\n"));
618 xpt_action(start_ccb);
622 proberequestdefaultnegotiation(struct cam_periph *periph)
624 struct ccb_trans_settings cts;
626 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
627 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
628 cts.type = CTS_TYPE_USER_SETTINGS;
629 xpt_action((union ccb *)&cts);
630 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
633 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
634 cts.type = CTS_TYPE_CURRENT_SETTINGS;
635 xpt_action((union ccb *)&cts);
639 * Backoff Negotiation Code- only pertinent for SPI devices.
642 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
644 struct ccb_trans_settings cts;
645 struct ccb_trans_settings_spi *spi;
647 memset(&cts, 0, sizeof (cts));
648 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
649 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
650 cts.type = CTS_TYPE_CURRENT_SETTINGS;
651 xpt_action((union ccb *)&cts);
652 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
654 xpt_print(periph->path,
655 "failed to get current device settings\n");
659 if (cts.transport != XPORT_SPI) {
661 xpt_print(periph->path, "not SPI transport\n");
665 spi = &cts.xport_specific.spi;
668 * We cannot renegotiate sync rate if we don't have one.
670 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
672 xpt_print(periph->path, "no sync rate known\n");
678 * We'll assert that we don't have to touch PPR options- the
679 * SIM will see what we do with period and offset and adjust
680 * the PPR options as appropriate.
684 * A sync rate with unknown or zero offset is nonsensical.
685 * A sync period of zero means Async.
687 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
688 || spi->sync_offset == 0 || spi->sync_period == 0) {
690 xpt_print(periph->path, "no sync rate available\n");
695 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
696 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
697 ("hit async: giving up on DV\n"));
703 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
704 * We don't try to remember 'last' settings to see if the SIM actually
705 * gets into the speed we want to set. We check on the SIM telling
706 * us that a requested speed is bad, but otherwise don't try and
707 * check the speed due to the asynchronous and handshake nature
710 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
713 if (spi->sync_period >= 0xf) {
714 spi->sync_period = 0;
715 spi->sync_offset = 0;
716 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
717 ("setting to async for DV\n"));
719 * Once we hit async, we don't want to try
722 device->flags |= CAM_DEV_DV_HIT_BOTTOM;
723 } else if (bootverbose) {
724 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
725 ("DV: period 0x%x\n", spi->sync_period));
726 printf("setting period to 0x%x\n", spi->sync_period);
728 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
729 cts.type = CTS_TYPE_CURRENT_SETTINGS;
730 xpt_action((union ccb *)&cts);
731 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
734 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
735 ("DV: failed to set period 0x%x\n", spi->sync_period));
736 if (spi->sync_period == 0) {
744 probedone(struct cam_periph *periph, union ccb *done_ccb)
746 struct ccb_trans_settings cts;
747 struct ata_params *ident_buf;
749 struct cam_path *path;
755 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
757 softc = (probe_softc *)periph->softc;
758 path = done_ccb->ccb_h.path;
759 priority = done_ccb->ccb_h.pinfo.priority;
760 ident_buf = &path->device->ident_data;
762 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
763 if (softc->restart) {
765 cam_error_print(done_ccb,
766 CAM_ESF_ALL, CAM_EPF_ALL);
768 } else if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART)
770 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
771 /* Don't wedge the queue */
772 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
775 status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
776 if (softc->restart) {
778 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
781 if (softc->faults < 10)
786 /* Old PIO2 devices may not support mode setting. */
787 } else if (softc->action == PROBE_SETMODE &&
788 status == CAM_ATA_STATUS_ERROR &&
789 ata_max_pmode(ident_buf) <= ATA_PIO2 &&
790 (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) {
794 * Some old WD SATA disks report supported and enabled
795 * device-initiated interface power management, but return
796 * ABORT on attempt to disable it.
798 } else if (softc->action == PROBE_SETPM &&
799 status == CAM_ATA_STATUS_ERROR) {
803 * Some HP SATA disks report supported DMA Auto-Activation,
804 * but return ABORT on attempt to enable it.
806 } else if (softc->action == PROBE_SETDMAAA &&
807 status == CAM_ATA_STATUS_ERROR) {
812 * If we get to this point, we got an error status back
813 * from the inquiry and the error status doesn't require
814 * automatically retrying the command. Therefore, the
815 * inquiry failed. If we had inquiry information before
816 * for this device, but this latest inquiry command failed,
817 * the device has probably gone away. If this device isn't
818 * already marked unconfigured, notify the peripheral
819 * drivers that this device is no more.
821 device_fail: if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
822 xpt_async(AC_LOST_DEVICE, path, NULL);
829 switch (softc->action) {
832 int sign = (done_ccb->ataio.res.lba_high << 8) +
833 done_ccb->ataio.res.lba_mid;
835 xpt_print(path, "SIGNATURE: %04x\n", sign);
836 if (sign == 0x0000 &&
837 done_ccb->ccb_h.target_id != 15) {
838 path->device->protocol = PROTO_ATA;
839 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
840 } else if (sign == 0x9669 &&
841 done_ccb->ccb_h.target_id == 15) {
842 /* Report SIM that PM is present. */
843 bzero(&cts, sizeof(cts));
844 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
845 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
846 cts.type = CTS_TYPE_CURRENT_SETTINGS;
847 cts.xport_specific.sata.pm_present = 1;
848 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
849 xpt_action((union ccb *)&cts);
850 path->device->protocol = PROTO_SATAPM;
851 PROBE_SET_ACTION(softc, PROBE_PM_PID);
852 } else if (sign == 0xeb14 &&
853 done_ccb->ccb_h.target_id != 15) {
854 path->device->protocol = PROTO_SCSI;
855 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
857 if (done_ccb->ccb_h.target_id != 15) {
859 "Unexpected signature 0x%04x\n", sign);
863 xpt_release_ccb(done_ccb);
864 xpt_schedule(periph, priority);
869 struct ccb_pathinq cpi;
873 ident_buf = &softc->ident_data;
874 for (ptr = (int16_t *)ident_buf;
875 ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
876 *ptr = le16toh(*ptr);
878 if (strncmp(ident_buf->model, "FX", 2) &&
879 strncmp(ident_buf->model, "NEC", 3) &&
880 strncmp(ident_buf->model, "Pioneer", 7) &&
881 strncmp(ident_buf->model, "SHARP", 5)) {
882 ata_bswap(ident_buf->model, sizeof(ident_buf->model));
883 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
884 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
886 ata_btrim(ident_buf->model, sizeof(ident_buf->model));
887 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
888 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
889 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
890 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
891 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
892 /* Device may need spin-up before IDENTIFY become valid. */
893 if ((ident_buf->specconf == 0x37c8 ||
894 ident_buf->specconf == 0x738c) &&
895 ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
896 softc->spinup == 0)) {
897 PROBE_SET_ACTION(softc, PROBE_SPINUP);
898 xpt_release_ccb(done_ccb);
899 xpt_schedule(periph, priority);
902 ident_buf = &path->device->ident_data;
903 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
904 /* Check that it is the same device. */
905 if (bcmp(softc->ident_data.model, ident_buf->model,
906 sizeof(ident_buf->model)) ||
907 bcmp(softc->ident_data.revision, ident_buf->revision,
908 sizeof(ident_buf->revision)) ||
909 bcmp(softc->ident_data.serial, ident_buf->serial,
910 sizeof(ident_buf->serial))) {
911 /* Device changed. */
912 xpt_async(AC_LOST_DEVICE, path, NULL);
914 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
919 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
920 /* Clean up from previous instance of this device */
921 if (path->device->serial_num != NULL) {
922 free(path->device->serial_num, M_CAMXPT);
923 path->device->serial_num = NULL;
924 path->device->serial_num_len = 0;
926 path->device->serial_num =
927 (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
929 if (path->device->serial_num != NULL) {
930 bcopy(ident_buf->serial,
931 path->device->serial_num,
932 sizeof(ident_buf->serial));
933 path->device->serial_num[sizeof(ident_buf->serial)]
935 path->device->serial_num_len =
936 strlen(path->device->serial_num);
939 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
941 if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
942 path->device->mintags = path->device->maxtags =
943 ATA_QUEUE_LEN(ident_buf->queue) + 1;
945 ata_find_quirk(path->device);
946 if (path->device->mintags != 0 &&
947 path->bus->sim->max_tagged_dev_openings != 0) {
948 /* Check if the SIM does not want queued commands. */
949 bzero(&cpi, sizeof(cpi));
950 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
951 cpi.ccb_h.func_code = XPT_PATH_INQ;
952 xpt_action((union ccb *)&cpi);
953 if (cpi.ccb_h.status == CAM_REQ_CMP &&
954 (cpi.hba_inquiry & PI_TAG_ABLE)) {
955 /* Report SIM which tags are allowed. */
956 bzero(&cts, sizeof(cts));
957 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
958 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
959 cts.type = CTS_TYPE_CURRENT_SETTINGS;
960 cts.xport_specific.sata.tags = path->device->maxtags;
961 cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
962 xpt_action((union ccb *)&cts);
963 /* Reconfigure queues for tagged queueing. */
964 xpt_start_tags(path);
967 ata_device_transport(path);
968 PROBE_SET_ACTION(softc, PROBE_SETMODE);
969 xpt_release_ccb(done_ccb);
970 xpt_schedule(periph, priority);
975 xpt_print(path, "Spin-up done\n");
977 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
978 xpt_release_ccb(done_ccb);
979 xpt_schedule(periph, priority);
982 if (path->device->transport != XPORT_SATA)
984 /* Set supported bits. */
985 bzero(&cts, sizeof(cts));
986 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
987 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
988 cts.type = CTS_TYPE_CURRENT_SETTINGS;
989 xpt_action((union ccb *)&cts);
990 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
991 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
994 if (ident_buf->satacapabilities != 0xffff) {
995 if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
996 caps |= CTS_SATA_CAPS_D_PMREQ;
997 if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
998 caps |= CTS_SATA_CAPS_D_APST;
1000 /* Mask unwanted bits. */
1001 bzero(&cts, sizeof(cts));
1002 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1003 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1004 cts.type = CTS_TYPE_USER_SETTINGS;
1005 xpt_action((union ccb *)&cts);
1006 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1007 caps &= cts.xport_specific.sata.caps;
1010 /* Store result to SIM. */
1011 bzero(&cts, sizeof(cts));
1012 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1013 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1014 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1015 cts.xport_specific.sata.caps = caps;
1016 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1017 xpt_action((union ccb *)&cts);
1019 if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
1020 (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
1021 (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
1022 PROBE_SET_ACTION(softc, PROBE_SETPM);
1023 xpt_release_ccb(done_ccb);
1024 xpt_schedule(periph, priority);
1029 if (ident_buf->satacapabilities != 0xffff &&
1030 (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
1031 (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
1032 (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
1033 PROBE_SET_ACTION(softc, PROBE_SETAPST);
1034 xpt_release_ccb(done_ccb);
1035 xpt_schedule(periph, priority);
1040 if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
1041 (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
1042 (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
1043 PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1044 xpt_release_ccb(done_ccb);
1045 xpt_schedule(periph, priority);
1049 case PROBE_SETDMAAA:
1050 if ((ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) &&
1051 (!(softc->caps & CTS_SATA_CAPS_H_AN)) !=
1052 (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) {
1053 PROBE_SET_ACTION(softc, PROBE_SETAN);
1054 xpt_release_ccb(done_ccb);
1055 xpt_schedule(periph, priority);
1061 if (path->device->protocol == PROTO_ATA) {
1062 PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
1064 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1066 xpt_release_ccb(done_ccb);
1067 xpt_schedule(periph, priority);
1069 case PROBE_SET_MULTI:
1070 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1071 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1072 xpt_acquire_device(path->device);
1073 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1074 xpt_action(done_ccb);
1075 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1080 case PROBE_FULL_INQUIRY:
1082 struct scsi_inquiry_data *inq_buf;
1083 u_int8_t periph_qual, len;
1085 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1086 inq_buf = &path->device->inq_data;
1088 periph_qual = SID_QUAL(inq_buf);
1090 if (periph_qual != SID_QUAL_LU_CONNECTED)
1094 * We conservatively request only
1095 * SHORT_INQUIRY_LEN bytes of inquiry
1096 * information during our first try
1097 * at sending an INQUIRY. If the device
1098 * has more information to give,
1099 * perform a second request specifying
1100 * the amount of information the device
1101 * is willing to give.
1103 len = inq_buf->additional_length
1104 + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1105 if (softc->action == PROBE_INQUIRY
1106 && len > SHORT_INQUIRY_LENGTH) {
1107 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1108 xpt_release_ccb(done_ccb);
1109 xpt_schedule(periph, priority);
1113 ata_device_transport(path);
1114 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1115 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1116 xpt_acquire_device(path->device);
1117 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1118 xpt_action(done_ccb);
1119 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
1124 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1125 bzero(ident_buf, sizeof(*ident_buf));
1126 softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1127 (done_ccb->ataio.res.lba_mid << 16) +
1128 (done_ccb->ataio.res.lba_low << 8) +
1129 done_ccb->ataio.res.sector_count;
1130 ((uint32_t *)ident_buf)[0] = softc->pm_pid;
1131 snprintf(ident_buf->model, sizeof(ident_buf->model),
1132 "Port Multiplier %08x", softc->pm_pid);
1133 PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1134 xpt_release_ccb(done_ccb);
1135 xpt_schedule(periph, priority);
1138 softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1139 (done_ccb->ataio.res.lba_mid << 16) +
1140 (done_ccb->ataio.res.lba_low << 8) +
1141 done_ccb->ataio.res.sector_count;
1142 ((uint32_t *)ident_buf)[1] = softc->pm_prv;
1143 snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1144 "%04x", softc->pm_prv);
1145 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1146 /* Set supported bits. */
1147 bzero(&cts, sizeof(cts));
1148 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1149 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1150 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1151 xpt_action((union ccb *)&cts);
1152 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1153 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1156 /* All PMPs must support PM requests. */
1157 caps |= CTS_SATA_CAPS_D_PMREQ;
1158 /* Mask unwanted bits. */
1159 bzero(&cts, sizeof(cts));
1160 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1161 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1162 cts.type = CTS_TYPE_USER_SETTINGS;
1163 xpt_action((union ccb *)&cts);
1164 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1165 caps &= cts.xport_specific.sata.caps;
1168 /* Store result to SIM. */
1169 bzero(&cts, sizeof(cts));
1170 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1171 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1172 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1173 cts.xport_specific.sata.caps = caps;
1174 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1175 xpt_action((union ccb *)&cts);
1177 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1178 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1179 xpt_acquire_device(path->device);
1180 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1181 xpt_action(done_ccb);
1182 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1185 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1186 xpt_action(done_ccb);
1187 xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
1191 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
1192 ("probedone: invalid action state\n"));
1197 if (softc->restart) {
1199 xpt_release_ccb(done_ccb);
1200 probeschedule(periph);
1203 xpt_release_ccb(done_ccb);
1204 while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1205 TAILQ_REMOVE(&softc->request_ccbs,
1206 &done_ccb->ccb_h, periph_links.tqe);
1207 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1210 cam_release_devq(periph->path,
1211 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
1212 cam_periph_invalidate(periph);
1213 cam_periph_release_locked(periph);
1217 probecleanup(struct cam_periph *periph)
1219 free(periph->softc, M_CAMXPT);
1223 ata_find_quirk(struct cam_ed *device)
1225 struct ata_quirk_entry *quirk;
1228 match = cam_quirkmatch((caddr_t)&device->ident_data,
1229 (caddr_t)ata_quirk_table,
1230 ata_quirk_table_size,
1231 sizeof(*ata_quirk_table), ata_identify_match);
1234 panic("xpt_find_quirk: device didn't match wildcard entry!!");
1236 quirk = (struct ata_quirk_entry *)match;
1237 device->quirk = quirk;
1238 if (quirk->quirks & CAM_QUIRK_MAXTAGS)
1239 device->mintags = device->maxtags = quirk->maxtags;
1243 union ccb *request_ccb;
1244 struct ccb_pathinq *cpi;
1246 } ata_scan_bus_info;
1249 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1250 * As the scan progresses, xpt_scan_bus is used as the
1251 * callback on completion function.
1254 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1256 struct cam_path *path;
1257 ata_scan_bus_info *scan_info;
1258 union ccb *work_ccb, *reset_ccb;
1261 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1262 ("xpt_scan_bus\n"));
1263 switch (request_ccb->ccb_h.func_code) {
1266 /* Find out the characteristics of the bus */
1267 work_ccb = xpt_alloc_ccb_nowait();
1268 if (work_ccb == NULL) {
1269 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1270 xpt_done(request_ccb);
1273 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1274 request_ccb->ccb_h.pinfo.priority);
1275 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1276 xpt_action(work_ccb);
1277 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1278 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1279 xpt_free_ccb(work_ccb);
1280 xpt_done(request_ccb);
1284 /* We may need to reset bus first, if we haven't done it yet. */
1285 if ((work_ccb->cpi.hba_inquiry &
1286 (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1287 !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1288 !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1289 reset_ccb = xpt_alloc_ccb_nowait();
1290 if (reset_ccb == NULL) {
1291 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1292 xpt_free_ccb(work_ccb);
1293 xpt_done(request_ccb);
1296 xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1298 reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1299 xpt_action(reset_ccb);
1300 if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1301 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1302 xpt_free_ccb(reset_ccb);
1303 xpt_free_ccb(work_ccb);
1304 xpt_done(request_ccb);
1307 xpt_free_ccb(reset_ccb);
1310 /* Save some state for use while we probe for devices */
1311 scan_info = (ata_scan_bus_info *)
1312 malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1313 if (scan_info == NULL) {
1314 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1315 xpt_free_ccb(work_ccb);
1316 xpt_done(request_ccb);
1319 scan_info->request_ccb = request_ccb;
1320 scan_info->cpi = &work_ccb->cpi;
1321 /* If PM supported, probe it first. */
1322 if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1323 scan_info->counter = scan_info->cpi->max_target;
1325 scan_info->counter = 0;
1327 work_ccb = xpt_alloc_ccb_nowait();
1328 if (work_ccb == NULL) {
1329 free(scan_info, M_CAMXPT);
1330 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1331 xpt_done(request_ccb);
1336 work_ccb = request_ccb;
1337 /* Reuse the same CCB to query if a device was really found */
1338 scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1339 /* If there is PMP... */
1340 if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1341 (scan_info->counter == scan_info->cpi->max_target)) {
1342 if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1343 /* everything else will be probed by it */
1344 /* Free the current request path- we're done with it. */
1345 xpt_free_path(work_ccb->ccb_h.path);
1348 struct ccb_trans_settings cts;
1350 /* Report SIM that PM is absent. */
1351 bzero(&cts, sizeof(cts));
1352 xpt_setup_ccb(&cts.ccb_h,
1353 work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1354 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1355 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1356 cts.xport_specific.sata.pm_present = 0;
1357 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1358 xpt_action((union ccb *)&cts);
1361 /* Free the current request path- we're done with it. */
1362 xpt_free_path(work_ccb->ccb_h.path);
1363 if (scan_info->counter ==
1364 ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1365 0 : scan_info->cpi->max_target)) {
1367 xpt_free_ccb(work_ccb);
1368 xpt_free_ccb((union ccb *)scan_info->cpi);
1369 request_ccb = scan_info->request_ccb;
1370 free(scan_info, M_CAMXPT);
1371 request_ccb->ccb_h.status = CAM_REQ_CMP;
1372 xpt_done(request_ccb);
1375 /* Take next device. Wrap from max (PMP) to 0. */
1376 scan_info->counter = (scan_info->counter + 1 ) %
1377 (scan_info->cpi->max_target + 1);
1379 status = xpt_create_path(&path, xpt_periph,
1380 scan_info->request_ccb->ccb_h.path_id,
1381 scan_info->counter, 0);
1382 if (status != CAM_REQ_CMP) {
1383 printf("xpt_scan_bus: xpt_create_path failed"
1384 " with status %#x, bus scan halted\n",
1386 xpt_free_ccb(work_ccb);
1387 xpt_free_ccb((union ccb *)scan_info->cpi);
1388 request_ccb = scan_info->request_ccb;
1389 free(scan_info, M_CAMXPT);
1390 request_ccb->ccb_h.status = status;
1391 xpt_done(request_ccb);
1394 xpt_setup_ccb(&work_ccb->ccb_h, path,
1395 scan_info->request_ccb->ccb_h.pinfo.priority);
1396 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1397 work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1398 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1399 work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1400 xpt_action(work_ccb);
1408 ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1409 cam_flags flags, union ccb *request_ccb)
1411 struct ccb_pathinq cpi;
1413 struct cam_path *new_path;
1414 struct cam_periph *old_periph;
1416 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1418 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1419 cpi.ccb_h.func_code = XPT_PATH_INQ;
1420 xpt_action((union ccb *)&cpi);
1422 if (cpi.ccb_h.status != CAM_REQ_CMP) {
1423 if (request_ccb != NULL) {
1424 request_ccb->ccb_h.status = cpi.ccb_h.status;
1425 xpt_done(request_ccb);
1430 if (request_ccb == NULL) {
1431 request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1432 if (request_ccb == NULL) {
1433 xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1434 "can't continue\n");
1437 new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1438 if (new_path == NULL) {
1439 xpt_print(path, "xpt_scan_lun: can't allocate path, "
1440 "can't continue\n");
1441 free(request_ccb, M_CAMXPT);
1444 status = xpt_compile_path(new_path, xpt_periph,
1446 path->target->target_id,
1447 path->device->lun_id);
1449 if (status != CAM_REQ_CMP) {
1450 xpt_print(path, "xpt_scan_lun: can't compile path, "
1451 "can't continue\n");
1452 free(request_ccb, M_CAMXPT);
1453 free(new_path, M_CAMXPT);
1456 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1457 request_ccb->ccb_h.cbfcnp = xptscandone;
1458 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1459 request_ccb->crcn.flags = flags;
1462 if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1465 softc = (probe_softc *)old_periph->softc;
1466 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1470 status = cam_periph_alloc(proberegister, NULL, probecleanup,
1471 probestart, "aprobe",
1473 request_ccb->ccb_h.path, NULL, 0,
1476 if (status != CAM_REQ_CMP) {
1477 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1478 "returned an error, can't continue probe\n");
1479 request_ccb->ccb_h.status = status;
1480 xpt_done(request_ccb);
1486 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1488 xpt_release_path(done_ccb->ccb_h.path);
1489 free(done_ccb->ccb_h.path, M_CAMXPT);
1490 free(done_ccb, M_CAMXPT);
1493 static struct cam_ed *
1494 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1496 struct cam_path path;
1497 struct ata_quirk_entry *quirk;
1498 struct cam_ed *device;
1499 struct cam_ed *cur_device;
1501 device = xpt_alloc_device(bus, target, lun_id);
1506 * Take the default quirk entry until we have inquiry
1507 * data and can determine a better quirk to use.
1509 quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1510 device->quirk = (void *)quirk;
1511 device->mintags = 0;
1512 device->maxtags = 0;
1513 bzero(&device->inq_data, sizeof(device->inq_data));
1514 device->inq_flags = 0;
1515 device->queue_flags = 0;
1516 device->serial_num = NULL;
1517 device->serial_num_len = 0;
1520 * XXX should be limited by number of CCBs this bus can
1523 bus->sim->max_ccbs += device->ccbq.devq_openings;
1524 /* Insertion sort into our target's device list */
1525 cur_device = TAILQ_FIRST(&target->ed_entries);
1526 while (cur_device != NULL && cur_device->lun_id < lun_id)
1527 cur_device = TAILQ_NEXT(cur_device, links);
1528 if (cur_device != NULL) {
1529 TAILQ_INSERT_BEFORE(cur_device, device, links);
1531 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1533 target->generation++;
1534 if (lun_id != CAM_LUN_WILDCARD) {
1535 xpt_compile_path(&path,
1540 ata_device_transport(&path);
1541 xpt_release_path(&path);
1548 ata_device_transport(struct cam_path *path)
1550 struct ccb_pathinq cpi;
1551 struct ccb_trans_settings cts;
1552 struct scsi_inquiry_data *inq_buf = NULL;
1553 struct ata_params *ident_buf = NULL;
1555 /* Get transport information from the SIM */
1556 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1557 cpi.ccb_h.func_code = XPT_PATH_INQ;
1558 xpt_action((union ccb *)&cpi);
1560 path->device->transport = cpi.transport;
1561 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1562 inq_buf = &path->device->inq_data;
1563 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1564 ident_buf = &path->device->ident_data;
1565 if (path->device->protocol == PROTO_ATA) {
1566 path->device->protocol_version = ident_buf ?
1567 ata_version(ident_buf->version_major) : cpi.protocol_version;
1568 } else if (path->device->protocol == PROTO_SCSI) {
1569 path->device->protocol_version = inq_buf ?
1570 SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1572 path->device->transport_version = ident_buf ?
1573 ata_version(ident_buf->version_major) : cpi.transport_version;
1575 /* Tell the controller what we think */
1576 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1577 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1578 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1579 cts.transport = path->device->transport;
1580 cts.transport_version = path->device->transport_version;
1581 cts.protocol = path->device->protocol;
1582 cts.protocol_version = path->device->protocol_version;
1583 cts.proto_specific.valid = 0;
1585 if (path->device->transport == XPORT_ATA) {
1586 cts.xport_specific.ata.atapi =
1587 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1588 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1589 cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1591 cts.xport_specific.sata.atapi =
1592 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1593 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1594 cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1597 cts.xport_specific.valid = 0;
1598 xpt_action((union ccb *)&cts);
1602 ata_action(union ccb *start_ccb)
1605 switch (start_ccb->ccb_h.func_code) {
1606 case XPT_SET_TRAN_SETTINGS:
1608 ata_set_transfer_settings(&start_ccb->cts,
1609 start_ccb->ccb_h.path->device,
1610 /*async_update*/FALSE);
1615 ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1618 ata_scan_lun(start_ccb->ccb_h.path->periph,
1619 start_ccb->ccb_h.path, start_ccb->crcn.flags,
1622 case XPT_GET_TRAN_SETTINGS:
1624 struct cam_sim *sim;
1626 sim = start_ccb->ccb_h.path->bus->sim;
1627 (*(sim->sim_action))(sim, start_ccb);
1632 struct cam_ed *device;
1635 device = start_ccb->ccb_h.path->device;
1636 if (device->protocol == PROTO_SCSI &&
1637 (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1639 device->ident_data.config & ATA_PROTO_MASK;
1641 maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
1642 (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1644 if (start_ccb->csio.cdb_len > maxlen) {
1645 start_ccb->ccb_h.status = CAM_REQ_INVALID;
1646 xpt_done(start_ccb);
1652 xpt_action_default(start_ccb);
1658 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1661 struct ccb_pathinq cpi;
1662 struct ccb_trans_settings cur_cts;
1663 struct ccb_trans_settings_scsi *scsi;
1664 struct ccb_trans_settings_scsi *cur_scsi;
1665 struct cam_sim *sim;
1666 struct scsi_inquiry_data *inq_data;
1668 if (device == NULL) {
1669 cts->ccb_h.status = CAM_PATH_INVALID;
1670 xpt_done((union ccb *)cts);
1674 if (cts->protocol == PROTO_UNKNOWN
1675 || cts->protocol == PROTO_UNSPECIFIED) {
1676 cts->protocol = device->protocol;
1677 cts->protocol_version = device->protocol_version;
1680 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1681 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1682 cts->protocol_version = device->protocol_version;
1684 if (cts->protocol != device->protocol) {
1685 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1686 cts->protocol, device->protocol);
1687 cts->protocol = device->protocol;
1690 if (cts->protocol_version > device->protocol_version) {
1692 xpt_print(cts->ccb_h.path, "Down reving Protocol "
1693 "Version from %d to %d?\n", cts->protocol_version,
1694 device->protocol_version);
1696 cts->protocol_version = device->protocol_version;
1699 if (cts->transport == XPORT_UNKNOWN
1700 || cts->transport == XPORT_UNSPECIFIED) {
1701 cts->transport = device->transport;
1702 cts->transport_version = device->transport_version;
1705 if (cts->transport_version == XPORT_VERSION_UNKNOWN
1706 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1707 cts->transport_version = device->transport_version;
1709 if (cts->transport != device->transport) {
1710 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1711 cts->transport, device->transport);
1712 cts->transport = device->transport;
1715 if (cts->transport_version > device->transport_version) {
1717 xpt_print(cts->ccb_h.path, "Down reving Transport "
1718 "Version from %d to %d?\n", cts->transport_version,
1719 device->transport_version);
1721 cts->transport_version = device->transport_version;
1724 sim = cts->ccb_h.path->bus->sim;
1727 * Nothing more of interest to do unless
1728 * this is a device connected via the
1731 if (cts->protocol != PROTO_SCSI) {
1732 if (async_update == FALSE)
1733 (*(sim->sim_action))(sim, (union ccb *)cts);
1737 inq_data = &device->inq_data;
1738 scsi = &cts->proto_specific.scsi;
1739 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1740 cpi.ccb_h.func_code = XPT_PATH_INQ;
1741 xpt_action((union ccb *)&cpi);
1743 /* SCSI specific sanity checking */
1744 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1745 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1746 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1747 || (device->mintags == 0)) {
1749 * Can't tag on hardware that doesn't support tags,
1750 * doesn't have it enabled, or has broken tag support.
1752 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1755 if (async_update == FALSE) {
1757 * Perform sanity checking against what the
1758 * controller and device can do.
1760 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1761 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1762 cur_cts.type = cts->type;
1763 xpt_action((union ccb *)&cur_cts);
1764 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1767 cur_scsi = &cur_cts.proto_specific.scsi;
1768 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1769 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1770 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1772 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1773 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1776 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1777 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1781 * If we are transitioning from tags to no-tags or
1782 * vice-versa, we need to carefully freeze and restart
1783 * the queue so that we don't overlap tagged and non-tagged
1784 * commands. We also temporarily stop tags if there is
1785 * a change in transfer negotiation settings to allow
1786 * "tag-less" negotiation.
1788 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1789 || (device->inq_flags & SID_CmdQue) != 0)
1790 device_tagenb = TRUE;
1792 device_tagenb = FALSE;
1794 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1795 && device_tagenb == FALSE)
1796 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1797 && device_tagenb == TRUE)) {
1799 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1801 * Delay change to use tags until after a
1802 * few commands have gone to this device so
1803 * the controller has time to perform transfer
1804 * negotiations without tagged messages getting
1807 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1808 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1810 xpt_stop_tags(cts->ccb_h.path);
1814 if (async_update == FALSE)
1815 (*(sim->sim_action))(sim, (union ccb *)cts);
1819 * Handle any per-device event notifications that require action by the XPT.
1822 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1823 struct cam_ed *device, void *async_arg)
1826 struct cam_path newpath;
1829 * We only need to handle events for real devices.
1831 if (target->target_id == CAM_TARGET_WILDCARD
1832 || device->lun_id == CAM_LUN_WILDCARD)
1836 * We need our own path with wildcards expanded to
1837 * handle certain types of events.
1839 if ((async_code == AC_SENT_BDR)
1840 || (async_code == AC_BUS_RESET)
1841 || (async_code == AC_INQ_CHANGED))
1842 status = xpt_compile_path(&newpath, NULL,
1847 status = CAM_REQ_CMP_ERR;
1849 if (status == CAM_REQ_CMP) {
1850 if (async_code == AC_INQ_CHANGED) {
1852 * We've sent a start unit command, or
1853 * something similar to a device that
1854 * may have caused its inquiry data to
1855 * change. So we re-scan the device to
1856 * refresh the inquiry data for it.
1858 ata_scan_lun(newpath.periph, &newpath,
1859 CAM_EXPECT_INQ_CHANGE, NULL);
1861 /* We need to reinitialize device after reset. */
1862 ata_scan_lun(newpath.periph, &newpath,
1865 xpt_release_path(&newpath);
1866 } else if (async_code == AC_LOST_DEVICE &&
1867 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1868 device->flags |= CAM_DEV_UNCONFIGURED;
1869 xpt_release_device(device);
1870 } else if (async_code == AC_TRANSFER_NEG) {
1871 struct ccb_trans_settings *settings;
1873 settings = (struct ccb_trans_settings *)async_arg;
1874 ata_set_transfer_settings(settings, device,
1875 /*async_update*/TRUE);
1880 ata_announce_periph(struct cam_periph *periph)
1882 struct ccb_pathinq cpi;
1883 struct ccb_trans_settings cts;
1884 struct cam_path *path = periph->path;
1888 mtx_assert(periph->sim->mtx, MA_OWNED);
1890 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1891 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1892 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1893 xpt_action((union ccb*)&cts);
1894 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1896 /* Ask the SIM for its base transfer speed */
1897 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1898 cpi.ccb_h.func_code = XPT_PATH_INQ;
1899 xpt_action((union ccb *)&cpi);
1900 /* Report connection speed */
1901 speed = cpi.base_transfer_speed;
1902 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1903 struct ccb_trans_settings_ata *ata =
1904 &cts.xport_specific.ata;
1906 if (ata->valid & CTS_ATA_VALID_MODE)
1907 speed = ata_mode2speed(ata->mode);
1909 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1910 struct ccb_trans_settings_sata *sata =
1911 &cts.xport_specific.sata;
1913 if (sata->valid & CTS_SATA_VALID_REVISION)
1914 speed = ata_revision2speed(sata->revision);
1918 printf("%s%d: %d.%03dMB/s transfers",
1919 periph->periph_name, periph->unit_number,
1922 printf("%s%d: %dKB/s transfers", periph->periph_name,
1923 periph->unit_number, speed);
1924 /* Report additional information about connection */
1925 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1926 struct ccb_trans_settings_ata *ata =
1927 &cts.xport_specific.ata;
1930 if (ata->valid & CTS_ATA_VALID_MODE)
1931 printf("%s, ", ata_mode2string(ata->mode));
1932 if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
1933 printf("ATAPI %dbytes, ", ata->atapi);
1934 if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
1935 printf("PIO %dbytes", ata->bytecount);
1938 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1939 struct ccb_trans_settings_sata *sata =
1940 &cts.xport_specific.sata;
1943 if (sata->valid & CTS_SATA_VALID_REVISION)
1944 printf("SATA %d.x, ", sata->revision);
1947 if (sata->valid & CTS_SATA_VALID_MODE)
1948 printf("%s, ", ata_mode2string(sata->mode));
1949 if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
1950 printf("ATAPI %dbytes, ", sata->atapi);
1951 if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
1952 printf("PIO %dbytes", sata->bytecount);