]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/ata/ata_xpt.c
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
[FreeBSD/FreeBSD.git] / sys / cam / ata / ata_xpt.c
1 /*-
2  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
14  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/bus.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>
37 #include <sys/time.h>
38 #include <sys/conf.h>
39 #include <sys/fcntl.h>
40 #include <sys/interrupt.h>
41 #include <sys/sbuf.h>
42
43 #include <sys/eventhandler.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47
48 #include <cam/cam.h>
49 #include <cam/cam_ccb.h>
50 #include <cam/cam_queue.h>
51 #include <cam/cam_periph.h>
52 #include <cam/cam_sim.h>
53 #include <cam/cam_xpt.h>
54 #include <cam/cam_xpt_sim.h>
55 #include <cam/cam_xpt_periph.h>
56 #include <cam/cam_xpt_internal.h>
57 #include <cam/cam_debug.h>
58
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/ata/ata_all.h>
62 #include <machine/stdarg.h>     /* for xpt_print below */
63 #include "opt_cam.h"
64
65 struct ata_quirk_entry {
66         struct scsi_inquiry_pattern inq_pat;
67         u_int8_t quirks;
68 #define CAM_QUIRK_MAXTAGS       0x01
69         u_int mintags;
70         u_int maxtags;
71 };
72
73 static periph_init_t probe_periph_init;
74
75 static struct periph_driver probe_driver =
76 {
77         probe_periph_init, "aprobe",
78         TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
79         CAM_PERIPH_DRV_EARLY
80 };
81
82 PERIPHDRIVER_DECLARE(aprobe, probe_driver);
83
84 typedef enum {
85         PROBE_RESET,
86         PROBE_IDENTIFY,
87         PROBE_SPINUP,
88         PROBE_SETMODE,
89         PROBE_SETPM,
90         PROBE_SETAPST,
91         PROBE_SETDMAAA,
92         PROBE_SETAN,
93         PROBE_SET_MULTI,
94         PROBE_INQUIRY,
95         PROBE_FULL_INQUIRY,
96         PROBE_PM_PID,
97         PROBE_PM_PRV,
98         PROBE_IDENTIFY_SES,
99         PROBE_IDENTIFY_SAFTE,
100         PROBE_DONE,
101         PROBE_INVALID
102 } probe_action;
103
104 static char *probe_action_text[] = {
105         "PROBE_RESET",
106         "PROBE_IDENTIFY",
107         "PROBE_SPINUP",
108         "PROBE_SETMODE",
109         "PROBE_SETPM",
110         "PROBE_SETAPST",
111         "PROBE_SETDMAAA",
112         "PROBE_SETAN",
113         "PROBE_SET_MULTI",
114         "PROBE_INQUIRY",
115         "PROBE_FULL_INQUIRY",
116         "PROBE_PM_PID",
117         "PROBE_PM_PRV",
118         "PROBE_IDENTIFY_SES",
119         "PROBE_IDENTIFY_SAFTE",
120         "PROBE_DONE",
121         "PROBE_INVALID"
122 };
123
124 #define PROBE_SET_ACTION(softc, newaction)      \
125 do {                                                                    \
126         char **text;                                                    \
127         text = probe_action_text;                                       \
128         CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE,               \
129             ("Probe %s to %s\n", text[(softc)->action],                 \
130             text[(newaction)]));                                        \
131         (softc)->action = (newaction);                                  \
132 } while(0)
133
134 typedef enum {
135         PROBE_NO_ANNOUNCE       = 0x04
136 } probe_flags;
137
138 typedef struct {
139         TAILQ_HEAD(, ccb_hdr) request_ccbs;
140         struct ata_params       ident_data;
141         probe_action    action;
142         probe_flags     flags;
143         uint32_t        pm_pid;
144         uint32_t        pm_prv;
145         int             restart;
146         int             spinup;
147         int             faults;
148         u_int           caps;
149         struct cam_periph *periph;
150 } probe_softc;
151
152 static struct ata_quirk_entry ata_quirk_table[] =
153 {
154         {
155                 /* Default tagged queuing parameters for all devices */
156                 {
157                   T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
158                   /*vendor*/"*", /*product*/"*", /*revision*/"*"
159                 },
160                 /*quirks*/0, /*mintags*/0, /*maxtags*/0
161         },
162 };
163
164 static cam_status       proberegister(struct cam_periph *periph,
165                                       void *arg);
166 static void      probeschedule(struct cam_periph *probe_periph);
167 static void      probestart(struct cam_periph *periph, union ccb *start_ccb);
168 static void      proberequestdefaultnegotiation(struct cam_periph *periph);
169 static void      probedone(struct cam_periph *periph, union ccb *done_ccb);
170 static void      probecleanup(struct cam_periph *periph);
171 static void      ata_find_quirk(struct cam_ed *device);
172 static void      ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
173 static void      ata_scan_lun(struct cam_periph *periph,
174                                struct cam_path *path, cam_flags flags,
175                                union ccb *ccb);
176 static void      xptscandone(struct cam_periph *periph, union ccb *done_ccb);
177 static struct cam_ed *
178                  ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
179                                    lun_id_t lun_id);
180 static void      ata_device_transport(struct cam_path *path);
181 static void      ata_get_transfer_settings(struct ccb_trans_settings *cts);
182 static void      ata_set_transfer_settings(struct ccb_trans_settings *cts,
183                                             struct cam_path *path,
184                                             int async_update);
185 static void      ata_dev_async(u_int32_t async_code,
186                                 struct cam_eb *bus,
187                                 struct cam_et *target,
188                                 struct cam_ed *device,
189                                 void *async_arg);
190 static void      ata_action(union ccb *start_ccb);
191 static void      ata_announce_periph(struct cam_periph *periph);
192 static void      ata_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb);
193 static void      ata_proto_announce(struct cam_ed *device);
194 static void      ata_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb);
195 static void      ata_proto_denounce(struct cam_ed *device);
196 static void      ata_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb);
197 static void      ata_proto_debug_out(union ccb *ccb);
198 static void      semb_proto_announce(struct cam_ed *device);
199 static void      semb_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb);
200 static void      semb_proto_denounce(struct cam_ed *device);
201 static void      semb_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb);
202
203 static int ata_dma = 1;
204 static int atapi_dma = 1;
205
206 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
207 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
208
209 static struct xpt_xport_ops ata_xport_ops = {
210         .alloc_device = ata_alloc_device,
211         .action = ata_action,
212         .async = ata_dev_async,
213         .announce = ata_announce_periph,
214         .announce_sbuf = ata_announce_periph_sbuf,
215 };
216 #define ATA_XPT_XPORT(x, X)                     \
217 static struct xpt_xport ata_xport_ ## x = {     \
218         .xport = XPORT_ ## X,                   \
219         .name = #x,                             \
220         .ops = &ata_xport_ops,                  \
221 };                                              \
222 CAM_XPT_XPORT(ata_xport_ ## x);
223
224 ATA_XPT_XPORT(ata, ATA);
225 ATA_XPT_XPORT(sata, SATA);
226
227 #undef ATA_XPORT_XPORT
228
229 static struct xpt_proto_ops ata_proto_ops_ata = {
230         .announce = ata_proto_announce,
231         .announce_sbuf = ata_proto_announce_sbuf,
232         .denounce = ata_proto_denounce,
233         .denounce_sbuf = ata_proto_denounce_sbuf,
234         .debug_out = ata_proto_debug_out,
235 };
236 static struct xpt_proto ata_proto_ata = {
237         .proto = PROTO_ATA,
238         .name = "ata",
239         .ops = &ata_proto_ops_ata,
240 };
241
242 static struct xpt_proto_ops ata_proto_ops_satapm = {
243         .announce = ata_proto_announce,
244         .announce_sbuf = ata_proto_announce_sbuf,
245         .denounce = ata_proto_denounce,
246         .denounce_sbuf = ata_proto_denounce_sbuf,
247         .debug_out = ata_proto_debug_out,
248 };
249 static struct xpt_proto ata_proto_satapm = {
250         .proto = PROTO_SATAPM,
251         .name = "satapm",
252         .ops = &ata_proto_ops_satapm,
253 };
254
255 static struct xpt_proto_ops ata_proto_ops_semb = {
256         .announce = semb_proto_announce,
257         .announce_sbuf = semb_proto_announce_sbuf,
258         .denounce = semb_proto_denounce,
259         .denounce_sbuf = semb_proto_denounce_sbuf,
260         .debug_out = ata_proto_debug_out,
261 };
262 static struct xpt_proto ata_proto_semb = {
263         .proto = PROTO_SEMB,
264         .name = "semb",
265         .ops = &ata_proto_ops_semb,
266 };
267
268 CAM_XPT_PROTO(ata_proto_ata);
269 CAM_XPT_PROTO(ata_proto_satapm);
270 CAM_XPT_PROTO(ata_proto_semb);
271
272 static void
273 probe_periph_init()
274 {
275 }
276
277 static cam_status
278 proberegister(struct cam_periph *periph, void *arg)
279 {
280         union ccb *request_ccb; /* CCB representing the probe request */
281         cam_status status;
282         probe_softc *softc;
283
284         request_ccb = (union ccb *)arg;
285         if (request_ccb == NULL) {
286                 printf("proberegister: no probe CCB, "
287                        "can't register device\n");
288                 return(CAM_REQ_CMP_ERR);
289         }
290
291         softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_ZERO | M_NOWAIT);
292
293         if (softc == NULL) {
294                 printf("proberegister: Unable to probe new device. "
295                        "Unable to allocate softc\n");
296                 return(CAM_REQ_CMP_ERR);
297         }
298         TAILQ_INIT(&softc->request_ccbs);
299         TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
300                           periph_links.tqe);
301         softc->flags = 0;
302         periph->softc = softc;
303         softc->periph = periph;
304         softc->action = PROBE_INVALID;
305         status = cam_periph_acquire(periph);
306         if (status != CAM_REQ_CMP) {
307                 return (status);
308         }
309         CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
310         ata_device_transport(periph->path);
311         probeschedule(periph);
312         return(CAM_REQ_CMP);
313 }
314
315 static void
316 probeschedule(struct cam_periph *periph)
317 {
318         union ccb *ccb;
319         probe_softc *softc;
320
321         softc = (probe_softc *)periph->softc;
322         ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
323
324         if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
325             periph->path->device->protocol == PROTO_SATAPM ||
326             periph->path->device->protocol == PROTO_SEMB)
327                 PROBE_SET_ACTION(softc, PROBE_RESET);
328         else
329                 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
330
331         if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
332                 softc->flags |= PROBE_NO_ANNOUNCE;
333         else
334                 softc->flags &= ~PROBE_NO_ANNOUNCE;
335
336         xpt_schedule(periph, CAM_PRIORITY_XPT);
337 }
338
339 static void
340 probestart(struct cam_periph *periph, union ccb *start_ccb)
341 {
342         struct ccb_trans_settings cts;
343         struct ccb_ataio *ataio;
344         struct ccb_scsiio *csio;
345         probe_softc *softc;
346         struct cam_path *path;
347         struct ata_params *ident_buf;
348
349         CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
350
351         softc = (probe_softc *)periph->softc;
352         path = start_ccb->ccb_h.path;
353         ataio = &start_ccb->ataio;
354         csio = &start_ccb->csio;
355         ident_buf = &periph->path->device->ident_data;
356
357         if (softc->restart) {
358                 softc->restart = 0;
359                 if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
360                     path->device->protocol == PROTO_SATAPM ||
361                     path->device->protocol == PROTO_SEMB)
362                         softc->action = PROBE_RESET;
363                 else
364                         softc->action = PROBE_IDENTIFY;
365         }
366         switch (softc->action) {
367         case PROBE_RESET:
368                 cam_fill_ataio(ataio,
369                       0,
370                       probedone,
371                       /*flags*/CAM_DIR_NONE,
372                       0,
373                       /*data_ptr*/NULL,
374                       /*dxfer_len*/0,
375                       15 * 1000);
376                 ata_reset_cmd(ataio);
377                 break;
378         case PROBE_IDENTIFY:
379                 cam_fill_ataio(ataio,
380                       1,
381                       probedone,
382                       /*flags*/CAM_DIR_IN,
383                       0,
384                       /*data_ptr*/(u_int8_t *)&softc->ident_data,
385                       /*dxfer_len*/sizeof(softc->ident_data),
386                       30 * 1000);
387                 if (periph->path->device->protocol == PROTO_ATA)
388                         ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
389                 else
390                         ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
391                 break;
392         case PROBE_SPINUP:
393                 if (bootverbose)
394                         xpt_print(path, "Spinning up device\n");
395                 cam_fill_ataio(ataio,
396                       1,
397                       probedone,
398                       /*flags*/CAM_DIR_NONE | CAM_HIGH_POWER,
399                       0,
400                       /*data_ptr*/NULL,
401                       /*dxfer_len*/0,
402                       30 * 1000);
403                 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_PUIS_SPINUP, 0, 0);
404                 break;
405         case PROBE_SETMODE:
406         {
407                 int mode, wantmode;
408
409                 mode = 0;
410                 /* Fetch user modes from SIM. */
411                 bzero(&cts, sizeof(cts));
412                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
413                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
414                 cts.type = CTS_TYPE_USER_SETTINGS;
415                 xpt_action((union ccb *)&cts);
416                 if (path->device->transport == XPORT_ATA) {
417                         if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
418                                 mode = cts.xport_specific.ata.mode;
419                 } else {
420                         if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
421                                 mode = cts.xport_specific.sata.mode;
422                 }
423                 if (periph->path->device->protocol == PROTO_ATA) {
424                         if (ata_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
425                                 mode = ATA_PIO_MAX;
426                 } else {
427                         if (atapi_dma == 0 && (mode == 0 || mode > ATA_PIO_MAX))
428                                 mode = ATA_PIO_MAX;
429                 }
430 negotiate:
431                 /* Honor device capabilities. */
432                 wantmode = mode = ata_max_mode(ident_buf, mode);
433                 /* Report modes to SIM. */
434                 bzero(&cts, sizeof(cts));
435                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
436                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
437                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
438                 if (path->device->transport == XPORT_ATA) {
439                         cts.xport_specific.ata.mode = mode;
440                         cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
441                 } else {
442                         cts.xport_specific.sata.mode = mode;
443                         cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
444                 }
445                 xpt_action((union ccb *)&cts);
446                 /* Fetch current modes from SIM. */
447                 bzero(&cts, sizeof(cts));
448                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
449                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
450                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
451                 xpt_action((union ccb *)&cts);
452                 if (path->device->transport == XPORT_ATA) {
453                         if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
454                                 mode = cts.xport_specific.ata.mode;
455                 } else {
456                         if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
457                                 mode = cts.xport_specific.sata.mode;
458                 }
459                 /* If SIM disagree - renegotiate. */
460                 if (mode != wantmode)
461                         goto negotiate;
462                 /* Remember what transport thinks about DMA. */
463                 if (mode < ATA_DMA)
464                         path->device->inq_flags &= ~SID_DMA;
465                 else
466                         path->device->inq_flags |= SID_DMA;
467                 xpt_async(AC_GETDEV_CHANGED, path, NULL);
468                 cam_fill_ataio(ataio,
469                       1,
470                       probedone,
471                       /*flags*/CAM_DIR_NONE,
472                       0,
473                       /*data_ptr*/NULL,
474                       /*dxfer_len*/0,
475                       30 * 1000);
476                 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
477                 break;
478         }
479         case PROBE_SETPM:
480                 cam_fill_ataio(ataio,
481                     1,
482                     probedone,
483                     CAM_DIR_NONE,
484                     0,
485                     NULL,
486                     0,
487                     30*1000);
488                 ata_28bit_cmd(ataio, ATA_SETFEATURES,
489                     (softc->caps & CTS_SATA_CAPS_H_PMREQ) ? 0x10 : 0x90,
490                     0, 0x03);
491                 break;
492         case PROBE_SETAPST:
493                 cam_fill_ataio(ataio,
494                     1,
495                     probedone,
496                     CAM_DIR_NONE,
497                     0,
498                     NULL,
499                     0,
500                     30*1000);
501                 ata_28bit_cmd(ataio, ATA_SETFEATURES,
502                     (softc->caps & CTS_SATA_CAPS_H_APST) ? 0x10 : 0x90,
503                     0, 0x07);
504                 break;
505         case PROBE_SETDMAAA:
506                 cam_fill_ataio(ataio,
507                     1,
508                     probedone,
509                     CAM_DIR_NONE,
510                     0,
511                     NULL,
512                     0,
513                     30*1000);
514                 ata_28bit_cmd(ataio, ATA_SETFEATURES,
515                     (softc->caps & CTS_SATA_CAPS_H_DMAAA) ? 0x10 : 0x90,
516                     0, 0x02);
517                 break;
518         case PROBE_SETAN:
519                 /* Remember what transport thinks about AEN. */
520                 if (softc->caps & CTS_SATA_CAPS_H_AN)
521                         path->device->inq_flags |= SID_AEN;
522                 else
523                         path->device->inq_flags &= ~SID_AEN;
524                 xpt_async(AC_GETDEV_CHANGED, path, NULL);
525                 cam_fill_ataio(ataio,
526                     1,
527                     probedone,
528                     CAM_DIR_NONE,
529                     0,
530                     NULL,
531                     0,
532                     30*1000);
533                 ata_28bit_cmd(ataio, ATA_SETFEATURES,
534                     (softc->caps & CTS_SATA_CAPS_H_AN) ? 0x10 : 0x90,
535                     0, 0x05);
536                 break;
537         case PROBE_SET_MULTI:
538         {
539                 u_int sectors, bytecount;
540
541                 bytecount = 8192;       /* SATA maximum */
542                 /* Fetch user bytecount from SIM. */
543                 bzero(&cts, sizeof(cts));
544                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
545                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
546                 cts.type = CTS_TYPE_USER_SETTINGS;
547                 xpt_action((union ccb *)&cts);
548                 if (path->device->transport == XPORT_ATA) {
549                         if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
550                                 bytecount = cts.xport_specific.ata.bytecount;
551                 } else {
552                         if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
553                                 bytecount = cts.xport_specific.sata.bytecount;
554                 }
555                 /* Honor device capabilities. */
556                 sectors = max(1, min(ident_buf->sectors_intr & 0xff,
557                     bytecount / ata_logical_sector_size(ident_buf)));
558                 /* Report bytecount to SIM. */
559                 bzero(&cts, sizeof(cts));
560                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
561                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
562                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
563                 if (path->device->transport == XPORT_ATA) {
564                         cts.xport_specific.ata.bytecount = sectors *
565                             ata_logical_sector_size(ident_buf);
566                         cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
567                 } else {
568                         cts.xport_specific.sata.bytecount = sectors *
569                             ata_logical_sector_size(ident_buf);
570                         cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
571                 }
572                 xpt_action((union ccb *)&cts);
573                 /* Fetch current bytecount from SIM. */
574                 bzero(&cts, sizeof(cts));
575                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
576                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
577                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
578                 xpt_action((union ccb *)&cts);
579                 if (path->device->transport == XPORT_ATA) {
580                         if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
581                                 bytecount = cts.xport_specific.ata.bytecount;
582                 } else {
583                         if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
584                                 bytecount = cts.xport_specific.sata.bytecount;
585                 }
586                 sectors = bytecount / ata_logical_sector_size(ident_buf);
587
588                 cam_fill_ataio(ataio,
589                     1,
590                     probedone,
591                     CAM_DIR_NONE,
592                     0,
593                     NULL,
594                     0,
595                     30*1000);
596                 ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
597                 break;
598         }
599         case PROBE_INQUIRY:
600         {
601                 u_int bytecount;
602
603                 bytecount = 8192;       /* SATA maximum */
604                 /* Fetch user bytecount from SIM. */
605                 bzero(&cts, sizeof(cts));
606                 xpt_setup_ccb(&cts.ccb_h, 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 (path->device->transport == XPORT_ATA) {
611                         if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
612                                 bytecount = cts.xport_specific.ata.bytecount;
613                 } else {
614                         if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
615                                 bytecount = cts.xport_specific.sata.bytecount;
616                 }
617                 /* Honor device capabilities. */
618                 bytecount &= ~1;
619                 bytecount = max(2, min(65534, bytecount));
620                 if (ident_buf->satacapabilities != 0x0000 &&
621                     ident_buf->satacapabilities != 0xffff) {
622                         bytecount = min(8192, bytecount);
623                 }
624                 /* Report bytecount to SIM. */
625                 bzero(&cts, sizeof(cts));
626                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
627                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
628                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
629                 if (path->device->transport == XPORT_ATA) {
630                         cts.xport_specific.ata.bytecount = bytecount;
631                         cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
632                 } else {
633                         cts.xport_specific.sata.bytecount = bytecount;
634                         cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
635                 }
636                 xpt_action((union ccb *)&cts);
637                 /* FALLTHROUGH */
638         }
639         case PROBE_FULL_INQUIRY:
640         {
641                 u_int inquiry_len;
642                 struct scsi_inquiry_data *inq_buf =
643                     &periph->path->device->inq_data;
644
645                 if (softc->action == PROBE_INQUIRY)
646                         inquiry_len = SHORT_INQUIRY_LENGTH;
647                 else
648                         inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
649                 /*
650                  * Some parallel SCSI devices fail to send an
651                  * ignore wide residue message when dealing with
652                  * odd length inquiry requests.  Round up to be
653                  * safe.
654                  */
655                 inquiry_len = roundup2(inquiry_len, 2);
656                 scsi_inquiry(csio,
657                              /*retries*/1,
658                              probedone,
659                              MSG_SIMPLE_Q_TAG,
660                              (u_int8_t *)inq_buf,
661                              inquiry_len,
662                              /*evpd*/FALSE,
663                              /*page_code*/0,
664                              SSD_MIN_SIZE,
665                              /*timeout*/60 * 1000);
666                 break;
667         }
668         case PROBE_PM_PID:
669                 cam_fill_ataio(ataio,
670                       1,
671                       probedone,
672                       /*flags*/CAM_DIR_NONE,
673                       0,
674                       /*data_ptr*/NULL,
675                       /*dxfer_len*/0,
676                       10 * 1000);
677                 ata_pm_read_cmd(ataio, 0, 15);
678                 break;
679         case PROBE_PM_PRV:
680                 cam_fill_ataio(ataio,
681                       1,
682                       probedone,
683                       /*flags*/CAM_DIR_NONE,
684                       0,
685                       /*data_ptr*/NULL,
686                       /*dxfer_len*/0,
687                       10 * 1000);
688                 ata_pm_read_cmd(ataio, 1, 15);
689                 break;
690         case PROBE_IDENTIFY_SES:
691                 cam_fill_ataio(ataio,
692                       1,
693                       probedone,
694                       /*flags*/CAM_DIR_IN,
695                       0,
696                       /*data_ptr*/(u_int8_t *)&softc->ident_data,
697                       /*dxfer_len*/sizeof(softc->ident_data),
698                       30 * 1000);
699                 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 0xEC, 0x02,
700                     sizeof(softc->ident_data) / 4);
701                 break;
702         case PROBE_IDENTIFY_SAFTE:
703                 cam_fill_ataio(ataio,
704                       1,
705                       probedone,
706                       /*flags*/CAM_DIR_IN,
707                       0,
708                       /*data_ptr*/(u_int8_t *)&softc->ident_data,
709                       /*dxfer_len*/sizeof(softc->ident_data),
710                       30 * 1000);
711                 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 0xEC, 0x00,
712                     sizeof(softc->ident_data) / 4);
713                 break;
714         default:
715                 panic("probestart: invalid action state 0x%x\n", softc->action);
716         }
717         start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
718         xpt_action(start_ccb);
719 }
720
721 static void
722 proberequestdefaultnegotiation(struct cam_periph *periph)
723 {
724         struct ccb_trans_settings cts;
725
726         xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
727         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
728         cts.type = CTS_TYPE_USER_SETTINGS;
729         xpt_action((union ccb *)&cts);
730         if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
731                 return;
732         cts.xport_specific.valid = 0;
733         cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
734         cts.type = CTS_TYPE_CURRENT_SETTINGS;
735         xpt_action((union ccb *)&cts);
736 }
737
738 static void
739 probedone(struct cam_periph *periph, union ccb *done_ccb)
740 {
741         struct ccb_trans_settings cts;
742         struct ata_params *ident_buf;
743         struct scsi_inquiry_data *inq_buf;
744         probe_softc *softc;
745         struct cam_path *path;
746         cam_status status;
747         u_int32_t  priority;
748         u_int caps;
749         int changed = 1, found = 1;
750         static const uint8_t fake_device_id_hdr[8] =
751             {0, SVPD_DEVICE_ID, 0, 12,
752              SVPD_ID_CODESET_BINARY, SVPD_ID_TYPE_NAA, 0, 8};
753
754         CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
755
756         softc = (probe_softc *)periph->softc;
757         path = done_ccb->ccb_h.path;
758         priority = done_ccb->ccb_h.pinfo.priority;
759         ident_buf = &path->device->ident_data;
760         inq_buf = &path->device->inq_data;
761
762         if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
763                 if (cam_periph_error(done_ccb,
764                     0, softc->restart ? (SF_NO_RECOVERY | SF_NO_RETRY) : 0,
765                     NULL) == ERESTART) {
766 out:
767                         /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
768                         cam_release_devq(path, 0, 0, 0, FALSE);
769                         return;
770                 }
771                 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
772                         /* Don't wedge the queue */
773                         xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
774                 }
775                 status = done_ccb->ccb_h.status & CAM_STATUS_MASK;
776                 if (softc->restart) {
777                         softc->faults++;
778                         if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) ==
779                             CAM_CMD_TIMEOUT)
780                                 softc->faults += 4;
781                         if (softc->faults < 10)
782                                 goto done;
783                         else
784                                 softc->restart = 0;
785
786                 /* Old PIO2 devices may not support mode setting. */
787                 } else if (softc->action == PROBE_SETMODE &&
788                     status == CAM_ATA_STATUS_ERROR &&
789                     ata_max_pmode(ident_buf) <= ATA_PIO2 &&
790                     (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0) {
791                         goto noerror;
792
793                 /*
794                  * Some old WD SATA disks report supported and enabled
795                  * device-initiated interface power management, but return
796                  * ABORT on attempt to disable it.
797                  */
798                 } else if (softc->action == PROBE_SETPM &&
799                     status == CAM_ATA_STATUS_ERROR) {
800                         goto noerror;
801
802                 /*
803                  * Some old WD SATA disks have broken SPINUP handling.
804                  * If we really fail to spin up the disk, then there will be
805                  * some media access errors later on, but at least we will
806                  * have a device to interact with for recovery attempts.
807                  */
808                 } else if (softc->action == PROBE_SPINUP &&
809                     status == CAM_ATA_STATUS_ERROR) {
810                         goto noerror;
811
812                 /*
813                  * Some HP SATA disks report supported DMA Auto-Activation,
814                  * but return ABORT on attempt to enable it.
815                  */
816                 } else if (softc->action == PROBE_SETDMAAA &&
817                     status == CAM_ATA_STATUS_ERROR) {
818                         goto noerror;
819
820                 /*
821                  * SES and SAF-TE SEPs have different IDENTIFY commands,
822                  * but SATA specification doesn't tell how to identify them.
823                  * Until better way found, just try another if first fail.
824                  */
825                 } else if (softc->action == PROBE_IDENTIFY_SES &&
826                     status == CAM_ATA_STATUS_ERROR) {
827                         PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SAFTE);
828                         xpt_release_ccb(done_ccb);
829                         xpt_schedule(periph, priority);
830                         goto out;
831                 }
832
833                 /*
834                  * If we get to this point, we got an error status back
835                  * from the inquiry and the error status doesn't require
836                  * automatically retrying the command.  Therefore, the
837                  * inquiry failed.  If we had inquiry information before
838                  * for this device, but this latest inquiry command failed,
839                  * the device has probably gone away.  If this device isn't
840                  * already marked unconfigured, notify the peripheral
841                  * drivers that this device is no more.
842                  */
843 device_fail:    if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
844                         xpt_async(AC_LOST_DEVICE, path, NULL);
845                 PROBE_SET_ACTION(softc, PROBE_INVALID);
846                 found = 0;
847                 goto done;
848         }
849 noerror:
850         if (softc->restart)
851                 goto done;
852         switch (softc->action) {
853         case PROBE_RESET:
854         {
855                 int sign = (done_ccb->ataio.res.lba_high << 8) +
856                     done_ccb->ataio.res.lba_mid;
857                 CAM_DEBUG(path, CAM_DEBUG_PROBE,
858                     ("SIGNATURE: %04x\n", sign));
859                 if (sign == 0x0000 &&
860                     done_ccb->ccb_h.target_id != 15) {
861                         path->device->protocol = PROTO_ATA;
862                         PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
863                 } else if (sign == 0x9669 &&
864                     done_ccb->ccb_h.target_id == 15) {
865                         /* Report SIM that PM is present. */
866                         bzero(&cts, sizeof(cts));
867                         xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
868                         cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
869                         cts.type = CTS_TYPE_CURRENT_SETTINGS;
870                         cts.xport_specific.sata.pm_present = 1;
871                         cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
872                         xpt_action((union ccb *)&cts);
873                         path->device->protocol = PROTO_SATAPM;
874                         PROBE_SET_ACTION(softc, PROBE_PM_PID);
875                 } else if (sign == 0xc33c &&
876                     done_ccb->ccb_h.target_id != 15) {
877                         path->device->protocol = PROTO_SEMB;
878                         PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SES);
879                 } else if (sign == 0xeb14 &&
880                     done_ccb->ccb_h.target_id != 15) {
881                         path->device->protocol = PROTO_SCSI;
882                         PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
883                 } else {
884                         if (done_ccb->ccb_h.target_id != 15) {
885                                 xpt_print(path,
886                                     "Unexpected signature 0x%04x\n", sign);
887                         }
888                         goto device_fail;
889                 }
890                 xpt_release_ccb(done_ccb);
891                 xpt_schedule(periph, priority);
892                 goto out;
893         }
894         case PROBE_IDENTIFY:
895         {
896                 struct ccb_pathinq cpi;
897                 int16_t *ptr;
898                 int veto = 0;
899
900                 ident_buf = &softc->ident_data;
901                 for (ptr = (int16_t *)ident_buf;
902                      ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
903                         *ptr = le16toh(*ptr);
904                 }
905
906                 /*
907                  * Allow others to veto this ATA disk attachment.  This
908                  * is mainly used by VMs, whose disk controllers may
909                  * share the disks with the simulated ATA controllers.
910                  */
911                 EVENTHANDLER_INVOKE(ada_probe_veto, path, ident_buf, &veto);
912                 if (veto) {
913                         goto device_fail;
914                 }
915
916                 if (strncmp(ident_buf->model, "FX", 2) &&
917                     strncmp(ident_buf->model, "NEC", 3) &&
918                     strncmp(ident_buf->model, "Pioneer", 7) &&
919                     strncmp(ident_buf->model, "SHARP", 5)) {
920                         ata_bswap(ident_buf->model, sizeof(ident_buf->model));
921                         ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
922                         ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
923                 }
924                 ata_btrim(ident_buf->model, sizeof(ident_buf->model));
925                 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
926                 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
927                 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
928                 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
929                 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
930                 /* Device may need spin-up before IDENTIFY become valid. */
931                 if ((ident_buf->specconf == 0x37c8 ||
932                      ident_buf->specconf == 0x738c) &&
933                     ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
934                      softc->spinup == 0)) {
935                         PROBE_SET_ACTION(softc, PROBE_SPINUP);
936                         xpt_release_ccb(done_ccb);
937                         xpt_schedule(periph, priority);
938                         goto out;
939                 }
940                 ident_buf = &path->device->ident_data;
941                 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
942                         /* Check that it is the same device. */
943                         if (bcmp(softc->ident_data.model, ident_buf->model,
944                              sizeof(ident_buf->model)) ||
945                             bcmp(softc->ident_data.revision, ident_buf->revision,
946                              sizeof(ident_buf->revision)) ||
947                             bcmp(softc->ident_data.serial, ident_buf->serial,
948                              sizeof(ident_buf->serial))) {
949                                 /* Device changed. */
950                                 xpt_async(AC_LOST_DEVICE, path, NULL);
951                         } else {
952                                 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
953                                 changed = 0;
954                         }
955                 }
956                 if (changed) {
957                         bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
958                         /* Clean up from previous instance of this device */
959                         if (path->device->serial_num != NULL) {
960                                 free(path->device->serial_num, M_CAMXPT);
961                                 path->device->serial_num = NULL;
962                                 path->device->serial_num_len = 0;
963                         }
964                         if (path->device->device_id != NULL) {
965                                 free(path->device->device_id, M_CAMXPT);
966                                 path->device->device_id = NULL;
967                                 path->device->device_id_len = 0;
968                         }
969                         path->device->serial_num =
970                                 (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
971                                            M_CAMXPT, M_NOWAIT);
972                         if (path->device->serial_num != NULL) {
973                                 bcopy(ident_buf->serial,
974                                       path->device->serial_num,
975                                       sizeof(ident_buf->serial));
976                                 path->device->serial_num[sizeof(ident_buf->serial)]
977                                     = '\0';
978                                 path->device->serial_num_len =
979                                     strlen(path->device->serial_num);
980                         }
981                         if (ident_buf->enabled.extension &
982                             ATA_SUPPORT_64BITWWN) {
983                                 path->device->device_id =
984                                     malloc(16, M_CAMXPT, M_NOWAIT);
985                                 if (path->device->device_id != NULL) {
986                                         path->device->device_id_len = 16;
987                                         bcopy(&fake_device_id_hdr,
988                                             path->device->device_id, 8);
989                                         bcopy(ident_buf->wwn,
990                                             path->device->device_id + 8, 8);
991                                         ata_bswap(path->device->device_id + 8, 8);
992                                 }
993                         }
994
995                         path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
996                         xpt_async(AC_GETDEV_CHANGED, path, NULL);
997                 }
998                 if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
999                         path->device->mintags = 2;
1000                         path->device->maxtags =
1001                             ATA_QUEUE_LEN(ident_buf->queue) + 1;
1002                 }
1003                 ata_find_quirk(path->device);
1004                 if (path->device->mintags != 0 &&
1005                     path->bus->sim->max_tagged_dev_openings != 0) {
1006                         /* Check if the SIM does not want queued commands. */
1007                         bzero(&cpi, sizeof(cpi));
1008                         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1009                         cpi.ccb_h.func_code = XPT_PATH_INQ;
1010                         xpt_action((union ccb *)&cpi);
1011                         if (cpi.ccb_h.status == CAM_REQ_CMP &&
1012                             (cpi.hba_inquiry & PI_TAG_ABLE)) {
1013                                 /* Report SIM which tags are allowed. */
1014                                 bzero(&cts, sizeof(cts));
1015                                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1016                                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1017                                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1018                                 cts.xport_specific.sata.tags = path->device->maxtags;
1019                                 cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
1020                                 xpt_action((union ccb *)&cts);
1021                         }
1022                 }
1023                 ata_device_transport(path);
1024                 if (changed)
1025                         proberequestdefaultnegotiation(periph);
1026                 PROBE_SET_ACTION(softc, PROBE_SETMODE);
1027                 xpt_release_ccb(done_ccb);
1028                 xpt_schedule(periph, priority);
1029                 goto out;
1030         }
1031         case PROBE_SPINUP:
1032                 if (bootverbose)
1033                         xpt_print(path, "Spin-up done\n");
1034                 softc->spinup = 1;
1035                 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
1036                 xpt_release_ccb(done_ccb);
1037                 xpt_schedule(periph, priority);
1038                 goto out;
1039         case PROBE_SETMODE:
1040                 /* Set supported bits. */
1041                 bzero(&cts, sizeof(cts));
1042                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1043                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1044                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1045                 xpt_action((union ccb *)&cts);
1046                 if (path->device->transport == XPORT_SATA &&
1047                     cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1048                         caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1049                 else if (path->device->transport == XPORT_ATA &&
1050                     cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
1051                         caps = cts.xport_specific.ata.caps & CTS_ATA_CAPS_H;
1052                 else
1053                         caps = 0;
1054                 if (path->device->transport == XPORT_SATA &&
1055                     ident_buf->satacapabilities != 0xffff) {
1056                         if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
1057                                 caps |= CTS_SATA_CAPS_D_PMREQ;
1058                         if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
1059                                 caps |= CTS_SATA_CAPS_D_APST;
1060                 }
1061                 /* Mask unwanted bits. */
1062                 bzero(&cts, sizeof(cts));
1063                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1064                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1065                 cts.type = CTS_TYPE_USER_SETTINGS;
1066                 xpt_action((union ccb *)&cts);
1067                 if (path->device->transport == XPORT_SATA &&
1068                     cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1069                         caps &= cts.xport_specific.sata.caps;
1070                 else if (path->device->transport == XPORT_ATA &&
1071                     cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
1072                         caps &= cts.xport_specific.ata.caps;
1073                 else
1074                         caps = 0;
1075                 /*
1076                  * Remember what transport thinks about 48-bit DMA.  If
1077                  * capability information is not provided or transport is
1078                  * SATA, we take support for granted.
1079                  */
1080                 if (!(path->device->inq_flags & SID_DMA) ||
1081                     (path->device->transport == XPORT_ATA &&
1082                     (cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS) &&
1083                     !(caps & CTS_ATA_CAPS_H_DMA48)))
1084                         path->device->inq_flags &= ~SID_DMA48;
1085                 else
1086                         path->device->inq_flags |= SID_DMA48;
1087                 /* Store result to SIM. */
1088                 bzero(&cts, sizeof(cts));
1089                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1090                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1091                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1092                 if (path->device->transport == XPORT_SATA) {
1093                         cts.xport_specific.sata.caps = caps;
1094                         cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1095                 } else {
1096                         cts.xport_specific.ata.caps = caps;
1097                         cts.xport_specific.ata.valid = CTS_ATA_VALID_CAPS;
1098                 }
1099                 xpt_action((union ccb *)&cts);
1100                 softc->caps = caps;
1101                 if (path->device->transport != XPORT_SATA)
1102                         goto notsata;
1103                 if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
1104                     (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
1105                     (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
1106                         PROBE_SET_ACTION(softc, PROBE_SETPM);
1107                         xpt_release_ccb(done_ccb);
1108                         xpt_schedule(periph, priority);
1109                         goto out;
1110                 }
1111                 /* FALLTHROUGH */
1112         case PROBE_SETPM:
1113                 if (ident_buf->satacapabilities != 0xffff &&
1114                     (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
1115                     (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
1116                     (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
1117                         PROBE_SET_ACTION(softc, PROBE_SETAPST);
1118                         xpt_release_ccb(done_ccb);
1119                         xpt_schedule(periph, priority);
1120                         goto out;
1121                 }
1122                 /* FALLTHROUGH */
1123         case PROBE_SETAPST:
1124                 if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
1125                     (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
1126                     (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
1127                         PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1128                         xpt_release_ccb(done_ccb);
1129                         xpt_schedule(periph, priority);
1130                         goto out;
1131                 }
1132                 /* FALLTHROUGH */
1133         case PROBE_SETDMAAA:
1134                 if (path->device->protocol != PROTO_ATA &&
1135                     (ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) &&
1136                     (!(softc->caps & CTS_SATA_CAPS_H_AN)) !=
1137                     (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) {
1138                         PROBE_SET_ACTION(softc, PROBE_SETAN);
1139                         xpt_release_ccb(done_ccb);
1140                         xpt_schedule(periph, priority);
1141                         goto out;
1142                 }
1143                 /* FALLTHROUGH */
1144         case PROBE_SETAN:
1145 notsata:
1146                 if (path->device->protocol == PROTO_ATA) {
1147                         PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
1148                 } else {
1149                         PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1150                 }
1151                 xpt_release_ccb(done_ccb);
1152                 xpt_schedule(periph, priority);
1153                 goto out;
1154         case PROBE_SET_MULTI:
1155                 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1156                         path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1157                         xpt_acquire_device(path->device);
1158                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1159                         xpt_action(done_ccb);
1160                         xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1161                 }
1162                 PROBE_SET_ACTION(softc, PROBE_DONE);
1163                 break;
1164         case PROBE_INQUIRY:
1165         case PROBE_FULL_INQUIRY:
1166         {
1167                 u_int8_t periph_qual, len;
1168
1169                 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1170
1171                 periph_qual = SID_QUAL(inq_buf);
1172
1173                 if (periph_qual != SID_QUAL_LU_CONNECTED &&
1174                     periph_qual != SID_QUAL_LU_OFFLINE)
1175                         break;
1176
1177                 /*
1178                  * We conservatively request only
1179                  * SHORT_INQUIRY_LEN bytes of inquiry
1180                  * information during our first try
1181                  * at sending an INQUIRY. If the device
1182                  * has more information to give,
1183                  * perform a second request specifying
1184                  * the amount of information the device
1185                  * is willing to give.
1186                  */
1187                 len = inq_buf->additional_length
1188                     + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1189                 if (softc->action == PROBE_INQUIRY
1190                     && len > SHORT_INQUIRY_LENGTH) {
1191                         PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1192                         xpt_release_ccb(done_ccb);
1193                         xpt_schedule(periph, priority);
1194                         goto out;
1195                 }
1196
1197                 ata_device_transport(path);
1198                 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1199                         path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1200                         xpt_acquire_device(path->device);
1201                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1202                         xpt_action(done_ccb);
1203                         xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1204                 }
1205                 PROBE_SET_ACTION(softc, PROBE_DONE);
1206                 break;
1207         }
1208         case PROBE_PM_PID:
1209                 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1210                         bzero(ident_buf, sizeof(*ident_buf));
1211                 softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1212                     (done_ccb->ataio.res.lba_mid << 16) +
1213                     (done_ccb->ataio.res.lba_low << 8) +
1214                     done_ccb->ataio.res.sector_count;
1215                 ((uint32_t *)ident_buf)[0] = softc->pm_pid;
1216                 snprintf(ident_buf->model, sizeof(ident_buf->model),
1217                     "Port Multiplier %08x", softc->pm_pid);
1218                 PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1219                 xpt_release_ccb(done_ccb);
1220                 xpt_schedule(periph, priority);
1221                 goto out;
1222         case PROBE_PM_PRV:
1223                 softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1224                     (done_ccb->ataio.res.lba_mid << 16) +
1225                     (done_ccb->ataio.res.lba_low << 8) +
1226                     done_ccb->ataio.res.sector_count;
1227                 ((uint32_t *)ident_buf)[1] = softc->pm_prv;
1228                 snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1229                     "%04x", softc->pm_prv);
1230                 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1231                 ata_device_transport(path);
1232                 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
1233                         proberequestdefaultnegotiation(periph);
1234                 /* Set supported bits. */
1235                 bzero(&cts, sizeof(cts));
1236                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1237                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1238                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1239                 xpt_action((union ccb *)&cts);
1240                 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1241                         caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1242                 else
1243                         caps = 0;
1244                 /* All PMPs must support PM requests. */
1245                 caps |= CTS_SATA_CAPS_D_PMREQ;
1246                 /* Mask unwanted bits. */
1247                 bzero(&cts, sizeof(cts));
1248                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1249                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1250                 cts.type = CTS_TYPE_USER_SETTINGS;
1251                 xpt_action((union ccb *)&cts);
1252                 if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1253                         caps &= cts.xport_specific.sata.caps;
1254                 else
1255                         caps = 0;
1256                 /* Remember what transport thinks about AEN. */
1257                 if ((caps & CTS_SATA_CAPS_H_AN) && path->device->protocol != PROTO_ATA)
1258                         path->device->inq_flags |= SID_AEN;
1259                 else
1260                         path->device->inq_flags &= ~SID_AEN;
1261                 /* Store result to SIM. */
1262                 bzero(&cts, sizeof(cts));
1263                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1264                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1265                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1266                 cts.xport_specific.sata.caps = caps;
1267                 cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1268                 xpt_action((union ccb *)&cts);
1269                 softc->caps = caps;
1270                 xpt_async(AC_GETDEV_CHANGED, path, NULL);
1271                 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1272                         path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1273                         xpt_acquire_device(path->device);
1274                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1275                         xpt_action(done_ccb);
1276                         xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1277                 } else {
1278                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1279                         xpt_action(done_ccb);
1280                         xpt_async(AC_SCSI_AEN, path, done_ccb);
1281                 }
1282                 PROBE_SET_ACTION(softc, PROBE_DONE);
1283                 break;
1284         case PROBE_IDENTIFY_SES:
1285         case PROBE_IDENTIFY_SAFTE:
1286                 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1287                         /* Check that it is the same device. */
1288                         if (bcmp(&softc->ident_data, ident_buf, 53)) {
1289                                 /* Device changed. */
1290                                 xpt_async(AC_LOST_DEVICE, path, NULL);
1291                         } else {
1292                                 bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
1293                                 changed = 0;
1294                         }
1295                 }
1296                 if (changed) {
1297                         bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
1298                         /* Clean up from previous instance of this device */
1299                         if (path->device->device_id != NULL) {
1300                                 free(path->device->device_id, M_CAMXPT);
1301                                 path->device->device_id = NULL;
1302                                 path->device->device_id_len = 0;
1303                         }
1304                         path->device->device_id =
1305                             malloc(16, M_CAMXPT, M_NOWAIT);
1306                         if (path->device->device_id != NULL) {
1307                                 path->device->device_id_len = 16;
1308                                 bcopy(&fake_device_id_hdr,
1309                                     path->device->device_id, 8);
1310                                 bcopy(((uint8_t*)ident_buf) + 2,
1311                                     path->device->device_id + 8, 8);
1312                         }
1313
1314                         path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1315                 }
1316                 ata_device_transport(path);
1317                 if (changed)
1318                         proberequestdefaultnegotiation(periph);
1319
1320                 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1321                         path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1322                         xpt_acquire_device(path->device);
1323                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1324                         xpt_action(done_ccb);
1325                         xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1326                 }
1327                 PROBE_SET_ACTION(softc, PROBE_DONE);
1328                 break;
1329         default:
1330                 panic("probedone: invalid action state 0x%x\n", softc->action);
1331         }
1332 done:
1333         if (softc->restart) {
1334                 softc->restart = 0;
1335                 xpt_release_ccb(done_ccb);
1336                 probeschedule(periph);
1337                 goto out;
1338         }
1339         xpt_release_ccb(done_ccb);
1340         CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1341         while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1342                 TAILQ_REMOVE(&softc->request_ccbs,
1343                     &done_ccb->ccb_h, periph_links.tqe);
1344                 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1345                 xpt_done(done_ccb);
1346         }
1347         /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1348         cam_release_devq(path, 0, 0, 0, FALSE);
1349         cam_periph_invalidate(periph);
1350         cam_periph_release_locked(periph);
1351 }
1352
1353 static void
1354 probecleanup(struct cam_periph *periph)
1355 {
1356         free(periph->softc, M_CAMXPT);
1357 }
1358
1359 static void
1360 ata_find_quirk(struct cam_ed *device)
1361 {
1362         struct ata_quirk_entry *quirk;
1363         caddr_t match;
1364
1365         match = cam_quirkmatch((caddr_t)&device->ident_data,
1366                                (caddr_t)ata_quirk_table,
1367                                nitems(ata_quirk_table),
1368                                sizeof(*ata_quirk_table), ata_identify_match);
1369
1370         if (match == NULL)
1371                 panic("xpt_find_quirk: device didn't match wildcard entry!!");
1372
1373         quirk = (struct ata_quirk_entry *)match;
1374         device->quirk = quirk;
1375         if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
1376                 device->mintags = quirk->mintags;
1377                 device->maxtags = quirk->maxtags;
1378         }
1379 }
1380
1381 typedef struct {
1382         union   ccb *request_ccb;
1383         struct  ccb_pathinq *cpi;
1384         int     counter;
1385 } ata_scan_bus_info;
1386
1387 /*
1388  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1389  * As the scan progresses, xpt_scan_bus is used as the
1390  * callback on completion function.
1391  */
1392 static void
1393 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1394 {
1395         struct  cam_path *path;
1396         ata_scan_bus_info *scan_info;
1397         union   ccb *work_ccb, *reset_ccb;
1398         struct mtx *mtx;
1399         cam_status status;
1400
1401         CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1402                   ("xpt_scan_bus\n"));
1403         switch (request_ccb->ccb_h.func_code) {
1404         case XPT_SCAN_BUS:
1405         case XPT_SCAN_TGT:
1406                 /* Find out the characteristics of the bus */
1407                 work_ccb = xpt_alloc_ccb_nowait();
1408                 if (work_ccb == NULL) {
1409                         request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1410                         xpt_done(request_ccb);
1411                         return;
1412                 }
1413                 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1414                               request_ccb->ccb_h.pinfo.priority);
1415                 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1416                 xpt_action(work_ccb);
1417                 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1418                         request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1419                         xpt_free_ccb(work_ccb);
1420                         xpt_done(request_ccb);
1421                         return;
1422                 }
1423
1424                 /* We may need to reset bus first, if we haven't done it yet. */
1425                 if ((work_ccb->cpi.hba_inquiry &
1426                     (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1427                     !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1428                     !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1429                         reset_ccb = xpt_alloc_ccb_nowait();
1430                         if (reset_ccb == NULL) {
1431                                 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1432                                 xpt_free_ccb(work_ccb);
1433                                 xpt_done(request_ccb);
1434                                 return;
1435                         }
1436                         xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1437                               CAM_PRIORITY_NONE);
1438                         reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1439                         xpt_action(reset_ccb);
1440                         if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1441                                 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1442                                 xpt_free_ccb(reset_ccb);
1443                                 xpt_free_ccb(work_ccb);
1444                                 xpt_done(request_ccb);
1445                                 return;
1446                         }
1447                         xpt_free_ccb(reset_ccb);
1448                 }
1449
1450                 /* Save some state for use while we probe for devices */
1451                 scan_info = (ata_scan_bus_info *)
1452                     malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1453                 if (scan_info == NULL) {
1454                         request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1455                         xpt_free_ccb(work_ccb);
1456                         xpt_done(request_ccb);
1457                         return;
1458                 }
1459                 scan_info->request_ccb = request_ccb;
1460                 scan_info->cpi = &work_ccb->cpi;
1461                 /* If PM supported, probe it first. */
1462                 if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1463                         scan_info->counter = scan_info->cpi->max_target;
1464                 else
1465                         scan_info->counter = 0;
1466
1467                 work_ccb = xpt_alloc_ccb_nowait();
1468                 if (work_ccb == NULL) {
1469                         free(scan_info, M_CAMXPT);
1470                         request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1471                         xpt_done(request_ccb);
1472                         break;
1473                 }
1474                 mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
1475                 goto scan_next;
1476         case XPT_SCAN_LUN:
1477                 work_ccb = request_ccb;
1478                 /* Reuse the same CCB to query if a device was really found */
1479                 scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1480                 mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
1481                 mtx_lock(mtx);
1482                 /* If there is PMP... */
1483                 if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1484                     (scan_info->counter == scan_info->cpi->max_target)) {
1485                         if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1486                                 /* everything else will be probed by it */
1487                                 /* Free the current request path- we're done with it. */
1488                                 xpt_free_path(work_ccb->ccb_h.path);
1489                                 goto done;
1490                         } else {
1491                                 struct ccb_trans_settings cts;
1492
1493                                 /* Report SIM that PM is absent. */
1494                                 bzero(&cts, sizeof(cts));
1495                                 xpt_setup_ccb(&cts.ccb_h,
1496                                     work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1497                                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1498                                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1499                                 cts.xport_specific.sata.pm_present = 0;
1500                                 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1501                                 xpt_action((union ccb *)&cts);
1502                         }
1503                 }
1504                 /* Free the current request path- we're done with it. */
1505                 xpt_free_path(work_ccb->ccb_h.path);
1506                 if (scan_info->counter ==
1507                     ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1508                     0 : scan_info->cpi->max_target)) {
1509 done:
1510                         mtx_unlock(mtx);
1511                         xpt_free_ccb(work_ccb);
1512                         xpt_free_ccb((union ccb *)scan_info->cpi);
1513                         request_ccb = scan_info->request_ccb;
1514                         free(scan_info, M_CAMXPT);
1515                         request_ccb->ccb_h.status = CAM_REQ_CMP;
1516                         xpt_done(request_ccb);
1517                         break;
1518                 }
1519                 /* Take next device. Wrap from max (PMP) to 0. */
1520                 scan_info->counter = (scan_info->counter + 1 ) %
1521                     (scan_info->cpi->max_target + 1);
1522 scan_next:
1523                 status = xpt_create_path(&path, NULL,
1524                     scan_info->request_ccb->ccb_h.path_id,
1525                     scan_info->counter, 0);
1526                 if (status != CAM_REQ_CMP) {
1527                         if (request_ccb->ccb_h.func_code == XPT_SCAN_LUN)
1528                                 mtx_unlock(mtx);
1529                         printf("xpt_scan_bus: xpt_create_path failed"
1530                             " with status %#x, bus scan halted\n",
1531                             status);
1532                         xpt_free_ccb(work_ccb);
1533                         xpt_free_ccb((union ccb *)scan_info->cpi);
1534                         request_ccb = scan_info->request_ccb;
1535                         free(scan_info, M_CAMXPT);
1536                         request_ccb->ccb_h.status = status;
1537                         xpt_done(request_ccb);
1538                         break;
1539                 }
1540                 xpt_setup_ccb(&work_ccb->ccb_h, path,
1541                     scan_info->request_ccb->ccb_h.pinfo.priority);
1542                 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1543                 work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1544                 work_ccb->ccb_h.flags |= CAM_UNLOCKED;
1545                 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1546                 work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1547                 mtx_unlock(mtx);
1548                 if (request_ccb->ccb_h.func_code == XPT_SCAN_LUN)
1549                         mtx = NULL;
1550                 xpt_action(work_ccb);
1551                 if (mtx != NULL)
1552                         mtx_lock(mtx);
1553                 break;
1554         default:
1555                 break;
1556         }
1557 }
1558
1559 static void
1560 ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1561              cam_flags flags, union ccb *request_ccb)
1562 {
1563         struct ccb_pathinq cpi;
1564         cam_status status;
1565         struct cam_path *new_path;
1566         struct cam_periph *old_periph;
1567         int lock;
1568
1569         CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1570
1571         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1572         cpi.ccb_h.func_code = XPT_PATH_INQ;
1573         xpt_action((union ccb *)&cpi);
1574
1575         if (cpi.ccb_h.status != CAM_REQ_CMP) {
1576                 if (request_ccb != NULL) {
1577                         request_ccb->ccb_h.status = cpi.ccb_h.status;
1578                         xpt_done(request_ccb);
1579                 }
1580                 return;
1581         }
1582
1583         if (request_ccb == NULL) {
1584                 request_ccb = xpt_alloc_ccb_nowait();
1585                 if (request_ccb == NULL) {
1586                         xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1587                             "can't continue\n");
1588                         return;
1589                 }
1590                 status = xpt_create_path(&new_path, NULL,
1591                                           path->bus->path_id,
1592                                           path->target->target_id,
1593                                           path->device->lun_id);
1594                 if (status != CAM_REQ_CMP) {
1595                         xpt_print(path, "xpt_scan_lun: can't create path, "
1596                             "can't continue\n");
1597                         xpt_free_ccb(request_ccb);
1598                         return;
1599                 }
1600                 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1601                 request_ccb->ccb_h.cbfcnp = xptscandone;
1602                 request_ccb->ccb_h.flags |= CAM_UNLOCKED;
1603                 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1604                 request_ccb->crcn.flags = flags;
1605         }
1606
1607         lock = (xpt_path_owned(path) == 0);
1608         if (lock)
1609                 xpt_path_lock(path);
1610         if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1611                 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
1612                         probe_softc *softc;
1613
1614                         softc = (probe_softc *)old_periph->softc;
1615                         TAILQ_INSERT_TAIL(&softc->request_ccbs,
1616                                 &request_ccb->ccb_h, periph_links.tqe);
1617                         softc->restart = 1;
1618                 } else {
1619                         request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1620                         xpt_done(request_ccb);
1621                 }
1622         } else {
1623                 status = cam_periph_alloc(proberegister, NULL, probecleanup,
1624                                           probestart, "aprobe",
1625                                           CAM_PERIPH_BIO,
1626                                           request_ccb->ccb_h.path, NULL, 0,
1627                                           request_ccb);
1628
1629                 if (status != CAM_REQ_CMP) {
1630                         xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1631                             "returned an error, can't continue probe\n");
1632                         request_ccb->ccb_h.status = status;
1633                         xpt_done(request_ccb);
1634                 }
1635         }
1636         if (lock)
1637                 xpt_path_unlock(path);
1638 }
1639
1640 static void
1641 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1642 {
1643
1644         xpt_free_path(done_ccb->ccb_h.path);
1645         xpt_free_ccb(done_ccb);
1646 }
1647
1648 static struct cam_ed *
1649 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1650 {
1651         struct ata_quirk_entry *quirk;
1652         struct cam_ed *device;
1653
1654         device = xpt_alloc_device(bus, target, lun_id);
1655         if (device == NULL)
1656                 return (NULL);
1657
1658         /*
1659          * Take the default quirk entry until we have inquiry
1660          * data and can determine a better quirk to use.
1661          */
1662         quirk = &ata_quirk_table[nitems(ata_quirk_table) - 1];
1663         device->quirk = (void *)quirk;
1664         device->mintags = 0;
1665         device->maxtags = 0;
1666         bzero(&device->inq_data, sizeof(device->inq_data));
1667         device->inq_flags = 0;
1668         device->queue_flags = 0;
1669         device->serial_num = NULL;
1670         device->serial_num_len = 0;
1671         return (device);
1672 }
1673
1674 static void
1675 ata_device_transport(struct cam_path *path)
1676 {
1677         struct ccb_pathinq cpi;
1678         struct ccb_trans_settings cts;
1679         struct scsi_inquiry_data *inq_buf = NULL;
1680         struct ata_params *ident_buf = NULL;
1681
1682         /* Get transport information from the SIM */
1683         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1684         cpi.ccb_h.func_code = XPT_PATH_INQ;
1685         xpt_action((union ccb *)&cpi);
1686
1687         path->device->transport = cpi.transport;
1688         if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1689                 inq_buf = &path->device->inq_data;
1690         if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1691                 ident_buf = &path->device->ident_data;
1692         if (path->device->protocol == PROTO_ATA) {
1693                 path->device->protocol_version = ident_buf ?
1694                     ata_version(ident_buf->version_major) : cpi.protocol_version;
1695         } else if (path->device->protocol == PROTO_SCSI) {
1696                 path->device->protocol_version = inq_buf ?
1697                     SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1698         }
1699         path->device->transport_version = ident_buf ?
1700             ata_version(ident_buf->version_major) : cpi.transport_version;
1701
1702         /* Tell the controller what we think */
1703         xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1704         cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1705         cts.type = CTS_TYPE_CURRENT_SETTINGS;
1706         cts.transport = path->device->transport;
1707         cts.transport_version = path->device->transport_version;
1708         cts.protocol = path->device->protocol;
1709         cts.protocol_version = path->device->protocol_version;
1710         cts.proto_specific.valid = 0;
1711         if (ident_buf) {
1712                 if (path->device->transport == XPORT_ATA) {
1713                         cts.xport_specific.ata.atapi =
1714                             (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1715                             ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1716                             ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1717                         cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1718                 } else {
1719                         cts.xport_specific.sata.atapi =
1720                             (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1721                             ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1722                             ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1723                         cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1724                 }
1725         } else
1726                 cts.xport_specific.valid = 0;
1727         xpt_action((union ccb *)&cts);
1728 }
1729
1730 static void
1731 ata_dev_advinfo(union ccb *start_ccb)
1732 {
1733         struct cam_ed *device;
1734         struct ccb_dev_advinfo *cdai;
1735         off_t amt; 
1736
1737         start_ccb->ccb_h.status = CAM_REQ_INVALID;
1738         device = start_ccb->ccb_h.path->device;
1739         cdai = &start_ccb->cdai;
1740         switch(cdai->buftype) {
1741         case CDAI_TYPE_SCSI_DEVID:
1742                 if (cdai->flags & CDAI_FLAG_STORE)
1743                         return;
1744                 cdai->provsiz = device->device_id_len;
1745                 if (device->device_id_len == 0)
1746                         break;
1747                 amt = device->device_id_len;
1748                 if (cdai->provsiz > cdai->bufsiz)
1749                         amt = cdai->bufsiz;
1750                 memcpy(cdai->buf, device->device_id, amt);
1751                 break;
1752         case CDAI_TYPE_SERIAL_NUM:
1753                 if (cdai->flags & CDAI_FLAG_STORE)
1754                         return;
1755                 cdai->provsiz = device->serial_num_len;
1756                 if (device->serial_num_len == 0)
1757                         break;
1758                 amt = device->serial_num_len;
1759                 if (cdai->provsiz > cdai->bufsiz)
1760                         amt = cdai->bufsiz;
1761                 memcpy(cdai->buf, device->serial_num, amt);
1762                 break;
1763         case CDAI_TYPE_PHYS_PATH:
1764                 if (cdai->flags & CDAI_FLAG_STORE) {
1765                         if (device->physpath != NULL)
1766                                 free(device->physpath, M_CAMXPT);
1767                         device->physpath_len = cdai->bufsiz;
1768                         /* Clear existing buffer if zero length */
1769                         if (cdai->bufsiz == 0)
1770                                 break;
1771                         device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
1772                         if (device->physpath == NULL) {
1773                                 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
1774                                 return;
1775                         }
1776                         memcpy(device->physpath, cdai->buf, cdai->bufsiz);
1777                 } else {
1778                         cdai->provsiz = device->physpath_len;
1779                         if (device->physpath_len == 0)
1780                                 break;
1781                         amt = device->physpath_len;
1782                         if (cdai->provsiz > cdai->bufsiz)
1783                                 amt = cdai->bufsiz;
1784                         memcpy(cdai->buf, device->physpath, amt);
1785                 }
1786                 break;
1787         default:
1788                 return;
1789         }
1790         start_ccb->ccb_h.status = CAM_REQ_CMP;
1791
1792         if (cdai->flags & CDAI_FLAG_STORE) {
1793                 xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
1794                           (void *)(uintptr_t)cdai->buftype);
1795         }
1796 }
1797
1798 static void
1799 ata_action(union ccb *start_ccb)
1800 {
1801
1802         switch (start_ccb->ccb_h.func_code) {
1803         case XPT_SET_TRAN_SETTINGS:
1804         {
1805                 ata_set_transfer_settings(&start_ccb->cts,
1806                                            start_ccb->ccb_h.path,
1807                                            /*async_update*/FALSE);
1808                 break;
1809         }
1810         case XPT_SCAN_BUS:
1811         case XPT_SCAN_TGT:
1812                 ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1813                 break;
1814         case XPT_SCAN_LUN:
1815                 ata_scan_lun(start_ccb->ccb_h.path->periph,
1816                               start_ccb->ccb_h.path, start_ccb->crcn.flags,
1817                               start_ccb);
1818                 break;
1819         case XPT_GET_TRAN_SETTINGS:
1820         {
1821                 ata_get_transfer_settings(&start_ccb->cts);
1822                 break;
1823         }
1824         case XPT_SCSI_IO:
1825         {
1826                 struct cam_ed *device;
1827                 u_int   maxlen = 0;
1828
1829                 device = start_ccb->ccb_h.path->device;
1830                 if (device->protocol == PROTO_SCSI &&
1831                     (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1832                         uint16_t p =
1833                             device->ident_data.config & ATA_PROTO_MASK;
1834
1835                         maxlen =
1836                             (device->ident_data.config == ATA_PROTO_CFA) ? 0 :
1837                             (p == ATA_PROTO_ATAPI_16) ? 16 :
1838                             (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1839                 }
1840                 if (start_ccb->csio.cdb_len > maxlen) {
1841                         start_ccb->ccb_h.status = CAM_REQ_INVALID;
1842                         xpt_done(start_ccb);
1843                         break;
1844                 }
1845                 xpt_action_default(start_ccb);
1846                 break;
1847         }
1848         case XPT_DEV_ADVINFO:
1849         {
1850                 ata_dev_advinfo(start_ccb);
1851                 break;
1852         }
1853         default:
1854                 xpt_action_default(start_ccb);
1855                 break;
1856         }
1857 }
1858
1859 static void
1860 ata_get_transfer_settings(struct ccb_trans_settings *cts)
1861 {
1862         struct  ccb_trans_settings_ata *ata;
1863         struct  ccb_trans_settings_scsi *scsi;
1864         struct  cam_ed *device;
1865
1866         device = cts->ccb_h.path->device;
1867         xpt_action_default((union ccb *)cts);
1868
1869         if (cts->protocol == PROTO_UNKNOWN ||
1870             cts->protocol == PROTO_UNSPECIFIED) {
1871                 cts->protocol = device->protocol;
1872                 cts->protocol_version = device->protocol_version;
1873         }
1874
1875         if (cts->protocol == PROTO_ATA) {
1876                 ata = &cts->proto_specific.ata;
1877                 if ((ata->valid & CTS_ATA_VALID_TQ) == 0) {
1878                         ata->valid |= CTS_ATA_VALID_TQ;
1879                         if (cts->type == CTS_TYPE_USER_SETTINGS ||
1880                             (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1881                             (device->inq_flags & SID_CmdQue) != 0)
1882                                 ata->flags |= CTS_ATA_FLAGS_TAG_ENB;
1883                 }
1884         }
1885         if (cts->protocol == PROTO_SCSI) {
1886                 scsi = &cts->proto_specific.scsi;
1887                 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1888                         scsi->valid |= CTS_SCSI_VALID_TQ;
1889                         if (cts->type == CTS_TYPE_USER_SETTINGS ||
1890                             (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1891                             (device->inq_flags & SID_CmdQue) != 0)
1892                                 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1893                 }
1894         }
1895
1896         if (cts->transport == XPORT_UNKNOWN ||
1897             cts->transport == XPORT_UNSPECIFIED) {
1898                 cts->transport = device->transport;
1899                 cts->transport_version = device->transport_version;
1900         }
1901 }
1902
1903 static void
1904 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
1905                            int async_update)
1906 {
1907         struct  ccb_pathinq cpi;
1908         struct  ccb_trans_settings_ata *ata;
1909         struct  ccb_trans_settings_scsi *scsi;
1910         struct  ata_params *ident_data;
1911         struct  scsi_inquiry_data *inq_data;
1912         struct  cam_ed *device;
1913
1914         if (path == NULL || (device = path->device) == NULL) {
1915                 cts->ccb_h.status = CAM_PATH_INVALID;
1916                 xpt_done((union ccb *)cts);
1917                 return;
1918         }
1919
1920         if (cts->protocol == PROTO_UNKNOWN
1921          || cts->protocol == PROTO_UNSPECIFIED) {
1922                 cts->protocol = device->protocol;
1923                 cts->protocol_version = device->protocol_version;
1924         }
1925
1926         if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1927          || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1928                 cts->protocol_version = device->protocol_version;
1929
1930         if (cts->protocol != device->protocol) {
1931                 xpt_print(path, "Uninitialized Protocol %x:%x?\n",
1932                        cts->protocol, device->protocol);
1933                 cts->protocol = device->protocol;
1934         }
1935
1936         if (cts->protocol_version > device->protocol_version) {
1937                 if (bootverbose) {
1938                         xpt_print(path, "Down reving Protocol "
1939                             "Version from %d to %d?\n", cts->protocol_version,
1940                             device->protocol_version);
1941                 }
1942                 cts->protocol_version = device->protocol_version;
1943         }
1944
1945         if (cts->transport == XPORT_UNKNOWN
1946          || cts->transport == XPORT_UNSPECIFIED) {
1947                 cts->transport = device->transport;
1948                 cts->transport_version = device->transport_version;
1949         }
1950
1951         if (cts->transport_version == XPORT_VERSION_UNKNOWN
1952          || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1953                 cts->transport_version = device->transport_version;
1954
1955         if (cts->transport != device->transport) {
1956                 xpt_print(path, "Uninitialized Transport %x:%x?\n",
1957                     cts->transport, device->transport);
1958                 cts->transport = device->transport;
1959         }
1960
1961         if (cts->transport_version > device->transport_version) {
1962                 if (bootverbose) {
1963                         xpt_print(path, "Down reving Transport "
1964                             "Version from %d to %d?\n", cts->transport_version,
1965                             device->transport_version);
1966                 }
1967                 cts->transport_version = device->transport_version;
1968         }
1969
1970         ident_data = &device->ident_data;
1971         inq_data = &device->inq_data;
1972         if (cts->protocol == PROTO_ATA)
1973                 ata = &cts->proto_specific.ata;
1974         else
1975                 ata = NULL;
1976         if (cts->protocol == PROTO_SCSI)
1977                 scsi = &cts->proto_specific.scsi;
1978         else
1979                 scsi = NULL;
1980         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1981         cpi.ccb_h.func_code = XPT_PATH_INQ;
1982         xpt_action((union ccb *)&cpi);
1983
1984         /* Sanity checking */
1985         if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1986          || (ata && (ident_data->satacapabilities & ATA_SUPPORT_NCQ) == 0)
1987          || (scsi && (INQ_DATA_TQ_ENABLED(inq_data)) == 0)
1988          || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1989          || (device->mintags == 0)) {
1990                 /*
1991                  * Can't tag on hardware that doesn't support tags,
1992                  * doesn't have it enabled, or has broken tag support.
1993                  */
1994                 if (ata)
1995                         ata->flags &= ~CTS_ATA_FLAGS_TAG_ENB;
1996                 if (scsi)
1997                         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1998         }
1999
2000         /* Start/stop tags use. */
2001         if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
2002             ((ata && (ata->valid & CTS_ATA_VALID_TQ) != 0) ||
2003              (scsi && (scsi->valid & CTS_SCSI_VALID_TQ) != 0))) {
2004                 int nowt, newt = 0;
2005
2006                 nowt = ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
2007                         (device->inq_flags & SID_CmdQue) != 0);
2008                 if (ata)
2009                         newt = (ata->flags & CTS_ATA_FLAGS_TAG_ENB) != 0;
2010                 if (scsi)
2011                         newt = (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0;
2012
2013                 if (newt && !nowt) {
2014                         /*
2015                          * Delay change to use tags until after a
2016                          * few commands have gone to this device so
2017                          * the controller has time to perform transfer
2018                          * negotiations without tagged messages getting
2019                          * in the way.
2020                          */
2021                         device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2022                         device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2023                 } else if (nowt && !newt)
2024                         xpt_stop_tags(path);
2025         }
2026
2027         if (async_update == FALSE)
2028                 xpt_action_default((union ccb *)cts);
2029 }
2030
2031 /*
2032  * Handle any per-device event notifications that require action by the XPT.
2033  */
2034 static void
2035 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
2036               struct cam_ed *device, void *async_arg)
2037 {
2038         cam_status status;
2039         struct cam_path newpath;
2040
2041         /*
2042          * We only need to handle events for real devices.
2043          */
2044         if (target->target_id == CAM_TARGET_WILDCARD
2045          || device->lun_id == CAM_LUN_WILDCARD)
2046                 return;
2047
2048         /*
2049          * We need our own path with wildcards expanded to
2050          * handle certain types of events.
2051          */
2052         if ((async_code == AC_SENT_BDR)
2053          || (async_code == AC_BUS_RESET)
2054          || (async_code == AC_INQ_CHANGED))
2055                 status = xpt_compile_path(&newpath, NULL,
2056                                           bus->path_id,
2057                                           target->target_id,
2058                                           device->lun_id);
2059         else
2060                 status = CAM_REQ_CMP_ERR;
2061
2062         if (status == CAM_REQ_CMP) {
2063                 if (async_code == AC_INQ_CHANGED) {
2064                         /*
2065                          * We've sent a start unit command, or
2066                          * something similar to a device that
2067                          * may have caused its inquiry data to
2068                          * change. So we re-scan the device to
2069                          * refresh the inquiry data for it.
2070                          */
2071                         ata_scan_lun(newpath.periph, &newpath,
2072                                      CAM_EXPECT_INQ_CHANGE, NULL);
2073                 } else {
2074                         /* We need to reinitialize device after reset. */
2075                         ata_scan_lun(newpath.periph, &newpath,
2076                                      0, NULL);
2077                 }
2078                 xpt_release_path(&newpath);
2079         } else if (async_code == AC_LOST_DEVICE &&
2080             (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2081                 device->flags |= CAM_DEV_UNCONFIGURED;
2082                 xpt_release_device(device);
2083         } else if (async_code == AC_TRANSFER_NEG) {
2084                 struct ccb_trans_settings *settings;
2085                 struct cam_path path;
2086
2087                 settings = (struct ccb_trans_settings *)async_arg;
2088                 xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
2089                                  device->lun_id);
2090                 ata_set_transfer_settings(settings, &path,
2091                                           /*async_update*/TRUE);
2092                 xpt_release_path(&path);
2093         }
2094 }
2095
2096 static void
2097 _ata_announce_periph(struct cam_periph *periph, struct ccb_trans_settings *cts, u_int *speed)
2098 {
2099         struct  ccb_pathinq cpi;
2100         struct  cam_path *path = periph->path;
2101
2102         cam_periph_assert(periph, MA_OWNED);
2103
2104         xpt_setup_ccb(&cts->ccb_h, path, CAM_PRIORITY_NORMAL);
2105         cts->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2106         cts->type = CTS_TYPE_CURRENT_SETTINGS;
2107         xpt_action((union ccb*)cts);
2108         if ((cts->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2109                 return;
2110         /* Ask the SIM for its base transfer speed */
2111         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
2112         cpi.ccb_h.func_code = XPT_PATH_INQ;
2113         xpt_action((union ccb *)&cpi);
2114         /* Report connection speed */
2115         *speed = cpi.base_transfer_speed;
2116         if (cts->transport == XPORT_ATA) {
2117                 struct  ccb_trans_settings_pata *pata =
2118                     &cts->xport_specific.ata;
2119
2120                 if (pata->valid & CTS_ATA_VALID_MODE)
2121                         *speed = ata_mode2speed(pata->mode);
2122         }
2123         if (cts->transport == XPORT_SATA) {
2124                 struct  ccb_trans_settings_sata *sata =
2125                     &cts->xport_specific.sata;
2126
2127                 if (sata->valid & CTS_SATA_VALID_REVISION)
2128                         *speed = ata_revision2speed(sata->revision);
2129         }
2130 }
2131
2132 static void
2133 ata_announce_periph(struct cam_periph *periph)
2134 {
2135         struct ccb_trans_settings cts;
2136         u_int speed, mb;
2137
2138         _ata_announce_periph(periph, &cts, &speed);
2139         if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2140                 return;
2141
2142         mb = speed / 1000;
2143         if (mb > 0)
2144                 printf("%s%d: %d.%03dMB/s transfers",
2145                        periph->periph_name, periph->unit_number,
2146                        mb, speed % 1000);
2147         else
2148                 printf("%s%d: %dKB/s transfers", periph->periph_name,
2149                        periph->unit_number, speed);
2150         /* Report additional information about connection */
2151         if (cts.transport == XPORT_ATA) {
2152                 struct ccb_trans_settings_pata *pata =
2153                     &cts.xport_specific.ata;
2154
2155                 printf(" (");
2156                 if (pata->valid & CTS_ATA_VALID_MODE)
2157                         printf("%s, ", ata_mode2string(pata->mode));
2158                 if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
2159                         printf("ATAPI %dbytes, ", pata->atapi);
2160                 if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
2161                         printf("PIO %dbytes", pata->bytecount);
2162                 printf(")");
2163         }
2164         if (cts.transport == XPORT_SATA) {
2165                 struct ccb_trans_settings_sata *sata =
2166                     &cts.xport_specific.sata;
2167
2168                 printf(" (");
2169                 if (sata->valid & CTS_SATA_VALID_REVISION)
2170                         printf("SATA %d.x, ", sata->revision);
2171                 else
2172                         printf("SATA, ");
2173                 if (sata->valid & CTS_SATA_VALID_MODE)
2174                         printf("%s, ", ata_mode2string(sata->mode));
2175                 if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
2176                         printf("ATAPI %dbytes, ", sata->atapi);
2177                 if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
2178                         printf("PIO %dbytes", sata->bytecount);
2179                 printf(")");
2180         }
2181         printf("\n");
2182 }
2183
2184 static void
2185 ata_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
2186 {
2187         struct ccb_trans_settings cts;
2188         u_int speed, mb;
2189
2190         _ata_announce_periph(periph, &cts, &speed);
2191         if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2192                 return;
2193
2194         mb = speed / 1000;
2195         if (mb > 0)
2196                 sbuf_printf(sb, "%s%d: %d.%03dMB/s transfers",
2197                        periph->periph_name, periph->unit_number,
2198                        mb, speed % 1000);
2199         else
2200                 sbuf_printf(sb, "%s%d: %dKB/s transfers", periph->periph_name,
2201                        periph->unit_number, speed);
2202         /* Report additional information about connection */
2203         if (cts.transport == XPORT_ATA) {
2204                 struct ccb_trans_settings_pata *pata =
2205                     &cts.xport_specific.ata;
2206
2207                 sbuf_printf(sb, " (");
2208                 if (pata->valid & CTS_ATA_VALID_MODE)
2209                         sbuf_printf(sb, "%s, ", ata_mode2string(pata->mode));
2210                 if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
2211                         sbuf_printf(sb, "ATAPI %dbytes, ", pata->atapi);
2212                 if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
2213                         sbuf_printf(sb, "PIO %dbytes", pata->bytecount);
2214                 sbuf_printf(sb, ")");
2215         }
2216         if (cts.transport == XPORT_SATA) {
2217                 struct ccb_trans_settings_sata *sata =
2218                     &cts.xport_specific.sata;
2219
2220                 sbuf_printf(sb, " (");
2221                 if (sata->valid & CTS_SATA_VALID_REVISION)
2222                         sbuf_printf(sb, "SATA %d.x, ", sata->revision);
2223                 else
2224                         sbuf_printf(sb, "SATA, ");
2225                 if (sata->valid & CTS_SATA_VALID_MODE)
2226                         sbuf_printf(sb, "%s, ", ata_mode2string(sata->mode));
2227                 if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
2228                         sbuf_printf(sb, "ATAPI %dbytes, ", sata->atapi);
2229                 if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
2230                         sbuf_printf(sb, "PIO %dbytes", sata->bytecount);
2231                 sbuf_printf(sb, ")");
2232         }
2233         sbuf_printf(sb, "\n");
2234 }
2235
2236 static void
2237 ata_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
2238 {
2239         ata_print_ident_sbuf(&device->ident_data, sb);
2240 }
2241
2242 static void
2243 ata_proto_announce(struct cam_ed *device)
2244 {
2245         ata_print_ident(&device->ident_data);
2246 }
2247
2248 static void
2249 ata_proto_denounce(struct cam_ed *device)
2250 {
2251         ata_print_ident_short(&device->ident_data);
2252 }
2253
2254 static void
2255 ata_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
2256 {
2257         ata_print_ident_short_sbuf(&device->ident_data, sb);
2258 }
2259
2260 static void
2261 semb_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
2262 {
2263         semb_print_ident_sbuf((struct sep_identify_data *)&device->ident_data, sb);
2264 }
2265
2266 static void
2267 semb_proto_announce(struct cam_ed *device)
2268 {
2269         semb_print_ident((struct sep_identify_data *)&device->ident_data);
2270 }
2271
2272 static void
2273 semb_proto_denounce(struct cam_ed *device)
2274 {
2275         semb_print_ident_short((struct sep_identify_data *)&device->ident_data);
2276 }
2277
2278 static void
2279 semb_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
2280 {
2281         semb_print_ident_short_sbuf((struct sep_identify_data *)&device->ident_data, sb);
2282 }
2283
2284 static void
2285 ata_proto_debug_out(union ccb *ccb)
2286 {
2287         char cdb_str[(sizeof(struct ata_cmd) * 3) + 1];
2288
2289         if (ccb->ccb_h.func_code != XPT_ATA_IO)
2290                 return;
2291
2292         CAM_DEBUG(ccb->ccb_h.path,
2293             CAM_DEBUG_CDB,("%s. ACB: %s\n", ata_op_string(&ccb->ataio.cmd),
2294                 ata_cmd_string(&ccb->ataio.cmd, cdb_str, sizeof(cdb_str))));
2295 }