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;
139 struct cam_periph *periph;
142 static struct ata_quirk_entry ata_quirk_table[] =
145 /* Default tagged queuing parameters for all devices */
147 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
148 /*vendor*/"*", /*product*/"*", /*revision*/"*"
150 /*quirks*/0, /*maxtags*/0
154 static const int ata_quirk_table_size =
155 sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
157 static cam_status proberegister(struct cam_periph *periph,
159 static void probeschedule(struct cam_periph *probe_periph);
160 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
161 //static void proberequestdefaultnegotiation(struct cam_periph *periph);
162 //static int proberequestbackoff(struct cam_periph *periph,
163 // struct cam_ed *device);
164 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
165 static void probecleanup(struct cam_periph *periph);
166 static void ata_find_quirk(struct cam_ed *device);
167 static void ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
168 static void ata_scan_lun(struct cam_periph *periph,
169 struct cam_path *path, cam_flags flags,
171 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
172 static struct cam_ed *
173 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
175 static void ata_device_transport(struct cam_path *path);
176 static void ata_set_transfer_settings(struct ccb_trans_settings *cts,
177 struct cam_ed *device,
179 static void ata_dev_async(u_int32_t async_code,
181 struct cam_et *target,
182 struct cam_ed *device,
184 static void ata_action(union ccb *start_ccb);
185 static void ata_announce_periph(struct cam_periph *periph);
187 static struct xpt_xport ata_xport = {
188 .alloc_device = ata_alloc_device,
189 .action = ata_action,
190 .async = ata_dev_async,
191 .announce = ata_announce_periph,
206 proberegister(struct cam_periph *periph, void *arg)
208 union ccb *request_ccb; /* CCB representing the probe request */
212 request_ccb = (union ccb *)arg;
213 if (periph == NULL) {
214 printf("proberegister: periph was NULL!!\n");
215 return(CAM_REQ_CMP_ERR);
218 if (request_ccb == NULL) {
219 printf("proberegister: no probe CCB, "
220 "can't register device\n");
221 return(CAM_REQ_CMP_ERR);
224 softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
227 printf("proberegister: Unable to probe new device. "
228 "Unable to allocate softc\n");
229 return(CAM_REQ_CMP_ERR);
231 TAILQ_INIT(&softc->request_ccbs);
232 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
235 periph->softc = softc;
236 softc->periph = periph;
237 softc->action = PROBE_INVALID;
238 status = cam_periph_acquire(periph);
239 if (status != CAM_REQ_CMP) {
243 * Ensure nobody slip in until probe finish.
245 cam_freeze_devq_arg(periph->path,
246 RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
247 probeschedule(periph);
252 probeschedule(struct cam_periph *periph)
257 softc = (probe_softc *)periph->softc;
258 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
260 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
261 periph->path->device->protocol == PROTO_SATAPM)
262 PROBE_SET_ACTION(softc, PROBE_RESET);
264 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
266 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
267 softc->flags |= PROBE_NO_ANNOUNCE;
269 softc->flags &= ~PROBE_NO_ANNOUNCE;
271 xpt_schedule(periph, CAM_PRIORITY_XPT);
275 probestart(struct cam_periph *periph, union ccb *start_ccb)
277 struct ccb_trans_settings cts;
278 struct ccb_ataio *ataio;
279 struct ccb_scsiio *csio;
281 struct cam_path *path;
282 struct ata_params *ident_buf;
284 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
286 softc = (probe_softc *)periph->softc;
287 path = start_ccb->ccb_h.path;
288 ataio = &start_ccb->ataio;
289 csio = &start_ccb->csio;
290 ident_buf = &periph->path->device->ident_data;
292 if (softc->restart) {
294 if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
295 path->device->protocol == PROTO_SATAPM)
296 softc->action = PROBE_RESET;
298 softc->action = PROBE_IDENTIFY;
300 switch (softc->action) {
302 cam_fill_ataio(ataio,
305 /*flags*/CAM_DIR_NONE,
310 ata_reset_cmd(ataio);
313 cam_fill_ataio(ataio,
318 /*data_ptr*/(u_int8_t *)&softc->ident_data,
319 /*dxfer_len*/sizeof(softc->ident_data),
321 if (periph->path->device->protocol == PROTO_ATA)
322 ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
324 ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
328 xpt_print(path, "Spinning up device\n");
329 cam_fill_ataio(ataio,
332 /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
337 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
344 /* Fetch user modes from SIM. */
345 bzero(&cts, sizeof(cts));
346 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
347 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
348 cts.type = CTS_TYPE_USER_SETTINGS;
349 xpt_action((union ccb *)&cts);
350 if (path->device->transport == XPORT_ATA) {
351 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
352 mode = cts.xport_specific.ata.mode;
354 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
355 mode = cts.xport_specific.sata.mode;
358 /* Honor device capabilities. */
359 wantmode = mode = ata_max_mode(ident_buf, mode);
360 /* Report modes to SIM. */
361 bzero(&cts, sizeof(cts));
362 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
363 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
364 cts.type = CTS_TYPE_CURRENT_SETTINGS;
365 if (path->device->transport == XPORT_ATA) {
366 cts.xport_specific.ata.mode = mode;
367 cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
369 cts.xport_specific.sata.mode = mode;
370 cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
372 xpt_action((union ccb *)&cts);
373 /* Fetch current modes from SIM. */
374 bzero(&cts, sizeof(cts));
375 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
376 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
377 cts.type = CTS_TYPE_CURRENT_SETTINGS;
378 xpt_action((union ccb *)&cts);
379 if (path->device->transport == XPORT_ATA) {
380 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
381 mode = cts.xport_specific.ata.mode;
383 if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
384 mode = cts.xport_specific.sata.mode;
386 /* If SIM disagree - renegotiate. */
387 if (mode != wantmode)
389 cam_fill_ataio(ataio,
392 /*flags*/CAM_DIR_NONE,
397 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
401 cam_fill_ataio(ataio,
409 ata_28bit_cmd(ataio, ATA_SETFEATURES,
410 (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
414 cam_fill_ataio(ataio,
422 ata_28bit_cmd(ataio, ATA_SETFEATURES,
423 (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
427 cam_fill_ataio(ataio,
435 ata_28bit_cmd(ataio, ATA_SETFEATURES,
436 (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
439 case PROBE_SET_MULTI:
441 u_int sectors, bytecount;
443 bytecount = 8192; /* SATA maximum */
444 /* Fetch user bytecount from SIM. */
445 bzero(&cts, sizeof(cts));
446 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
447 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
448 cts.type = CTS_TYPE_USER_SETTINGS;
449 xpt_action((union ccb *)&cts);
450 if (path->device->transport == XPORT_ATA) {
451 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
452 bytecount = cts.xport_specific.ata.bytecount;
454 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
455 bytecount = cts.xport_specific.sata.bytecount;
457 /* Honor device capabilities. */
458 sectors = max(1, min(ident_buf->sectors_intr & 0xff,
459 bytecount / ata_logical_sector_size(ident_buf)));
460 /* Report bytecount to SIM. */
461 bzero(&cts, sizeof(cts));
462 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
463 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
464 cts.type = CTS_TYPE_CURRENT_SETTINGS;
465 if (path->device->transport == XPORT_ATA) {
466 cts.xport_specific.ata.bytecount = sectors *
467 ata_logical_sector_size(ident_buf);
468 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
470 cts.xport_specific.sata.bytecount = sectors *
471 ata_logical_sector_size(ident_buf);
472 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
474 xpt_action((union ccb *)&cts);
475 /* Fetch current bytecount from SIM. */
476 bzero(&cts, sizeof(cts));
477 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
478 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
479 cts.type = CTS_TYPE_CURRENT_SETTINGS;
480 xpt_action((union ccb *)&cts);
481 if (path->device->transport == XPORT_ATA) {
482 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
483 bytecount = cts.xport_specific.ata.bytecount;
485 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
486 bytecount = cts.xport_specific.sata.bytecount;
488 sectors = bytecount / ata_logical_sector_size(ident_buf);
490 cam_fill_ataio(ataio,
498 ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
505 bytecount = 8192; /* SATA maximum */
506 /* Fetch user bytecount from SIM. */
507 bzero(&cts, sizeof(cts));
508 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
509 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
510 cts.type = CTS_TYPE_USER_SETTINGS;
511 xpt_action((union ccb *)&cts);
512 if (path->device->transport == XPORT_ATA) {
513 if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
514 bytecount = cts.xport_specific.ata.bytecount;
516 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
517 bytecount = cts.xport_specific.sata.bytecount;
519 /* Honor device capabilities. */
521 bytecount = max(2, min(65534, bytecount));
522 if (ident_buf->satacapabilities != 0x0000 &&
523 ident_buf->satacapabilities != 0xffff) {
524 bytecount = min(8192, bytecount);
526 /* Report bytecount to SIM. */
527 bzero(&cts, sizeof(cts));
528 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
529 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
530 cts.type = CTS_TYPE_CURRENT_SETTINGS;
531 if (path->device->transport == XPORT_ATA) {
532 cts.xport_specific.ata.bytecount = bytecount;
533 cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
535 cts.xport_specific.sata.bytecount = bytecount;
536 cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
538 xpt_action((union ccb *)&cts);
541 case PROBE_FULL_INQUIRY:
544 struct scsi_inquiry_data *inq_buf =
545 &periph->path->device->inq_data;
547 if (softc->action == PROBE_INQUIRY)
548 inquiry_len = SHORT_INQUIRY_LENGTH;
550 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
552 * Some parallel SCSI devices fail to send an
553 * ignore wide residue message when dealing with
554 * odd length inquiry requests. Round up to be
557 inquiry_len = roundup2(inquiry_len, 2);
567 /*timeout*/60 * 1000);
571 cam_fill_ataio(ataio,
574 /*flags*/CAM_DIR_NONE,
579 ata_pm_read_cmd(ataio, 0, 15);
582 cam_fill_ataio(ataio,
585 /*flags*/CAM_DIR_NONE,
590 ata_pm_read_cmd(ataio, 1, 15);
593 CAM_DEBUG(path, CAM_DEBUG_INFO,
594 ("probestart: invalid action state\n"));
598 xpt_action(start_ccb);
602 proberequestdefaultnegotiation(struct cam_periph *periph)
604 struct ccb_trans_settings cts;
606 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
607 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
608 cts.type = CTS_TYPE_USER_SETTINGS;
609 xpt_action((union ccb *)&cts);
610 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
613 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
614 cts.type = CTS_TYPE_CURRENT_SETTINGS;
615 xpt_action((union ccb *)&cts);
619 * Backoff Negotiation Code- only pertinent for SPI devices.
622 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
624 struct ccb_trans_settings cts;
625 struct ccb_trans_settings_spi *spi;
627 memset(&cts, 0, sizeof (cts));
628 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
629 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
630 cts.type = CTS_TYPE_CURRENT_SETTINGS;
631 xpt_action((union ccb *)&cts);
632 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
634 xpt_print(periph->path,
635 "failed to get current device settings\n");
639 if (cts.transport != XPORT_SPI) {
641 xpt_print(periph->path, "not SPI transport\n");
645 spi = &cts.xport_specific.spi;
648 * We cannot renegotiate sync rate if we don't have one.
650 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
652 xpt_print(periph->path, "no sync rate known\n");
658 * We'll assert that we don't have to touch PPR options- the
659 * SIM will see what we do with period and offset and adjust
660 * the PPR options as appropriate.
664 * A sync rate with unknown or zero offset is nonsensical.
665 * A sync period of zero means Async.
667 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
668 || spi->sync_offset == 0 || spi->sync_period == 0) {
670 xpt_print(periph->path, "no sync rate available\n");
675 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
676 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
677 ("hit async: giving up on DV\n"));
683 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
684 * We don't try to remember 'last' settings to see if the SIM actually
685 * gets into the speed we want to set. We check on the SIM telling
686 * us that a requested speed is bad, but otherwise don't try and
687 * check the speed due to the asynchronous and handshake nature
690 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
693 if (spi->sync_period >= 0xf) {
694 spi->sync_period = 0;
695 spi->sync_offset = 0;
696 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
697 ("setting to async for DV\n"));
699 * Once we hit async, we don't want to try
702 device->flags |= CAM_DEV_DV_HIT_BOTTOM;
703 } else if (bootverbose) {
704 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
705 ("DV: period 0x%x\n", spi->sync_period));
706 printf("setting period to 0x%x\n", spi->sync_period);
708 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
709 cts.type = CTS_TYPE_CURRENT_SETTINGS;
710 xpt_action((union ccb *)&cts);
711 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
714 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
715 ("DV: failed to set period 0x%x\n", spi->sync_period));
716 if (spi->sync_period == 0) {
724 probedone(struct cam_periph *periph, union ccb *done_ccb)
726 struct ccb_trans_settings cts;
727 struct ata_params *ident_buf;
729 struct cam_path *path;
734 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
736 softc = (probe_softc *)periph->softc;
737 path = done_ccb->ccb_h.path;
738 priority = done_ccb->ccb_h.pinfo.priority;
739 ident_buf = &path->device->ident_data;
741 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
742 if (softc->restart) {
744 cam_error_print(done_ccb,
745 CAM_ESF_ALL, CAM_EPF_ALL);
747 } else if (cam_periph_error(done_ccb, 0, 0, NULL) == ERESTART)
749 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
750 /* Don't wedge the queue */
751 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
754 if (softc->restart) {
756 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
759 if (softc->faults < 10)
764 /* Old PIO2 devices may not support mode setting. */
765 if (softc->action == PROBE_SETMODE &&
766 ata_max_pmode(ident_buf) <= ATA_PIO2 &&
767 (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
770 * If we get to this point, we got an error status back
771 * from the inquiry and the error status doesn't require
772 * automatically retrying the command. Therefore, the
773 * inquiry failed. If we had inquiry information before
774 * for this device, but this latest inquiry command failed,
775 * the device has probably gone away. If this device isn't
776 * already marked unconfigured, notify the peripheral
777 * drivers that this device is no more.
779 device_fail: if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
780 xpt_async(AC_LOST_DEVICE, path, NULL);
787 switch (softc->action) {
790 int sign = (done_ccb->ataio.res.lba_high << 8) +
791 done_ccb->ataio.res.lba_mid;
793 xpt_print(path, "SIGNATURE: %04x\n", sign);
794 if (sign == 0x0000 &&
795 done_ccb->ccb_h.target_id != 15) {
796 path->device->protocol = PROTO_ATA;
797 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
798 } else if (sign == 0x9669 &&
799 done_ccb->ccb_h.target_id == 15) {
800 /* Report SIM that PM is present. */
801 bzero(&cts, sizeof(cts));
802 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
803 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
804 cts.type = CTS_TYPE_CURRENT_SETTINGS;
805 cts.xport_specific.sata.pm_present = 1;
806 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
807 xpt_action((union ccb *)&cts);
808 path->device->protocol = PROTO_SATAPM;
809 PROBE_SET_ACTION(softc, PROBE_PM_PID);
810 } else if (sign == 0xeb14 &&
811 done_ccb->ccb_h.target_id != 15) {
812 path->device->protocol = PROTO_SCSI;
813 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
815 if (done_ccb->ccb_h.target_id != 15) {
817 "Unexpected signature 0x%04x\n", sign);
821 xpt_release_ccb(done_ccb);
822 xpt_schedule(periph, priority);
827 struct ccb_pathinq cpi;
831 ident_buf = &softc->ident_data;
832 for (ptr = (int16_t *)ident_buf;
833 ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
834 *ptr = le16toh(*ptr);
836 if (strncmp(ident_buf->model, "FX", 2) &&
837 strncmp(ident_buf->model, "NEC", 3) &&
838 strncmp(ident_buf->model, "Pioneer", 7) &&
839 strncmp(ident_buf->model, "SHARP", 5)) {
840 ata_bswap(ident_buf->model, sizeof(ident_buf->model));
841 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
842 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
844 ata_btrim(ident_buf->model, sizeof(ident_buf->model));
845 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
846 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
847 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
848 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
849 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
850 /* Device may need spin-up before IDENTIFY become valid. */
851 if ((ident_buf->specconf == 0x37c8 ||
852 ident_buf->specconf == 0x738c) &&
853 ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
854 softc->spinup == 0)) {
855 PROBE_SET_ACTION(softc, PROBE_SPINUP);
856 xpt_release_ccb(done_ccb);
857 xpt_schedule(periph, priority);
860 ident_buf = &path->device->ident_data;
861 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
862 /* Check that it is the same device. */
863 if (bcmp(softc->ident_data.model, ident_buf->model,
864 sizeof(ident_buf->model)) ||
865 bcmp(softc->ident_data.revision, ident_buf->revision,
866 sizeof(ident_buf->revision)) ||
867 bcmp(softc->ident_data.serial, ident_buf->serial,
868 sizeof(ident_buf->serial))) {
869 /* Device changed. */
870 xpt_async(AC_LOST_DEVICE, path, NULL);
872 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
877 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
878 /* Clean up from previous instance of this device */
879 if (path->device->serial_num != NULL) {
880 free(path->device->serial_num, M_CAMXPT);
881 path->device->serial_num = NULL;
882 path->device->serial_num_len = 0;
884 path->device->serial_num =
885 (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
887 if (path->device->serial_num != NULL) {
888 bcopy(ident_buf->serial,
889 path->device->serial_num,
890 sizeof(ident_buf->serial));
891 path->device->serial_num[sizeof(ident_buf->serial)]
893 path->device->serial_num_len =
894 strlen(path->device->serial_num);
897 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
899 if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
900 path->device->mintags = path->device->maxtags =
901 ATA_QUEUE_LEN(ident_buf->queue) + 1;
903 ata_find_quirk(path->device);
904 if (path->device->mintags != 0 &&
905 path->bus->sim->max_tagged_dev_openings != 0) {
906 /* Check if the SIM does not want queued commands. */
907 bzero(&cpi, sizeof(cpi));
908 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
909 cpi.ccb_h.func_code = XPT_PATH_INQ;
910 xpt_action((union ccb *)&cpi);
911 if (cpi.ccb_h.status == CAM_REQ_CMP &&
912 (cpi.hba_inquiry & PI_TAG_ABLE)) {
913 /* Report SIM which tags are allowed. */
914 bzero(&cts, sizeof(cts));
915 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
916 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
917 cts.type = CTS_TYPE_CURRENT_SETTINGS;
918 cts.xport_specific.sata.tags = path->device->maxtags;
919 cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
920 xpt_action((union ccb *)&cts);
921 /* Reconfigure queues for tagged queueing. */
922 xpt_start_tags(path);
925 ata_device_transport(path);
926 PROBE_SET_ACTION(softc, PROBE_SETMODE);
927 xpt_release_ccb(done_ccb);
928 xpt_schedule(periph, priority);
933 xpt_print(path, "Spin-up done\n");
935 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
936 xpt_release_ccb(done_ccb);
937 xpt_schedule(periph, priority);
940 if (path->device->transport != XPORT_SATA)
942 /* Set supported bits. */
943 bzero(&cts, sizeof(cts));
944 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
945 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
946 cts.type = CTS_TYPE_CURRENT_SETTINGS;
947 xpt_action((union ccb *)&cts);
948 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
949 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
952 if (ident_buf->satacapabilities != 0xffff) {
953 if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
954 caps |= CTS_SATA_CAPS_D_PMREQ;
955 if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
956 caps |= CTS_SATA_CAPS_D_APST;
958 /* Mask unwanted bits. */
959 bzero(&cts, sizeof(cts));
960 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
961 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
962 cts.type = CTS_TYPE_USER_SETTINGS;
963 xpt_action((union ccb *)&cts);
964 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
965 caps &= cts.xport_specific.sata.caps;
968 /* Store result to SIM. */
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.caps = caps;
974 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
975 xpt_action((union ccb *)&cts);
977 if (ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) {
978 PROBE_SET_ACTION(softc, PROBE_SETPM);
979 xpt_release_ccb(done_ccb);
980 xpt_schedule(periph, priority);
985 if (ident_buf->satacapabilities != 0xffff &&
986 ident_buf->satacapabilities & ATA_SUPPORT_DAPST) {
987 PROBE_SET_ACTION(softc, PROBE_SETAPST);
988 xpt_release_ccb(done_ccb);
989 xpt_schedule(periph, priority);
994 if (ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) {
995 PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
996 xpt_release_ccb(done_ccb);
997 xpt_schedule(periph, priority);
1001 case PROBE_SETDMAAA:
1003 if (path->device->protocol == PROTO_ATA) {
1004 PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
1006 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1008 xpt_release_ccb(done_ccb);
1009 xpt_schedule(periph, priority);
1011 case PROBE_SET_MULTI:
1012 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1013 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1014 xpt_acquire_device(path->device);
1015 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1016 xpt_action(done_ccb);
1017 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1022 case PROBE_FULL_INQUIRY:
1024 struct scsi_inquiry_data *inq_buf;
1025 u_int8_t periph_qual, len;
1027 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1028 inq_buf = &path->device->inq_data;
1030 periph_qual = SID_QUAL(inq_buf);
1032 if (periph_qual != SID_QUAL_LU_CONNECTED)
1036 * We conservatively request only
1037 * SHORT_INQUIRY_LEN bytes of inquiry
1038 * information during our first try
1039 * at sending an INQUIRY. If the device
1040 * has more information to give,
1041 * perform a second request specifying
1042 * the amount of information the device
1043 * is willing to give.
1045 len = inq_buf->additional_length
1046 + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1047 if (softc->action == PROBE_INQUIRY
1048 && len > SHORT_INQUIRY_LENGTH) {
1049 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1050 xpt_release_ccb(done_ccb);
1051 xpt_schedule(periph, priority);
1055 ata_device_transport(path);
1056 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1057 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1058 xpt_acquire_device(path->device);
1059 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1060 xpt_action(done_ccb);
1061 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
1066 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1067 bzero(ident_buf, sizeof(*ident_buf));
1068 softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1069 (done_ccb->ataio.res.lba_mid << 16) +
1070 (done_ccb->ataio.res.lba_low << 8) +
1071 done_ccb->ataio.res.sector_count;
1072 ((uint32_t *)ident_buf)[0] = softc->pm_pid;
1073 snprintf(ident_buf->model, sizeof(ident_buf->model),
1074 "Port Multiplier %08x", softc->pm_pid);
1075 PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1076 xpt_release_ccb(done_ccb);
1077 xpt_schedule(periph, priority);
1080 softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1081 (done_ccb->ataio.res.lba_mid << 16) +
1082 (done_ccb->ataio.res.lba_low << 8) +
1083 done_ccb->ataio.res.sector_count;
1084 ((uint32_t *)ident_buf)[1] = softc->pm_prv;
1085 snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1086 "%04x", softc->pm_prv);
1087 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1088 /* Set supported bits. */
1089 bzero(&cts, sizeof(cts));
1090 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1091 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1092 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1093 xpt_action((union ccb *)&cts);
1094 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1095 caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1098 /* All PMPs must support PM requests. */
1099 caps |= CTS_SATA_CAPS_D_PMREQ;
1100 /* Mask unwanted bits. */
1101 bzero(&cts, sizeof(cts));
1102 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1103 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1104 cts.type = CTS_TYPE_USER_SETTINGS;
1105 xpt_action((union ccb *)&cts);
1106 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1107 caps &= cts.xport_specific.sata.caps;
1110 /* Store result to SIM. */
1111 bzero(&cts, sizeof(cts));
1112 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1113 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1114 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1115 cts.xport_specific.sata.caps = caps;
1116 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1117 xpt_action((union ccb *)&cts);
1119 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1120 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1121 xpt_acquire_device(path->device);
1122 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1123 xpt_action(done_ccb);
1124 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1127 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1128 xpt_action(done_ccb);
1129 xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
1133 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
1134 ("probedone: invalid action state\n"));
1139 if (softc->restart) {
1141 xpt_release_ccb(done_ccb);
1142 probeschedule(periph);
1145 xpt_release_ccb(done_ccb);
1146 while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1147 TAILQ_REMOVE(&softc->request_ccbs,
1148 &done_ccb->ccb_h, periph_links.tqe);
1149 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1152 cam_release_devq(periph->path,
1153 RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
1154 cam_periph_invalidate(periph);
1155 cam_periph_release_locked(periph);
1159 probecleanup(struct cam_periph *periph)
1161 free(periph->softc, M_CAMXPT);
1165 ata_find_quirk(struct cam_ed *device)
1167 struct ata_quirk_entry *quirk;
1170 match = cam_quirkmatch((caddr_t)&device->ident_data,
1171 (caddr_t)ata_quirk_table,
1172 ata_quirk_table_size,
1173 sizeof(*ata_quirk_table), ata_identify_match);
1176 panic("xpt_find_quirk: device didn't match wildcard entry!!");
1178 quirk = (struct ata_quirk_entry *)match;
1179 device->quirk = quirk;
1180 if (quirk->quirks & CAM_QUIRK_MAXTAGS)
1181 device->mintags = device->maxtags = quirk->maxtags;
1185 union ccb *request_ccb;
1186 struct ccb_pathinq *cpi;
1188 } ata_scan_bus_info;
1191 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1192 * As the scan progresses, xpt_scan_bus is used as the
1193 * callback on completion function.
1196 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1198 struct cam_path *path;
1199 ata_scan_bus_info *scan_info;
1200 union ccb *work_ccb, *reset_ccb;
1203 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1204 ("xpt_scan_bus\n"));
1205 switch (request_ccb->ccb_h.func_code) {
1207 /* Find out the characteristics of the bus */
1208 work_ccb = xpt_alloc_ccb_nowait();
1209 if (work_ccb == NULL) {
1210 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1211 xpt_done(request_ccb);
1214 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1215 request_ccb->ccb_h.pinfo.priority);
1216 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1217 xpt_action(work_ccb);
1218 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1219 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1220 xpt_free_ccb(work_ccb);
1221 xpt_done(request_ccb);
1225 /* We may need to reset bus first, if we haven't done it yet. */
1226 if ((work_ccb->cpi.hba_inquiry &
1227 (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1228 !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1229 !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1230 reset_ccb = xpt_alloc_ccb_nowait();
1231 if (reset_ccb == NULL) {
1232 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1233 xpt_free_ccb(work_ccb);
1234 xpt_done(request_ccb);
1237 xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1239 reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1240 xpt_action(reset_ccb);
1241 if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1242 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1243 xpt_free_ccb(reset_ccb);
1244 xpt_free_ccb(work_ccb);
1245 xpt_done(request_ccb);
1248 xpt_free_ccb(reset_ccb);
1251 /* Save some state for use while we probe for devices */
1252 scan_info = (ata_scan_bus_info *)
1253 malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1254 if (scan_info == NULL) {
1255 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1256 xpt_free_ccb(work_ccb);
1257 xpt_done(request_ccb);
1260 scan_info->request_ccb = request_ccb;
1261 scan_info->cpi = &work_ccb->cpi;
1262 /* If PM supported, probe it first. */
1263 if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1264 scan_info->counter = scan_info->cpi->max_target;
1266 scan_info->counter = 0;
1268 work_ccb = xpt_alloc_ccb_nowait();
1269 if (work_ccb == NULL) {
1270 free(scan_info, M_CAMXPT);
1271 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1272 xpt_done(request_ccb);
1277 work_ccb = request_ccb;
1278 /* Reuse the same CCB to query if a device was really found */
1279 scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1280 /* If there is PMP... */
1281 if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1282 (scan_info->counter == scan_info->cpi->max_target)) {
1283 if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1284 /* everything else will be probed by it */
1285 /* Free the current request path- we're done with it. */
1286 xpt_free_path(work_ccb->ccb_h.path);
1289 struct ccb_trans_settings cts;
1291 /* Report SIM that PM is absent. */
1292 bzero(&cts, sizeof(cts));
1293 xpt_setup_ccb(&cts.ccb_h,
1294 work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1295 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1296 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1297 cts.xport_specific.sata.pm_present = 0;
1298 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1299 xpt_action((union ccb *)&cts);
1302 /* Free the current request path- we're done with it. */
1303 xpt_free_path(work_ccb->ccb_h.path);
1304 if (scan_info->counter ==
1305 ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1306 0 : scan_info->cpi->max_target)) {
1308 xpt_free_ccb(work_ccb);
1309 xpt_free_ccb((union ccb *)scan_info->cpi);
1310 request_ccb = scan_info->request_ccb;
1311 free(scan_info, M_CAMXPT);
1312 request_ccb->ccb_h.status = CAM_REQ_CMP;
1313 xpt_done(request_ccb);
1316 /* Take next device. Wrap from max (PMP) to 0. */
1317 scan_info->counter = (scan_info->counter + 1 ) %
1318 (scan_info->cpi->max_target + 1);
1320 status = xpt_create_path(&path, xpt_periph,
1321 scan_info->request_ccb->ccb_h.path_id,
1322 scan_info->counter, 0);
1323 if (status != CAM_REQ_CMP) {
1324 printf("xpt_scan_bus: xpt_create_path failed"
1325 " with status %#x, bus scan halted\n",
1327 xpt_free_ccb(work_ccb);
1328 xpt_free_ccb((union ccb *)scan_info->cpi);
1329 request_ccb = scan_info->request_ccb;
1330 free(scan_info, M_CAMXPT);
1331 request_ccb->ccb_h.status = status;
1332 xpt_done(request_ccb);
1335 xpt_setup_ccb(&work_ccb->ccb_h, path,
1336 scan_info->request_ccb->ccb_h.pinfo.priority);
1337 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1338 work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1339 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1340 work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1341 xpt_action(work_ccb);
1349 ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1350 cam_flags flags, union ccb *request_ccb)
1352 struct ccb_pathinq cpi;
1354 struct cam_path *new_path;
1355 struct cam_periph *old_periph;
1357 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1359 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1360 cpi.ccb_h.func_code = XPT_PATH_INQ;
1361 xpt_action((union ccb *)&cpi);
1363 if (cpi.ccb_h.status != CAM_REQ_CMP) {
1364 if (request_ccb != NULL) {
1365 request_ccb->ccb_h.status = cpi.ccb_h.status;
1366 xpt_done(request_ccb);
1371 if (request_ccb == NULL) {
1372 request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1373 if (request_ccb == NULL) {
1374 xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1375 "can't continue\n");
1378 new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1379 if (new_path == NULL) {
1380 xpt_print(path, "xpt_scan_lun: can't allocate path, "
1381 "can't continue\n");
1382 free(request_ccb, M_CAMXPT);
1385 status = xpt_compile_path(new_path, xpt_periph,
1387 path->target->target_id,
1388 path->device->lun_id);
1390 if (status != CAM_REQ_CMP) {
1391 xpt_print(path, "xpt_scan_lun: can't compile path, "
1392 "can't continue\n");
1393 free(request_ccb, M_CAMXPT);
1394 free(new_path, M_CAMXPT);
1397 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1398 request_ccb->ccb_h.cbfcnp = xptscandone;
1399 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1400 request_ccb->crcn.flags = flags;
1403 if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1406 softc = (probe_softc *)old_periph->softc;
1407 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1411 status = cam_periph_alloc(proberegister, NULL, probecleanup,
1412 probestart, "aprobe",
1414 request_ccb->ccb_h.path, NULL, 0,
1417 if (status != CAM_REQ_CMP) {
1418 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1419 "returned an error, can't continue probe\n");
1420 request_ccb->ccb_h.status = status;
1421 xpt_done(request_ccb);
1427 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1429 xpt_release_path(done_ccb->ccb_h.path);
1430 free(done_ccb->ccb_h.path, M_CAMXPT);
1431 free(done_ccb, M_CAMXPT);
1434 static struct cam_ed *
1435 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1437 struct cam_path path;
1438 struct ata_quirk_entry *quirk;
1439 struct cam_ed *device;
1440 struct cam_ed *cur_device;
1442 device = xpt_alloc_device(bus, target, lun_id);
1447 * Take the default quirk entry until we have inquiry
1448 * data and can determine a better quirk to use.
1450 quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1451 device->quirk = (void *)quirk;
1452 device->mintags = 0;
1453 device->maxtags = 0;
1454 bzero(&device->inq_data, sizeof(device->inq_data));
1455 device->inq_flags = 0;
1456 device->queue_flags = 0;
1457 device->serial_num = NULL;
1458 device->serial_num_len = 0;
1461 * XXX should be limited by number of CCBs this bus can
1464 bus->sim->max_ccbs += device->ccbq.devq_openings;
1465 /* Insertion sort into our target's device list */
1466 cur_device = TAILQ_FIRST(&target->ed_entries);
1467 while (cur_device != NULL && cur_device->lun_id < lun_id)
1468 cur_device = TAILQ_NEXT(cur_device, links);
1469 if (cur_device != NULL) {
1470 TAILQ_INSERT_BEFORE(cur_device, device, links);
1472 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1474 target->generation++;
1475 if (lun_id != CAM_LUN_WILDCARD) {
1476 xpt_compile_path(&path,
1481 ata_device_transport(&path);
1482 xpt_release_path(&path);
1489 ata_device_transport(struct cam_path *path)
1491 struct ccb_pathinq cpi;
1492 struct ccb_trans_settings cts;
1493 struct scsi_inquiry_data *inq_buf = NULL;
1494 struct ata_params *ident_buf = NULL;
1496 /* Get transport information from the SIM */
1497 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1498 cpi.ccb_h.func_code = XPT_PATH_INQ;
1499 xpt_action((union ccb *)&cpi);
1501 path->device->transport = cpi.transport;
1502 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1503 inq_buf = &path->device->inq_data;
1504 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1505 ident_buf = &path->device->ident_data;
1506 if (path->device->protocol == PROTO_ATA) {
1507 path->device->protocol_version = ident_buf ?
1508 ata_version(ident_buf->version_major) : cpi.protocol_version;
1509 } else if (path->device->protocol == PROTO_SCSI) {
1510 path->device->protocol_version = inq_buf ?
1511 SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1513 path->device->transport_version = ident_buf ?
1514 ata_version(ident_buf->version_major) : cpi.transport_version;
1516 /* Tell the controller what we think */
1517 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1518 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1519 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1520 cts.transport = path->device->transport;
1521 cts.transport_version = path->device->transport_version;
1522 cts.protocol = path->device->protocol;
1523 cts.protocol_version = path->device->protocol_version;
1524 cts.proto_specific.valid = 0;
1526 if (path->device->transport == XPORT_ATA) {
1527 cts.xport_specific.ata.atapi =
1528 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1529 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1530 cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1532 cts.xport_specific.sata.atapi =
1533 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1534 ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1535 cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1538 cts.xport_specific.valid = 0;
1539 xpt_action((union ccb *)&cts);
1543 ata_action(union ccb *start_ccb)
1546 switch (start_ccb->ccb_h.func_code) {
1547 case XPT_SET_TRAN_SETTINGS:
1549 ata_set_transfer_settings(&start_ccb->cts,
1550 start_ccb->ccb_h.path->device,
1551 /*async_update*/FALSE);
1555 ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1558 ata_scan_lun(start_ccb->ccb_h.path->periph,
1559 start_ccb->ccb_h.path, start_ccb->crcn.flags,
1562 case XPT_GET_TRAN_SETTINGS:
1564 struct cam_sim *sim;
1566 sim = start_ccb->ccb_h.path->bus->sim;
1567 (*(sim->sim_action))(sim, start_ccb);
1572 struct cam_ed *device;
1575 device = start_ccb->ccb_h.path->device;
1576 if (device->protocol == PROTO_SCSI &&
1577 (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1579 device->ident_data.config & ATA_PROTO_MASK;
1581 maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
1582 (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1584 if (start_ccb->csio.cdb_len > maxlen) {
1585 start_ccb->ccb_h.status = CAM_REQ_INVALID;
1586 xpt_done(start_ccb);
1592 xpt_action_default(start_ccb);
1598 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1601 struct ccb_pathinq cpi;
1602 struct ccb_trans_settings cur_cts;
1603 struct ccb_trans_settings_scsi *scsi;
1604 struct ccb_trans_settings_scsi *cur_scsi;
1605 struct cam_sim *sim;
1606 struct scsi_inquiry_data *inq_data;
1608 if (device == NULL) {
1609 cts->ccb_h.status = CAM_PATH_INVALID;
1610 xpt_done((union ccb *)cts);
1614 if (cts->protocol == PROTO_UNKNOWN
1615 || cts->protocol == PROTO_UNSPECIFIED) {
1616 cts->protocol = device->protocol;
1617 cts->protocol_version = device->protocol_version;
1620 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1621 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1622 cts->protocol_version = device->protocol_version;
1624 if (cts->protocol != device->protocol) {
1625 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1626 cts->protocol, device->protocol);
1627 cts->protocol = device->protocol;
1630 if (cts->protocol_version > device->protocol_version) {
1632 xpt_print(cts->ccb_h.path, "Down reving Protocol "
1633 "Version from %d to %d?\n", cts->protocol_version,
1634 device->protocol_version);
1636 cts->protocol_version = device->protocol_version;
1639 if (cts->transport == XPORT_UNKNOWN
1640 || cts->transport == XPORT_UNSPECIFIED) {
1641 cts->transport = device->transport;
1642 cts->transport_version = device->transport_version;
1645 if (cts->transport_version == XPORT_VERSION_UNKNOWN
1646 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1647 cts->transport_version = device->transport_version;
1649 if (cts->transport != device->transport) {
1650 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1651 cts->transport, device->transport);
1652 cts->transport = device->transport;
1655 if (cts->transport_version > device->transport_version) {
1657 xpt_print(cts->ccb_h.path, "Down reving Transport "
1658 "Version from %d to %d?\n", cts->transport_version,
1659 device->transport_version);
1661 cts->transport_version = device->transport_version;
1664 sim = cts->ccb_h.path->bus->sim;
1667 * Nothing more of interest to do unless
1668 * this is a device connected via the
1671 if (cts->protocol != PROTO_SCSI) {
1672 if (async_update == FALSE)
1673 (*(sim->sim_action))(sim, (union ccb *)cts);
1677 inq_data = &device->inq_data;
1678 scsi = &cts->proto_specific.scsi;
1679 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1680 cpi.ccb_h.func_code = XPT_PATH_INQ;
1681 xpt_action((union ccb *)&cpi);
1683 /* SCSI specific sanity checking */
1684 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1685 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1686 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1687 || (device->mintags == 0)) {
1689 * Can't tag on hardware that doesn't support tags,
1690 * doesn't have it enabled, or has broken tag support.
1692 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1695 if (async_update == FALSE) {
1697 * Perform sanity checking against what the
1698 * controller and device can do.
1700 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1701 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1702 cur_cts.type = cts->type;
1703 xpt_action((union ccb *)&cur_cts);
1704 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1707 cur_scsi = &cur_cts.proto_specific.scsi;
1708 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1709 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1710 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1712 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1713 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1716 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1717 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1721 * If we are transitioning from tags to no-tags or
1722 * vice-versa, we need to carefully freeze and restart
1723 * the queue so that we don't overlap tagged and non-tagged
1724 * commands. We also temporarily stop tags if there is
1725 * a change in transfer negotiation settings to allow
1726 * "tag-less" negotiation.
1728 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1729 || (device->inq_flags & SID_CmdQue) != 0)
1730 device_tagenb = TRUE;
1732 device_tagenb = FALSE;
1734 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1735 && device_tagenb == FALSE)
1736 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1737 && device_tagenb == TRUE)) {
1739 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1741 * Delay change to use tags until after a
1742 * few commands have gone to this device so
1743 * the controller has time to perform transfer
1744 * negotiations without tagged messages getting
1747 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1748 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1750 xpt_stop_tags(cts->ccb_h.path);
1754 if (async_update == FALSE)
1755 (*(sim->sim_action))(sim, (union ccb *)cts);
1759 * Handle any per-device event notifications that require action by the XPT.
1762 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1763 struct cam_ed *device, void *async_arg)
1766 struct cam_path newpath;
1769 * We only need to handle events for real devices.
1771 if (target->target_id == CAM_TARGET_WILDCARD
1772 || device->lun_id == CAM_LUN_WILDCARD)
1776 * We need our own path with wildcards expanded to
1777 * handle certain types of events.
1779 if ((async_code == AC_SENT_BDR)
1780 || (async_code == AC_BUS_RESET)
1781 || (async_code == AC_INQ_CHANGED))
1782 status = xpt_compile_path(&newpath, NULL,
1787 status = CAM_REQ_CMP_ERR;
1789 if (status == CAM_REQ_CMP) {
1790 if (async_code == AC_INQ_CHANGED) {
1792 * We've sent a start unit command, or
1793 * something similar to a device that
1794 * may have caused its inquiry data to
1795 * change. So we re-scan the device to
1796 * refresh the inquiry data for it.
1798 ata_scan_lun(newpath.periph, &newpath,
1799 CAM_EXPECT_INQ_CHANGE, NULL);
1801 /* We need to reinitialize device after reset. */
1802 ata_scan_lun(newpath.periph, &newpath,
1805 xpt_release_path(&newpath);
1806 } else if (async_code == AC_LOST_DEVICE &&
1807 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1808 device->flags |= CAM_DEV_UNCONFIGURED;
1809 xpt_release_device(device);
1810 } else if (async_code == AC_TRANSFER_NEG) {
1811 struct ccb_trans_settings *settings;
1813 settings = (struct ccb_trans_settings *)async_arg;
1814 ata_set_transfer_settings(settings, device,
1815 /*async_update*/TRUE);
1820 ata_announce_periph(struct cam_periph *periph)
1822 struct ccb_pathinq cpi;
1823 struct ccb_trans_settings cts;
1824 struct cam_path *path = periph->path;
1828 mtx_assert(periph->sim->mtx, MA_OWNED);
1830 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1831 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1832 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1833 xpt_action((union ccb*)&cts);
1834 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1836 /* Ask the SIM for its base transfer speed */
1837 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1838 cpi.ccb_h.func_code = XPT_PATH_INQ;
1839 xpt_action((union ccb *)&cpi);
1840 /* Report connection speed */
1841 speed = cpi.base_transfer_speed;
1842 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1843 struct ccb_trans_settings_ata *ata =
1844 &cts.xport_specific.ata;
1846 if (ata->valid & CTS_ATA_VALID_MODE)
1847 speed = ata_mode2speed(ata->mode);
1849 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1850 struct ccb_trans_settings_sata *sata =
1851 &cts.xport_specific.sata;
1853 if (sata->valid & CTS_SATA_VALID_REVISION)
1854 speed = ata_revision2speed(sata->revision);
1858 printf("%s%d: %d.%03dMB/s transfers",
1859 periph->periph_name, periph->unit_number,
1862 printf("%s%d: %dKB/s transfers", periph->periph_name,
1863 periph->unit_number, speed);
1864 /* Report additional information about connection */
1865 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
1866 struct ccb_trans_settings_ata *ata =
1867 &cts.xport_specific.ata;
1870 if (ata->valid & CTS_ATA_VALID_MODE)
1871 printf("%s, ", ata_mode2string(ata->mode));
1872 if ((ata->valid & CTS_ATA_VALID_ATAPI) && ata->atapi != 0)
1873 printf("ATAPI %dbytes, ", ata->atapi);
1874 if (ata->valid & CTS_ATA_VALID_BYTECOUNT)
1875 printf("PIO %dbytes", ata->bytecount);
1878 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1879 struct ccb_trans_settings_sata *sata =
1880 &cts.xport_specific.sata;
1883 if (sata->valid & CTS_SATA_VALID_REVISION)
1884 printf("SATA %d.x, ", sata->revision);
1887 if (sata->valid & CTS_SATA_VALID_MODE)
1888 printf("%s, ", ata_mode2string(sata->mode));
1889 if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
1890 printf("ATAPI %dbytes, ", sata->atapi);
1891 if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
1892 printf("PIO %dbytes", sata->bytecount);