]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cam/ata/ata_xpt.c
MFC r203376, r203384:
[FreeBSD/stable/8.git] / sys / cam / ata / ata_xpt.c
1 /*-
2  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/endian.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/time.h>
38 #include <sys/conf.h>
39 #include <sys/fcntl.h>
40 #include <sys/md5.h>
41 #include <sys/interrupt.h>
42 #include <sys/sbuf.h>
43
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47
48 #ifdef PC98
49 #include <pc98/pc98/pc98_machdep.h>     /* geometry translation */
50 #endif
51
52 #include <cam/cam.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_xpt_internal.h>
61 #include <cam/cam_debug.h>
62
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/ata/ata_all.h>
66 #include <machine/stdarg.h>     /* for xpt_print below */
67 #include "opt_cam.h"
68
69 struct ata_quirk_entry {
70         struct scsi_inquiry_pattern inq_pat;
71         u_int8_t quirks;
72 #define CAM_QUIRK_MAXTAGS       0x01
73         u_int maxtags;
74 };
75
76 static periph_init_t probe_periph_init;
77
78 static struct periph_driver probe_driver =
79 {
80         probe_periph_init, "aprobe",
81         TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
82         CAM_PERIPH_DRV_EARLY
83 };
84
85 PERIPHDRIVER_DECLARE(aprobe, probe_driver);
86
87 typedef enum {
88         PROBE_RESET,
89         PROBE_IDENTIFY,
90         PROBE_SETMODE,
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_SETMODE",
103         "PROBE_SET_MULTI",
104         "PROBE_INQUIRY",
105         "PROBE_FULL_INQUIRY",
106         "PROBE_PM_PID",
107         "PROBE_PM_PRV",
108         "PROBE_INVALID"
109 };
110
111 #define PROBE_SET_ACTION(softc, newaction)      \
112 do {                                                                    \
113         char **text;                                                    \
114         text = probe_action_text;                                       \
115         CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,                \
116             ("Probe %s to %s\n", text[(softc)->action],                 \
117             text[(newaction)]));                                        \
118         (softc)->action = (newaction);                                  \
119 } while(0)
120
121 typedef enum {
122         PROBE_NO_ANNOUNCE       = 0x04
123 } probe_flags;
124
125 typedef struct {
126         TAILQ_HEAD(, ccb_hdr) request_ccbs;
127         probe_action    action;
128         union ccb       saved_ccb;
129         probe_flags     flags;
130         u_int8_t        digest[16];
131         uint32_t        pm_pid;
132         uint32_t        pm_prv;
133         int             restart;
134         struct cam_periph *periph;
135 } probe_softc;
136
137 static struct ata_quirk_entry ata_quirk_table[] =
138 {
139         {
140                 /* Default tagged queuing parameters for all devices */
141                 {
142                   T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
143                   /*vendor*/"*", /*product*/"*", /*revision*/"*"
144                 },
145                 /*quirks*/0, /*maxtags*/0
146         },
147 };
148
149 static const int ata_quirk_table_size =
150         sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
151
152 static cam_status       proberegister(struct cam_periph *periph,
153                                       void *arg);
154 static void      probeschedule(struct cam_periph *probe_periph);
155 static void      probestart(struct cam_periph *periph, union ccb *start_ccb);
156 //static void    proberequestdefaultnegotiation(struct cam_periph *periph);
157 //static int       proberequestbackoff(struct cam_periph *periph,
158 //                                   struct cam_ed *device);
159 static void      probedone(struct cam_periph *periph, union ccb *done_ccb);
160 static void      probecleanup(struct cam_periph *periph);
161 static void      ata_find_quirk(struct cam_ed *device);
162 static void      ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
163 static void      ata_scan_lun(struct cam_periph *periph,
164                                struct cam_path *path, cam_flags flags,
165                                union ccb *ccb);
166 static void      xptscandone(struct cam_periph *periph, union ccb *done_ccb);
167 static struct cam_ed *
168                  ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
169                                    lun_id_t lun_id);
170 static void      ata_device_transport(struct cam_path *path);
171 static void      ata_set_transfer_settings(struct ccb_trans_settings *cts,
172                                             struct cam_ed *device,
173                                             int async_update);
174 static void      ata_dev_async(u_int32_t async_code,
175                                 struct cam_eb *bus,
176                                 struct cam_et *target,
177                                 struct cam_ed *device,
178                                 void *async_arg);
179 static void      ata_action(union ccb *start_ccb);
180
181 static struct xpt_xport ata_xport = {
182         .alloc_device = ata_alloc_device,
183         .action = ata_action,
184         .async = ata_dev_async,
185 };
186
187 struct xpt_xport *
188 ata_get_xport(void)
189 {
190         return (&ata_xport);
191 }
192
193 static void
194 probe_periph_init()
195 {
196 }
197
198 static cam_status
199 proberegister(struct cam_periph *periph, void *arg)
200 {
201         union ccb *request_ccb; /* CCB representing the probe request */
202         cam_status status;
203         probe_softc *softc;
204
205         request_ccb = (union ccb *)arg;
206         if (periph == NULL) {
207                 printf("proberegister: periph was NULL!!\n");
208                 return(CAM_REQ_CMP_ERR);
209         }
210
211         if (request_ccb == NULL) {
212                 printf("proberegister: no probe CCB, "
213                        "can't register device\n");
214                 return(CAM_REQ_CMP_ERR);
215         }
216
217         softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
218
219         if (softc == NULL) {
220                 printf("proberegister: Unable to probe new device. "
221                        "Unable to allocate softc\n");
222                 return(CAM_REQ_CMP_ERR);
223         }
224         TAILQ_INIT(&softc->request_ccbs);
225         TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
226                           periph_links.tqe);
227         softc->flags = 0;
228         periph->softc = softc;
229         softc->periph = periph;
230         softc->action = PROBE_INVALID;
231         status = cam_periph_acquire(periph);
232         if (status != CAM_REQ_CMP) {
233                 return (status);
234         }
235         /*
236          * Ensure nobody slip in until probe finish.
237          */
238         cam_freeze_devq_arg(periph->path,
239             RELSIM_RELEASE_RUNLEVEL, CAM_RL_XPT + 1);
240         probeschedule(periph);
241         return(CAM_REQ_CMP);
242 }
243
244 static void
245 probeschedule(struct cam_periph *periph)
246 {
247         union ccb *ccb;
248         probe_softc *softc;
249
250         softc = (probe_softc *)periph->softc;
251         ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
252
253         if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
254             periph->path->device->protocol == PROTO_SATAPM)
255                 PROBE_SET_ACTION(softc, PROBE_RESET);
256         else
257                 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
258
259         if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
260                 softc->flags |= PROBE_NO_ANNOUNCE;
261         else
262                 softc->flags &= ~PROBE_NO_ANNOUNCE;
263
264         xpt_schedule(periph, CAM_PRIORITY_XPT);
265 }
266
267 static void
268 probestart(struct cam_periph *periph, union ccb *start_ccb)
269 {
270         struct ccb_trans_settings cts;
271         struct ccb_ataio *ataio;
272         struct ccb_scsiio *csio;
273         probe_softc *softc;
274         struct cam_path *path;
275         struct ata_params *ident_buf;
276
277         CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
278
279         softc = (probe_softc *)periph->softc;
280         path = start_ccb->ccb_h.path;
281         ataio = &start_ccb->ataio;
282         csio = &start_ccb->csio;
283         ident_buf = &periph->path->device->ident_data;
284
285         if (softc->restart) {
286                 softc->restart = 0;
287                 if ((path->device->flags & CAM_DEV_UNCONFIGURED) ||
288                     path->device->protocol == PROTO_SATAPM)
289                         softc->action = PROBE_RESET;
290                 else
291                         softc->action = PROBE_IDENTIFY;
292         }
293         switch (softc->action) {
294         case PROBE_RESET:
295                 cam_fill_ataio(ataio,
296                       0,
297                       probedone,
298                       /*flags*/CAM_DIR_NONE,
299                       0,
300                       /*data_ptr*/NULL,
301                       /*dxfer_len*/0,
302                       15 * 1000);
303                 ata_reset_cmd(ataio);
304                 break;
305         case PROBE_IDENTIFY:
306                 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
307                         /* Prepare check that it is the same device. */
308                         MD5_CTX context;
309
310                         MD5Init(&context);
311                         MD5Update(&context,
312                             (unsigned char *)ident_buf->model,
313                             sizeof(ident_buf->model));
314                         MD5Update(&context,
315                             (unsigned char *)ident_buf->revision,
316                             sizeof(ident_buf->revision));
317                         MD5Update(&context,
318                             (unsigned char *)ident_buf->serial,
319                             sizeof(ident_buf->serial));
320                         MD5Final(softc->digest, &context);
321                 }
322                 cam_fill_ataio(ataio,
323                       1,
324                       probedone,
325                       /*flags*/CAM_DIR_IN,
326                       0,
327                       /*data_ptr*/(u_int8_t *)ident_buf,
328                       /*dxfer_len*/sizeof(struct ata_params),
329                       30 * 1000);
330                 if (periph->path->device->protocol == PROTO_ATA)
331                         ata_28bit_cmd(ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
332                 else
333                         ata_28bit_cmd(ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
334                 break;
335         case PROBE_SETMODE:
336         {
337                 int mode, wantmode;
338
339                 mode = 0;
340                 /* Fetch user modes from SIM. */
341                 bzero(&cts, sizeof(cts));
342                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
343                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
344                 cts.type = CTS_TYPE_USER_SETTINGS;
345                 xpt_action((union ccb *)&cts);
346                 if (path->device->transport == XPORT_ATA) {
347                         if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
348                                 mode = cts.xport_specific.ata.mode;
349                 } else {
350                         if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
351                                 mode = cts.xport_specific.sata.mode;
352                 }
353 negotiate:
354                 /* Honor device capabilities. */
355                 wantmode = mode = ata_max_mode(ident_buf, mode);
356                 /* Report modes to SIM. */
357                 bzero(&cts, sizeof(cts));
358                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
359                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
360                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
361                 if (path->device->transport == XPORT_ATA) {
362                         cts.xport_specific.ata.mode = mode;
363                         cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
364                 } else {
365                         cts.xport_specific.sata.mode = mode;
366                         cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
367                 }
368                 xpt_action((union ccb *)&cts);
369                 /* Fetch current modes from SIM. */
370                 bzero(&cts, sizeof(cts));
371                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
372                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
373                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
374                 xpt_action((union ccb *)&cts);
375                 if (path->device->transport == XPORT_ATA) {
376                         if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
377                                 mode = cts.xport_specific.ata.mode;
378                 } else {
379                         if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
380                                 mode = cts.xport_specific.sata.mode;
381                 }
382                 /* If SIM disagree - renegotiate. */
383                 if (mode != wantmode)
384                         goto negotiate;
385                 cam_fill_ataio(ataio,
386                       1,
387                       probedone,
388                       /*flags*/CAM_DIR_NONE,
389                       0,
390                       /*data_ptr*/NULL,
391                       /*dxfer_len*/0,
392                       30 * 1000);
393                 ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
394                 break;
395         }
396         case PROBE_SET_MULTI:
397         {
398                 u_int sectors, bytecount;
399
400                 bytecount = 8192;       /* SATA maximum */
401                 /* Fetch user bytecount from SIM. */
402                 bzero(&cts, sizeof(cts));
403                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
404                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
405                 cts.type = CTS_TYPE_USER_SETTINGS;
406                 xpt_action((union ccb *)&cts);
407                 if (path->device->transport == XPORT_ATA) {
408                         if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
409                                 bytecount = cts.xport_specific.ata.bytecount;
410                 } else {
411                         if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
412                                 bytecount = cts.xport_specific.sata.bytecount;
413                 }
414                 /* Honor device capabilities. */
415                 sectors = max(1, min(ident_buf->sectors_intr & 0xff,
416                     bytecount / ata_logical_sector_size(ident_buf)));
417                 /* Report bytecount to SIM. */
418                 bzero(&cts, sizeof(cts));
419                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
420                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
421                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
422                 if (path->device->transport == XPORT_ATA) {
423                         cts.xport_specific.ata.bytecount = sectors *
424                             ata_logical_sector_size(ident_buf);
425                         cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
426                 } else {
427                         cts.xport_specific.sata.bytecount = sectors *
428                             ata_logical_sector_size(ident_buf);
429                         cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
430                 }
431                 xpt_action((union ccb *)&cts);
432                 /* Fetch current bytecount from SIM. */
433                 bzero(&cts, sizeof(cts));
434                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
435                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
436                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
437                 xpt_action((union ccb *)&cts);
438                 if (path->device->transport == XPORT_ATA) {
439                         if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
440                                 bytecount = cts.xport_specific.ata.bytecount;
441                 } else {
442                         if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
443                                 bytecount = cts.xport_specific.sata.bytecount;
444                 }
445                 sectors = bytecount / ata_logical_sector_size(ident_buf);
446
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_SET_MULTI, 0, 0, sectors);
456                 break;
457         }
458         case PROBE_INQUIRY:
459         {
460                 u_int bytecount;
461
462                 bytecount = 8192;       /* SATA maximum */
463                 /* Fetch user bytecount from SIM. */
464                 bzero(&cts, sizeof(cts));
465                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
466                 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
467                 cts.type = CTS_TYPE_USER_SETTINGS;
468                 xpt_action((union ccb *)&cts);
469                 if (path->device->transport == XPORT_ATA) {
470                         if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
471                                 bytecount = cts.xport_specific.ata.bytecount;
472                 } else {
473                         if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
474                                 bytecount = cts.xport_specific.sata.bytecount;
475                 }
476                 /* Honor device capabilities. */
477                 bytecount &= ~1;
478                 bytecount = max(2, min(65534, bytecount));
479                 if (ident_buf->satacapabilities != 0x0000 &&
480                     ident_buf->satacapabilities != 0xffff) {
481                         bytecount = min(8192, bytecount);
482                 }
483                 /* Report bytecount to SIM. */
484                 bzero(&cts, sizeof(cts));
485                 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
486                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
487                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
488                 if (path->device->transport == XPORT_ATA) {
489                         cts.xport_specific.ata.bytecount = bytecount;
490                         cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
491                 } else {
492                         cts.xport_specific.sata.bytecount = bytecount;
493                         cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
494                 }
495                 xpt_action((union ccb *)&cts);
496                 /* FALLTHROUGH */
497         }
498         case PROBE_FULL_INQUIRY:
499         {
500                 u_int inquiry_len;
501                 struct scsi_inquiry_data *inq_buf =
502                     &periph->path->device->inq_data;
503
504                 if (softc->action == PROBE_INQUIRY)
505                         inquiry_len = SHORT_INQUIRY_LENGTH;
506                 else
507                         inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
508                 /*
509                  * Some parallel SCSI devices fail to send an
510                  * ignore wide residue message when dealing with
511                  * odd length inquiry requests.  Round up to be
512                  * safe.
513                  */
514                 inquiry_len = roundup2(inquiry_len, 2);
515                 scsi_inquiry(csio,
516                              /*retries*/1,
517                              probedone,
518                              MSG_SIMPLE_Q_TAG,
519                              (u_int8_t *)inq_buf,
520                              inquiry_len,
521                              /*evpd*/FALSE,
522                              /*page_code*/0,
523                              SSD_MIN_SIZE,
524                              /*timeout*/60 * 1000);
525                 break;
526         }
527         case PROBE_PM_PID:
528                 cam_fill_ataio(ataio,
529                       1,
530                       probedone,
531                       /*flags*/CAM_DIR_NONE,
532                       0,
533                       /*data_ptr*/NULL,
534                       /*dxfer_len*/0,
535                       10 * 1000);
536                 ata_pm_read_cmd(ataio, 0, 15);
537                 break;
538         case PROBE_PM_PRV:
539                 cam_fill_ataio(ataio,
540                       1,
541                       probedone,
542                       /*flags*/CAM_DIR_NONE,
543                       0,
544                       /*data_ptr*/NULL,
545                       /*dxfer_len*/0,
546                       10 * 1000);
547                 ata_pm_read_cmd(ataio, 1, 15);
548                 break;
549         case PROBE_INVALID:
550                 CAM_DEBUG(path, CAM_DEBUG_INFO,
551                     ("probestart: invalid action state\n"));
552         default:
553                 break;
554         }
555         xpt_action(start_ccb);
556 }
557 #if 0
558 static void
559 proberequestdefaultnegotiation(struct cam_periph *periph)
560 {
561         struct ccb_trans_settings cts;
562
563         xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
564         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
565         cts.type = CTS_TYPE_USER_SETTINGS;
566         xpt_action((union ccb *)&cts);
567         if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
568                 return;
569         }
570         cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
571         cts.type = CTS_TYPE_CURRENT_SETTINGS;
572         xpt_action((union ccb *)&cts);
573 }
574
575 /*
576  * Backoff Negotiation Code- only pertinent for SPI devices.
577  */
578 static int
579 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
580 {
581         struct ccb_trans_settings cts;
582         struct ccb_trans_settings_spi *spi;
583
584         memset(&cts, 0, sizeof (cts));
585         xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
586         cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
587         cts.type = CTS_TYPE_CURRENT_SETTINGS;
588         xpt_action((union ccb *)&cts);
589         if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
590                 if (bootverbose) {
591                         xpt_print(periph->path,
592                             "failed to get current device settings\n");
593                 }
594                 return (0);
595         }
596         if (cts.transport != XPORT_SPI) {
597                 if (bootverbose) {
598                         xpt_print(periph->path, "not SPI transport\n");
599                 }
600                 return (0);
601         }
602         spi = &cts.xport_specific.spi;
603
604         /*
605          * We cannot renegotiate sync rate if we don't have one.
606          */
607         if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
608                 if (bootverbose) {
609                         xpt_print(periph->path, "no sync rate known\n");
610                 }
611                 return (0);
612         }
613
614         /*
615          * We'll assert that we don't have to touch PPR options- the
616          * SIM will see what we do with period and offset and adjust
617          * the PPR options as appropriate.
618          */
619
620         /*
621          * A sync rate with unknown or zero offset is nonsensical.
622          * A sync period of zero means Async.
623          */
624         if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
625          || spi->sync_offset == 0 || spi->sync_period == 0) {
626                 if (bootverbose) {
627                         xpt_print(periph->path, "no sync rate available\n");
628                 }
629                 return (0);
630         }
631
632         if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
633                 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
634                     ("hit async: giving up on DV\n"));
635                 return (0);
636         }
637
638
639         /*
640          * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
641          * We don't try to remember 'last' settings to see if the SIM actually
642          * gets into the speed we want to set. We check on the SIM telling
643          * us that a requested speed is bad, but otherwise don't try and
644          * check the speed due to the asynchronous and handshake nature
645          * of speed setting.
646          */
647         spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
648         for (;;) {
649                 spi->sync_period++;
650                 if (spi->sync_period >= 0xf) {
651                         spi->sync_period = 0;
652                         spi->sync_offset = 0;
653                         CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
654                             ("setting to async for DV\n"));
655                         /*
656                          * Once we hit async, we don't want to try
657                          * any more settings.
658                          */
659                         device->flags |= CAM_DEV_DV_HIT_BOTTOM;
660                 } else if (bootverbose) {
661                         CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
662                             ("DV: period 0x%x\n", spi->sync_period));
663                         printf("setting period to 0x%x\n", spi->sync_period);
664                 }
665                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
666                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
667                 xpt_action((union ccb *)&cts);
668                 if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
669                         break;
670                 }
671                 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
672                     ("DV: failed to set period 0x%x\n", spi->sync_period));
673                 if (spi->sync_period == 0) {
674                         return (0);
675                 }
676         }
677         return (1);
678 }
679 #endif
680 static void
681 probedone(struct cam_periph *periph, union ccb *done_ccb)
682 {
683         struct ccb_trans_settings cts;
684         struct ata_params *ident_buf;
685         probe_softc *softc;
686         struct cam_path *path;
687         u_int32_t  priority;
688         int found = 1;
689
690         CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
691
692         softc = (probe_softc *)periph->softc;
693         path = done_ccb->ccb_h.path;
694         priority = done_ccb->ccb_h.pinfo.priority;
695         ident_buf = &path->device->ident_data;
696
697         if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
698 device_fail:    if (cam_periph_error(done_ccb, 0, 0,
699                     &softc->saved_ccb) == ERESTART) {
700                         return;
701                 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
702                         /* Don't wedge the queue */
703                         xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
704                                          /*run_queue*/TRUE);
705                 }
706                 /* Old PIO2 devices may not support mode setting. */
707                 if (softc->action == PROBE_SETMODE &&
708                     ata_max_pmode(ident_buf) <= ATA_PIO2 &&
709                     (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
710                         goto noerror;
711                 /*
712                  * If we get to this point, we got an error status back
713                  * from the inquiry and the error status doesn't require
714                  * automatically retrying the command.  Therefore, the
715                  * inquiry failed.  If we had inquiry information before
716                  * for this device, but this latest inquiry command failed,
717                  * the device has probably gone away.  If this device isn't
718                  * already marked unconfigured, notify the peripheral
719                  * drivers that this device is no more.
720                  */
721                 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
722                         xpt_async(AC_LOST_DEVICE, path, NULL);
723                 found = 0;
724                 goto done;
725         }
726 noerror:
727         switch (softc->action) {
728         case PROBE_RESET:
729         {
730                 int sign = (done_ccb->ataio.res.lba_high << 8) +
731                     done_ccb->ataio.res.lba_mid;
732                 if (bootverbose)
733                         xpt_print(path, "SIGNATURE: %04x\n", sign);
734                 if (sign == 0x0000 &&
735                     done_ccb->ccb_h.target_id != 15) {
736                         path->device->protocol = PROTO_ATA;
737                         PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
738                 } else if (sign == 0x9669 &&
739                     done_ccb->ccb_h.target_id == 15) {
740                         /* Report SIM that PM is present. */
741                         bzero(&cts, sizeof(cts));
742                         xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
743                         cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
744                         cts.type = CTS_TYPE_CURRENT_SETTINGS;
745                         cts.xport_specific.sata.pm_present = 1;
746                         cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
747                         xpt_action((union ccb *)&cts);
748                         path->device->protocol = PROTO_SATAPM;
749                         PROBE_SET_ACTION(softc, PROBE_PM_PID);
750                 } else if (sign == 0xeb14 &&
751                     done_ccb->ccb_h.target_id != 15) {
752                         path->device->protocol = PROTO_SCSI;
753                         PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
754                 } else {
755                         if (done_ccb->ccb_h.target_id != 15) {
756                                 xpt_print(path,
757                                     "Unexpected signature 0x%04x\n", sign);
758                         }
759                         goto device_fail;
760                 }
761                 xpt_release_ccb(done_ccb);
762                 xpt_schedule(periph, priority);
763                 return;
764         }
765         case PROBE_IDENTIFY:
766         {
767                 int16_t *ptr;
768
769                 for (ptr = (int16_t *)ident_buf;
770                      ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
771                         *ptr = le16toh(*ptr);
772                 }
773                 if (strncmp(ident_buf->model, "FX", 2) &&
774                     strncmp(ident_buf->model, "NEC", 3) &&
775                     strncmp(ident_buf->model, "Pioneer", 7) &&
776                     strncmp(ident_buf->model, "SHARP", 5)) {
777                         ata_bswap(ident_buf->model, sizeof(ident_buf->model));
778                         ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
779                         ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
780                 }
781                 ata_btrim(ident_buf->model, sizeof(ident_buf->model));
782                 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
783                 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
784                 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
785                 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
786                 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
787
788                 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
789                         /* Check that it is the same device. */
790                         MD5_CTX context;
791                         u_int8_t digest[16];
792
793                         MD5Init(&context);
794                         MD5Update(&context,
795                             (unsigned char *)ident_buf->model,
796                             sizeof(ident_buf->model));
797                         MD5Update(&context,
798                             (unsigned char *)ident_buf->revision,
799                             sizeof(ident_buf->revision));
800                         MD5Update(&context,
801                             (unsigned char *)ident_buf->serial,
802                             sizeof(ident_buf->serial));
803                         MD5Final(digest, &context);
804                         if (bcmp(digest, softc->digest, sizeof(digest))) {
805                                 /* Device changed. */
806                                 xpt_async(AC_LOST_DEVICE, path, NULL);
807                         }
808                 } else {
809                         /* Clean up from previous instance of this device */
810                         if (path->device->serial_num != NULL) {
811                                 free(path->device->serial_num, M_CAMXPT);
812                                 path->device->serial_num = NULL;
813                                 path->device->serial_num_len = 0;
814                         }
815                         path->device->serial_num =
816                                 (u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
817                                            M_CAMXPT, M_NOWAIT);
818                         if (path->device->serial_num != NULL) {
819                                 bcopy(ident_buf->serial,
820                                       path->device->serial_num,
821                                       sizeof(ident_buf->serial));
822                                 path->device->serial_num[sizeof(ident_buf->serial)]
823                                     = '\0';
824                                 path->device->serial_num_len =
825                                     strlen(path->device->serial_num);
826                         }
827
828                         path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
829                 }
830                 if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
831                         path->device->mintags = path->device->maxtags =
832                             ATA_QUEUE_LEN(ident_buf->queue) + 1;
833                 }
834                 ata_find_quirk(path->device);
835                 if (path->device->mintags != 0 &&
836                     path->bus->sim->max_tagged_dev_openings != 0) {
837                         /* Report SIM which tags are allowed. */
838                         bzero(&cts, sizeof(cts));
839                         xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
840                         cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
841                         cts.type = CTS_TYPE_CURRENT_SETTINGS;
842                         cts.xport_specific.sata.tags = path->device->maxtags;
843                         cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
844                         xpt_action((union ccb *)&cts);
845                         /* Reconfigure queues for tagged queueing. */
846                         xpt_start_tags(path);
847                 }
848                 ata_device_transport(path);
849                 PROBE_SET_ACTION(softc, PROBE_SETMODE);
850                 xpt_release_ccb(done_ccb);
851                 xpt_schedule(periph, priority);
852                 return;
853         }
854         case PROBE_SETMODE:
855                 if (path->device->protocol == PROTO_ATA) {
856                         PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
857                 } else {
858                         PROBE_SET_ACTION(softc, PROBE_INQUIRY);
859                 }
860                 xpt_release_ccb(done_ccb);
861                 xpt_schedule(periph, priority);
862                 return;
863         case PROBE_SET_MULTI:
864                 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
865                         path->device->flags &= ~CAM_DEV_UNCONFIGURED;
866                         xpt_acquire_device(path->device);
867                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
868                         xpt_action(done_ccb);
869                         xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
870                             done_ccb);
871                 }
872                 break;
873         case PROBE_INQUIRY:
874         case PROBE_FULL_INQUIRY:
875         {
876                 struct scsi_inquiry_data *inq_buf;
877                 u_int8_t periph_qual, len;
878
879                 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
880                 inq_buf = &path->device->inq_data;
881
882                 periph_qual = SID_QUAL(inq_buf);
883
884                 if (periph_qual != SID_QUAL_LU_CONNECTED)
885                         break;
886
887                 /*
888                  * We conservatively request only
889                  * SHORT_INQUIRY_LEN bytes of inquiry
890                  * information during our first try
891                  * at sending an INQUIRY. If the device
892                  * has more information to give,
893                  * perform a second request specifying
894                  * the amount of information the device
895                  * is willing to give.
896                  */
897                 len = inq_buf->additional_length
898                     + offsetof(struct scsi_inquiry_data, additional_length) + 1;
899                 if (softc->action == PROBE_INQUIRY
900                     && len > SHORT_INQUIRY_LENGTH) {
901                         PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
902                         xpt_release_ccb(done_ccb);
903                         xpt_schedule(periph, priority);
904                         return;
905                 }
906
907                 ata_device_transport(path);
908                 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
909                         path->device->flags &= ~CAM_DEV_UNCONFIGURED;
910                         xpt_acquire_device(path->device);
911                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
912                         xpt_action(done_ccb);
913                         xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
914                 }
915                 break;
916         }
917         case PROBE_PM_PID:
918                 if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
919                         bzero(ident_buf, sizeof(*ident_buf));
920                 softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
921                     (done_ccb->ataio.res.lba_mid << 16) +
922                     (done_ccb->ataio.res.lba_low << 8) +
923                     done_ccb->ataio.res.sector_count;
924                 ((uint32_t *)ident_buf)[0] = softc->pm_pid;
925                 snprintf(ident_buf->model, sizeof(ident_buf->model),
926                     "Port Multiplier %08x", softc->pm_pid);
927                 PROBE_SET_ACTION(softc, PROBE_PM_PRV);
928                 xpt_release_ccb(done_ccb);
929                 xpt_schedule(periph, priority);
930                 return;
931         case PROBE_PM_PRV:
932                 softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
933                     (done_ccb->ataio.res.lba_mid << 16) +
934                     (done_ccb->ataio.res.lba_low << 8) +
935                     done_ccb->ataio.res.sector_count;
936                 ((uint32_t *)ident_buf)[1] = softc->pm_prv;
937                 snprintf(ident_buf->revision, sizeof(ident_buf->revision),
938                     "%04x", softc->pm_prv);
939                 path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
940                 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
941                         path->device->flags &= ~CAM_DEV_UNCONFIGURED;
942                         xpt_acquire_device(path->device);
943                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
944                         xpt_action(done_ccb);
945                         xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
946                             done_ccb);
947                 } else {
948                         done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
949                         xpt_action(done_ccb);
950                         xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
951                 }
952                 break;
953         case PROBE_INVALID:
954                 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
955                     ("probedone: invalid action state\n"));
956         default:
957                 break;
958         }
959 done:
960         if (softc->restart) {
961                 softc->restart = 0;
962                 xpt_release_ccb(done_ccb);
963                 probeschedule(periph);
964                 return;
965         }
966         xpt_release_ccb(done_ccb);
967         while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
968                 TAILQ_REMOVE(&softc->request_ccbs,
969                     &done_ccb->ccb_h, periph_links.tqe);
970                 done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
971                 xpt_done(done_ccb);
972         }
973         cam_release_devq(periph->path,
974             RELSIM_RELEASE_RUNLEVEL, 0, CAM_RL_XPT + 1, FALSE);
975         cam_periph_invalidate(periph);
976         cam_periph_release_locked(periph);
977 }
978
979 static void
980 probecleanup(struct cam_periph *periph)
981 {
982         free(periph->softc, M_CAMXPT);
983 }
984
985 static void
986 ata_find_quirk(struct cam_ed *device)
987 {
988         struct ata_quirk_entry *quirk;
989         caddr_t match;
990
991         match = cam_quirkmatch((caddr_t)&device->ident_data,
992                                (caddr_t)ata_quirk_table,
993                                ata_quirk_table_size,
994                                sizeof(*ata_quirk_table), ata_identify_match);
995
996         if (match == NULL)
997                 panic("xpt_find_quirk: device didn't match wildcard entry!!");
998
999         quirk = (struct ata_quirk_entry *)match;
1000         device->quirk = quirk;
1001         if (quirk->quirks & CAM_QUIRK_MAXTAGS)
1002                 device->mintags = device->maxtags = quirk->maxtags;
1003 }
1004
1005 typedef struct {
1006         union   ccb *request_ccb;
1007         struct  ccb_pathinq *cpi;
1008         int     counter;
1009 } ata_scan_bus_info;
1010
1011 /*
1012  * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1013  * As the scan progresses, xpt_scan_bus is used as the
1014  * callback on completion function.
1015  */
1016 static void
1017 ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1018 {
1019         struct  cam_path *path;
1020         ata_scan_bus_info *scan_info;
1021         union   ccb *work_ccb, *reset_ccb;
1022         cam_status status;
1023
1024         CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1025                   ("xpt_scan_bus\n"));
1026         switch (request_ccb->ccb_h.func_code) {
1027         case XPT_SCAN_BUS:
1028                 /* Find out the characteristics of the bus */
1029                 work_ccb = xpt_alloc_ccb_nowait();
1030                 if (work_ccb == NULL) {
1031                         request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1032                         xpt_done(request_ccb);
1033                         return;
1034                 }
1035                 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1036                               request_ccb->ccb_h.pinfo.priority);
1037                 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1038                 xpt_action(work_ccb);
1039                 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1040                         request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1041                         xpt_free_ccb(work_ccb);
1042                         xpt_done(request_ccb);
1043                         return;
1044                 }
1045
1046                 /* We may need to reset bus first, if we haven't done it yet. */
1047                 if ((work_ccb->cpi.hba_inquiry &
1048                     (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1049                     !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1050                     !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1051                         reset_ccb = xpt_alloc_ccb_nowait();
1052                         xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1053                               CAM_PRIORITY_NONE);
1054                         reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1055                         xpt_action(reset_ccb);
1056                         if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1057                                 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1058                                 xpt_free_ccb(reset_ccb);
1059                                 xpt_free_ccb(work_ccb);
1060                                 xpt_done(request_ccb);
1061                                 return;
1062                         }
1063                         xpt_free_ccb(reset_ccb);
1064                 }
1065
1066                 /* Save some state for use while we probe for devices */
1067                 scan_info = (ata_scan_bus_info *)
1068                     malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1069                 if (scan_info == NULL) {
1070                         request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1071                         xpt_done(request_ccb);
1072                         return;
1073                 }
1074                 scan_info->request_ccb = request_ccb;
1075                 scan_info->cpi = &work_ccb->cpi;
1076                 /* If PM supported, probe it first. */
1077                 if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1078                         scan_info->counter = scan_info->cpi->max_target;
1079                 else
1080                         scan_info->counter = 0;
1081
1082                 work_ccb = xpt_alloc_ccb_nowait();
1083                 if (work_ccb == NULL) {
1084                         free(scan_info, M_CAMXPT);
1085                         request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1086                         xpt_done(request_ccb);
1087                         break;
1088                 }
1089                 goto scan_next;
1090         case XPT_SCAN_LUN:
1091                 work_ccb = request_ccb;
1092                 /* Reuse the same CCB to query if a device was really found */
1093                 scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1094                 /* Free the current request path- we're done with it. */
1095                 xpt_free_path(work_ccb->ccb_h.path);
1096                 /* If there is PMP... */
1097                 if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1098                     (scan_info->counter == scan_info->cpi->max_target)) {
1099                         if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1100                                 /* everything else willbe probed by it */
1101                                 goto done;
1102                         } else {
1103                                 struct ccb_trans_settings cts;
1104
1105                                 /* Report SIM that PM is absent. */
1106                                 bzero(&cts, sizeof(cts));
1107                                 xpt_setup_ccb(&cts.ccb_h,
1108                                     scan_info->request_ccb->ccb_h.path, 1);
1109                                 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1110                                 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1111                                 cts.xport_specific.sata.pm_present = 0;
1112                                 cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1113                                 xpt_action((union ccb *)&cts);
1114                         }
1115                 }
1116                 if (scan_info->counter ==
1117                     ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1118                     0 : scan_info->cpi->max_target)) {
1119 done:
1120                         xpt_free_ccb(work_ccb);
1121                         xpt_free_ccb((union ccb *)scan_info->cpi);
1122                         request_ccb = scan_info->request_ccb;
1123                         free(scan_info, M_CAMXPT);
1124                         request_ccb->ccb_h.status = CAM_REQ_CMP;
1125                         xpt_done(request_ccb);
1126                         break;
1127                 }
1128                 /* Take next device. Wrap from max (PMP) to 0. */
1129                 scan_info->counter = (scan_info->counter + 1 ) %
1130                     (scan_info->cpi->max_target + 1);
1131 scan_next:
1132                 status = xpt_create_path(&path, xpt_periph,
1133                     scan_info->request_ccb->ccb_h.path_id,
1134                     scan_info->counter, 0);
1135                 if (status != CAM_REQ_CMP) {
1136                         printf("xpt_scan_bus: xpt_create_path failed"
1137                             " with status %#x, bus scan halted\n",
1138                             status);
1139                         xpt_free_ccb(work_ccb);
1140                         xpt_free_ccb((union ccb *)scan_info->cpi);
1141                         request_ccb = scan_info->request_ccb;
1142                         free(scan_info, M_CAMXPT);
1143                         request_ccb->ccb_h.status = status;
1144                         xpt_done(request_ccb);
1145                         break;
1146                 }
1147                 xpt_setup_ccb(&work_ccb->ccb_h, path,
1148                     scan_info->request_ccb->ccb_h.pinfo.priority);
1149                 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1150                 work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1151                 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1152                 work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1153                 xpt_action(work_ccb);
1154                 break;
1155         default:
1156                 break;
1157         }
1158 }
1159
1160 static void
1161 ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1162              cam_flags flags, union ccb *request_ccb)
1163 {
1164         struct ccb_pathinq cpi;
1165         cam_status status;
1166         struct cam_path *new_path;
1167         struct cam_periph *old_periph;
1168
1169         CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1170
1171         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1172         cpi.ccb_h.func_code = XPT_PATH_INQ;
1173         xpt_action((union ccb *)&cpi);
1174
1175         if (cpi.ccb_h.status != CAM_REQ_CMP) {
1176                 if (request_ccb != NULL) {
1177                         request_ccb->ccb_h.status = cpi.ccb_h.status;
1178                         xpt_done(request_ccb);
1179                 }
1180                 return;
1181         }
1182
1183         if (request_ccb == NULL) {
1184                 request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1185                 if (request_ccb == NULL) {
1186                         xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1187                             "can't continue\n");
1188                         return;
1189                 }
1190                 new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1191                 if (new_path == NULL) {
1192                         xpt_print(path, "xpt_scan_lun: can't allocate path, "
1193                             "can't continue\n");
1194                         free(request_ccb, M_CAMXPT);
1195                         return;
1196                 }
1197                 status = xpt_compile_path(new_path, xpt_periph,
1198                                           path->bus->path_id,
1199                                           path->target->target_id,
1200                                           path->device->lun_id);
1201
1202                 if (status != CAM_REQ_CMP) {
1203                         xpt_print(path, "xpt_scan_lun: can't compile path, "
1204                             "can't continue\n");
1205                         free(request_ccb, M_CAMXPT);
1206                         free(new_path, M_CAMXPT);
1207                         return;
1208                 }
1209                 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1210                 request_ccb->ccb_h.cbfcnp = xptscandone;
1211                 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1212                 request_ccb->crcn.flags = flags;
1213         }
1214
1215         if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1216                 probe_softc *softc;
1217
1218                 softc = (probe_softc *)old_periph->softc;
1219                 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1220                                   periph_links.tqe);
1221                 softc->restart = 1;
1222         } else {
1223                 status = cam_periph_alloc(proberegister, NULL, probecleanup,
1224                                           probestart, "aprobe",
1225                                           CAM_PERIPH_BIO,
1226                                           request_ccb->ccb_h.path, NULL, 0,
1227                                           request_ccb);
1228
1229                 if (status != CAM_REQ_CMP) {
1230                         xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1231                             "returned an error, can't continue probe\n");
1232                         request_ccb->ccb_h.status = status;
1233                         xpt_done(request_ccb);
1234                 }
1235         }
1236 }
1237
1238 static void
1239 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1240 {
1241         xpt_release_path(done_ccb->ccb_h.path);
1242         free(done_ccb->ccb_h.path, M_CAMXPT);
1243         free(done_ccb, M_CAMXPT);
1244 }
1245
1246 static struct cam_ed *
1247 ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1248 {
1249         struct cam_path path;
1250         struct ata_quirk_entry *quirk;
1251         struct cam_ed *device;
1252         struct cam_ed *cur_device;
1253
1254         device = xpt_alloc_device(bus, target, lun_id);
1255         if (device == NULL)
1256                 return (NULL);
1257
1258         /*
1259          * Take the default quirk entry until we have inquiry
1260          * data and can determine a better quirk to use.
1261          */
1262         quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1263         device->quirk = (void *)quirk;
1264         device->mintags = 0;
1265         device->maxtags = 0;
1266         bzero(&device->inq_data, sizeof(device->inq_data));
1267         device->inq_flags = 0;
1268         device->queue_flags = 0;
1269         device->serial_num = NULL;
1270         device->serial_num_len = 0;
1271
1272         /*
1273          * XXX should be limited by number of CCBs this bus can
1274          * do.
1275          */
1276         bus->sim->max_ccbs += device->ccbq.devq_openings;
1277         /* Insertion sort into our target's device list */
1278         cur_device = TAILQ_FIRST(&target->ed_entries);
1279         while (cur_device != NULL && cur_device->lun_id < lun_id)
1280                 cur_device = TAILQ_NEXT(cur_device, links);
1281         if (cur_device != NULL) {
1282                 TAILQ_INSERT_BEFORE(cur_device, device, links);
1283         } else {
1284                 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1285         }
1286         target->generation++;
1287         if (lun_id != CAM_LUN_WILDCARD) {
1288                 xpt_compile_path(&path,
1289                                  NULL,
1290                                  bus->path_id,
1291                                  target->target_id,
1292                                  lun_id);
1293                 ata_device_transport(&path);
1294                 xpt_release_path(&path);
1295         }
1296
1297         return (device);
1298 }
1299
1300 static void
1301 ata_device_transport(struct cam_path *path)
1302 {
1303         struct ccb_pathinq cpi;
1304         struct ccb_trans_settings cts;
1305         struct scsi_inquiry_data *inq_buf = NULL;
1306         struct ata_params *ident_buf = NULL;
1307
1308         /* Get transport information from the SIM */
1309         xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1310         cpi.ccb_h.func_code = XPT_PATH_INQ;
1311         xpt_action((union ccb *)&cpi);
1312
1313         path->device->transport = cpi.transport;
1314         if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1315                 inq_buf = &path->device->inq_data;
1316         if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1317                 ident_buf = &path->device->ident_data;
1318         if (path->device->protocol == PROTO_ATA) {
1319                 path->device->protocol_version = ident_buf ?
1320                     ata_version(ident_buf->version_major) : cpi.protocol_version;
1321         } else if (path->device->protocol == PROTO_SCSI) {
1322                 path->device->protocol_version = inq_buf ?
1323                     SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1324         }
1325         path->device->transport_version = ident_buf ?
1326             ata_version(ident_buf->version_major) : cpi.transport_version;
1327
1328         /* Tell the controller what we think */
1329         xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1330         cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1331         cts.type = CTS_TYPE_CURRENT_SETTINGS;
1332         cts.transport = path->device->transport;
1333         cts.transport_version = path->device->transport_version;
1334         cts.protocol = path->device->protocol;
1335         cts.protocol_version = path->device->protocol_version;
1336         cts.proto_specific.valid = 0;
1337         if (ident_buf) {
1338                 if (path->device->transport == XPORT_ATA) {
1339                         cts.xport_specific.ata.atapi = 
1340                             ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1341                             ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1342                         cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1343                 } else {
1344                         cts.xport_specific.sata.atapi = 
1345                             ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1346                             ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1347                         cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1348                 }
1349         } else
1350                 cts.xport_specific.valid = 0;
1351         xpt_action((union ccb *)&cts);
1352 }
1353
1354 static void
1355 ata_action(union ccb *start_ccb)
1356 {
1357
1358         switch (start_ccb->ccb_h.func_code) {
1359         case XPT_SET_TRAN_SETTINGS:
1360         {
1361                 ata_set_transfer_settings(&start_ccb->cts,
1362                                            start_ccb->ccb_h.path->device,
1363                                            /*async_update*/FALSE);
1364                 break;
1365         }
1366         case XPT_SCAN_BUS:
1367                 ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1368                 break;
1369         case XPT_SCAN_LUN:
1370                 ata_scan_lun(start_ccb->ccb_h.path->periph,
1371                               start_ccb->ccb_h.path, start_ccb->crcn.flags,
1372                               start_ccb);
1373                 break;
1374         case XPT_GET_TRAN_SETTINGS:
1375         {
1376                 struct cam_sim *sim;
1377
1378                 sim = start_ccb->ccb_h.path->bus->sim;
1379                 (*(sim->sim_action))(sim, start_ccb);
1380                 break;
1381         }
1382         case XPT_SCSI_IO:
1383         {
1384                 struct cam_ed *device;
1385                 u_int   maxlen = 0;
1386
1387                 device = start_ccb->ccb_h.path->device;
1388                 if (device->protocol == PROTO_SCSI &&
1389                     (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1390                         uint16_t p =
1391                             device->ident_data.config & ATA_PROTO_MASK;
1392
1393                         maxlen = (p == ATA_PROTO_ATAPI_16) ? 16 :
1394                             (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1395                 }
1396                 if (start_ccb->csio.cdb_len > maxlen) {
1397                         start_ccb->ccb_h.status = CAM_REQ_INVALID;
1398                         xpt_done(start_ccb);
1399                         break;
1400                 }
1401                 /* FALLTHROUGH */
1402         }
1403         default:
1404                 xpt_action_default(start_ccb);
1405                 break;
1406         }
1407 }
1408
1409 static void
1410 ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1411                            int async_update)
1412 {
1413         struct  ccb_pathinq cpi;
1414         struct  ccb_trans_settings cur_cts;
1415         struct  ccb_trans_settings_scsi *scsi;
1416         struct  ccb_trans_settings_scsi *cur_scsi;
1417         struct  cam_sim *sim;
1418         struct  scsi_inquiry_data *inq_data;
1419
1420         if (device == NULL) {
1421                 cts->ccb_h.status = CAM_PATH_INVALID;
1422                 xpt_done((union ccb *)cts);
1423                 return;
1424         }
1425
1426         if (cts->protocol == PROTO_UNKNOWN
1427          || cts->protocol == PROTO_UNSPECIFIED) {
1428                 cts->protocol = device->protocol;
1429                 cts->protocol_version = device->protocol_version;
1430         }
1431
1432         if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1433          || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1434                 cts->protocol_version = device->protocol_version;
1435
1436         if (cts->protocol != device->protocol) {
1437                 xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1438                        cts->protocol, device->protocol);
1439                 cts->protocol = device->protocol;
1440         }
1441
1442         if (cts->protocol_version > device->protocol_version) {
1443                 if (bootverbose) {
1444                         xpt_print(cts->ccb_h.path, "Down reving Protocol "
1445                             "Version from %d to %d?\n", cts->protocol_version,
1446                             device->protocol_version);
1447                 }
1448                 cts->protocol_version = device->protocol_version;
1449         }
1450
1451         if (cts->transport == XPORT_UNKNOWN
1452          || cts->transport == XPORT_UNSPECIFIED) {
1453                 cts->transport = device->transport;
1454                 cts->transport_version = device->transport_version;
1455         }
1456
1457         if (cts->transport_version == XPORT_VERSION_UNKNOWN
1458          || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1459                 cts->transport_version = device->transport_version;
1460
1461         if (cts->transport != device->transport) {
1462                 xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1463                     cts->transport, device->transport);
1464                 cts->transport = device->transport;
1465         }
1466
1467         if (cts->transport_version > device->transport_version) {
1468                 if (bootverbose) {
1469                         xpt_print(cts->ccb_h.path, "Down reving Transport "
1470                             "Version from %d to %d?\n", cts->transport_version,
1471                             device->transport_version);
1472                 }
1473                 cts->transport_version = device->transport_version;
1474         }
1475
1476         sim = cts->ccb_h.path->bus->sim;
1477
1478         /*
1479          * Nothing more of interest to do unless
1480          * this is a device connected via the
1481          * SCSI protocol.
1482          */
1483         if (cts->protocol != PROTO_SCSI) {
1484                 if (async_update == FALSE)
1485                         (*(sim->sim_action))(sim, (union ccb *)cts);
1486                 return;
1487         }
1488
1489         inq_data = &device->inq_data;
1490         scsi = &cts->proto_specific.scsi;
1491         xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1492         cpi.ccb_h.func_code = XPT_PATH_INQ;
1493         xpt_action((union ccb *)&cpi);
1494
1495         /* SCSI specific sanity checking */
1496         if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1497          || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1498          || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1499          || (device->mintags == 0)) {
1500                 /*
1501                  * Can't tag on hardware that doesn't support tags,
1502                  * doesn't have it enabled, or has broken tag support.
1503                  */
1504                 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1505         }
1506
1507         if (async_update == FALSE) {
1508                 /*
1509                  * Perform sanity checking against what the
1510                  * controller and device can do.
1511                  */
1512                 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NONE);
1513                 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1514                 cur_cts.type = cts->type;
1515                 xpt_action((union ccb *)&cur_cts);
1516                 if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1517                         return;
1518                 }
1519                 cur_scsi = &cur_cts.proto_specific.scsi;
1520                 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1521                         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1522                         scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1523                 }
1524                 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1525                         scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1526         }
1527
1528         if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1529          && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1530                 int device_tagenb;
1531
1532                 /*
1533                  * If we are transitioning from tags to no-tags or
1534                  * vice-versa, we need to carefully freeze and restart
1535                  * the queue so that we don't overlap tagged and non-tagged
1536                  * commands.  We also temporarily stop tags if there is
1537                  * a change in transfer negotiation settings to allow
1538                  * "tag-less" negotiation.
1539                  */
1540                 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1541                  || (device->inq_flags & SID_CmdQue) != 0)
1542                         device_tagenb = TRUE;
1543                 else
1544                         device_tagenb = FALSE;
1545
1546                 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1547                   && device_tagenb == FALSE)
1548                  || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1549                   && device_tagenb == TRUE)) {
1550
1551                         if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1552                                 /*
1553                                  * Delay change to use tags until after a
1554                                  * few commands have gone to this device so
1555                                  * the controller has time to perform transfer
1556                                  * negotiations without tagged messages getting
1557                                  * in the way.
1558                                  */
1559                                 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1560                                 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1561                         } else {
1562                                 xpt_stop_tags(cts->ccb_h.path);
1563                         }
1564                 }
1565         }
1566         if (async_update == FALSE)
1567                 (*(sim->sim_action))(sim, (union ccb *)cts);
1568 }
1569
1570 /*
1571  * Handle any per-device event notifications that require action by the XPT.
1572  */
1573 static void
1574 ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1575               struct cam_ed *device, void *async_arg)
1576 {
1577         cam_status status;
1578         struct cam_path newpath;
1579
1580         /*
1581          * We only need to handle events for real devices.
1582          */
1583         if (target->target_id == CAM_TARGET_WILDCARD
1584          || device->lun_id == CAM_LUN_WILDCARD)
1585                 return;
1586
1587         /*
1588          * We need our own path with wildcards expanded to
1589          * handle certain types of events.
1590          */
1591         if ((async_code == AC_SENT_BDR)
1592          || (async_code == AC_BUS_RESET)
1593          || (async_code == AC_INQ_CHANGED))
1594                 status = xpt_compile_path(&newpath, NULL,
1595                                           bus->path_id,
1596                                           target->target_id,
1597                                           device->lun_id);
1598         else
1599                 status = CAM_REQ_CMP_ERR;
1600
1601         if (status == CAM_REQ_CMP) {
1602                 if (async_code == AC_INQ_CHANGED) {
1603                         /*
1604                          * We've sent a start unit command, or
1605                          * something similar to a device that
1606                          * may have caused its inquiry data to
1607                          * change. So we re-scan the device to
1608                          * refresh the inquiry data for it.
1609                          */
1610                         ata_scan_lun(newpath.periph, &newpath,
1611                                      CAM_EXPECT_INQ_CHANGE, NULL);
1612                 } else {
1613                         /* We need to reinitialize device after reset. */
1614                         ata_scan_lun(newpath.periph, &newpath,
1615                                      0, NULL);
1616                 }
1617                 xpt_release_path(&newpath);
1618         } else if (async_code == AC_LOST_DEVICE &&
1619             (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1620                 device->flags |= CAM_DEV_UNCONFIGURED;
1621                 xpt_release_device(device);
1622         } else if (async_code == AC_TRANSFER_NEG) {
1623                 struct ccb_trans_settings *settings;
1624
1625                 settings = (struct ccb_trans_settings *)async_arg;
1626                 ata_set_transfer_settings(settings, device,
1627                                           /*async_update*/TRUE);
1628         }
1629 }
1630