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