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