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