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