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
72 static periph_init_t probe_periph_init;
74 static struct periph_driver probe_driver =
76 probe_periph_init, "aprobe",
77 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
81 PERIPHDRIVER_DECLARE(aprobe, probe_driver);
101 static char *probe_action_text[] = {
112 "PROBE_FULL_INQUIRY",
119 #define PROBE_SET_ACTION(softc, newaction) \
122 text = probe_action_text; \
123 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \
124 ("Probe %s to %s\n", text[(softc)->action], \
125 text[(newaction)])); \
126 (softc)->action = (newaction); \
130 PROBE_NO_ANNOUNCE = 0x04
134 TAILQ_HEAD(, ccb_hdr) request_ccbs;
135 struct ata_params ident_data;
144 struct cam_periph *periph;
147 static struct ata_quirk_entry ata_quirk_table[] =
150 /* Default tagged queuing parameters for all devices */
152 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
153 /*vendor*/"*", /*product*/"*", /*revision*/"*"
155 /*quirks*/0, /*mintags*/0, /*maxtags*/0
159 static const int ata_quirk_table_size =
160 sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
162 static cam_status proberegister(struct cam_periph *periph,
164 static void probeschedule(struct cam_periph *probe_periph);
165 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
166 static void proberequestdefaultnegotiation(struct cam_periph *periph);
167 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
168 static void probecleanup(struct cam_periph *periph);
169 static void ata_find_quirk(struct cam_ed *device);
170 static void ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
171 static void ata_scan_lun(struct cam_periph *periph,
172 struct cam_path *path, cam_flags flags,
174 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
175 static struct cam_ed *
176 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
178 static void ata_device_transport(struct cam_path *path);
179 static void ata_get_transfer_settings(struct ccb_trans_settings *cts);
180 static void ata_set_transfer_settings(struct ccb_trans_settings *cts,
181 struct cam_ed *device,
183 static void ata_dev_async(u_int32_t async_code,
185 struct cam_et *target,
186 struct cam_ed *device,
188 static void ata_action(union ccb *start_ccb);
189 static void ata_announce_periph(struct cam_periph *periph);
191 static int ata_dma = 1;
192 static int atapi_dma = 1;
194 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
195 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
197 static struct xpt_xport ata_xport = {
198 .alloc_device = ata_alloc_device,
199 .action = ata_action,
200 .async = ata_dev_async,
201 .announce = ata_announce_periph,
216 proberegister(struct cam_periph *periph, void *arg)
218 union ccb *request_ccb; /* CCB representing the probe request */
222 request_ccb = (union ccb *)arg;
223 if (periph == NULL) {
224 printf("proberegister: periph was NULL!!\n");
225 return(CAM_REQ_CMP_ERR);
228 if (request_ccb == NULL) {
229 printf("proberegister: no probe CCB, "
230 "can't register device\n");
231 return(CAM_REQ_CMP_ERR);
234 softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
237 printf("proberegister: Unable to probe new device. "
238 "Unable to allocate softc\n");
239 return(CAM_REQ_CMP_ERR);
241 TAILQ_INIT(&softc->request_ccbs);
242 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
245 periph->softc = softc;
246 softc->periph = periph;
247 softc->action = PROBE_INVALID;
248 status = cam_periph_acquire(periph);
249 if (status != CAM_REQ_CMP) {
252 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
255 * Ensure nobody slip in until probe finish.
257 cam_freeze_devq_arg(periph->path,
258 RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
259 probeschedule(periph);
264 probeschedule(struct cam_periph *periph)
269 softc = (probe_softc *)periph->softc;
270 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
272 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
273 periph->path->device->protocol == PROTO_SATAPM)
274 PROBE_SET_ACTION(softc, PROBE_RESET);
276 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
278 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
279 softc->flags |= PROBE_NO_ANNOUNCE;
281 softc->flags &= ~PROBE_NO_ANNOUNCE;
283 xpt_schedule(periph, CAM_PRIORITY_XPT);
287 probestart(struct cam_periph *periph, union ccb *start_ccb)
289 struct ccb_trans_settings cts;
290 struct ccb_ataio *ataio;
291 struct ccb_scsiio *csio;
293 struct cam_path *path;
294 struct ata_params *ident_buf;
296 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
298 softc = (probe_softc *)periph->softc;
299 path = start_ccb->ccb_h.path;
300 ataio = &start_ccb->ataio;
301 csio = &start_ccb->csio;
302 ident_buf = &periph->path->device->ident_data;
304 if (softc->restart) {
306 if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
307 path->device->protocol == PROTO_SATAPM)
308 softc->action = PROBE_RESET;
310 softc->action = PROBE_IDENTIFY;
312 switch (softc->action) {
314 cam_fill_ataio(ataio,
317 /*flags*/CAM_DIR_NONE,
322 ata_reset_cmd(ataio);
325 cam_fill_ataio(ataio,
330 /*data_ptr*/(u_int8_t *)&softc->ident_data,
331 /*dxfer_len*/sizeof(softc->ident_data),
333 if (periph->path->device->protocol == PROTO_ATA)
334 ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
336 ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
340 xpt_print(path, "Spinning up device\n");
341 cam_fill_ataio(ataio,
344 /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
349 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
356 /* Fetch user modes from SIM. */
357 bzero(&cts, sizeof(cts));
358 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
359 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
360 cts.type = CTS_TYPE_USER_SETTINGS;
361 xpt_action((union ccb *)&cts);
362 if (path->device->transport == XPORT_ATA) {
363 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
364 mode = cts.xport_specific.ata.mode;
366 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
367 mode = cts.xport_specific.sata.mode;
369 if (periph->path->device->protocol == PROTO_ATA) {
370 if (ata_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
373 if (atapi_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
377 /* Honor device capabilities. */
378 wantmode = mode = ata_max_mode(ident_buf, mode);
379 /* Report modes to SIM. */
380 bzero(&cts, sizeof(cts));
381 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
382 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
383 cts.type = CTS_TYPE_CURRENT_SETTINGS;
384 if (path->device->transport == XPORT_ATA) {
385 cts.xport_specific.ata.mode = mode;
386 cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
388 cts.xport_specific.sata.mode = mode;
389 cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
391 xpt_action((union ccb *)&cts);
392 /* Fetch current modes from SIM. */
393 bzero(&cts, sizeof(cts));
394 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
395 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
396 cts.type = CTS_TYPE_CURRENT_SETTINGS;
397 xpt_action((union ccb *)&cts);
398 if (path->device->transport == XPORT_ATA) {
399 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
400 mode = cts.xport_specific.ata.mode;
402 if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
403 mode = cts.xport_specific.sata.mode;
405 /* If SIM disagree - renegotiate. */
406 if (mode != wantmode)
408 /* Remember what transport thinks about DMA. */
410 path->device->inq_flags &= ~SID_DMA;
412 path->device->inq_flags |= SID_DMA;
413 xpt_async(AC_GETDEV_CHANGED, path, NULL);
414 cam_fill_ataio(ataio,
417 /*flags*/CAM_DIR_NONE,
422 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
426 cam_fill_ataio(ataio,
434 ata_28bit_cmd(ataio, ATA_SETFEATURES,
435 (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
439 cam_fill_ataio(ataio,
447 ata_28bit_cmd(ataio, ATA_SETFEATURES,
448 (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
452 cam_fill_ataio(ataio,
460 ata_28bit_cmd(ataio, ATA_SETFEATURES,
461 (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
465 cam_fill_ataio(ataio,
473 ata_28bit_cmd(ataio, ATA_SETFEATURES,
474 (softc->caps & CTS_SATA_CAPS_H_AN) ? 0x10 : 0x90,
477 case PROBE_SET_MULTI:
479 u_int sectors, bytecount;
481 bytecount = 8192; /* SATA maximum */
482 /* Fetch user bytecount from SIM. */
483 bzero(&cts, sizeof(cts));
484 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
485 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
486 cts.type = CTS_TYPE_USER_SETTINGS;
487 xpt_action((union ccb *)&cts);
488 if (path->device->transport == XPORT_ATA) {
489 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
490 bytecount = cts.xport_specific.ata.bytecount;
492 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
493 bytecount = cts.xport_specific.sata.bytecount;
495 /* Honor device capabilities. */
496 sectors = max(1, min(ident_buf->sectors_intr & 0xff,
497 bytecount / ata_logical_sector_size(ident_buf)));
498 /* Report bytecount to SIM. */
499 bzero(&cts, sizeof(cts));
500 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
501 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
502 cts.type = CTS_TYPE_CURRENT_SETTINGS;
503 if (path->device->transport == XPORT_ATA) {
504 cts.xport_specific.ata.bytecount = sectors *
505 ata_logical_sector_size(ident_buf);
506 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
508 cts.xport_specific.sata.bytecount = sectors *
509 ata_logical_sector_size(ident_buf);
510 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
512 xpt_action((union ccb *)&cts);
513 /* Fetch current bytecount from SIM. */
514 bzero(&cts, sizeof(cts));
515 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
516 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
517 cts.type = CTS_TYPE_CURRENT_SETTINGS;
518 xpt_action((union ccb *)&cts);
519 if (path->device->transport == XPORT_ATA) {
520 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
521 bytecount = cts.xport_specific.ata.bytecount;
523 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
524 bytecount = cts.xport_specific.sata.bytecount;
526 sectors = bytecount / ata_logical_sector_size(ident_buf);
528 cam_fill_ataio(ataio,
536 ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
543 bytecount = 8192; /* SATA maximum */
544 /* Fetch user bytecount from SIM. */
545 bzero(&cts, sizeof(cts));
546 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
547 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
548 cts.type = CTS_TYPE_USER_SETTINGS;
549 xpt_action((union ccb *)&cts);
550 if (path->device->transport == XPORT_ATA) {
551 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
552 bytecount = cts.xport_specific.ata.bytecount;
554 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
555 bytecount = cts.xport_specific.sata.bytecount;
557 /* Honor device capabilities. */
559 bytecount = max(2, min(65534, bytecount));
560 if (ident_buf->satacapabilities != 0x0000 &&
561 ident_buf->satacapabilities != 0xffff) {
562 bytecount = min(8192, bytecount);
564 /* Report bytecount to SIM. */
565 bzero(&cts, sizeof(cts));
566 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
567 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
568 cts.type = CTS_TYPE_CURRENT_SETTINGS;
569 if (path->device->transport == XPORT_ATA) {
570 cts.xport_specific.ata.bytecount = bytecount;
571 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
573 cts.xport_specific.sata.bytecount = bytecount;
574 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
576 xpt_action((union ccb *)&cts);
579 case PROBE_FULL_INQUIRY:
582 struct scsi_inquiry_data *inq_buf =
583 &periph->path->device->inq_data;
585 if (softc->action == PROBE_INQUIRY)
586 inquiry_len = SHORT_INQUIRY_LENGTH;
588 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
590 * Some parallel SCSI devices fail to send an
591 * ignore wide residue message when dealing with
592 * odd length inquiry requests. Round up to be
595 inquiry_len = roundup2(inquiry_len, 2);
605 /*timeout*/60 * 1000);
609 cam_fill_ataio(ataio,
612 /*flags*/CAM_DIR_NONE,
617 ata_pm_read_cmd(ataio, 0, 15);
620 cam_fill_ataio(ataio,
623 /*flags*/CAM_DIR_NONE,
628 ata_pm_read_cmd(ataio, 1, 15);
631 panic("probestart: invalid action state 0x%x\n", softc->action);
633 xpt_action(start_ccb);
637 proberequestdefaultnegotiation(struct cam_periph *periph)
639 struct ccb_trans_settings cts;
641 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
642 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
643 cts.type = CTS_TYPE_USER_SETTINGS;
644 xpt_action((union ccb *)&cts);
645 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
647 cts.xport_specific.valid = 0;
648 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
649 cts.type = CTS_TYPE_CURRENT_SETTINGS;
650 xpt_action((union ccb *)&cts);
654 probedone(struct cam_periph *periph, union ccb *done_ccb)
656 struct ccb_trans_settings cts;
657 struct ata_params *ident_buf;
659 struct cam_path *path;
665 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
667 softc = (probe_softc *)periph->softc;
668 path = done_ccb->ccb_h.path;
669 priority = done_ccb->ccb_h.pinfo.priority;
670 ident_buf = &path->device->ident_data;
672 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
673 if (softc->restart) {
675 cam_error_print(done_ccb,
676 CAM_ESF_ALL, CAM_EPF_ALL);
678 } else if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART)
680 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
681 /* Don't wedge the queue */
682 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
685 status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
686 if (softc->restart) {
688 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
691 if (softc->faults < 10)
696 /* Old PIO2 devices may not support mode setting. */
697 } else if (softc->action == PROBE_SETMODE &&
698 status == CAM_ATA_STATUS_ERROR &&
699 ata_max_pmode(ident_buf) <= ATA_PIO2 &&
700 (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) {
704 * Some old WD SATA disks report supported and enabled
705 * device-initiated interface power management, but return
706 * ABORT on attempt to disable it.
708 } else if (softc->action == PROBE_SETPM &&
709 status == CAM_ATA_STATUS_ERROR) {
713 * Some HP SATA disks report supported DMA Auto-Activation,
714 * but return ABORT on attempt to enable it.
716 } else if (softc->action == PROBE_SETDMAAA &&
717 status == CAM_ATA_STATUS_ERROR) {
722 * If we get to this point, we got an error status back
723 * from the inquiry and the error status doesn't require
724 * automatically retrying the command. Therefore, the
725 * inquiry failed. If we had inquiry information before
726 * for this device, but this latest inquiry command failed,
727 * the device has probably gone away. If this device isn't
728 * already marked unconfigured, notify the peripheral
729 * drivers that this device is no more.
731 device_fail: if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
732 xpt_async(AC_LOST_DEVICE, path, NULL);
733 PROBE_SET_ACTION(softc, PROBE_INVALID);
740 switch (softc->action) {
743 int sign = (done_ccb->ataio.res.lba_high << 8) +
744 done_ccb->ataio.res.lba_mid;
745 CAM_DEBUG(path, CAM_DEBUG_PROBE,
746 ("SIGNATURE: %04x\n", sign));
747 if (sign == 0x0000 &&
748 done_ccb->ccb_h.target_id != 15) {
749 path->device->protocol = PROTO_ATA;
750 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
751 } else if (sign == 0x9669 &&
752 done_ccb->ccb_h.target_id == 15) {
753 /* Report SIM that PM is present. */
754 bzero(&cts, sizeof(cts));
755 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
756 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
757 cts.type = CTS_TYPE_CURRENT_SETTINGS;
758 cts.xport_specific.sata.pm_present = 1;
759 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
760 xpt_action((union ccb *)&cts);
761 path->device->protocol = PROTO_SATAPM;
762 PROBE_SET_ACTION(softc, PROBE_PM_PID);
763 } else if (sign == 0xeb14 &&
764 done_ccb->ccb_h.target_id != 15) {
765 path->device->protocol = PROTO_SCSI;
766 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
768 if (done_ccb->ccb_h.target_id != 15) {
770 "Unexpected signature 0x%04x\n", sign);
774 xpt_release_ccb(done_ccb);
775 xpt_schedule(periph, priority);
780 struct ccb_pathinq cpi;
784 ident_buf = &softc->ident_data;
785 for (ptr = (int16_t *)ident_buf;
786 ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
787 *ptr = le16toh(*ptr);
789 if (strncmp(ident_buf->model, "FX", 2) &&
790 strncmp(ident_buf->model, "NEC", 3) &&
791 strncmp(ident_buf->model, "Pioneer", 7) &&
792 strncmp(ident_buf->model, "SHARP", 5)) {
793 ata_bswap(ident_buf->model, sizeof(ident_buf->model));
794 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
795 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
797 ata_btrim(ident_buf->model, sizeof(ident_buf->model));
798 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
799 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
800 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
801 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
802 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
803 /* Device may need spin-up before IDENTIFY become valid. */
804 if ((ident_buf->specconf == 0x37c8 ||
805 ident_buf->specconf == 0x738c) &&
806 ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
807 softc->spinup == 0)) {
808 PROBE_SET_ACTION(softc, PROBE_SPINUP);
809 xpt_release_ccb(done_ccb);
810 xpt_schedule(periph, priority);
813 ident_buf = &path->device->ident_data;
814 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
815 /* Check that it is the same device. */
816 if (bcmp(softc->ident_data.model, ident_buf->model,
817 sizeof(ident_buf->model)) ||
818 bcmp(softc->ident_data.revision, ident_buf->revision,
819 sizeof(ident_buf->revision)) ||
820 bcmp(softc->ident_data.serial, ident_buf->serial,
821 sizeof(ident_buf->serial))) {
822 /* Device changed. */
823 xpt_async(AC_LOST_DEVICE, path, NULL);
825 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
830 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
831 /* Clean up from previous instance of this device */
832 if (path->device->serial_num != NULL) {
833 free(path->device->serial_num, M_CAMXPT);
834 path->device->serial_num = NULL;
835 path->device->serial_num_len = 0;
837 path->device->serial_num =
838 (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
840 if (path->device->serial_num != NULL) {
841 bcopy(ident_buf->serial,
842 path->device->serial_num,
843 sizeof(ident_buf->serial));
844 path->device->serial_num[sizeof(ident_buf->serial)]
846 path->device->serial_num_len =
847 strlen(path->device->serial_num);
850 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
851 xpt_async(AC_GETDEV_CHANGED, path, NULL);
853 if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
854 path->device->mintags = 2;
855 path->device->maxtags =
856 ATA_QUEUE_LEN(ident_buf->queue) + 1;
858 ata_find_quirk(path->device);
859 if (path->device->mintags != 0 &&
860 path->bus->sim->max_tagged_dev_openings != 0) {
861 /* Check if the SIM does not want queued commands. */
862 bzero(&cpi, sizeof(cpi));
863 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
864 cpi.ccb_h.func_code = XPT_PATH_INQ;
865 xpt_action((union ccb *)&cpi);
866 if (cpi.ccb_h.status == CAM_REQ_CMP &&
867 (cpi.hba_inquiry & PI_TAG_ABLE)) {
868 /* Report SIM which tags are allowed. */
869 bzero(&cts, sizeof(cts));
870 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
871 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
872 cts.type = CTS_TYPE_CURRENT_SETTINGS;
873 cts.xport_specific.sata.tags = path->device->maxtags;
874 cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
875 xpt_action((union ccb *)&cts);
878 ata_device_transport(path);
880 proberequestdefaultnegotiation(periph);
881 PROBE_SET_ACTION(softc, PROBE_SETMODE);
882 xpt_release_ccb(done_ccb);
883 xpt_schedule(periph, priority);
888 xpt_print(path, "Spin-up done\n");
890 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
891 xpt_release_ccb(done_ccb);
892 xpt_schedule(periph, priority);
895 if (path->device->transport != XPORT_SATA)
897 /* Set supported bits. */
898 bzero(&cts, sizeof(cts));
899 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
900 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
901 cts.type = CTS_TYPE_CURRENT_SETTINGS;
902 xpt_action((union ccb *)&cts);
903 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
904 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
907 if (ident_buf->satacapabilities != 0xffff) {
908 if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
909 caps |= CTS_SATA_CAPS_D_PMREQ;
910 if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
911 caps |= CTS_SATA_CAPS_D_APST;
913 /* Mask unwanted bits. */
914 bzero(&cts, sizeof(cts));
915 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
916 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
917 cts.type = CTS_TYPE_USER_SETTINGS;
918 xpt_action((union ccb *)&cts);
919 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
920 caps &= cts.xport_specific.sata.caps;
923 /* Store result to SIM. */
924 bzero(&cts, sizeof(cts));
925 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
926 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
927 cts.type = CTS_TYPE_CURRENT_SETTINGS;
928 cts.xport_specific.sata.caps = caps;
929 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
930 xpt_action((union ccb *)&cts);
932 if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
933 (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
934 (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
935 PROBE_SET_ACTION(softc, PROBE_SETPM);
936 xpt_release_ccb(done_ccb);
937 xpt_schedule(periph, priority);
942 if (ident_buf->satacapabilities != 0xffff &&
943 (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
944 (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
945 (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
946 PROBE_SET_ACTION(softc, PROBE_SETAPST);
947 xpt_release_ccb(done_ccb);
948 xpt_schedule(periph, priority);
953 if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
954 (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
955 (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
956 PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
957 xpt_release_ccb(done_ccb);
958 xpt_schedule(periph, priority);
963 if ((ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) &&
964 (!(softc->caps & CTS_SATA_CAPS_H_AN)) !=
965 (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) {
966 PROBE_SET_ACTION(softc, PROBE_SETAN);
967 xpt_release_ccb(done_ccb);
968 xpt_schedule(periph, priority);
974 if (path->device->protocol == PROTO_ATA) {
975 PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
977 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
979 xpt_release_ccb(done_ccb);
980 xpt_schedule(periph, priority);
982 case PROBE_SET_MULTI:
983 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
984 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
985 xpt_acquire_device(path->device);
986 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
987 xpt_action(done_ccb);
988 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
991 PROBE_SET_ACTION(softc, PROBE_DONE);
994 case PROBE_FULL_INQUIRY:
996 struct scsi_inquiry_data *inq_buf;
997 u_int8_t periph_qual, len;
999 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1000 inq_buf = &path->device->inq_data;
1002 periph_qual = SID_QUAL(inq_buf);
1004 if (periph_qual != SID_QUAL_LU_CONNECTED)
1008 * We conservatively request only
1009 * SHORT_INQUIRY_LEN bytes of inquiry
1010 * information during our first try
1011 * at sending an INQUIRY. If the device
1012 * has more information to give,
1013 * perform a second request specifying
1014 * the amount of information the device
1015 * is willing to give.
1017 len = inq_buf->additional_length
1018 + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1019 if (softc->action == PROBE_INQUIRY
1020 && len > SHORT_INQUIRY_LENGTH) {
1021 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1022 xpt_release_ccb(done_ccb);
1023 xpt_schedule(periph, priority);
1027 ata_device_transport(path);
1028 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1029 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1030 xpt_acquire_device(path->device);
1031 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1032 xpt_action(done_ccb);
1033 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
1035 PROBE_SET_ACTION(softc, PROBE_DONE);
1039 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1040 bzero(ident_buf, sizeof(*ident_buf));
1041 softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1042 (done_ccb->ataio.res.lba_mid << 16) +
1043 (done_ccb->ataio.res.lba_low << 8) +
1044 done_ccb->ataio.res.sector_count;
1045 ((uint32_t *)ident_buf)[0] = softc->pm_pid;
1046 snprintf(ident_buf->model, sizeof(ident_buf->model),
1047 "Port Multiplier %08x", softc->pm_pid);
1048 PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1049 xpt_release_ccb(done_ccb);
1050 xpt_schedule(periph, priority);
1053 softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1054 (done_ccb->ataio.res.lba_mid << 16) +
1055 (done_ccb->ataio.res.lba_low << 8) +
1056 done_ccb->ataio.res.sector_count;
1057 ((uint32_t *)ident_buf)[1] = softc->pm_prv;
1058 snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1059 "%04x", softc->pm_prv);
1060 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1061 ata_device_transport(path);
1062 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
1063 proberequestdefaultnegotiation(periph);
1064 /* Set supported bits. */
1065 bzero(&cts, sizeof(cts));
1066 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1067 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1068 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1069 xpt_action((union ccb *)&cts);
1070 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1071 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1074 /* All PMPs must support PM requests. */
1075 caps |= CTS_SATA_CAPS_D_PMREQ;
1076 /* Mask unwanted bits. */
1077 bzero(&cts, sizeof(cts));
1078 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1079 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1080 cts.type = CTS_TYPE_USER_SETTINGS;
1081 xpt_action((union ccb *)&cts);
1082 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1083 caps &= cts.xport_specific.sata.caps;
1086 /* Store result to SIM. */
1087 bzero(&cts, sizeof(cts));
1088 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1089 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1090 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1091 cts.xport_specific.sata.caps = caps;
1092 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1093 xpt_action((union ccb *)&cts);
1095 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1096 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1097 xpt_acquire_device(path->device);
1098 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1099 xpt_action(done_ccb);
1100 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1103 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1104 xpt_action(done_ccb);
1105 xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
1107 PROBE_SET_ACTION(softc, PROBE_DONE);
1110 panic("probedone: invalid action state 0x%x\n", softc->action);
1113 if (softc->restart) {
1115 xpt_release_ccb(done_ccb);
1116 probeschedule(periph);
1119 xpt_release_ccb(done_ccb);
1120 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1121 while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1122 TAILQ_REMOVE(&softc->request_ccbs,
1123 &done_ccb->ccb_h, periph_links.tqe);
1124 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1127 cam_periph_invalidate(periph);
1128 cam_release_devq(periph->path,
1129 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
1130 cam_periph_release_locked(periph);
1134 probecleanup(struct cam_periph *periph)
1136 free(periph->softc, M_CAMXPT);
1140 ata_find_quirk(struct cam_ed *device)
1142 struct ata_quirk_entry *quirk;
1145 match = cam_quirkmatch((caddr_t)&device->ident_data,
1146 (caddr_t)ata_quirk_table,
1147 ata_quirk_table_size,
1148 sizeof(*ata_quirk_table), ata_identify_match);
1151 panic("xpt_find_quirk: device didn't match wildcard entry!!");
1153 quirk = (struct ata_quirk_entry *)match;
1154 device->quirk = quirk;
1155 if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
1156 device->mintags = quirk->mintags;
1157 device->maxtags = quirk->maxtags;
1162 union ccb *request_ccb;
1163 struct ccb_pathinq *cpi;
1165 } ata_scan_bus_info;
1168 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1169 * As the scan progresses, xpt_scan_bus is used as the
1170 * callback on completion function.
1173 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1175 struct cam_path *path;
1176 ata_scan_bus_info *scan_info;
1177 union ccb *work_ccb, *reset_ccb;
1180 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1181 ("xpt_scan_bus\n"));
1182 switch (request_ccb->ccb_h.func_code) {
1185 /* Find out the characteristics of the bus */
1186 work_ccb = xpt_alloc_ccb_nowait();
1187 if (work_ccb == NULL) {
1188 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1189 xpt_done(request_ccb);
1192 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1193 request_ccb->ccb_h.pinfo.priority);
1194 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1195 xpt_action(work_ccb);
1196 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1197 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1198 xpt_free_ccb(work_ccb);
1199 xpt_done(request_ccb);
1203 /* We may need to reset bus first, if we haven't done it yet. */
1204 if ((work_ccb->cpi.hba_inquiry &
1205 (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1206 !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1207 !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1208 reset_ccb = xpt_alloc_ccb_nowait();
1209 if (reset_ccb == NULL) {
1210 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1211 xpt_free_ccb(work_ccb);
1212 xpt_done(request_ccb);
1215 xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1217 reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1218 xpt_action(reset_ccb);
1219 if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1220 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1221 xpt_free_ccb(reset_ccb);
1222 xpt_free_ccb(work_ccb);
1223 xpt_done(request_ccb);
1226 xpt_free_ccb(reset_ccb);
1229 /* Save some state for use while we probe for devices */
1230 scan_info = (ata_scan_bus_info *)
1231 malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1232 if (scan_info == NULL) {
1233 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1234 xpt_free_ccb(work_ccb);
1235 xpt_done(request_ccb);
1238 scan_info->request_ccb = request_ccb;
1239 scan_info->cpi = &work_ccb->cpi;
1240 /* If PM supported, probe it first. */
1241 if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1242 scan_info->counter = scan_info->cpi->max_target;
1244 scan_info->counter = 0;
1246 work_ccb = xpt_alloc_ccb_nowait();
1247 if (work_ccb == NULL) {
1248 free(scan_info, M_CAMXPT);
1249 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1250 xpt_done(request_ccb);
1255 work_ccb = request_ccb;
1256 /* Reuse the same CCB to query if a device was really found */
1257 scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1258 /* If there is PMP... */
1259 if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1260 (scan_info->counter == scan_info->cpi->max_target)) {
1261 if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1262 /* everything else will be probed by it */
1263 /* Free the current request path- we're done with it. */
1264 xpt_free_path(work_ccb->ccb_h.path);
1267 struct ccb_trans_settings cts;
1269 /* Report SIM that PM is absent. */
1270 bzero(&cts, sizeof(cts));
1271 xpt_setup_ccb(&cts.ccb_h,
1272 work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1273 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1274 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1275 cts.xport_specific.sata.pm_present = 0;
1276 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1277 xpt_action((union ccb *)&cts);
1280 /* Free the current request path- we're done with it. */
1281 xpt_free_path(work_ccb->ccb_h.path);
1282 if (scan_info->counter ==
1283 ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1284 0 : scan_info->cpi->max_target)) {
1286 xpt_free_ccb(work_ccb);
1287 xpt_free_ccb((union ccb *)scan_info->cpi);
1288 request_ccb = scan_info->request_ccb;
1289 free(scan_info, M_CAMXPT);
1290 request_ccb->ccb_h.status = CAM_REQ_CMP;
1291 xpt_done(request_ccb);
1294 /* Take next device. Wrap from max (PMP) to 0. */
1295 scan_info->counter = (scan_info->counter + 1 ) %
1296 (scan_info->cpi->max_target + 1);
1298 status = xpt_create_path(&path, xpt_periph,
1299 scan_info->request_ccb->ccb_h.path_id,
1300 scan_info->counter, 0);
1301 if (status != CAM_REQ_CMP) {
1302 printf("xpt_scan_bus: xpt_create_path failed"
1303 " with status %#x, bus scan halted\n",
1305 xpt_free_ccb(work_ccb);
1306 xpt_free_ccb((union ccb *)scan_info->cpi);
1307 request_ccb = scan_info->request_ccb;
1308 free(scan_info, M_CAMXPT);
1309 request_ccb->ccb_h.status = status;
1310 xpt_done(request_ccb);
1313 xpt_setup_ccb(&work_ccb->ccb_h, path,
1314 scan_info->request_ccb->ccb_h.pinfo.priority);
1315 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1316 work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1317 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1318 work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1319 xpt_action(work_ccb);
1327 ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1328 cam_flags flags, union ccb *request_ccb)
1330 struct ccb_pathinq cpi;
1332 struct cam_path *new_path;
1333 struct cam_periph *old_periph;
1335 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1337 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1338 cpi.ccb_h.func_code = XPT_PATH_INQ;
1339 xpt_action((union ccb *)&cpi);
1341 if (cpi.ccb_h.status != CAM_REQ_CMP) {
1342 if (request_ccb != NULL) {
1343 request_ccb->ccb_h.status = cpi.ccb_h.status;
1344 xpt_done(request_ccb);
1349 if (request_ccb == NULL) {
1350 request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1351 if (request_ccb == NULL) {
1352 xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1353 "can't continue\n");
1356 new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1357 if (new_path == NULL) {
1358 xpt_print(path, "xpt_scan_lun: can't allocate path, "
1359 "can't continue\n");
1360 free(request_ccb, M_CAMXPT);
1363 status = xpt_compile_path(new_path, xpt_periph,
1365 path->target->target_id,
1366 path->device->lun_id);
1368 if (status != CAM_REQ_CMP) {
1369 xpt_print(path, "xpt_scan_lun: can't compile path, "
1370 "can't continue\n");
1371 free(request_ccb, M_CAMXPT);
1372 free(new_path, M_CAMXPT);
1375 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1376 request_ccb->ccb_h.cbfcnp = xptscandone;
1377 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1378 request_ccb->crcn.flags = flags;
1381 if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1382 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
1385 softc = (probe_softc *)old_periph->softc;
1386 TAILQ_INSERT_TAIL(&softc->request_ccbs,
1387 &request_ccb->ccb_h, periph_links.tqe);
1390 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1391 xpt_done(request_ccb);
1394 status = cam_periph_alloc(proberegister, NULL, probecleanup,
1395 probestart, "aprobe",
1397 request_ccb->ccb_h.path, NULL, 0,
1400 if (status != CAM_REQ_CMP) {
1401 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1402 "returned an error, can't continue probe\n");
1403 request_ccb->ccb_h.status = status;
1404 xpt_done(request_ccb);
1410 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1412 xpt_release_path(done_ccb->ccb_h.path);
1413 free(done_ccb->ccb_h.path, M_CAMXPT);
1414 free(done_ccb, M_CAMXPT);
1417 static struct cam_ed *
1418 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1420 struct cam_path path;
1421 struct ata_quirk_entry *quirk;
1422 struct cam_ed *device;
1423 struct cam_ed *cur_device;
1425 device = xpt_alloc_device(bus, target, lun_id);
1430 * Take the default quirk entry until we have inquiry
1431 * data and can determine a better quirk to use.
1433 quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1434 device->quirk = (void *)quirk;
1435 device->mintags = 0;
1436 device->maxtags = 0;
1437 bzero(&device->inq_data, sizeof(device->inq_data));
1438 device->inq_flags = 0;
1439 device->queue_flags = 0;
1440 device->serial_num = NULL;
1441 device->serial_num_len = 0;
1444 * XXX should be limited by number of CCBs this bus can
1447 bus->sim->max_ccbs += device->ccbq.devq_openings;
1448 /* Insertion sort into our target's device list */
1449 cur_device = TAILQ_FIRST(&target->ed_entries);
1450 while (cur_device != NULL && cur_device->lun_id < lun_id)
1451 cur_device = TAILQ_NEXT(cur_device, links);
1452 if (cur_device != NULL) {
1453 TAILQ_INSERT_BEFORE(cur_device, device, links);
1455 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1457 target->generation++;
1458 if (lun_id != CAM_LUN_WILDCARD) {
1459 xpt_compile_path(&path,
1464 ata_device_transport(&path);
1465 xpt_release_path(&path);
1472 ata_device_transport(struct cam_path *path)
1474 struct ccb_pathinq cpi;
1475 struct ccb_trans_settings cts;
1476 struct scsi_inquiry_data *inq_buf = NULL;
1477 struct ata_params *ident_buf = NULL;
1479 /* Get transport information from the SIM */
1480 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1481 cpi.ccb_h.func_code = XPT_PATH_INQ;
1482 xpt_action((union ccb *)&cpi);
1484 path->device->transport = cpi.transport;
1485 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1486 inq_buf = &path->device->inq_data;
1487 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1488 ident_buf = &path->device->ident_data;
1489 if (path->device->protocol == PROTO_ATA) {
1490 path->device->protocol_version = ident_buf ?
1491 ata_version(ident_buf->version_major) : cpi.protocol_version;
1492 } else if (path->device->protocol == PROTO_SCSI) {
1493 path->device->protocol_version = inq_buf ?
1494 SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1496 path->device->transport_version = ident_buf ?
1497 ata_version(ident_buf->version_major) : cpi.transport_version;
1499 /* Tell the controller what we think */
1500 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1501 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1502 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1503 cts.transport = path->device->transport;
1504 cts.transport_version = path->device->transport_version;
1505 cts.protocol = path->device->protocol;
1506 cts.protocol_version = path->device->protocol_version;
1507 cts.proto_specific.valid = 0;
1509 if (path->device->transport == XPORT_ATA) {
1510 cts.xport_specific.ata.atapi =
1511 (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1512 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1513 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1514 cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1516 cts.xport_specific.sata.atapi =
1517 (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1518 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1519 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1520 cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1523 cts.xport_specific.valid = 0;
1524 xpt_action((union ccb *)&cts);
1528 ata_action(union ccb *start_ccb)
1531 switch (start_ccb->ccb_h.func_code) {
1532 case XPT_SET_TRAN_SETTINGS:
1534 ata_set_transfer_settings(&start_ccb->cts,
1535 start_ccb->ccb_h.path->device,
1536 /*async_update*/FALSE);
1541 ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1544 ata_scan_lun(start_ccb->ccb_h.path->periph,
1545 start_ccb->ccb_h.path, start_ccb->crcn.flags,
1548 case XPT_GET_TRAN_SETTINGS:
1550 ata_get_transfer_settings(&start_ccb->cts);
1555 struct cam_ed *device;
1558 device = start_ccb->ccb_h.path->device;
1559 if (device->protocol == PROTO_SCSI &&
1560 (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1562 device->ident_data.config & ATA_PROTO_MASK;
1565 (device->ident_data.config == ATA_PROTO_CFA) ? 0 :
1566 (p == ATA_PROTO_ATAPI_16) ? 16 :
1567 (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1569 if (start_ccb->csio.cdb_len > maxlen) {
1570 start_ccb->ccb_h.status = CAM_REQ_INVALID;
1571 xpt_done(start_ccb);
1577 xpt_action_default(start_ccb);
1583 ata_get_transfer_settings(struct ccb_trans_settings *cts)
1585 struct ccb_trans_settings_ata *ata;
1586 struct ccb_trans_settings_scsi *scsi;
1587 struct cam_ed *device;
1588 struct cam_sim *sim;
1590 device = cts->ccb_h.path->device;
1591 sim = cts->ccb_h.path->bus->sim;
1592 (*(sim->sim_action))(sim, (union ccb *)cts);
1594 if (cts->protocol == PROTO_UNKNOWN ||
1595 cts->protocol == PROTO_UNSPECIFIED) {
1596 cts->protocol = device->protocol;
1597 cts->protocol_version = device->protocol_version;
1600 if (cts->protocol == PROTO_ATA) {
1601 ata = &cts->proto_specific.ata;
1602 if ((ata->valid & CTS_ATA_VALID_TQ) == 0) {
1603 ata->valid |= CTS_ATA_VALID_TQ;
1604 if (cts->type == CTS_TYPE_USER_SETTINGS ||
1605 (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1606 (device->inq_flags & SID_CmdQue) != 0)
1607 ata->flags |= CTS_ATA_FLAGS_TAG_ENB;
1610 if (cts->protocol == PROTO_SCSI) {
1611 scsi = &cts->proto_specific.scsi;
1612 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1613 scsi->valid |= CTS_SCSI_VALID_TQ;
1614 if (cts->type == CTS_TYPE_USER_SETTINGS ||
1615 (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1616 (device->inq_flags & SID_CmdQue) != 0)
1617 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1621 if (cts->transport == XPORT_UNKNOWN ||
1622 cts->transport == XPORT_UNSPECIFIED) {
1623 cts->transport = device->transport;
1624 cts->transport_version = device->transport_version;
1629 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1632 struct ccb_pathinq cpi;
1633 struct ccb_trans_settings_ata *ata;
1634 struct ccb_trans_settings_scsi *scsi;
1635 struct cam_sim *sim;
1636 struct ata_params *ident_data;
1637 struct scsi_inquiry_data *inq_data;
1639 if (device == NULL) {
1640 cts->ccb_h.status = CAM_PATH_INVALID;
1641 xpt_done((union ccb *)cts);
1645 if (cts->protocol == PROTO_UNKNOWN
1646 || cts->protocol == PROTO_UNSPECIFIED) {
1647 cts->protocol = device->protocol;
1648 cts->protocol_version = device->protocol_version;
1651 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1652 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1653 cts->protocol_version = device->protocol_version;
1655 if (cts->protocol != device->protocol) {
1656 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1657 cts->protocol, device->protocol);
1658 cts->protocol = device->protocol;
1661 if (cts->protocol_version > device->protocol_version) {
1663 xpt_print(cts->ccb_h.path, "Down reving Protocol "
1664 "Version from %d to %d?\n", cts->protocol_version,
1665 device->protocol_version);
1667 cts->protocol_version = device->protocol_version;
1670 if (cts->transport == XPORT_UNKNOWN
1671 || cts->transport == XPORT_UNSPECIFIED) {
1672 cts->transport = device->transport;
1673 cts->transport_version = device->transport_version;
1676 if (cts->transport_version == XPORT_VERSION_UNKNOWN
1677 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1678 cts->transport_version = device->transport_version;
1680 if (cts->transport != device->transport) {
1681 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1682 cts->transport, device->transport);
1683 cts->transport = device->transport;
1686 if (cts->transport_version > device->transport_version) {
1688 xpt_print(cts->ccb_h.path, "Down reving Transport "
1689 "Version from %d to %d?\n", cts->transport_version,
1690 device->transport_version);
1692 cts->transport_version = device->transport_version;
1695 sim = cts->ccb_h.path->bus->sim;
1696 ident_data = &device->ident_data;
1697 inq_data = &device->inq_data;
1698 if (cts->protocol == PROTO_ATA)
1699 ata = &cts->proto_specific.ata;
1702 if (cts->protocol == PROTO_SCSI)
1703 scsi = &cts->proto_specific.scsi;
1706 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1707 cpi.ccb_h.func_code = XPT_PATH_INQ;
1708 xpt_action((union ccb *)&cpi);
1710 /* Sanity checking */
1711 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1712 || (ata && (ident_data->satacapabilities & ATA_SUPPORT_NCQ) == 0)
1713 || (scsi && (INQ_DATA_TQ_ENABLED(inq_data)) == 0)
1714 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1715 || (device->mintags == 0)) {
1717 * Can't tag on hardware that doesn't support tags,
1718 * doesn't have it enabled, or has broken tag support.
1721 ata->flags &= ~CTS_ATA_FLAGS_TAG_ENB;
1723 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1726 /* Start/stop tags use. */
1727 if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1728 ((ata && (ata->valid & CTS_ATA_VALID_TQ) != 0) ||
1729 (scsi && (scsi->valid & CTS_SCSI_VALID_TQ) != 0))) {
1732 nowt = ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1733 (device->inq_flags & SID_CmdQue) != 0);
1735 newt = (ata->flags & CTS_ATA_FLAGS_TAG_ENB) != 0;
1737 newt = (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0;
1739 if (newt && !nowt) {
1741 * Delay change to use tags until after a
1742 * few commands have gone to this device so
1743 * the controller has time to perform transfer
1744 * negotiations without tagged messages getting
1747 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1748 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1749 } else if (nowt && !newt)
1750 xpt_stop_tags(cts->ccb_h.path);
1753 if (async_update == FALSE)
1754 (*(sim->sim_action))(sim, (union ccb *)cts);
1758 * Handle any per-device event notifications that require action by the XPT.
1761 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1762 struct cam_ed *device, void *async_arg)
1765 struct cam_path newpath;
1768 * We only need to handle events for real devices.
1770 if (target->target_id == CAM_TARGET_WILDCARD
1771 || device->lun_id == CAM_LUN_WILDCARD)
1775 * We need our own path with wildcards expanded to
1776 * handle certain types of events.
1778 if ((async_code == AC_SENT_BDR)
1779 || (async_code == AC_BUS_RESET)
1780 || (async_code == AC_INQ_CHANGED))
1781 status = xpt_compile_path(&newpath, NULL,
1786 status = CAM_REQ_CMP_ERR;
1788 if (status == CAM_REQ_CMP) {
1789 if (async_code == AC_INQ_CHANGED) {
1791 * We've sent a start unit command, or
1792 * something similar to a device that
1793 * may have caused its inquiry data to
1794 * change. So we re-scan the device to
1795 * refresh the inquiry data for it.
1797 ata_scan_lun(newpath.periph, &newpath,
1798 CAM_EXPECT_INQ_CHANGE, NULL);
1800 /* We need to reinitialize device after reset. */
1801 ata_scan_lun(newpath.periph, &newpath,
1804 xpt_release_path(&newpath);
1805 } else if (async_code == AC_LOST_DEVICE &&
1806 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1807 device->flags |= CAM_DEV_UNCONFIGURED;
1808 xpt_release_device(device);
1809 } else if (async_code == AC_TRANSFER_NEG) {
1810 struct ccb_trans_settings *settings;
1812 settings = (struct ccb_trans_settings *)async_arg;
1813 ata_set_transfer_settings(settings, device,
1814 /*async_update*/TRUE);
1819 ata_announce_periph(struct cam_periph *periph)
1821 struct ccb_pathinq cpi;
1822 struct ccb_trans_settings cts;
1823 struct cam_path *path = periph->path;
1827 mtx_assert(periph->sim->mtx, MA_OWNED);
1829 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1830 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1831 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1832 xpt_action((union ccb*)&cts);
1833 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1835 /* Ask the SIM for its base transfer speed */
1836 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1837 cpi.ccb_h.func_code = XPT_PATH_INQ;
1838 xpt_action((union ccb *)&cpi);
1839 /* Report connection speed */
1840 speed = cpi.base_transfer_speed;
1841 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1842 struct ccb_trans_settings_pata *pata =
1843 &cts.xport_specific.ata;
1845 if (pata->valid & CTS_ATA_VALID_MODE)
1846 speed = ata_mode2speed(pata->mode);
1848 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1849 struct ccb_trans_settings_sata *sata =
1850 &cts.xport_specific.sata;
1852 if (sata->valid & CTS_SATA_VALID_REVISION)
1853 speed = ata_revision2speed(sata->revision);
1857 printf("%s%d: %d.%03dMB/s transfers",
1858 periph->periph_name, periph->unit_number,
1861 printf("%s%d: %dKB/s transfers", periph->periph_name,
1862 periph->unit_number, speed);
1863 /* Report additional information about connection */
1864 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1865 struct ccb_trans_settings_pata *pata =
1866 &cts.xport_specific.ata;
1869 if (pata->valid & CTS_ATA_VALID_MODE)
1870 printf("%s, ", ata_mode2string(pata->mode));
1871 if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
1872 printf("ATAPI %dbytes, ", pata->atapi);
1873 if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
1874 printf("PIO %dbytes", pata->bytecount);
1877 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1878 struct ccb_trans_settings_sata *sata =
1879 &cts.xport_specific.sata;
1882 if (sata->valid & CTS_SATA_VALID_REVISION)
1883 printf("SATA %d.x, ", sata->revision);
1886 if (sata->valid & CTS_SATA_VALID_MODE)
1887 printf("%s, ", ata_mode2string(sata->mode));
1888 if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
1889 printf("ATAPI %dbytes, ", sata->atapi);
1890 if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
1891 printf("PIO %dbytes", sata->bytecount);