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