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