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);
98 static char *probe_action_text[] = {
108 "PROBE_FULL_INQUIRY",
114 #define PROBE_SET_ACTION(softc, newaction) \
117 text = probe_action_text; \
118 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO, \
119 ("Probe %s to %s\n", text[(softc)->action], \
120 text[(newaction)])); \
121 (softc)->action = (newaction); \
125 PROBE_NO_ANNOUNCE = 0x04
129 TAILQ_HEAD(, ccb_hdr) request_ccbs;
130 struct ata_params ident_data;
138 struct cam_periph *periph;
141 static struct ata_quirk_entry ata_quirk_table[] =
144 /* Default tagged queuing parameters for all devices */
146 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
147 /*vendor*/"*", /*product*/"*", /*revision*/"*"
149 /*quirks*/0, /*maxtags*/0
153 static const int ata_quirk_table_size =
154 sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
156 static cam_status proberegister(struct cam_periph *periph,
158 static void probeschedule(struct cam_periph *probe_periph);
159 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
160 //static void proberequestdefaultnegotiation(struct cam_periph *periph);
161 //static int proberequestbackoff(struct cam_periph *periph,
162 // struct cam_ed *device);
163 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
164 static void probecleanup(struct cam_periph *periph);
165 static void ata_find_quirk(struct cam_ed *device);
166 static void ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
167 static void ata_scan_lun(struct cam_periph *periph,
168 struct cam_path *path, cam_flags flags,
170 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
171 static struct cam_ed *
172 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
174 static void ata_device_transport(struct cam_path *path);
175 static void ata_set_transfer_settings(struct ccb_trans_settings *cts,
176 struct cam_ed *device,
178 static void ata_dev_async(u_int32_t async_code,
180 struct cam_et *target,
181 struct cam_ed *device,
183 static void ata_action(union ccb *start_ccb);
184 static void ata_announce_periph(struct cam_periph *periph);
186 static struct xpt_xport ata_xport = {
187 .alloc_device = ata_alloc_device,
188 .action = ata_action,
189 .async = ata_dev_async,
190 .announce = ata_announce_periph,
205 proberegister(struct cam_periph *periph, void *arg)
207 union ccb *request_ccb; /* CCB representing the probe request */
211 request_ccb = (union ccb *)arg;
212 if (periph == NULL) {
213 printf("proberegister: periph was NULL!!\n");
214 return(CAM_REQ_CMP_ERR);
217 if (request_ccb == NULL) {
218 printf("proberegister: no probe CCB, "
219 "can't register device\n");
220 return(CAM_REQ_CMP_ERR);
223 softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
226 printf("proberegister: Unable to probe new device. "
227 "Unable to allocate softc\n");
228 return(CAM_REQ_CMP_ERR);
230 TAILQ_INIT(&softc->request_ccbs);
231 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
234 periph->softc = softc;
235 softc->periph = periph;
236 softc->action = PROBE_INVALID;
237 status = cam_periph_acquire(periph);
238 if (status != CAM_REQ_CMP) {
242 * Ensure nobody slip in until probe finish.
244 cam_freeze_devq_arg(periph->path,
245 RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
246 probeschedule(periph);
251 probeschedule(struct cam_periph *periph)
256 softc = (probe_softc *)periph->softc;
257 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
259 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
260 periph->path->device->protocol == PROTO_SATAPM)
261 PROBE_SET_ACTION(softc, PROBE_RESET);
263 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
265 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
266 softc->flags |= PROBE_NO_ANNOUNCE;
268 softc->flags &= ~PROBE_NO_ANNOUNCE;
270 xpt_schedule(periph, CAM_PRIORITY_XPT);
274 probestart(struct cam_periph *periph, union ccb *start_ccb)
276 struct ccb_trans_settings cts;
277 struct ccb_ataio *ataio;
278 struct ccb_scsiio *csio;
280 struct cam_path *path;
281 struct ata_params *ident_buf;
283 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
285 softc = (probe_softc *)periph->softc;
286 path = start_ccb->ccb_h.path;
287 ataio = &start_ccb->ataio;
288 csio = &start_ccb->csio;
289 ident_buf = &periph->path->device->ident_data;
291 if (softc->restart) {
293 if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
294 path->device->protocol == PROTO_SATAPM)
295 softc->action = PROBE_RESET;
297 softc->action = PROBE_IDENTIFY;
299 switch (softc->action) {
301 cam_fill_ataio(ataio,
304 /*flags*/CAM_DIR_NONE,
309 ata_reset_cmd(ataio);
312 cam_fill_ataio(ataio,
317 /*data_ptr*/(u_int8_t *)&softc->ident_data,
318 /*dxfer_len*/sizeof(softc->ident_data),
320 if (periph->path->device->protocol == PROTO_ATA)
321 ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
323 ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
327 xpt_print(path, "Spinning up device\n");
328 cam_fill_ataio(ataio,
331 /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
336 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
343 /* Fetch user modes from SIM. */
344 bzero(&cts, sizeof(cts));
345 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
346 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
347 cts.type = CTS_TYPE_USER_SETTINGS;
348 xpt_action((union ccb *)&cts);
349 if (path->device->transport == XPORT_ATA) {
350 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
351 mode = cts.xport_specific.ata.mode;
353 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
354 mode = cts.xport_specific.sata.mode;
357 /* Honor device capabilities. */
358 wantmode = mode = ata_max_mode(ident_buf, mode);
359 /* Report modes to SIM. */
360 bzero(&cts, sizeof(cts));
361 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
362 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
363 cts.type = CTS_TYPE_CURRENT_SETTINGS;
364 if (path->device->transport == XPORT_ATA) {
365 cts.xport_specific.ata.mode = mode;
366 cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
368 cts.xport_specific.sata.mode = mode;
369 cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
371 xpt_action((union ccb *)&cts);
372 /* Fetch current modes from SIM. */
373 bzero(&cts, sizeof(cts));
374 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
375 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
376 cts.type = CTS_TYPE_CURRENT_SETTINGS;
377 xpt_action((union ccb *)&cts);
378 if (path->device->transport == XPORT_ATA) {
379 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
380 mode = cts.xport_specific.ata.mode;
382 if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
383 mode = cts.xport_specific.sata.mode;
385 /* If SIM disagree - renegotiate. */
386 if (mode != wantmode)
388 cam_fill_ataio(ataio,
391 /*flags*/CAM_DIR_NONE,
396 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
400 cam_fill_ataio(ataio,
408 ata_28bit_cmd(ataio, ATA_SETFEATURES,
409 (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
413 cam_fill_ataio(ataio,
421 ata_28bit_cmd(ataio, ATA_SETFEATURES,
422 (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
426 cam_fill_ataio(ataio,
434 ata_28bit_cmd(ataio, ATA_SETFEATURES,
435 (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
438 case PROBE_SET_MULTI:
440 u_int sectors, bytecount;
442 bytecount = 8192; /* SATA maximum */
443 /* Fetch user bytecount from SIM. */
444 bzero(&cts, sizeof(cts));
445 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
446 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
447 cts.type = CTS_TYPE_USER_SETTINGS;
448 xpt_action((union ccb *)&cts);
449 if (path->device->transport == XPORT_ATA) {
450 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
451 bytecount = cts.xport_specific.ata.bytecount;
453 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
454 bytecount = cts.xport_specific.sata.bytecount;
456 /* Honor device capabilities. */
457 sectors = max(1, min(ident_buf->sectors_intr & 0xff,
458 bytecount / ata_logical_sector_size(ident_buf)));
459 /* Report bytecount to SIM. */
460 bzero(&cts, sizeof(cts));
461 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
462 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
463 cts.type = CTS_TYPE_CURRENT_SETTINGS;
464 if (path->device->transport == XPORT_ATA) {
465 cts.xport_specific.ata.bytecount = sectors *
466 ata_logical_sector_size(ident_buf);
467 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
469 cts.xport_specific.sata.bytecount = sectors *
470 ata_logical_sector_size(ident_buf);
471 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
473 xpt_action((union ccb *)&cts);
474 /* Fetch current bytecount from SIM. */
475 bzero(&cts, sizeof(cts));
476 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
477 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
478 cts.type = CTS_TYPE_CURRENT_SETTINGS;
479 xpt_action((union ccb *)&cts);
480 if (path->device->transport == XPORT_ATA) {
481 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
482 bytecount = cts.xport_specific.ata.bytecount;
484 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
485 bytecount = cts.xport_specific.sata.bytecount;
487 sectors = bytecount / ata_logical_sector_size(ident_buf);
489 cam_fill_ataio(ataio,
497 ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
504 bytecount = 8192; /* SATA maximum */
505 /* Fetch user bytecount from SIM. */
506 bzero(&cts, sizeof(cts));
507 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
508 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
509 cts.type = CTS_TYPE_USER_SETTINGS;
510 xpt_action((union ccb *)&cts);
511 if (path->device->transport == XPORT_ATA) {
512 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
513 bytecount = cts.xport_specific.ata.bytecount;
515 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
516 bytecount = cts.xport_specific.sata.bytecount;
518 /* Honor device capabilities. */
520 bytecount = max(2, min(65534, bytecount));
521 if (ident_buf->satacapabilities != 0x0000 &&
522 ident_buf->satacapabilities != 0xffff) {
523 bytecount = min(8192, bytecount);
525 /* Report bytecount to SIM. */
526 bzero(&cts, sizeof(cts));
527 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
528 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
529 cts.type = CTS_TYPE_CURRENT_SETTINGS;
530 if (path->device->transport == XPORT_ATA) {
531 cts.xport_specific.ata.bytecount = bytecount;
532 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
534 cts.xport_specific.sata.bytecount = bytecount;
535 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
537 xpt_action((union ccb *)&cts);
540 case PROBE_FULL_INQUIRY:
543 struct scsi_inquiry_data *inq_buf =
544 &periph->path->device->inq_data;
546 if (softc->action == PROBE_INQUIRY)
547 inquiry_len = SHORT_INQUIRY_LENGTH;
549 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
551 * Some parallel SCSI devices fail to send an
552 * ignore wide residue message when dealing with
553 * odd length inquiry requests. Round up to be
556 inquiry_len = roundup2(inquiry_len, 2);
566 /*timeout*/60 * 1000);
570 cam_fill_ataio(ataio,
573 /*flags*/CAM_DIR_NONE,
578 ata_pm_read_cmd(ataio, 0, 15);
581 cam_fill_ataio(ataio,
584 /*flags*/CAM_DIR_NONE,
589 ata_pm_read_cmd(ataio, 1, 15);
592 CAM_DEBUG(path, CAM_DEBUG_INFO,
593 ("probestart: invalid action state\n"));
597 xpt_action(start_ccb);
601 proberequestdefaultnegotiation(struct cam_periph *periph)
603 struct ccb_trans_settings cts;
605 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
606 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
607 cts.type = CTS_TYPE_USER_SETTINGS;
608 xpt_action((union ccb *)&cts);
609 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
612 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
613 cts.type = CTS_TYPE_CURRENT_SETTINGS;
614 xpt_action((union ccb *)&cts);
618 * Backoff Negotiation Code- only pertinent for SPI devices.
621 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
623 struct ccb_trans_settings cts;
624 struct ccb_trans_settings_spi *spi;
626 memset(&cts, 0, sizeof (cts));
627 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
628 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
629 cts.type = CTS_TYPE_CURRENT_SETTINGS;
630 xpt_action((union ccb *)&cts);
631 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
633 xpt_print(periph->path,
634 "failed to get current device settings\n");
638 if (cts.transport != XPORT_SPI) {
640 xpt_print(periph->path, "not SPI transport\n");
644 spi = &cts.xport_specific.spi;
647 * We cannot renegotiate sync rate if we don't have one.
649 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
651 xpt_print(periph->path, "no sync rate known\n");
657 * We'll assert that we don't have to touch PPR options- the
658 * SIM will see what we do with period and offset and adjust
659 * the PPR options as appropriate.
663 * A sync rate with unknown or zero offset is nonsensical.
664 * A sync period of zero means Async.
666 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
667 || spi->sync_offset == 0 || spi->sync_period == 0) {
669 xpt_print(periph->path, "no sync rate available\n");
674 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
675 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
676 ("hit async: giving up on DV\n"));
682 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
683 * We don't try to remember 'last' settings to see if the SIM actually
684 * gets into the speed we want to set. We check on the SIM telling
685 * us that a requested speed is bad, but otherwise don't try and
686 * check the speed due to the asynchronous and handshake nature
689 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
692 if (spi->sync_period >= 0xf) {
693 spi->sync_period = 0;
694 spi->sync_offset = 0;
695 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
696 ("setting to async for DV\n"));
698 * Once we hit async, we don't want to try
701 device->flags |= CAM_DEV_DV_HIT_BOTTOM;
702 } else if (bootverbose) {
703 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
704 ("DV: period 0x%x\n", spi->sync_period));
705 printf("setting period to 0x%x\n", spi->sync_period);
707 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
708 cts.type = CTS_TYPE_CURRENT_SETTINGS;
709 xpt_action((union ccb *)&cts);
710 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
713 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
714 ("DV: failed to set period 0x%x\n", spi->sync_period));
715 if (spi->sync_period == 0) {
723 probedone(struct cam_periph *periph, union ccb *done_ccb)
725 struct ccb_trans_settings cts;
726 struct ata_params *ident_buf;
728 struct cam_path *path;
733 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
735 softc = (probe_softc *)periph->softc;
736 path = done_ccb->ccb_h.path;
737 priority = done_ccb->ccb_h.pinfo.priority;
738 ident_buf = &path->device->ident_data;
740 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
741 device_fail: if ((!softc->restart) &&
742 cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART) {
744 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
745 /* Don't wedge the queue */
746 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
749 /* Old PIO2 devices may not support mode setting. */
750 if (softc->action == PROBE_SETMODE &&
751 ata_max_pmode(ident_buf) <= ATA_PIO2 &&
752 (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
755 * If we get to this point, we got an error status back
756 * from the inquiry and the error status doesn't require
757 * automatically retrying the command. Therefore, the
758 * inquiry failed. If we had inquiry information before
759 * for this device, but this latest inquiry command failed,
760 * the device has probably gone away. If this device isn't
761 * already marked unconfigured, notify the peripheral
762 * drivers that this device is no more.
764 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
765 xpt_async(AC_LOST_DEVICE, path, NULL);
772 switch (softc->action) {
775 int sign = (done_ccb->ataio.res.lba_high << 8) +
776 done_ccb->ataio.res.lba_mid;
778 xpt_print(path, "SIGNATURE: %04x\n", sign);
779 if (sign == 0x0000 &&
780 done_ccb->ccb_h.target_id != 15) {
781 path->device->protocol = PROTO_ATA;
782 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
783 } else if (sign == 0x9669 &&
784 done_ccb->ccb_h.target_id == 15) {
785 /* Report SIM that PM is present. */
786 bzero(&cts, sizeof(cts));
787 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
788 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
789 cts.type = CTS_TYPE_CURRENT_SETTINGS;
790 cts.xport_specific.sata.pm_present = 1;
791 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
792 xpt_action((union ccb *)&cts);
793 path->device->protocol = PROTO_SATAPM;
794 PROBE_SET_ACTION(softc, PROBE_PM_PID);
795 } else if (sign == 0xeb14 &&
796 done_ccb->ccb_h.target_id != 15) {
797 path->device->protocol = PROTO_SCSI;
798 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
800 if (done_ccb->ccb_h.target_id != 15) {
802 "Unexpected signature 0x%04x\n", sign);
806 xpt_release_ccb(done_ccb);
807 xpt_schedule(periph, priority);
812 struct ccb_pathinq cpi;
816 ident_buf = &softc->ident_data;
817 for (ptr = (int16_t *)ident_buf;
818 ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
819 *ptr = le16toh(*ptr);
821 if (strncmp(ident_buf->model, "FX", 2) &&
822 strncmp(ident_buf->model, "NEC", 3) &&
823 strncmp(ident_buf->model, "Pioneer", 7) &&
824 strncmp(ident_buf->model, "SHARP", 5)) {
825 ata_bswap(ident_buf->model, sizeof(ident_buf->model));
826 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
827 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
829 ata_btrim(ident_buf->model, sizeof(ident_buf->model));
830 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
831 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
832 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
833 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
834 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
835 /* Device may need spin-up before IDENTIFY become valid. */
836 if ((ident_buf->specconf == 0x37c8 ||
837 ident_buf->specconf == 0x738c) &&
838 ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
839 softc->spinup == 0)) {
840 PROBE_SET_ACTION(softc, PROBE_SPINUP);
841 xpt_release_ccb(done_ccb);
842 xpt_schedule(periph, priority);
845 ident_buf = &path->device->ident_data;
846 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
847 /* Check that it is the same device. */
848 if (bcmp(softc->ident_data.model, ident_buf->model,
849 sizeof(ident_buf->model)) ||
850 bcmp(softc->ident_data.revision, ident_buf->revision,
851 sizeof(ident_buf->revision)) ||
852 bcmp(softc->ident_data.serial, ident_buf->serial,
853 sizeof(ident_buf->serial))) {
854 /* Device changed. */
855 xpt_async(AC_LOST_DEVICE, path, NULL);
857 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
862 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
863 /* Clean up from previous instance of this device */
864 if (path->device->serial_num != NULL) {
865 free(path->device->serial_num, M_CAMXPT);
866 path->device->serial_num = NULL;
867 path->device->serial_num_len = 0;
869 path->device->serial_num =
870 (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
872 if (path->device->serial_num != NULL) {
873 bcopy(ident_buf->serial,
874 path->device->serial_num,
875 sizeof(ident_buf->serial));
876 path->device->serial_num[sizeof(ident_buf->serial)]
878 path->device->serial_num_len =
879 strlen(path->device->serial_num);
882 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
884 if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
885 path->device->mintags = path->device->maxtags =
886 ATA_QUEUE_LEN(ident_buf->queue) + 1;
888 ata_find_quirk(path->device);
889 if (path->device->mintags != 0 &&
890 path->bus->sim->max_tagged_dev_openings != 0) {
891 /* Check if the SIM does not want queued commands. */
892 bzero(&cpi, sizeof(cpi));
893 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
894 cpi.ccb_h.func_code = XPT_PATH_INQ;
895 xpt_action((union ccb *)&cpi);
896 if (cpi.ccb_h.status == CAM_REQ_CMP &&
897 (cpi.hba_inquiry & PI_TAG_ABLE)) {
898 /* Report SIM which tags are allowed. */
899 bzero(&cts, sizeof(cts));
900 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
901 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
902 cts.type = CTS_TYPE_CURRENT_SETTINGS;
903 cts.xport_specific.sata.tags = path->device->maxtags;
904 cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
905 xpt_action((union ccb *)&cts);
906 /* Reconfigure queues for tagged queueing. */
907 xpt_start_tags(path);
910 ata_device_transport(path);
911 PROBE_SET_ACTION(softc, PROBE_SETMODE);
912 xpt_release_ccb(done_ccb);
913 xpt_schedule(periph, priority);
918 xpt_print(path, "Spin-up done\n");
920 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
921 xpt_release_ccb(done_ccb);
922 xpt_schedule(periph, priority);
925 if (path->device->transport != XPORT_SATA)
927 /* Set supported bits. */
928 bzero(&cts, sizeof(cts));
929 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
930 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
931 cts.type = CTS_TYPE_CURRENT_SETTINGS;
932 xpt_action((union ccb *)&cts);
933 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
934 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
937 if (ident_buf->satacapabilities != 0xffff) {
938 if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
939 caps |= CTS_SATA_CAPS_D_PMREQ;
940 if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
941 caps |= CTS_SATA_CAPS_D_APST;
943 /* Mask unwanted bits. */
944 bzero(&cts, sizeof(cts));
945 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
946 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
947 cts.type = CTS_TYPE_USER_SETTINGS;
948 xpt_action((union ccb *)&cts);
949 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
950 caps &= cts.xport_specific.sata.caps;
951 /* Store result to SIM. */
952 bzero(&cts, sizeof(cts));
953 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
954 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
955 cts.type = CTS_TYPE_CURRENT_SETTINGS;
956 cts.xport_specific.sata.caps = caps;
957 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
958 xpt_action((union ccb *)&cts);
960 if (ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) {
961 PROBE_SET_ACTION(softc, PROBE_SETPM);
962 xpt_release_ccb(done_ccb);
963 xpt_schedule(periph, priority);
968 if (ident_buf->satacapabilities != 0xffff &&
969 ident_buf->satacapabilities & ATA_SUPPORT_DAPST) {
970 PROBE_SET_ACTION(softc, PROBE_SETAPST);
971 xpt_release_ccb(done_ccb);
972 xpt_schedule(periph, priority);
977 if (ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) {
978 PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
979 xpt_release_ccb(done_ccb);
980 xpt_schedule(periph, priority);
986 if (path->device->protocol == PROTO_ATA) {
987 PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
989 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
991 xpt_release_ccb(done_ccb);
992 xpt_schedule(periph, priority);
994 case PROBE_SET_MULTI:
995 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
996 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
997 xpt_acquire_device(path->device);
998 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
999 xpt_action(done_ccb);
1000 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1005 case PROBE_FULL_INQUIRY:
1007 struct scsi_inquiry_data *inq_buf;
1008 u_int8_t periph_qual, len;
1010 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1011 inq_buf = &path->device->inq_data;
1013 periph_qual = SID_QUAL(inq_buf);
1015 if (periph_qual != SID_QUAL_LU_CONNECTED)
1019 * We conservatively request only
1020 * SHORT_INQUIRY_LEN bytes of inquiry
1021 * information during our first try
1022 * at sending an INQUIRY. If the device
1023 * has more information to give,
1024 * perform a second request specifying
1025 * the amount of information the device
1026 * is willing to give.
1028 len = inq_buf->additional_length
1029 + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1030 if (softc->action == PROBE_INQUIRY
1031 && len > SHORT_INQUIRY_LENGTH) {
1032 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1033 xpt_release_ccb(done_ccb);
1034 xpt_schedule(periph, priority);
1038 ata_device_transport(path);
1039 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1040 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1041 xpt_acquire_device(path->device);
1042 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1043 xpt_action(done_ccb);
1044 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
1049 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1050 bzero(ident_buf, sizeof(*ident_buf));
1051 softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1052 (done_ccb->ataio.res.lba_mid << 16) +
1053 (done_ccb->ataio.res.lba_low << 8) +
1054 done_ccb->ataio.res.sector_count;
1055 ((uint32_t *)ident_buf)[0] = softc->pm_pid;
1056 snprintf(ident_buf->model, sizeof(ident_buf->model),
1057 "Port Multiplier %08x", softc->pm_pid);
1058 PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1059 xpt_release_ccb(done_ccb);
1060 xpt_schedule(periph, priority);
1063 softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1064 (done_ccb->ataio.res.lba_mid << 16) +
1065 (done_ccb->ataio.res.lba_low << 8) +
1066 done_ccb->ataio.res.sector_count;
1067 ((uint32_t *)ident_buf)[1] = softc->pm_prv;
1068 snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1069 "%04x", softc->pm_prv);
1070 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1071 /* Set supported bits. */
1072 bzero(&cts, sizeof(cts));
1073 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1074 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1075 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1076 xpt_action((union ccb *)&cts);
1077 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1078 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1081 /* All PMPs must support PM requests. */
1082 caps |= CTS_SATA_CAPS_D_PMREQ;
1083 /* Mask unwanted bits. */
1084 bzero(&cts, sizeof(cts));
1085 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1086 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1087 cts.type = CTS_TYPE_USER_SETTINGS;
1088 xpt_action((union ccb *)&cts);
1089 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1090 caps &= cts.xport_specific.sata.caps;
1091 /* Store result to SIM. */
1092 bzero(&cts, sizeof(cts));
1093 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1094 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1095 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1096 cts.xport_specific.sata.caps = caps;
1097 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1098 xpt_action((union ccb *)&cts);
1100 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1101 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1102 xpt_acquire_device(path->device);
1103 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1104 xpt_action(done_ccb);
1105 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1108 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1109 xpt_action(done_ccb);
1110 xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
1114 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
1115 ("probedone: invalid action state\n"));
1120 if (softc->restart) {
1122 xpt_release_ccb(done_ccb);
1123 probeschedule(periph);
1126 xpt_release_ccb(done_ccb);
1127 while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1128 TAILQ_REMOVE(&softc->request_ccbs,
1129 &done_ccb->ccb_h, periph_links.tqe);
1130 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1133 cam_release_devq(periph->path,
1134 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
1135 cam_periph_invalidate(periph);
1136 cam_periph_release_locked(periph);
1140 probecleanup(struct cam_periph *periph)
1142 free(periph->softc, M_CAMXPT);
1146 ata_find_quirk(struct cam_ed *device)
1148 struct ata_quirk_entry *quirk;
1151 match = cam_quirkmatch((caddr_t)&device->ident_data,
1152 (caddr_t)ata_quirk_table,
1153 ata_quirk_table_size,
1154 sizeof(*ata_quirk_table), ata_identify_match);
1157 panic("xpt_find_quirk: device didn't match wildcard entry!!");
1159 quirk = (struct ata_quirk_entry *)match;
1160 device->quirk = quirk;
1161 if (quirk->quirks & CAM_QUIRK_MAXTAGS)
1162 device->mintags = device->maxtags = quirk->maxtags;
1166 union ccb *request_ccb;
1167 struct ccb_pathinq *cpi;
1169 } ata_scan_bus_info;
1172 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1173 * As the scan progresses, xpt_scan_bus is used as the
1174 * callback on completion function.
1177 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1179 struct cam_path *path;
1180 ata_scan_bus_info *scan_info;
1181 union ccb *work_ccb, *reset_ccb;
1184 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1185 ("xpt_scan_bus\n"));
1186 switch (request_ccb->ccb_h.func_code) {
1188 /* Find out the characteristics of the bus */
1189 work_ccb = xpt_alloc_ccb_nowait();
1190 if (work_ccb == NULL) {
1191 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1192 xpt_done(request_ccb);
1195 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1196 request_ccb->ccb_h.pinfo.priority);
1197 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1198 xpt_action(work_ccb);
1199 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1200 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1201 xpt_free_ccb(work_ccb);
1202 xpt_done(request_ccb);
1206 /* We may need to reset bus first, if we haven't done it yet. */
1207 if ((work_ccb->cpi.hba_inquiry &
1208 (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1209 !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1210 !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1211 reset_ccb = xpt_alloc_ccb_nowait();
1212 xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1214 reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1215 xpt_action(reset_ccb);
1216 if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1217 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1218 xpt_free_ccb(reset_ccb);
1219 xpt_free_ccb(work_ccb);
1220 xpt_done(request_ccb);
1223 xpt_free_ccb(reset_ccb);
1226 /* Save some state for use while we probe for devices */
1227 scan_info = (ata_scan_bus_info *)
1228 malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1229 if (scan_info == NULL) {
1230 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1231 xpt_done(request_ccb);
1234 scan_info->request_ccb = request_ccb;
1235 scan_info->cpi = &work_ccb->cpi;
1236 /* If PM supported, probe it first. */
1237 if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1238 scan_info->counter = scan_info->cpi->max_target;
1240 scan_info->counter = 0;
1242 work_ccb = xpt_alloc_ccb_nowait();
1243 if (work_ccb == NULL) {
1244 free(scan_info, M_CAMXPT);
1245 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1246 xpt_done(request_ccb);
1251 work_ccb = request_ccb;
1252 /* Reuse the same CCB to query if a device was really found */
1253 scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1254 /* If there is PMP... */
1255 if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1256 (scan_info->counter == scan_info->cpi->max_target)) {
1257 if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1258 /* everything else will be probed by it */
1259 /* Free the current request path- we're done with it. */
1260 xpt_free_path(work_ccb->ccb_h.path);
1263 struct ccb_trans_settings cts;
1265 /* Report SIM that PM is absent. */
1266 bzero(&cts, sizeof(cts));
1267 xpt_setup_ccb(&cts.ccb_h,
1268 work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1269 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1270 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1271 cts.xport_specific.sata.pm_present = 0;
1272 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1273 xpt_action((union ccb *)&cts);
1276 /* Free the current request path- we're done with it. */
1277 xpt_free_path(work_ccb->ccb_h.path);
1278 if (scan_info->counter ==
1279 ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1280 0 : scan_info->cpi->max_target)) {
1282 xpt_free_ccb(work_ccb);
1283 xpt_free_ccb((union ccb *)scan_info->cpi);
1284 request_ccb = scan_info->request_ccb;
1285 free(scan_info, M_CAMXPT);
1286 request_ccb->ccb_h.status = CAM_REQ_CMP;
1287 xpt_done(request_ccb);
1290 /* Take next device. Wrap from max (PMP) to 0. */
1291 scan_info->counter = (scan_info->counter + 1 ) %
1292 (scan_info->cpi->max_target + 1);
1294 status = xpt_create_path(&path, xpt_periph,
1295 scan_info->request_ccb->ccb_h.path_id,
1296 scan_info->counter, 0);
1297 if (status != CAM_REQ_CMP) {
1298 printf("xpt_scan_bus: xpt_create_path failed"
1299 " with status %#x, bus scan halted\n",
1301 xpt_free_ccb(work_ccb);
1302 xpt_free_ccb((union ccb *)scan_info->cpi);
1303 request_ccb = scan_info->request_ccb;
1304 free(scan_info, M_CAMXPT);
1305 request_ccb->ccb_h.status = status;
1306 xpt_done(request_ccb);
1309 xpt_setup_ccb(&work_ccb->ccb_h, path,
1310 scan_info->request_ccb->ccb_h.pinfo.priority);
1311 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1312 work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1313 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1314 work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1315 xpt_action(work_ccb);
1323 ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1324 cam_flags flags, union ccb *request_ccb)
1326 struct ccb_pathinq cpi;
1328 struct cam_path *new_path;
1329 struct cam_periph *old_periph;
1331 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1333 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1334 cpi.ccb_h.func_code = XPT_PATH_INQ;
1335 xpt_action((union ccb *)&cpi);
1337 if (cpi.ccb_h.status != CAM_REQ_CMP) {
1338 if (request_ccb != NULL) {
1339 request_ccb->ccb_h.status = cpi.ccb_h.status;
1340 xpt_done(request_ccb);
1345 if (request_ccb == NULL) {
1346 request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1347 if (request_ccb == NULL) {
1348 xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1349 "can't continue\n");
1352 new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1353 if (new_path == NULL) {
1354 xpt_print(path, "xpt_scan_lun: can't allocate path, "
1355 "can't continue\n");
1356 free(request_ccb, M_CAMXPT);
1359 status = xpt_compile_path(new_path, xpt_periph,
1361 path->target->target_id,
1362 path->device->lun_id);
1364 if (status != CAM_REQ_CMP) {
1365 xpt_print(path, "xpt_scan_lun: can't compile path, "
1366 "can't continue\n");
1367 free(request_ccb, M_CAMXPT);
1368 free(new_path, M_CAMXPT);
1371 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1372 request_ccb->ccb_h.cbfcnp = xptscandone;
1373 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1374 request_ccb->crcn.flags = flags;
1377 if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1380 softc = (probe_softc *)old_periph->softc;
1381 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1385 status = cam_periph_alloc(proberegister, NULL, probecleanup,
1386 probestart, "aprobe",
1388 request_ccb->ccb_h.path, NULL, 0,
1391 if (status != CAM_REQ_CMP) {
1392 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1393 "returned an error, can't continue probe\n");
1394 request_ccb->ccb_h.status = status;
1395 xpt_done(request_ccb);
1401 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1403 xpt_release_path(done_ccb->ccb_h.path);
1404 free(done_ccb->ccb_h.path, M_CAMXPT);
1405 free(done_ccb, M_CAMXPT);
1408 static struct cam_ed *
1409 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1411 struct cam_path path;
1412 struct ata_quirk_entry *quirk;
1413 struct cam_ed *device;
1414 struct cam_ed *cur_device;
1416 device = xpt_alloc_device(bus, target, lun_id);
1421 * Take the default quirk entry until we have inquiry
1422 * data and can determine a better quirk to use.
1424 quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1425 device->quirk = (void *)quirk;
1426 device->mintags = 0;
1427 device->maxtags = 0;
1428 bzero(&device->inq_data, sizeof(device->inq_data));
1429 device->inq_flags = 0;
1430 device->queue_flags = 0;
1431 device->serial_num = NULL;
1432 device->serial_num_len = 0;
1435 * XXX should be limited by number of CCBs this bus can
1438 bus->sim->max_ccbs += device->ccbq.devq_openings;
1439 /* Insertion sort into our target's device list */
1440 cur_device = TAILQ_FIRST(&target->ed_entries);
1441 while (cur_device != NULL && cur_device->lun_id < lun_id)
1442 cur_device = TAILQ_NEXT(cur_device, links);
1443 if (cur_device != NULL) {
1444 TAILQ_INSERT_BEFORE(cur_device, device, links);
1446 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1448 target->generation++;
1449 if (lun_id != CAM_LUN_WILDCARD) {
1450 xpt_compile_path(&path,
1455 ata_device_transport(&path);
1456 xpt_release_path(&path);
1463 ata_device_transport(struct cam_path *path)
1465 struct ccb_pathinq cpi;
1466 struct ccb_trans_settings cts;
1467 struct scsi_inquiry_data *inq_buf = NULL;
1468 struct ata_params *ident_buf = NULL;
1470 /* Get transport information from the SIM */
1471 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1472 cpi.ccb_h.func_code = XPT_PATH_INQ;
1473 xpt_action((union ccb *)&cpi);
1475 path->device->transport = cpi.transport;
1476 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1477 inq_buf = &path->device->inq_data;
1478 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1479 ident_buf = &path->device->ident_data;
1480 if (path->device->protocol == PROTO_ATA) {
1481 path->device->protocol_version = ident_buf ?
1482 ata_version(ident_buf->version_major) : cpi.protocol_version;
1483 } else if (path->device->protocol == PROTO_SCSI) {
1484 path->device->protocol_version = inq_buf ?
1485 SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1487 path->device->transport_version = ident_buf ?
1488 ata_version(ident_buf->version_major) : cpi.transport_version;
1490 /* Tell the controller what we think */
1491 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1492 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1493 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1494 cts.transport = path->device->transport;
1495 cts.transport_version = path->device->transport_version;
1496 cts.protocol = path->device->protocol;
1497 cts.protocol_version = path->device->protocol_version;
1498 cts.proto_specific.valid = 0;
1500 if (path->device->transport == XPORT_ATA) {
1501 cts.xport_specific.ata.atapi =
1502 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1503 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1504 cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1506 cts.xport_specific.sata.atapi =
1507 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1508 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1509 cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1512 cts.xport_specific.valid = 0;
1513 xpt_action((union ccb *)&cts);
1517 ata_action(union ccb *start_ccb)
1520 switch (start_ccb->ccb_h.func_code) {
1521 case XPT_SET_TRAN_SETTINGS:
1523 ata_set_transfer_settings(&start_ccb->cts,
1524 start_ccb->ccb_h.path->device,
1525 /*async_update*/FALSE);
1529 ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1532 ata_scan_lun(start_ccb->ccb_h.path->periph,
1533 start_ccb->ccb_h.path, start_ccb->crcn.flags,
1536 case XPT_GET_TRAN_SETTINGS:
1538 struct cam_sim *sim;
1540 sim = start_ccb->ccb_h.path->bus->sim;
1541 (*(sim->sim_action))(sim, start_ccb);
1546 struct cam_ed *device;
1549 device = start_ccb->ccb_h.path->device;
1550 if (device->protocol == PROTO_SCSI &&
1551 (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1553 device->ident_data.config & ATA_PROTO_MASK;
1555 maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
1556 (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1558 if (start_ccb->csio.cdb_len > maxlen) {
1559 start_ccb->ccb_h.status = CAM_REQ_INVALID;
1560 xpt_done(start_ccb);
1566 xpt_action_default(start_ccb);
1572 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1575 struct ccb_pathinq cpi;
1576 struct ccb_trans_settings cur_cts;
1577 struct ccb_trans_settings_scsi *scsi;
1578 struct ccb_trans_settings_scsi *cur_scsi;
1579 struct cam_sim *sim;
1580 struct scsi_inquiry_data *inq_data;
1582 if (device == NULL) {
1583 cts->ccb_h.status = CAM_PATH_INVALID;
1584 xpt_done((union ccb *)cts);
1588 if (cts->protocol == PROTO_UNKNOWN
1589 || cts->protocol == PROTO_UNSPECIFIED) {
1590 cts->protocol = device->protocol;
1591 cts->protocol_version = device->protocol_version;
1594 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1595 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1596 cts->protocol_version = device->protocol_version;
1598 if (cts->protocol != device->protocol) {
1599 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1600 cts->protocol, device->protocol);
1601 cts->protocol = device->protocol;
1604 if (cts->protocol_version > device->protocol_version) {
1606 xpt_print(cts->ccb_h.path, "Down reving Protocol "
1607 "Version from %d to %d?\n", cts->protocol_version,
1608 device->protocol_version);
1610 cts->protocol_version = device->protocol_version;
1613 if (cts->transport == XPORT_UNKNOWN
1614 || cts->transport == XPORT_UNSPECIFIED) {
1615 cts->transport = device->transport;
1616 cts->transport_version = device->transport_version;
1619 if (cts->transport_version == XPORT_VERSION_UNKNOWN
1620 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1621 cts->transport_version = device->transport_version;
1623 if (cts->transport != device->transport) {
1624 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1625 cts->transport, device->transport);
1626 cts->transport = device->transport;
1629 if (cts->transport_version > device->transport_version) {
1631 xpt_print(cts->ccb_h.path, "Down reving Transport "
1632 "Version from %d to %d?\n", cts->transport_version,
1633 device->transport_version);
1635 cts->transport_version = device->transport_version;
1638 sim = cts->ccb_h.path->bus->sim;
1641 * Nothing more of interest to do unless
1642 * this is a device connected via the
1645 if (cts->protocol != PROTO_SCSI) {
1646 if (async_update == FALSE)
1647 (*(sim->sim_action))(sim, (union ccb *)cts);
1651 inq_data = &device->inq_data;
1652 scsi = &cts->proto_specific.scsi;
1653 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1654 cpi.ccb_h.func_code = XPT_PATH_INQ;
1655 xpt_action((union ccb *)&cpi);
1657 /* SCSI specific sanity checking */
1658 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1659 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1660 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1661 || (device->mintags == 0)) {
1663 * Can't tag on hardware that doesn't support tags,
1664 * doesn't have it enabled, or has broken tag support.
1666 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1669 if (async_update == FALSE) {
1671 * Perform sanity checking against what the
1672 * controller and device can do.
1674 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1675 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1676 cur_cts.type = cts->type;
1677 xpt_action((union ccb *)&cur_cts);
1678 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1681 cur_scsi = &cur_cts.proto_specific.scsi;
1682 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1683 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1684 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1686 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1687 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1690 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1691 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1695 * If we are transitioning from tags to no-tags or
1696 * vice-versa, we need to carefully freeze and restart
1697 * the queue so that we don't overlap tagged and non-tagged
1698 * commands. We also temporarily stop tags if there is
1699 * a change in transfer negotiation settings to allow
1700 * "tag-less" negotiation.
1702 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1703 || (device->inq_flags & SID_CmdQue) != 0)
1704 device_tagenb = TRUE;
1706 device_tagenb = FALSE;
1708 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1709 && device_tagenb == FALSE)
1710 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1711 && device_tagenb == TRUE)) {
1713 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1715 * Delay change to use tags until after a
1716 * few commands have gone to this device so
1717 * the controller has time to perform transfer
1718 * negotiations without tagged messages getting
1721 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1722 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1724 xpt_stop_tags(cts->ccb_h.path);
1728 if (async_update == FALSE)
1729 (*(sim->sim_action))(sim, (union ccb *)cts);
1733 * Handle any per-device event notifications that require action by the XPT.
1736 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1737 struct cam_ed *device, void *async_arg)
1740 struct cam_path newpath;
1743 * We only need to handle events for real devices.
1745 if (target->target_id == CAM_TARGET_WILDCARD
1746 || device->lun_id == CAM_LUN_WILDCARD)
1750 * We need our own path with wildcards expanded to
1751 * handle certain types of events.
1753 if ((async_code == AC_SENT_BDR)
1754 || (async_code == AC_BUS_RESET)
1755 || (async_code == AC_INQ_CHANGED))
1756 status = xpt_compile_path(&newpath, NULL,
1761 status = CAM_REQ_CMP_ERR;
1763 if (status == CAM_REQ_CMP) {
1764 if (async_code == AC_INQ_CHANGED) {
1766 * We've sent a start unit command, or
1767 * something similar to a device that
1768 * may have caused its inquiry data to
1769 * change. So we re-scan the device to
1770 * refresh the inquiry data for it.
1772 ata_scan_lun(newpath.periph, &newpath,
1773 CAM_EXPECT_INQ_CHANGE, NULL);
1775 /* We need to reinitialize device after reset. */
1776 ata_scan_lun(newpath.periph, &newpath,
1779 xpt_release_path(&newpath);
1780 } else if (async_code == AC_LOST_DEVICE &&
1781 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1782 device->flags |= CAM_DEV_UNCONFIGURED;
1783 xpt_release_device(device);
1784 } else if (async_code == AC_TRANSFER_NEG) {
1785 struct ccb_trans_settings *settings;
1787 settings = (struct ccb_trans_settings *)async_arg;
1788 ata_set_transfer_settings(settings, device,
1789 /*async_update*/TRUE);
1794 ata_announce_periph(struct cam_periph *periph)
1796 struct ccb_pathinq cpi;
1797 struct ccb_trans_settings cts;
1798 struct cam_path *path = periph->path;
1802 mtx_assert(periph->sim->mtx, MA_OWNED);
1804 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1805 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1806 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1807 xpt_action((union ccb*)&cts);
1808 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1810 /* Ask the SIM for its base transfer speed */
1811 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1812 cpi.ccb_h.func_code = XPT_PATH_INQ;
1813 xpt_action((union ccb *)&cpi);
1814 /* Report connection speed */
1815 speed = cpi.base_transfer_speed;
1816 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1817 struct ccb_trans_settings_ata *ata =
1818 &cts.xport_specific.ata;
1820 if (ata->valid & CTS_ATA_VALID_MODE)
1821 speed = ata_mode2speed(ata->mode);
1823 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1824 struct ccb_trans_settings_sata *sata =
1825 &cts.xport_specific.sata;
1827 if (sata->valid & CTS_SATA_VALID_REVISION)
1828 speed = ata_revision2speed(sata->revision);
1832 printf("%s%d: %d.%03dMB/s transfers",
1833 periph->periph_name, periph->unit_number,
1836 printf("%s%d: %dKB/s transfers", periph->periph_name,
1837 periph->unit_number, speed);
1838 /* Report additional information about connection */
1839 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1840 struct ccb_trans_settings_ata *ata =
1841 &cts.xport_specific.ata;
1844 if (ata->valid & CTS_ATA_VALID_MODE)
1845 printf("%s, ", ata_mode2string(ata->mode));
1846 if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
1847 printf("ATAPI %dbytes, ", ata->atapi);
1848 if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
1849 printf("PIO %dbytes", ata->bytecount);
1852 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1853 struct ccb_trans_settings_sata *sata =
1854 &cts.xport_specific.sata;
1857 if (sata->valid & CTS_SATA_VALID_REVISION)
1858 printf("SATA %d.x, ", sata->revision);
1861 if (sata->valid & CTS_SATA_VALID_MODE)
1862 printf("%s, ", ata_mode2string(sata->mode));
1863 if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
1864 printf("ATAPI %dbytes, ", sata->atapi);
1865 if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
1866 printf("PIO %dbytes", sata->bytecount);