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