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