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