2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
40 #include <sys/interrupt.h>
44 #include <sys/mutex.h>
45 #include <sys/sysctl.h>
48 #include <cam/cam_ccb.h>
49 #include <cam/cam_queue.h>
50 #include <cam/cam_periph.h>
51 #include <cam/cam_sim.h>
52 #include <cam/cam_xpt.h>
53 #include <cam/cam_xpt_sim.h>
54 #include <cam/cam_xpt_periph.h>
55 #include <cam/cam_xpt_internal.h>
56 #include <cam/cam_debug.h>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_message.h>
60 #include <cam/ata/ata_all.h>
61 #include <machine/stdarg.h> /* for xpt_print below */
64 struct ata_quirk_entry {
65 struct scsi_inquiry_pattern inq_pat;
67 #define CAM_QUIRK_MAXTAGS 0x01
71 static periph_init_t probe_periph_init;
73 static struct periph_driver probe_driver =
75 probe_periph_init, "aprobe",
76 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
80 PERIPHDRIVER_DECLARE(aprobe, probe_driver);
99 static char *probe_action_text[] = {
110 "PROBE_FULL_INQUIRY",
116 #define PROBE_SET_ACTION(softc, newaction) \
119 text = probe_action_text; \
120 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO, \
121 ("Probe %s to %s\n", text[(softc)->action], \
122 text[(newaction)])); \
123 (softc)->action = (newaction); \
127 PROBE_NO_ANNOUNCE = 0x04
131 TAILQ_HEAD(, ccb_hdr) request_ccbs;
132 struct ata_params ident_data;
141 struct cam_periph *periph;
144 static struct ata_quirk_entry ata_quirk_table[] =
147 /* Default tagged queuing parameters for all devices */
149 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
150 /*vendor*/"*", /*product*/"*", /*revision*/"*"
152 /*quirks*/0, /*maxtags*/0
156 static const int ata_quirk_table_size =
157 sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
159 static cam_status proberegister(struct cam_periph *periph,
161 static void probeschedule(struct cam_periph *probe_periph);
162 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
163 //static void proberequestdefaultnegotiation(struct cam_periph *periph);
164 //static int proberequestbackoff(struct cam_periph *periph,
165 // struct cam_ed *device);
166 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
167 static void probecleanup(struct cam_periph *periph);
168 static void ata_find_quirk(struct cam_ed *device);
169 static void ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
170 static void ata_scan_lun(struct cam_periph *periph,
171 struct cam_path *path, cam_flags flags,
173 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
174 static struct cam_ed *
175 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
177 static void ata_device_transport(struct cam_path *path);
178 static void ata_set_transfer_settings(struct ccb_trans_settings *cts,
179 struct cam_ed *device,
181 static void ata_dev_async(u_int32_t async_code,
183 struct cam_et *target,
184 struct cam_ed *device,
186 static void ata_action(union ccb *start_ccb);
187 static void ata_announce_periph(struct cam_periph *periph);
189 static int ata_dma = 1;
190 static int atapi_dma = 1;
192 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
193 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
195 static struct xpt_xport ata_xport = {
196 .alloc_device = ata_alloc_device,
197 .action = ata_action,
198 .async = ata_dev_async,
199 .announce = ata_announce_periph,
214 proberegister(struct cam_periph *periph, void *arg)
216 union ccb *request_ccb; /* CCB representing the probe request */
220 request_ccb = (union ccb *)arg;
221 if (periph == NULL) {
222 printf("proberegister: periph was NULL!!\n");
223 return(CAM_REQ_CMP_ERR);
226 if (request_ccb == NULL) {
227 printf("proberegister: no probe CCB, "
228 "can't register device\n");
229 return(CAM_REQ_CMP_ERR);
232 softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
235 printf("proberegister: Unable to probe new device. "
236 "Unable to allocate softc\n");
237 return(CAM_REQ_CMP_ERR);
239 TAILQ_INIT(&softc->request_ccbs);
240 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
243 periph->softc = softc;
244 softc->periph = periph;
245 softc->action = PROBE_INVALID;
246 status = cam_periph_acquire(periph);
247 if (status != CAM_REQ_CMP) {
251 * Ensure nobody slip in until probe finish.
253 cam_freeze_devq_arg(periph->path,
254 RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
255 probeschedule(periph);
260 probeschedule(struct cam_periph *periph)
265 softc = (probe_softc *)periph->softc;
266 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
268 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
269 periph->path->device->protocol == PROTO_SATAPM)
270 PROBE_SET_ACTION(softc, PROBE_RESET);
272 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
274 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
275 softc->flags |= PROBE_NO_ANNOUNCE;
277 softc->flags &= ~PROBE_NO_ANNOUNCE;
279 xpt_schedule(periph, CAM_PRIORITY_XPT);
283 probestart(struct cam_periph *periph, union ccb *start_ccb)
285 struct ccb_trans_settings cts;
286 struct ccb_ataio *ataio;
287 struct ccb_scsiio *csio;
289 struct cam_path *path;
290 struct ata_params *ident_buf;
292 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
294 softc = (probe_softc *)periph->softc;
295 path = start_ccb->ccb_h.path;
296 ataio = &start_ccb->ataio;
297 csio = &start_ccb->csio;
298 ident_buf = &periph->path->device->ident_data;
300 if (softc->restart) {
302 if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
303 path->device->protocol == PROTO_SATAPM)
304 softc->action = PROBE_RESET;
306 softc->action = PROBE_IDENTIFY;
308 switch (softc->action) {
310 cam_fill_ataio(ataio,
313 /*flags*/CAM_DIR_NONE,
318 ata_reset_cmd(ataio);
321 cam_fill_ataio(ataio,
326 /*data_ptr*/(u_int8_t *)&softc->ident_data,
327 /*dxfer_len*/sizeof(softc->ident_data),
329 if (periph->path->device->protocol == PROTO_ATA)
330 ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
332 ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
336 xpt_print(path, "Spinning up device\n");
337 cam_fill_ataio(ataio,
340 /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
345 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
352 /* Fetch user modes from SIM. */
353 bzero(&cts, sizeof(cts));
354 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
355 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
356 cts.type = CTS_TYPE_USER_SETTINGS;
357 xpt_action((union ccb *)&cts);
358 if (path->device->transport == XPORT_ATA) {
359 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
360 mode = cts.xport_specific.ata.mode;
362 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
363 mode = cts.xport_specific.sata.mode;
365 if (periph->path->device->protocol == PROTO_ATA) {
366 if (ata_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
369 if (atapi_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
373 /* Honor device capabilities. */
374 wantmode = mode = ata_max_mode(ident_buf, mode);
375 /* Report modes to SIM. */
376 bzero(&cts, sizeof(cts));
377 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
378 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
379 cts.type = CTS_TYPE_CURRENT_SETTINGS;
380 if (path->device->transport == XPORT_ATA) {
381 cts.xport_specific.ata.mode = mode;
382 cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
384 cts.xport_specific.sata.mode = mode;
385 cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
387 xpt_action((union ccb *)&cts);
388 /* Fetch current modes from SIM. */
389 bzero(&cts, sizeof(cts));
390 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
391 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
392 cts.type = CTS_TYPE_CURRENT_SETTINGS;
393 xpt_action((union ccb *)&cts);
394 if (path->device->transport == XPORT_ATA) {
395 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
396 mode = cts.xport_specific.ata.mode;
398 if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
399 mode = cts.xport_specific.sata.mode;
401 /* If SIM disagree - renegotiate. */
402 if (mode != wantmode)
404 /* Remember what transport thinks about DMA. */
406 path->device->inq_flags &= ~SID_DMA;
408 path->device->inq_flags |= SID_DMA;
409 cam_fill_ataio(ataio,
412 /*flags*/CAM_DIR_NONE,
417 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
421 cam_fill_ataio(ataio,
429 ata_28bit_cmd(ataio, ATA_SETFEATURES,
430 (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
434 cam_fill_ataio(ataio,
442 ata_28bit_cmd(ataio, ATA_SETFEATURES,
443 (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
447 cam_fill_ataio(ataio,
455 ata_28bit_cmd(ataio, ATA_SETFEATURES,
456 (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
460 cam_fill_ataio(ataio,
468 ata_28bit_cmd(ataio, ATA_SETFEATURES,
469 (softc->caps & CTS_SATA_CAPS_H_AN) ? 0x10 : 0x90,
472 case PROBE_SET_MULTI:
474 u_int sectors, bytecount;
476 bytecount = 8192; /* SATA maximum */
477 /* Fetch user bytecount from SIM. */
478 bzero(&cts, sizeof(cts));
479 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
480 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
481 cts.type = CTS_TYPE_USER_SETTINGS;
482 xpt_action((union ccb *)&cts);
483 if (path->device->transport == XPORT_ATA) {
484 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
485 bytecount = cts.xport_specific.ata.bytecount;
487 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
488 bytecount = cts.xport_specific.sata.bytecount;
490 /* Honor device capabilities. */
491 sectors = max(1, min(ident_buf->sectors_intr & 0xff,
492 bytecount / ata_logical_sector_size(ident_buf)));
493 /* Report bytecount to SIM. */
494 bzero(&cts, sizeof(cts));
495 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
496 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
497 cts.type = CTS_TYPE_CURRENT_SETTINGS;
498 if (path->device->transport == XPORT_ATA) {
499 cts.xport_specific.ata.bytecount = sectors *
500 ata_logical_sector_size(ident_buf);
501 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
503 cts.xport_specific.sata.bytecount = sectors *
504 ata_logical_sector_size(ident_buf);
505 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
507 xpt_action((union ccb *)&cts);
508 /* Fetch current bytecount from SIM. */
509 bzero(&cts, sizeof(cts));
510 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
511 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
512 cts.type = CTS_TYPE_CURRENT_SETTINGS;
513 xpt_action((union ccb *)&cts);
514 if (path->device->transport == XPORT_ATA) {
515 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
516 bytecount = cts.xport_specific.ata.bytecount;
518 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
519 bytecount = cts.xport_specific.sata.bytecount;
521 sectors = bytecount / ata_logical_sector_size(ident_buf);
523 cam_fill_ataio(ataio,
531 ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
538 bytecount = 8192; /* SATA maximum */
539 /* Fetch user bytecount from SIM. */
540 bzero(&cts, sizeof(cts));
541 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
542 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
543 cts.type = CTS_TYPE_USER_SETTINGS;
544 xpt_action((union ccb *)&cts);
545 if (path->device->transport == XPORT_ATA) {
546 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
547 bytecount = cts.xport_specific.ata.bytecount;
549 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
550 bytecount = cts.xport_specific.sata.bytecount;
552 /* Honor device capabilities. */
554 bytecount = max(2, min(65534, bytecount));
555 if (ident_buf->satacapabilities != 0x0000 &&
556 ident_buf->satacapabilities != 0xffff) {
557 bytecount = min(8192, bytecount);
559 /* Report bytecount to SIM. */
560 bzero(&cts, sizeof(cts));
561 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
562 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
563 cts.type = CTS_TYPE_CURRENT_SETTINGS;
564 if (path->device->transport == XPORT_ATA) {
565 cts.xport_specific.ata.bytecount = bytecount;
566 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
568 cts.xport_specific.sata.bytecount = bytecount;
569 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
571 xpt_action((union ccb *)&cts);
574 case PROBE_FULL_INQUIRY:
577 struct scsi_inquiry_data *inq_buf =
578 &periph->path->device->inq_data;
580 if (softc->action == PROBE_INQUIRY)
581 inquiry_len = SHORT_INQUIRY_LENGTH;
583 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
585 * Some parallel SCSI devices fail to send an
586 * ignore wide residue message when dealing with
587 * odd length inquiry requests. Round up to be
590 inquiry_len = roundup2(inquiry_len, 2);
600 /*timeout*/60 * 1000);
604 cam_fill_ataio(ataio,
607 /*flags*/CAM_DIR_NONE,
612 ata_pm_read_cmd(ataio, 0, 15);
615 cam_fill_ataio(ataio,
618 /*flags*/CAM_DIR_NONE,
623 ata_pm_read_cmd(ataio, 1, 15);
626 CAM_DEBUG(path, CAM_DEBUG_INFO,
627 ("probestart: invalid action state\n"));
631 xpt_action(start_ccb);
635 proberequestdefaultnegotiation(struct cam_periph *periph)
637 struct ccb_trans_settings cts;
639 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
640 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
641 cts.type = CTS_TYPE_USER_SETTINGS;
642 xpt_action((union ccb *)&cts);
643 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
646 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
647 cts.type = CTS_TYPE_CURRENT_SETTINGS;
648 xpt_action((union ccb *)&cts);
652 * Backoff Negotiation Code- only pertinent for SPI devices.
655 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
657 struct ccb_trans_settings cts;
658 struct ccb_trans_settings_spi *spi;
660 memset(&cts, 0, sizeof (cts));
661 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
662 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
663 cts.type = CTS_TYPE_CURRENT_SETTINGS;
664 xpt_action((union ccb *)&cts);
665 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
667 xpt_print(periph->path,
668 "failed to get current device settings\n");
672 if (cts.transport != XPORT_SPI) {
674 xpt_print(periph->path, "not SPI transport\n");
678 spi = &cts.xport_specific.spi;
681 * We cannot renegotiate sync rate if we don't have one.
683 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
685 xpt_print(periph->path, "no sync rate known\n");
691 * We'll assert that we don't have to touch PPR options- the
692 * SIM will see what we do with period and offset and adjust
693 * the PPR options as appropriate.
697 * A sync rate with unknown or zero offset is nonsensical.
698 * A sync period of zero means Async.
700 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
701 || spi->sync_offset == 0 || spi->sync_period == 0) {
703 xpt_print(periph->path, "no sync rate available\n");
708 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
709 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
710 ("hit async: giving up on DV\n"));
716 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
717 * We don't try to remember 'last' settings to see if the SIM actually
718 * gets into the speed we want to set. We check on the SIM telling
719 * us that a requested speed is bad, but otherwise don't try and
720 * check the speed due to the asynchronous and handshake nature
723 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
726 if (spi->sync_period >= 0xf) {
727 spi->sync_period = 0;
728 spi->sync_offset = 0;
729 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
730 ("setting to async for DV\n"));
732 * Once we hit async, we don't want to try
735 device->flags |= CAM_DEV_DV_HIT_BOTTOM;
736 } else if (bootverbose) {
737 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
738 ("DV: period 0x%x\n", spi->sync_period));
739 printf("setting period to 0x%x\n", spi->sync_period);
741 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
742 cts.type = CTS_TYPE_CURRENT_SETTINGS;
743 xpt_action((union ccb *)&cts);
744 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
747 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
748 ("DV: failed to set period 0x%x\n", spi->sync_period));
749 if (spi->sync_period == 0) {
757 probedone(struct cam_periph *periph, union ccb *done_ccb)
759 struct ccb_trans_settings cts;
760 struct ata_params *ident_buf;
762 struct cam_path *path;
768 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
770 softc = (probe_softc *)periph->softc;
771 path = done_ccb->ccb_h.path;
772 priority = done_ccb->ccb_h.pinfo.priority;
773 ident_buf = &path->device->ident_data;
775 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
776 if (softc->restart) {
778 cam_error_print(done_ccb,
779 CAM_ESF_ALL, CAM_EPF_ALL);
781 } else if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART)
783 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
784 /* Don't wedge the queue */
785 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
788 status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
789 if (softc->restart) {
791 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
794 if (softc->faults < 10)
799 /* Old PIO2 devices may not support mode setting. */
800 } else if (softc->action == PROBE_SETMODE &&
801 status == CAM_ATA_STATUS_ERROR &&
802 ata_max_pmode(ident_buf) <= ATA_PIO2 &&
803 (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) {
807 * Some old WD SATA disks report supported and enabled
808 * device-initiated interface power management, but return
809 * ABORT on attempt to disable it.
811 } else if (softc->action == PROBE_SETPM &&
812 status == CAM_ATA_STATUS_ERROR) {
816 * Some HP SATA disks report supported DMA Auto-Activation,
817 * but return ABORT on attempt to enable it.
819 } else if (softc->action == PROBE_SETDMAAA &&
820 status == CAM_ATA_STATUS_ERROR) {
825 * If we get to this point, we got an error status back
826 * from the inquiry and the error status doesn't require
827 * automatically retrying the command. Therefore, the
828 * inquiry failed. If we had inquiry information before
829 * for this device, but this latest inquiry command failed,
830 * the device has probably gone away. If this device isn't
831 * already marked unconfigured, notify the peripheral
832 * drivers that this device is no more.
834 device_fail: if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
835 xpt_async(AC_LOST_DEVICE, path, NULL);
842 switch (softc->action) {
845 int sign = (done_ccb->ataio.res.lba_high << 8) +
846 done_ccb->ataio.res.lba_mid;
848 xpt_print(path, "SIGNATURE: %04x\n", sign);
849 if (sign == 0x0000 &&
850 done_ccb->ccb_h.target_id != 15) {
851 path->device->protocol = PROTO_ATA;
852 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
853 } else if (sign == 0x9669 &&
854 done_ccb->ccb_h.target_id == 15) {
855 /* Report SIM that PM is present. */
856 bzero(&cts, sizeof(cts));
857 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
858 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
859 cts.type = CTS_TYPE_CURRENT_SETTINGS;
860 cts.xport_specific.sata.pm_present = 1;
861 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
862 xpt_action((union ccb *)&cts);
863 path->device->protocol = PROTO_SATAPM;
864 PROBE_SET_ACTION(softc, PROBE_PM_PID);
865 } else if (sign == 0xeb14 &&
866 done_ccb->ccb_h.target_id != 15) {
867 path->device->protocol = PROTO_SCSI;
868 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
870 if (done_ccb->ccb_h.target_id != 15) {
872 "Unexpected signature 0x%04x\n", sign);
876 xpt_release_ccb(done_ccb);
877 xpt_schedule(periph, priority);
882 struct ccb_pathinq cpi;
886 ident_buf = &softc->ident_data;
887 for (ptr = (int16_t *)ident_buf;
888 ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
889 *ptr = le16toh(*ptr);
891 if (strncmp(ident_buf->model, "FX", 2) &&
892 strncmp(ident_buf->model, "NEC", 3) &&
893 strncmp(ident_buf->model, "Pioneer", 7) &&
894 strncmp(ident_buf->model, "SHARP", 5)) {
895 ata_bswap(ident_buf->model, sizeof(ident_buf->model));
896 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
897 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
899 ata_btrim(ident_buf->model, sizeof(ident_buf->model));
900 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
901 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
902 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
903 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
904 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
905 /* Device may need spin-up before IDENTIFY become valid. */
906 if ((ident_buf->specconf == 0x37c8 ||
907 ident_buf->specconf == 0x738c) &&
908 ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
909 softc->spinup == 0)) {
910 PROBE_SET_ACTION(softc, PROBE_SPINUP);
911 xpt_release_ccb(done_ccb);
912 xpt_schedule(periph, priority);
915 ident_buf = &path->device->ident_data;
916 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
917 /* Check that it is the same device. */
918 if (bcmp(softc->ident_data.model, ident_buf->model,
919 sizeof(ident_buf->model)) ||
920 bcmp(softc->ident_data.revision, ident_buf->revision,
921 sizeof(ident_buf->revision)) ||
922 bcmp(softc->ident_data.serial, ident_buf->serial,
923 sizeof(ident_buf->serial))) {
924 /* Device changed. */
925 xpt_async(AC_LOST_DEVICE, path, NULL);
927 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
932 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
933 /* Clean up from previous instance of this device */
934 if (path->device->serial_num != NULL) {
935 free(path->device->serial_num, M_CAMXPT);
936 path->device->serial_num = NULL;
937 path->device->serial_num_len = 0;
939 path->device->serial_num =
940 (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
942 if (path->device->serial_num != NULL) {
943 bcopy(ident_buf->serial,
944 path->device->serial_num,
945 sizeof(ident_buf->serial));
946 path->device->serial_num[sizeof(ident_buf->serial)]
948 path->device->serial_num_len =
949 strlen(path->device->serial_num);
952 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
954 if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
955 path->device->mintags = path->device->maxtags =
956 ATA_QUEUE_LEN(ident_buf->queue) + 1;
958 ata_find_quirk(path->device);
959 if (path->device->mintags != 0 &&
960 path->bus->sim->max_tagged_dev_openings != 0) {
961 /* Check if the SIM does not want queued commands. */
962 bzero(&cpi, sizeof(cpi));
963 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
964 cpi.ccb_h.func_code = XPT_PATH_INQ;
965 xpt_action((union ccb *)&cpi);
966 if (cpi.ccb_h.status == CAM_REQ_CMP &&
967 (cpi.hba_inquiry & PI_TAG_ABLE)) {
968 /* Report SIM which tags are allowed. */
969 bzero(&cts, sizeof(cts));
970 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
971 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
972 cts.type = CTS_TYPE_CURRENT_SETTINGS;
973 cts.xport_specific.sata.tags = path->device->maxtags;
974 cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
975 xpt_action((union ccb *)&cts);
976 /* Reconfigure queues for tagged queueing. */
977 xpt_start_tags(path);
980 ata_device_transport(path);
981 PROBE_SET_ACTION(softc, PROBE_SETMODE);
982 xpt_release_ccb(done_ccb);
983 xpt_schedule(periph, priority);
988 xpt_print(path, "Spin-up done\n");
990 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
991 xpt_release_ccb(done_ccb);
992 xpt_schedule(periph, priority);
995 if (path->device->transport != XPORT_SATA)
997 /* Set supported bits. */
998 bzero(&cts, sizeof(cts));
999 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1000 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1001 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1002 xpt_action((union ccb *)&cts);
1003 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1004 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1007 if (ident_buf->satacapabilities != 0xffff) {
1008 if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
1009 caps |= CTS_SATA_CAPS_D_PMREQ;
1010 if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
1011 caps |= CTS_SATA_CAPS_D_APST;
1013 /* Mask unwanted bits. */
1014 bzero(&cts, sizeof(cts));
1015 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1016 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1017 cts.type = CTS_TYPE_USER_SETTINGS;
1018 xpt_action((union ccb *)&cts);
1019 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1020 caps &= cts.xport_specific.sata.caps;
1023 /* Store result to SIM. */
1024 bzero(&cts, sizeof(cts));
1025 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1026 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1027 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1028 cts.xport_specific.sata.caps = caps;
1029 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1030 xpt_action((union ccb *)&cts);
1032 if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
1033 (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
1034 (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
1035 PROBE_SET_ACTION(softc, PROBE_SETPM);
1036 xpt_release_ccb(done_ccb);
1037 xpt_schedule(periph, priority);
1042 if (ident_buf->satacapabilities != 0xffff &&
1043 (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
1044 (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
1045 (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
1046 PROBE_SET_ACTION(softc, PROBE_SETAPST);
1047 xpt_release_ccb(done_ccb);
1048 xpt_schedule(periph, priority);
1053 if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
1054 (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
1055 (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
1056 PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1057 xpt_release_ccb(done_ccb);
1058 xpt_schedule(periph, priority);
1062 case PROBE_SETDMAAA:
1063 if ((ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) &&
1064 (!(softc->caps & CTS_SATA_CAPS_H_AN)) !=
1065 (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) {
1066 PROBE_SET_ACTION(softc, PROBE_SETAN);
1067 xpt_release_ccb(done_ccb);
1068 xpt_schedule(periph, priority);
1074 if (path->device->protocol == PROTO_ATA) {
1075 PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
1077 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1079 xpt_release_ccb(done_ccb);
1080 xpt_schedule(periph, priority);
1082 case PROBE_SET_MULTI:
1083 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1084 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1085 xpt_acquire_device(path->device);
1086 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1087 xpt_action(done_ccb);
1088 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1093 case PROBE_FULL_INQUIRY:
1095 struct scsi_inquiry_data *inq_buf;
1096 u_int8_t periph_qual, len;
1098 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1099 inq_buf = &path->device->inq_data;
1101 periph_qual = SID_QUAL(inq_buf);
1103 if (periph_qual != SID_QUAL_LU_CONNECTED)
1107 * We conservatively request only
1108 * SHORT_INQUIRY_LEN bytes of inquiry
1109 * information during our first try
1110 * at sending an INQUIRY. If the device
1111 * has more information to give,
1112 * perform a second request specifying
1113 * the amount of information the device
1114 * is willing to give.
1116 len = inq_buf->additional_length
1117 + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1118 if (softc->action == PROBE_INQUIRY
1119 && len > SHORT_INQUIRY_LENGTH) {
1120 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1121 xpt_release_ccb(done_ccb);
1122 xpt_schedule(periph, priority);
1126 ata_device_transport(path);
1127 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1128 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1129 xpt_acquire_device(path->device);
1130 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1131 xpt_action(done_ccb);
1132 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
1137 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1138 bzero(ident_buf, sizeof(*ident_buf));
1139 softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1140 (done_ccb->ataio.res.lba_mid << 16) +
1141 (done_ccb->ataio.res.lba_low << 8) +
1142 done_ccb->ataio.res.sector_count;
1143 ((uint32_t *)ident_buf)[0] = softc->pm_pid;
1144 snprintf(ident_buf->model, sizeof(ident_buf->model),
1145 "Port Multiplier %08x", softc->pm_pid);
1146 PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1147 xpt_release_ccb(done_ccb);
1148 xpt_schedule(periph, priority);
1151 softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1152 (done_ccb->ataio.res.lba_mid << 16) +
1153 (done_ccb->ataio.res.lba_low << 8) +
1154 done_ccb->ataio.res.sector_count;
1155 ((uint32_t *)ident_buf)[1] = softc->pm_prv;
1156 snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1157 "%04x", softc->pm_prv);
1158 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1159 /* Set supported bits. */
1160 bzero(&cts, sizeof(cts));
1161 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1162 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1163 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1164 xpt_action((union ccb *)&cts);
1165 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1166 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1169 /* All PMPs must support PM requests. */
1170 caps |= CTS_SATA_CAPS_D_PMREQ;
1171 /* Mask unwanted bits. */
1172 bzero(&cts, sizeof(cts));
1173 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1174 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1175 cts.type = CTS_TYPE_USER_SETTINGS;
1176 xpt_action((union ccb *)&cts);
1177 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1178 caps &= cts.xport_specific.sata.caps;
1181 /* Store result to SIM. */
1182 bzero(&cts, sizeof(cts));
1183 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1184 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1185 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1186 cts.xport_specific.sata.caps = caps;
1187 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1188 xpt_action((union ccb *)&cts);
1190 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1191 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1192 xpt_acquire_device(path->device);
1193 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1194 xpt_action(done_ccb);
1195 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1198 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1199 xpt_action(done_ccb);
1200 xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
1204 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
1205 ("probedone: invalid action state\n"));
1210 if (softc->restart) {
1212 xpt_release_ccb(done_ccb);
1213 probeschedule(periph);
1216 xpt_release_ccb(done_ccb);
1217 while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1218 TAILQ_REMOVE(&softc->request_ccbs,
1219 &done_ccb->ccb_h, periph_links.tqe);
1220 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1223 cam_release_devq(periph->path,
1224 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
1225 cam_periph_invalidate(periph);
1226 cam_periph_release_locked(periph);
1230 probecleanup(struct cam_periph *periph)
1232 free(periph->softc, M_CAMXPT);
1236 ata_find_quirk(struct cam_ed *device)
1238 struct ata_quirk_entry *quirk;
1241 match = cam_quirkmatch((caddr_t)&device->ident_data,
1242 (caddr_t)ata_quirk_table,
1243 ata_quirk_table_size,
1244 sizeof(*ata_quirk_table), ata_identify_match);
1247 panic("xpt_find_quirk: device didn't match wildcard entry!!");
1249 quirk = (struct ata_quirk_entry *)match;
1250 device->quirk = quirk;
1251 if (quirk->quirks & CAM_QUIRK_MAXTAGS)
1252 device->mintags = device->maxtags = quirk->maxtags;
1256 union ccb *request_ccb;
1257 struct ccb_pathinq *cpi;
1259 } ata_scan_bus_info;
1262 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1263 * As the scan progresses, xpt_scan_bus is used as the
1264 * callback on completion function.
1267 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1269 struct cam_path *path;
1270 ata_scan_bus_info *scan_info;
1271 union ccb *work_ccb, *reset_ccb;
1274 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1275 ("xpt_scan_bus\n"));
1276 switch (request_ccb->ccb_h.func_code) {
1279 /* Find out the characteristics of the bus */
1280 work_ccb = xpt_alloc_ccb_nowait();
1281 if (work_ccb == NULL) {
1282 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1283 xpt_done(request_ccb);
1286 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1287 request_ccb->ccb_h.pinfo.priority);
1288 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1289 xpt_action(work_ccb);
1290 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1291 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1292 xpt_free_ccb(work_ccb);
1293 xpt_done(request_ccb);
1297 /* We may need to reset bus first, if we haven't done it yet. */
1298 if ((work_ccb->cpi.hba_inquiry &
1299 (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1300 !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1301 !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1302 reset_ccb = xpt_alloc_ccb_nowait();
1303 if (reset_ccb == NULL) {
1304 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1305 xpt_free_ccb(work_ccb);
1306 xpt_done(request_ccb);
1309 xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1311 reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1312 xpt_action(reset_ccb);
1313 if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1314 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1315 xpt_free_ccb(reset_ccb);
1316 xpt_free_ccb(work_ccb);
1317 xpt_done(request_ccb);
1320 xpt_free_ccb(reset_ccb);
1323 /* Save some state for use while we probe for devices */
1324 scan_info = (ata_scan_bus_info *)
1325 malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1326 if (scan_info == NULL) {
1327 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1328 xpt_free_ccb(work_ccb);
1329 xpt_done(request_ccb);
1332 scan_info->request_ccb = request_ccb;
1333 scan_info->cpi = &work_ccb->cpi;
1334 /* If PM supported, probe it first. */
1335 if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1336 scan_info->counter = scan_info->cpi->max_target;
1338 scan_info->counter = 0;
1340 work_ccb = xpt_alloc_ccb_nowait();
1341 if (work_ccb == NULL) {
1342 free(scan_info, M_CAMXPT);
1343 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1344 xpt_done(request_ccb);
1349 work_ccb = request_ccb;
1350 /* Reuse the same CCB to query if a device was really found */
1351 scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1352 /* If there is PMP... */
1353 if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1354 (scan_info->counter == scan_info->cpi->max_target)) {
1355 if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1356 /* everything else will be probed by it */
1357 /* Free the current request path- we're done with it. */
1358 xpt_free_path(work_ccb->ccb_h.path);
1361 struct ccb_trans_settings cts;
1363 /* Report SIM that PM is absent. */
1364 bzero(&cts, sizeof(cts));
1365 xpt_setup_ccb(&cts.ccb_h,
1366 work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1367 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1368 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1369 cts.xport_specific.sata.pm_present = 0;
1370 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1371 xpt_action((union ccb *)&cts);
1374 /* Free the current request path- we're done with it. */
1375 xpt_free_path(work_ccb->ccb_h.path);
1376 if (scan_info->counter ==
1377 ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1378 0 : scan_info->cpi->max_target)) {
1380 xpt_free_ccb(work_ccb);
1381 xpt_free_ccb((union ccb *)scan_info->cpi);
1382 request_ccb = scan_info->request_ccb;
1383 free(scan_info, M_CAMXPT);
1384 request_ccb->ccb_h.status = CAM_REQ_CMP;
1385 xpt_done(request_ccb);
1388 /* Take next device. Wrap from max (PMP) to 0. */
1389 scan_info->counter = (scan_info->counter + 1 ) %
1390 (scan_info->cpi->max_target + 1);
1392 status = xpt_create_path(&path, xpt_periph,
1393 scan_info->request_ccb->ccb_h.path_id,
1394 scan_info->counter, 0);
1395 if (status != CAM_REQ_CMP) {
1396 printf("xpt_scan_bus: xpt_create_path failed"
1397 " with status %#x, bus scan halted\n",
1399 xpt_free_ccb(work_ccb);
1400 xpt_free_ccb((union ccb *)scan_info->cpi);
1401 request_ccb = scan_info->request_ccb;
1402 free(scan_info, M_CAMXPT);
1403 request_ccb->ccb_h.status = status;
1404 xpt_done(request_ccb);
1407 xpt_setup_ccb(&work_ccb->ccb_h, path,
1408 scan_info->request_ccb->ccb_h.pinfo.priority);
1409 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1410 work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1411 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1412 work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1413 xpt_action(work_ccb);
1421 ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1422 cam_flags flags, union ccb *request_ccb)
1424 struct ccb_pathinq cpi;
1426 struct cam_path *new_path;
1427 struct cam_periph *old_periph;
1429 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1431 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1432 cpi.ccb_h.func_code = XPT_PATH_INQ;
1433 xpt_action((union ccb *)&cpi);
1435 if (cpi.ccb_h.status != CAM_REQ_CMP) {
1436 if (request_ccb != NULL) {
1437 request_ccb->ccb_h.status = cpi.ccb_h.status;
1438 xpt_done(request_ccb);
1443 if (request_ccb == NULL) {
1444 request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1445 if (request_ccb == NULL) {
1446 xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1447 "can't continue\n");
1450 new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1451 if (new_path == NULL) {
1452 xpt_print(path, "xpt_scan_lun: can't allocate path, "
1453 "can't continue\n");
1454 free(request_ccb, M_CAMXPT);
1457 status = xpt_compile_path(new_path, xpt_periph,
1459 path->target->target_id,
1460 path->device->lun_id);
1462 if (status != CAM_REQ_CMP) {
1463 xpt_print(path, "xpt_scan_lun: can't compile path, "
1464 "can't continue\n");
1465 free(request_ccb, M_CAMXPT);
1466 free(new_path, M_CAMXPT);
1469 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1470 request_ccb->ccb_h.cbfcnp = xptscandone;
1471 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1472 request_ccb->crcn.flags = flags;
1475 if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1478 softc = (probe_softc *)old_periph->softc;
1479 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1483 status = cam_periph_alloc(proberegister, NULL, probecleanup,
1484 probestart, "aprobe",
1486 request_ccb->ccb_h.path, NULL, 0,
1489 if (status != CAM_REQ_CMP) {
1490 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1491 "returned an error, can't continue probe\n");
1492 request_ccb->ccb_h.status = status;
1493 xpt_done(request_ccb);
1499 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1501 xpt_release_path(done_ccb->ccb_h.path);
1502 free(done_ccb->ccb_h.path, M_CAMXPT);
1503 free(done_ccb, M_CAMXPT);
1506 static struct cam_ed *
1507 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1509 struct cam_path path;
1510 struct ata_quirk_entry *quirk;
1511 struct cam_ed *device;
1512 struct cam_ed *cur_device;
1514 device = xpt_alloc_device(bus, target, lun_id);
1519 * Take the default quirk entry until we have inquiry
1520 * data and can determine a better quirk to use.
1522 quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1523 device->quirk = (void *)quirk;
1524 device->mintags = 0;
1525 device->maxtags = 0;
1526 bzero(&device->inq_data, sizeof(device->inq_data));
1527 device->inq_flags = 0;
1528 device->queue_flags = 0;
1529 device->serial_num = NULL;
1530 device->serial_num_len = 0;
1533 * XXX should be limited by number of CCBs this bus can
1536 bus->sim->max_ccbs += device->ccbq.devq_openings;
1537 /* Insertion sort into our target's device list */
1538 cur_device = TAILQ_FIRST(&target->ed_entries);
1539 while (cur_device != NULL && cur_device->lun_id < lun_id)
1540 cur_device = TAILQ_NEXT(cur_device, links);
1541 if (cur_device != NULL) {
1542 TAILQ_INSERT_BEFORE(cur_device, device, links);
1544 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1546 target->generation++;
1547 if (lun_id != CAM_LUN_WILDCARD) {
1548 xpt_compile_path(&path,
1553 ata_device_transport(&path);
1554 xpt_release_path(&path);
1561 ata_device_transport(struct cam_path *path)
1563 struct ccb_pathinq cpi;
1564 struct ccb_trans_settings cts;
1565 struct scsi_inquiry_data *inq_buf = NULL;
1566 struct ata_params *ident_buf = NULL;
1568 /* Get transport information from the SIM */
1569 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1570 cpi.ccb_h.func_code = XPT_PATH_INQ;
1571 xpt_action((union ccb *)&cpi);
1573 path->device->transport = cpi.transport;
1574 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1575 inq_buf = &path->device->inq_data;
1576 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1577 ident_buf = &path->device->ident_data;
1578 if (path->device->protocol == PROTO_ATA) {
1579 path->device->protocol_version = ident_buf ?
1580 ata_version(ident_buf->version_major) : cpi.protocol_version;
1581 } else if (path->device->protocol == PROTO_SCSI) {
1582 path->device->protocol_version = inq_buf ?
1583 SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1585 path->device->transport_version = ident_buf ?
1586 ata_version(ident_buf->version_major) : cpi.transport_version;
1588 /* Tell the controller what we think */
1589 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1590 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1591 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1592 cts.transport = path->device->transport;
1593 cts.transport_version = path->device->transport_version;
1594 cts.protocol = path->device->protocol;
1595 cts.protocol_version = path->device->protocol_version;
1596 cts.proto_specific.valid = 0;
1598 if (path->device->transport == XPORT_ATA) {
1599 cts.xport_specific.ata.atapi =
1600 (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1601 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1602 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1603 cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1605 cts.xport_specific.sata.atapi =
1606 (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1607 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1608 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1609 cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1612 cts.xport_specific.valid = 0;
1613 xpt_action((union ccb *)&cts);
1617 ata_dev_advinfo(union ccb *start_ccb)
1619 struct cam_ed *device;
1620 struct ccb_dev_advinfo *cdai;
1623 start_ccb->ccb_h.status = CAM_REQ_INVALID;
1624 device = start_ccb->ccb_h.path->device;
1625 cdai = &start_ccb->cdai;
1626 switch(cdai->buftype) {
1627 case CDAI_TYPE_SERIAL_NUM:
1628 if (cdai->flags & CDAI_FLAG_STORE)
1630 start_ccb->ccb_h.status = CAM_REQ_CMP;
1631 cdai->provsiz = device->serial_num_len;
1632 if (device->serial_num_len == 0)
1634 amt = device->serial_num_len;
1635 if (cdai->provsiz > cdai->bufsiz)
1637 memcpy(cdai->buf, device->serial_num, amt);
1645 ata_action(union ccb *start_ccb)
1648 switch (start_ccb->ccb_h.func_code) {
1649 case XPT_SET_TRAN_SETTINGS:
1651 ata_set_transfer_settings(&start_ccb->cts,
1652 start_ccb->ccb_h.path->device,
1653 /*async_update*/FALSE);
1658 ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1661 ata_scan_lun(start_ccb->ccb_h.path->periph,
1662 start_ccb->ccb_h.path, start_ccb->crcn.flags,
1665 case XPT_GET_TRAN_SETTINGS:
1667 struct cam_sim *sim;
1669 sim = start_ccb->ccb_h.path->bus->sim;
1670 (*(sim->sim_action))(sim, start_ccb);
1675 struct cam_ed *device;
1678 device = start_ccb->ccb_h.path->device;
1679 if (device->protocol == PROTO_SCSI &&
1680 (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1682 device->ident_data.config & ATA_PROTO_MASK;
1685 (device->ident_data.config == ATA_PROTO_CFA) ? 0 :
1686 (p == ATA_PROTO_ATAPI_16) ? 16 :
1687 (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1689 if (start_ccb->csio.cdb_len > maxlen) {
1690 start_ccb->ccb_h.status = CAM_REQ_INVALID;
1691 xpt_done(start_ccb);
1694 xpt_action_default(start_ccb);
1697 case XPT_DEV_ADVINFO:
1699 ata_dev_advinfo(start_ccb);
1703 xpt_action_default(start_ccb);
1709 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1712 struct ccb_pathinq cpi;
1713 struct ccb_trans_settings cur_cts;
1714 struct ccb_trans_settings_scsi *scsi;
1715 struct ccb_trans_settings_scsi *cur_scsi;
1716 struct cam_sim *sim;
1717 struct scsi_inquiry_data *inq_data;
1719 if (device == NULL) {
1720 cts->ccb_h.status = CAM_PATH_INVALID;
1721 xpt_done((union ccb *)cts);
1725 if (cts->protocol == PROTO_UNKNOWN
1726 || cts->protocol == PROTO_UNSPECIFIED) {
1727 cts->protocol = device->protocol;
1728 cts->protocol_version = device->protocol_version;
1731 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1732 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1733 cts->protocol_version = device->protocol_version;
1735 if (cts->protocol != device->protocol) {
1736 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1737 cts->protocol, device->protocol);
1738 cts->protocol = device->protocol;
1741 if (cts->protocol_version > device->protocol_version) {
1743 xpt_print(cts->ccb_h.path, "Down reving Protocol "
1744 "Version from %d to %d?\n", cts->protocol_version,
1745 device->protocol_version);
1747 cts->protocol_version = device->protocol_version;
1750 if (cts->transport == XPORT_UNKNOWN
1751 || cts->transport == XPORT_UNSPECIFIED) {
1752 cts->transport = device->transport;
1753 cts->transport_version = device->transport_version;
1756 if (cts->transport_version == XPORT_VERSION_UNKNOWN
1757 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1758 cts->transport_version = device->transport_version;
1760 if (cts->transport != device->transport) {
1761 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1762 cts->transport, device->transport);
1763 cts->transport = device->transport;
1766 if (cts->transport_version > device->transport_version) {
1768 xpt_print(cts->ccb_h.path, "Down reving Transport "
1769 "Version from %d to %d?\n", cts->transport_version,
1770 device->transport_version);
1772 cts->transport_version = device->transport_version;
1775 sim = cts->ccb_h.path->bus->sim;
1778 * Nothing more of interest to do unless
1779 * this is a device connected via the
1782 if (cts->protocol != PROTO_SCSI) {
1783 if (async_update == FALSE)
1784 (*(sim->sim_action))(sim, (union ccb *)cts);
1788 inq_data = &device->inq_data;
1789 scsi = &cts->proto_specific.scsi;
1790 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1791 cpi.ccb_h.func_code = XPT_PATH_INQ;
1792 xpt_action((union ccb *)&cpi);
1794 /* SCSI specific sanity checking */
1795 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1796 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1797 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1798 || (device->mintags == 0)) {
1800 * Can't tag on hardware that doesn't support tags,
1801 * doesn't have it enabled, or has broken tag support.
1803 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1806 if (async_update == FALSE) {
1808 * Perform sanity checking against what the
1809 * controller and device can do.
1811 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1812 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1813 cur_cts.type = cts->type;
1814 xpt_action((union ccb *)&cur_cts);
1815 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1818 cur_scsi = &cur_cts.proto_specific.scsi;
1819 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1820 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1821 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1823 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1824 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1827 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1828 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1832 * If we are transitioning from tags to no-tags or
1833 * vice-versa, we need to carefully freeze and restart
1834 * the queue so that we don't overlap tagged and non-tagged
1835 * commands. We also temporarily stop tags if there is
1836 * a change in transfer negotiation settings to allow
1837 * "tag-less" negotiation.
1839 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1840 || (device->inq_flags & SID_CmdQue) != 0)
1841 device_tagenb = TRUE;
1843 device_tagenb = FALSE;
1845 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1846 && device_tagenb == FALSE)
1847 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1848 && device_tagenb == TRUE)) {
1850 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1852 * Delay change to use tags until after a
1853 * few commands have gone to this device so
1854 * the controller has time to perform transfer
1855 * negotiations without tagged messages getting
1858 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1859 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1861 xpt_stop_tags(cts->ccb_h.path);
1865 if (async_update == FALSE)
1866 (*(sim->sim_action))(sim, (union ccb *)cts);
1870 * Handle any per-device event notifications that require action by the XPT.
1873 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1874 struct cam_ed *device, void *async_arg)
1877 struct cam_path newpath;
1880 * We only need to handle events for real devices.
1882 if (target->target_id == CAM_TARGET_WILDCARD
1883 || device->lun_id == CAM_LUN_WILDCARD)
1887 * We need our own path with wildcards expanded to
1888 * handle certain types of events.
1890 if ((async_code == AC_SENT_BDR)
1891 || (async_code == AC_BUS_RESET)
1892 || (async_code == AC_INQ_CHANGED))
1893 status = xpt_compile_path(&newpath, NULL,
1898 status = CAM_REQ_CMP_ERR;
1900 if (status == CAM_REQ_CMP) {
1901 if (async_code == AC_INQ_CHANGED) {
1903 * We've sent a start unit command, or
1904 * something similar to a device that
1905 * may have caused its inquiry data to
1906 * change. So we re-scan the device to
1907 * refresh the inquiry data for it.
1909 ata_scan_lun(newpath.periph, &newpath,
1910 CAM_EXPECT_INQ_CHANGE, NULL);
1912 /* We need to reinitialize device after reset. */
1913 ata_scan_lun(newpath.periph, &newpath,
1916 xpt_release_path(&newpath);
1917 } else if (async_code == AC_LOST_DEVICE &&
1918 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1919 device->flags |= CAM_DEV_UNCONFIGURED;
1920 xpt_release_device(device);
1921 } else if (async_code == AC_TRANSFER_NEG) {
1922 struct ccb_trans_settings *settings;
1924 settings = (struct ccb_trans_settings *)async_arg;
1925 ata_set_transfer_settings(settings, device,
1926 /*async_update*/TRUE);
1931 ata_announce_periph(struct cam_periph *periph)
1933 struct ccb_pathinq cpi;
1934 struct ccb_trans_settings cts;
1935 struct cam_path *path = periph->path;
1939 mtx_assert(periph->sim->mtx, MA_OWNED);
1941 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1942 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1943 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1944 xpt_action((union ccb*)&cts);
1945 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1947 /* Ask the SIM for its base transfer speed */
1948 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1949 cpi.ccb_h.func_code = XPT_PATH_INQ;
1950 xpt_action((union ccb *)&cpi);
1951 /* Report connection speed */
1952 speed = cpi.base_transfer_speed;
1953 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1954 struct ccb_trans_settings_ata *ata =
1955 &cts.xport_specific.ata;
1957 if (ata->valid & CTS_ATA_VALID_MODE)
1958 speed = ata_mode2speed(ata->mode);
1960 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1961 struct ccb_trans_settings_sata *sata =
1962 &cts.xport_specific.sata;
1964 if (sata->valid & CTS_SATA_VALID_REVISION)
1965 speed = ata_revision2speed(sata->revision);
1969 printf("%s%d: %d.%03dMB/s transfers",
1970 periph->periph_name, periph->unit_number,
1973 printf("%s%d: %dKB/s transfers", periph->periph_name,
1974 periph->unit_number, speed);
1975 /* Report additional information about connection */
1976 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1977 struct ccb_trans_settings_ata *ata =
1978 &cts.xport_specific.ata;
1981 if (ata->valid & CTS_ATA_VALID_MODE)
1982 printf("%s, ", ata_mode2string(ata->mode));
1983 if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
1984 printf("ATAPI %dbytes, ", ata->atapi);
1985 if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
1986 printf("PIO %dbytes", ata->bytecount);
1989 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1990 struct ccb_trans_settings_sata *sata =
1991 &cts.xport_specific.sata;
1994 if (sata->valid & CTS_SATA_VALID_REVISION)
1995 printf("SATA %d.x, ", sata->revision);
1998 if (sata->valid & CTS_SATA_VALID_MODE)
1999 printf("%s, ", ata_mode2string(sata->mode));
2000 if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
2001 printf("ATAPI %dbytes, ", sata->atapi);
2002 if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
2003 printf("PIO %dbytes", sata->bytecount);