ata_xpt.c revision 323736
1/*-
2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/11/sys/cam/ata/ata_xpt.c 323736 2017-09-19 07:39:32Z avg $");
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 old WD SATA disks have broken SPINUP handling.
744		 * If we really fail to spin up the disk, then there will be
745		 * some media access errors later on, but at least we will
746		 * have a device to interact with for recovery attempts.
747		 */
748		} else if (softc->action == PROBE_SPINUP &&
749		    status == CAM_ATA_STATUS_ERROR) {
750			goto noerror;
751
752		/*
753		 * Some HP SATA disks report supported DMA Auto-Activation,
754		 * but return ABORT on attempt to enable it.
755		 */
756		} else if (softc->action == PROBE_SETDMAAA &&
757		    status == CAM_ATA_STATUS_ERROR) {
758			goto noerror;
759
760		/*
761		 * SES and SAF-TE SEPs have different IDENTIFY commands,
762		 * but SATA specification doesn't tell how to identify them.
763		 * Until better way found, just try another if first fail.
764		 */
765		} else if (softc->action == PROBE_IDENTIFY_SES &&
766		    status == CAM_ATA_STATUS_ERROR) {
767			PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SAFTE);
768			xpt_release_ccb(done_ccb);
769			xpt_schedule(periph, priority);
770			goto out;
771		}
772
773		/*
774		 * If we get to this point, we got an error status back
775		 * from the inquiry and the error status doesn't require
776		 * automatically retrying the command.  Therefore, the
777		 * inquiry failed.  If we had inquiry information before
778		 * for this device, but this latest inquiry command failed,
779		 * the device has probably gone away.  If this device isn't
780		 * already marked unconfigured, notify the peripheral
781		 * drivers that this device is no more.
782		 */
783device_fail:	if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
784			xpt_async(AC_LOST_DEVICE, path, NULL);
785		PROBE_SET_ACTION(softc, PROBE_INVALID);
786		found = 0;
787		goto done;
788	}
789noerror:
790	if (softc->restart)
791		goto done;
792	switch (softc->action) {
793	case PROBE_RESET:
794	{
795		int sign = (done_ccb->ataio.res.lba_high << 8) +
796		    done_ccb->ataio.res.lba_mid;
797		CAM_DEBUG(path, CAM_DEBUG_PROBE,
798		    ("SIGNATURE: %04x\n", sign));
799		if (sign == 0x0000 &&
800		    done_ccb->ccb_h.target_id != 15) {
801			path->device->protocol = PROTO_ATA;
802			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
803		} else if (sign == 0x9669 &&
804		    done_ccb->ccb_h.target_id == 15) {
805			/* Report SIM that PM is present. */
806			bzero(&cts, sizeof(cts));
807			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
808			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
809			cts.type = CTS_TYPE_CURRENT_SETTINGS;
810			cts.xport_specific.sata.pm_present = 1;
811			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
812			xpt_action((union ccb *)&cts);
813			path->device->protocol = PROTO_SATAPM;
814			PROBE_SET_ACTION(softc, PROBE_PM_PID);
815		} else if (sign == 0xc33c &&
816		    done_ccb->ccb_h.target_id != 15) {
817			path->device->protocol = PROTO_SEMB;
818			PROBE_SET_ACTION(softc, PROBE_IDENTIFY_SES);
819		} else if (sign == 0xeb14 &&
820		    done_ccb->ccb_h.target_id != 15) {
821			path->device->protocol = PROTO_SCSI;
822			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
823		} else {
824			if (done_ccb->ccb_h.target_id != 15) {
825				xpt_print(path,
826				    "Unexpected signature 0x%04x\n", sign);
827			}
828			goto device_fail;
829		}
830		xpt_release_ccb(done_ccb);
831		xpt_schedule(periph, priority);
832		goto out;
833	}
834	case PROBE_IDENTIFY:
835	{
836		struct ccb_pathinq cpi;
837		int16_t *ptr;
838		int veto = 0;
839
840		ident_buf = &softc->ident_data;
841		for (ptr = (int16_t *)ident_buf;
842		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
843			*ptr = le16toh(*ptr);
844		}
845
846		/*
847		 * Allow others to veto this ATA disk attachment.  This
848		 * is mainly used by VMs, whose disk controllers may
849		 * share the disks with the simulated ATA controllers.
850		 */
851		EVENTHANDLER_INVOKE(ada_probe_veto, path, ident_buf, &veto);
852		if (veto) {
853			goto device_fail;
854		}
855
856		if (strncmp(ident_buf->model, "FX", 2) &&
857		    strncmp(ident_buf->model, "NEC", 3) &&
858		    strncmp(ident_buf->model, "Pioneer", 7) &&
859		    strncmp(ident_buf->model, "SHARP", 5)) {
860			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
861			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
862			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
863		}
864		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
865		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
866		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
867		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
868		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
869		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
870		/* Device may need spin-up before IDENTIFY become valid. */
871		if ((ident_buf->specconf == 0x37c8 ||
872		     ident_buf->specconf == 0x738c) &&
873		    ((ident_buf->config & ATA_RESP_INCOMPLETE) ||
874		     softc->spinup == 0)) {
875			PROBE_SET_ACTION(softc, PROBE_SPINUP);
876			xpt_release_ccb(done_ccb);
877			xpt_schedule(periph, priority);
878			goto out;
879		}
880		ident_buf = &path->device->ident_data;
881		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
882			/* Check that it is the same device. */
883			if (bcmp(softc->ident_data.model, ident_buf->model,
884			     sizeof(ident_buf->model)) ||
885			    bcmp(softc->ident_data.revision, ident_buf->revision,
886			     sizeof(ident_buf->revision)) ||
887			    bcmp(softc->ident_data.serial, ident_buf->serial,
888			     sizeof(ident_buf->serial))) {
889				/* Device changed. */
890				xpt_async(AC_LOST_DEVICE, path, NULL);
891			} else {
892				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
893				changed = 0;
894			}
895		}
896		if (changed) {
897			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
898			/* Clean up from previous instance of this device */
899			if (path->device->serial_num != NULL) {
900				free(path->device->serial_num, M_CAMXPT);
901				path->device->serial_num = NULL;
902				path->device->serial_num_len = 0;
903			}
904			if (path->device->device_id != NULL) {
905				free(path->device->device_id, M_CAMXPT);
906				path->device->device_id = NULL;
907				path->device->device_id_len = 0;
908			}
909			path->device->serial_num =
910				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
911					   M_CAMXPT, M_NOWAIT);
912			if (path->device->serial_num != NULL) {
913				bcopy(ident_buf->serial,
914				      path->device->serial_num,
915				      sizeof(ident_buf->serial));
916				path->device->serial_num[sizeof(ident_buf->serial)]
917				    = '\0';
918				path->device->serial_num_len =
919				    strlen(path->device->serial_num);
920			}
921			if (ident_buf->enabled.extension &
922			    ATA_SUPPORT_64BITWWN) {
923				path->device->device_id =
924				    malloc(16, M_CAMXPT, M_NOWAIT);
925				if (path->device->device_id != NULL) {
926					path->device->device_id_len = 16;
927					bcopy(&fake_device_id_hdr,
928					    path->device->device_id, 8);
929					bcopy(ident_buf->wwn,
930					    path->device->device_id + 8, 8);
931					ata_bswap(path->device->device_id + 8, 8);
932				}
933			}
934
935			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
936			xpt_async(AC_GETDEV_CHANGED, path, NULL);
937		}
938		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
939			path->device->mintags = 2;
940			path->device->maxtags =
941			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
942		}
943		ata_find_quirk(path->device);
944		if (path->device->mintags != 0 &&
945		    path->bus->sim->max_tagged_dev_openings != 0) {
946			/* Check if the SIM does not want queued commands. */
947			bzero(&cpi, sizeof(cpi));
948			xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
949			cpi.ccb_h.func_code = XPT_PATH_INQ;
950			xpt_action((union ccb *)&cpi);
951			if (cpi.ccb_h.status == CAM_REQ_CMP &&
952			    (cpi.hba_inquiry & PI_TAG_ABLE)) {
953				/* Report SIM which tags are allowed. */
954				bzero(&cts, sizeof(cts));
955				xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
956				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
957				cts.type = CTS_TYPE_CURRENT_SETTINGS;
958				cts.xport_specific.sata.tags = path->device->maxtags;
959				cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
960				xpt_action((union ccb *)&cts);
961			}
962		}
963		ata_device_transport(path);
964		if (changed)
965			proberequestdefaultnegotiation(periph);
966		PROBE_SET_ACTION(softc, PROBE_SETMODE);
967		xpt_release_ccb(done_ccb);
968		xpt_schedule(periph, priority);
969		goto out;
970	}
971	case PROBE_SPINUP:
972		if (bootverbose)
973			xpt_print(path, "Spin-up done\n");
974		softc->spinup = 1;
975		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
976		xpt_release_ccb(done_ccb);
977		xpt_schedule(periph, priority);
978		goto out;
979	case PROBE_SETMODE:
980		/* Set supported bits. */
981		bzero(&cts, sizeof(cts));
982		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
983		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
984		cts.type = CTS_TYPE_CURRENT_SETTINGS;
985		xpt_action((union ccb *)&cts);
986		if (path->device->transport == XPORT_SATA &&
987		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
988			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
989		else if (path->device->transport == XPORT_ATA &&
990		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
991			caps = cts.xport_specific.ata.caps & CTS_ATA_CAPS_H;
992		else
993			caps = 0;
994		if (path->device->transport == XPORT_SATA &&
995		    ident_buf->satacapabilities != 0xffff) {
996			if (ident_buf->satacapabilities & ATA_SUPPORT_IFPWRMNGTRCV)
997				caps |= CTS_SATA_CAPS_D_PMREQ;
998			if (ident_buf->satacapabilities & ATA_SUPPORT_HAPST)
999				caps |= CTS_SATA_CAPS_D_APST;
1000		}
1001		/* Mask unwanted bits. */
1002		bzero(&cts, sizeof(cts));
1003		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1004		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1005		cts.type = CTS_TYPE_USER_SETTINGS;
1006		xpt_action((union ccb *)&cts);
1007		if (path->device->transport == XPORT_SATA &&
1008		    cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1009			caps &= cts.xport_specific.sata.caps;
1010		else if (path->device->transport == XPORT_ATA &&
1011		    cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS)
1012			caps &= cts.xport_specific.ata.caps;
1013		else
1014			caps = 0;
1015		/*
1016		 * Remember what transport thinks about 48-bit DMA.  If
1017		 * capability information is not provided or transport is
1018		 * SATA, we take support for granted.
1019		 */
1020		if (!(path->device->inq_flags & SID_DMA) ||
1021		    (path->device->transport == XPORT_ATA &&
1022		    (cts.xport_specific.ata.valid & CTS_ATA_VALID_CAPS) &&
1023		    !(caps & CTS_ATA_CAPS_H_DMA48)))
1024			path->device->inq_flags &= ~SID_DMA48;
1025		else
1026			path->device->inq_flags |= SID_DMA48;
1027		/* Store result to SIM. */
1028		bzero(&cts, sizeof(cts));
1029		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1030		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1031		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1032		if (path->device->transport == XPORT_SATA) {
1033			cts.xport_specific.sata.caps = caps;
1034			cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1035		} else {
1036			cts.xport_specific.ata.caps = caps;
1037			cts.xport_specific.ata.valid = CTS_ATA_VALID_CAPS;
1038		}
1039		xpt_action((union ccb *)&cts);
1040		softc->caps = caps;
1041		if (path->device->transport != XPORT_SATA)
1042			goto notsata;
1043		if ((ident_buf->satasupport & ATA_SUPPORT_IFPWRMNGT) &&
1044		    (!(softc->caps & CTS_SATA_CAPS_H_PMREQ)) !=
1045		    (!(ident_buf->sataenabled & ATA_SUPPORT_IFPWRMNGT))) {
1046			PROBE_SET_ACTION(softc, PROBE_SETPM);
1047			xpt_release_ccb(done_ccb);
1048			xpt_schedule(periph, priority);
1049			goto out;
1050		}
1051		/* FALLTHROUGH */
1052	case PROBE_SETPM:
1053		if (ident_buf->satacapabilities != 0xffff &&
1054		    (ident_buf->satacapabilities & ATA_SUPPORT_DAPST) &&
1055		    (!(softc->caps & CTS_SATA_CAPS_H_APST)) !=
1056		    (!(ident_buf->sataenabled & ATA_ENABLED_DAPST))) {
1057			PROBE_SET_ACTION(softc, PROBE_SETAPST);
1058			xpt_release_ccb(done_ccb);
1059			xpt_schedule(periph, priority);
1060			goto out;
1061		}
1062		/* FALLTHROUGH */
1063	case PROBE_SETAPST:
1064		if ((ident_buf->satasupport & ATA_SUPPORT_AUTOACTIVATE) &&
1065		    (!(softc->caps & CTS_SATA_CAPS_H_DMAAA)) !=
1066		    (!(ident_buf->sataenabled & ATA_SUPPORT_AUTOACTIVATE))) {
1067			PROBE_SET_ACTION(softc, PROBE_SETDMAAA);
1068			xpt_release_ccb(done_ccb);
1069			xpt_schedule(periph, priority);
1070			goto out;
1071		}
1072		/* FALLTHROUGH */
1073	case PROBE_SETDMAAA:
1074		if (path->device->protocol != PROTO_ATA &&
1075		    (ident_buf->satasupport & ATA_SUPPORT_ASYNCNOTIF) &&
1076		    (!(softc->caps & CTS_SATA_CAPS_H_AN)) !=
1077		    (!(ident_buf->sataenabled & ATA_SUPPORT_ASYNCNOTIF))) {
1078			PROBE_SET_ACTION(softc, PROBE_SETAN);
1079			xpt_release_ccb(done_ccb);
1080			xpt_schedule(periph, priority);
1081			goto out;
1082		}
1083		/* FALLTHROUGH */
1084	case PROBE_SETAN:
1085notsata:
1086		if (path->device->protocol == PROTO_ATA) {
1087			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
1088		} else {
1089			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1090		}
1091		xpt_release_ccb(done_ccb);
1092		xpt_schedule(periph, priority);
1093		goto out;
1094	case PROBE_SET_MULTI:
1095		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1096			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1097			xpt_acquire_device(path->device);
1098			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1099			xpt_action(done_ccb);
1100			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1101		}
1102		PROBE_SET_ACTION(softc, PROBE_DONE);
1103		break;
1104	case PROBE_INQUIRY:
1105	case PROBE_FULL_INQUIRY:
1106	{
1107		u_int8_t periph_qual, len;
1108
1109		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1110
1111		periph_qual = SID_QUAL(inq_buf);
1112
1113		if (periph_qual != SID_QUAL_LU_CONNECTED &&
1114		    periph_qual != SID_QUAL_LU_OFFLINE)
1115			break;
1116
1117		/*
1118		 * We conservatively request only
1119		 * SHORT_INQUIRY_LEN bytes of inquiry
1120		 * information during our first try
1121		 * at sending an INQUIRY. If the device
1122		 * has more information to give,
1123		 * perform a second request specifying
1124		 * the amount of information the device
1125		 * is willing to give.
1126		 */
1127		len = inq_buf->additional_length
1128		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
1129		if (softc->action == PROBE_INQUIRY
1130		    && len > SHORT_INQUIRY_LENGTH) {
1131			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1132			xpt_release_ccb(done_ccb);
1133			xpt_schedule(periph, priority);
1134			goto out;
1135		}
1136
1137		ata_device_transport(path);
1138		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1139			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1140			xpt_acquire_device(path->device);
1141			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1142			xpt_action(done_ccb);
1143			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1144		}
1145		PROBE_SET_ACTION(softc, PROBE_DONE);
1146		break;
1147	}
1148	case PROBE_PM_PID:
1149		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
1150			bzero(ident_buf, sizeof(*ident_buf));
1151		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
1152		    (done_ccb->ataio.res.lba_mid << 16) +
1153		    (done_ccb->ataio.res.lba_low << 8) +
1154		    done_ccb->ataio.res.sector_count;
1155		((uint32_t *)ident_buf)[0] = softc->pm_pid;
1156		snprintf(ident_buf->model, sizeof(ident_buf->model),
1157		    "Port Multiplier %08x", softc->pm_pid);
1158		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
1159		xpt_release_ccb(done_ccb);
1160		xpt_schedule(periph, priority);
1161		goto out;
1162	case PROBE_PM_PRV:
1163		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
1164		    (done_ccb->ataio.res.lba_mid << 16) +
1165		    (done_ccb->ataio.res.lba_low << 8) +
1166		    done_ccb->ataio.res.sector_count;
1167		((uint32_t *)ident_buf)[1] = softc->pm_prv;
1168		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
1169		    "%04x", softc->pm_prv);
1170		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1171		ata_device_transport(path);
1172		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
1173			proberequestdefaultnegotiation(periph);
1174		/* Set supported bits. */
1175		bzero(&cts, sizeof(cts));
1176		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1177		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1178		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1179		xpt_action((union ccb *)&cts);
1180		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1181			caps = cts.xport_specific.sata.caps & CTS_SATA_CAPS_H;
1182		else
1183			caps = 0;
1184		/* All PMPs must support PM requests. */
1185		caps |= CTS_SATA_CAPS_D_PMREQ;
1186		/* Mask unwanted bits. */
1187		bzero(&cts, sizeof(cts));
1188		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1189		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1190		cts.type = CTS_TYPE_USER_SETTINGS;
1191		xpt_action((union ccb *)&cts);
1192		if (cts.xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1193			caps &= cts.xport_specific.sata.caps;
1194		else
1195			caps = 0;
1196		/* Remember what transport thinks about AEN. */
1197		if ((caps & CTS_SATA_CAPS_H_AN) && path->device->protocol != PROTO_ATA)
1198			path->device->inq_flags |= SID_AEN;
1199		else
1200			path->device->inq_flags &= ~SID_AEN;
1201		/* Store result to SIM. */
1202		bzero(&cts, sizeof(cts));
1203		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1204		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1205		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1206		cts.xport_specific.sata.caps = caps;
1207		cts.xport_specific.sata.valid = CTS_SATA_VALID_CAPS;
1208		xpt_action((union ccb *)&cts);
1209		softc->caps = caps;
1210		xpt_async(AC_GETDEV_CHANGED, path, NULL);
1211		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1212			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1213			xpt_acquire_device(path->device);
1214			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1215			xpt_action(done_ccb);
1216			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1217		} else {
1218			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1219			xpt_action(done_ccb);
1220			xpt_async(AC_SCSI_AEN, path, done_ccb);
1221		}
1222		PROBE_SET_ACTION(softc, PROBE_DONE);
1223		break;
1224	case PROBE_IDENTIFY_SES:
1225	case PROBE_IDENTIFY_SAFTE:
1226		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1227			/* Check that it is the same device. */
1228			if (bcmp(&softc->ident_data, ident_buf, 53)) {
1229				/* Device changed. */
1230				xpt_async(AC_LOST_DEVICE, path, NULL);
1231			} else {
1232				bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
1233				changed = 0;
1234			}
1235		}
1236		if (changed) {
1237			bcopy(&softc->ident_data, ident_buf, sizeof(struct ata_params));
1238			/* Clean up from previous instance of this device */
1239			if (path->device->device_id != NULL) {
1240				free(path->device->device_id, M_CAMXPT);
1241				path->device->device_id = NULL;
1242				path->device->device_id_len = 0;
1243			}
1244			path->device->device_id =
1245			    malloc(16, M_CAMXPT, M_NOWAIT);
1246			if (path->device->device_id != NULL) {
1247				path->device->device_id_len = 16;
1248				bcopy(&fake_device_id_hdr,
1249				    path->device->device_id, 8);
1250				bcopy(((uint8_t*)ident_buf) + 2,
1251				    path->device->device_id + 8, 8);
1252			}
1253
1254			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
1255		}
1256		ata_device_transport(path);
1257		if (changed)
1258			proberequestdefaultnegotiation(periph);
1259
1260		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1261			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1262			xpt_acquire_device(path->device);
1263			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1264			xpt_action(done_ccb);
1265			xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1266		}
1267		PROBE_SET_ACTION(softc, PROBE_DONE);
1268		break;
1269	default:
1270		panic("probedone: invalid action state 0x%x\n", softc->action);
1271	}
1272done:
1273	if (softc->restart) {
1274		softc->restart = 0;
1275		xpt_release_ccb(done_ccb);
1276		probeschedule(periph);
1277		goto out;
1278	}
1279	xpt_release_ccb(done_ccb);
1280	CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1281	while ((done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs))) {
1282		TAILQ_REMOVE(&softc->request_ccbs,
1283		    &done_ccb->ccb_h, periph_links.tqe);
1284		done_ccb->ccb_h.status = found ? CAM_REQ_CMP : CAM_REQ_CMP_ERR;
1285		xpt_done(done_ccb);
1286	}
1287	/* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1288	cam_release_devq(path, 0, 0, 0, FALSE);
1289	cam_periph_invalidate(periph);
1290	cam_periph_release_locked(periph);
1291}
1292
1293static void
1294probecleanup(struct cam_periph *periph)
1295{
1296	free(periph->softc, M_CAMXPT);
1297}
1298
1299static void
1300ata_find_quirk(struct cam_ed *device)
1301{
1302	struct ata_quirk_entry *quirk;
1303	caddr_t	match;
1304
1305	match = cam_quirkmatch((caddr_t)&device->ident_data,
1306			       (caddr_t)ata_quirk_table,
1307			       nitems(ata_quirk_table),
1308			       sizeof(*ata_quirk_table), ata_identify_match);
1309
1310	if (match == NULL)
1311		panic("xpt_find_quirk: device didn't match wildcard entry!!");
1312
1313	quirk = (struct ata_quirk_entry *)match;
1314	device->quirk = quirk;
1315	if (quirk->quirks & CAM_QUIRK_MAXTAGS) {
1316		device->mintags = quirk->mintags;
1317		device->maxtags = quirk->maxtags;
1318	}
1319}
1320
1321typedef struct {
1322	union	ccb *request_ccb;
1323	struct 	ccb_pathinq *cpi;
1324	int	counter;
1325} ata_scan_bus_info;
1326
1327/*
1328 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1329 * As the scan progresses, xpt_scan_bus is used as the
1330 * callback on completion function.
1331 */
1332static void
1333ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1334{
1335	struct	cam_path *path;
1336	ata_scan_bus_info *scan_info;
1337	union	ccb *work_ccb, *reset_ccb;
1338	struct mtx *mtx;
1339	cam_status status;
1340
1341	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1342		  ("xpt_scan_bus\n"));
1343	switch (request_ccb->ccb_h.func_code) {
1344	case XPT_SCAN_BUS:
1345	case XPT_SCAN_TGT:
1346		/* Find out the characteristics of the bus */
1347		work_ccb = xpt_alloc_ccb_nowait();
1348		if (work_ccb == NULL) {
1349			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1350			xpt_done(request_ccb);
1351			return;
1352		}
1353		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1354			      request_ccb->ccb_h.pinfo.priority);
1355		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1356		xpt_action(work_ccb);
1357		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1358			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1359			xpt_free_ccb(work_ccb);
1360			xpt_done(request_ccb);
1361			return;
1362		}
1363
1364		/* We may need to reset bus first, if we haven't done it yet. */
1365		if ((work_ccb->cpi.hba_inquiry &
1366		    (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1367		    !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1368		    !timevalisset(&request_ccb->ccb_h.path->bus->last_reset)) {
1369			reset_ccb = xpt_alloc_ccb_nowait();
1370			if (reset_ccb == NULL) {
1371				request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1372				xpt_free_ccb(work_ccb);
1373				xpt_done(request_ccb);
1374				return;
1375			}
1376			xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1377			      CAM_PRIORITY_NONE);
1378			reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1379			xpt_action(reset_ccb);
1380			if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1381				request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1382				xpt_free_ccb(reset_ccb);
1383				xpt_free_ccb(work_ccb);
1384				xpt_done(request_ccb);
1385				return;
1386			}
1387			xpt_free_ccb(reset_ccb);
1388		}
1389
1390		/* Save some state for use while we probe for devices */
1391		scan_info = (ata_scan_bus_info *)
1392		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1393		if (scan_info == NULL) {
1394			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1395			xpt_free_ccb(work_ccb);
1396			xpt_done(request_ccb);
1397			return;
1398		}
1399		scan_info->request_ccb = request_ccb;
1400		scan_info->cpi = &work_ccb->cpi;
1401		/* If PM supported, probe it first. */
1402		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1403			scan_info->counter = scan_info->cpi->max_target;
1404		else
1405			scan_info->counter = 0;
1406
1407		work_ccb = xpt_alloc_ccb_nowait();
1408		if (work_ccb == NULL) {
1409			free(scan_info, M_CAMXPT);
1410			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1411			xpt_done(request_ccb);
1412			break;
1413		}
1414		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
1415		goto scan_next;
1416	case XPT_SCAN_LUN:
1417		work_ccb = request_ccb;
1418		/* Reuse the same CCB to query if a device was really found */
1419		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1420		mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
1421		mtx_lock(mtx);
1422		/* If there is PMP... */
1423		if ((scan_info->cpi->hba_inquiry & PI_SATAPM) &&
1424		    (scan_info->counter == scan_info->cpi->max_target)) {
1425			if (work_ccb->ccb_h.status == CAM_REQ_CMP) {
1426				/* everything else will be probed by it */
1427				/* Free the current request path- we're done with it. */
1428				xpt_free_path(work_ccb->ccb_h.path);
1429				goto done;
1430			} else {
1431				struct ccb_trans_settings cts;
1432
1433				/* Report SIM that PM is absent. */
1434				bzero(&cts, sizeof(cts));
1435				xpt_setup_ccb(&cts.ccb_h,
1436				    work_ccb->ccb_h.path, CAM_PRIORITY_NONE);
1437				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1438				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1439				cts.xport_specific.sata.pm_present = 0;
1440				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1441				xpt_action((union ccb *)&cts);
1442			}
1443		}
1444		/* Free the current request path- we're done with it. */
1445		xpt_free_path(work_ccb->ccb_h.path);
1446		if (scan_info->counter ==
1447		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ?
1448		    0 : scan_info->cpi->max_target)) {
1449done:
1450			mtx_unlock(mtx);
1451			xpt_free_ccb(work_ccb);
1452			xpt_free_ccb((union ccb *)scan_info->cpi);
1453			request_ccb = scan_info->request_ccb;
1454			free(scan_info, M_CAMXPT);
1455			request_ccb->ccb_h.status = CAM_REQ_CMP;
1456			xpt_done(request_ccb);
1457			break;
1458		}
1459		/* Take next device. Wrap from max (PMP) to 0. */
1460		scan_info->counter = (scan_info->counter + 1 ) %
1461		    (scan_info->cpi->max_target + 1);
1462scan_next:
1463		status = xpt_create_path(&path, NULL,
1464		    scan_info->request_ccb->ccb_h.path_id,
1465		    scan_info->counter, 0);
1466		if (status != CAM_REQ_CMP) {
1467			if (request_ccb->ccb_h.func_code == XPT_SCAN_LUN)
1468				mtx_unlock(mtx);
1469			printf("xpt_scan_bus: xpt_create_path failed"
1470			    " with status %#x, bus scan halted\n",
1471			    status);
1472			xpt_free_ccb(work_ccb);
1473			xpt_free_ccb((union ccb *)scan_info->cpi);
1474			request_ccb = scan_info->request_ccb;
1475			free(scan_info, M_CAMXPT);
1476			request_ccb->ccb_h.status = status;
1477			xpt_done(request_ccb);
1478			break;
1479		}
1480		xpt_setup_ccb(&work_ccb->ccb_h, path,
1481		    scan_info->request_ccb->ccb_h.pinfo.priority);
1482		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1483		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1484		work_ccb->ccb_h.flags |= CAM_UNLOCKED;
1485		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1486		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1487		mtx_unlock(mtx);
1488		if (request_ccb->ccb_h.func_code == XPT_SCAN_LUN)
1489			mtx = NULL;
1490		xpt_action(work_ccb);
1491		if (mtx != NULL)
1492			mtx_lock(mtx);
1493		break;
1494	default:
1495		break;
1496	}
1497}
1498
1499static void
1500ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1501	     cam_flags flags, union ccb *request_ccb)
1502{
1503	struct ccb_pathinq cpi;
1504	cam_status status;
1505	struct cam_path *new_path;
1506	struct cam_periph *old_periph;
1507	int lock;
1508
1509	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_scan_lun\n"));
1510
1511	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1512	cpi.ccb_h.func_code = XPT_PATH_INQ;
1513	xpt_action((union ccb *)&cpi);
1514
1515	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1516		if (request_ccb != NULL) {
1517			request_ccb->ccb_h.status = cpi.ccb_h.status;
1518			xpt_done(request_ccb);
1519		}
1520		return;
1521	}
1522
1523	if (request_ccb == NULL) {
1524		request_ccb = xpt_alloc_ccb_nowait();
1525		if (request_ccb == NULL) {
1526			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1527			    "can't continue\n");
1528			return;
1529		}
1530		status = xpt_create_path(&new_path, NULL,
1531					  path->bus->path_id,
1532					  path->target->target_id,
1533					  path->device->lun_id);
1534		if (status != CAM_REQ_CMP) {
1535			xpt_print(path, "xpt_scan_lun: can't create path, "
1536			    "can't continue\n");
1537			xpt_free_ccb(request_ccb);
1538			return;
1539		}
1540		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
1541		request_ccb->ccb_h.cbfcnp = xptscandone;
1542		request_ccb->ccb_h.flags |= CAM_UNLOCKED;
1543		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1544		request_ccb->crcn.flags = flags;
1545	}
1546
1547	lock = (xpt_path_owned(path) == 0);
1548	if (lock)
1549		xpt_path_lock(path);
1550	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1551		if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
1552			probe_softc *softc;
1553
1554			softc = (probe_softc *)old_periph->softc;
1555			TAILQ_INSERT_TAIL(&softc->request_ccbs,
1556				&request_ccb->ccb_h, periph_links.tqe);
1557			softc->restart = 1;
1558		} else {
1559			request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1560			xpt_done(request_ccb);
1561		}
1562	} else {
1563		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1564					  probestart, "aprobe",
1565					  CAM_PERIPH_BIO,
1566					  request_ccb->ccb_h.path, NULL, 0,
1567					  request_ccb);
1568
1569		if (status != CAM_REQ_CMP) {
1570			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1571			    "returned an error, can't continue probe\n");
1572			request_ccb->ccb_h.status = status;
1573			xpt_done(request_ccb);
1574		}
1575	}
1576	if (lock)
1577		xpt_path_unlock(path);
1578}
1579
1580static void
1581xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1582{
1583
1584	xpt_free_path(done_ccb->ccb_h.path);
1585	xpt_free_ccb(done_ccb);
1586}
1587
1588static struct cam_ed *
1589ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1590{
1591	struct ata_quirk_entry *quirk;
1592	struct cam_ed *device;
1593
1594	device = xpt_alloc_device(bus, target, lun_id);
1595	if (device == NULL)
1596		return (NULL);
1597
1598	/*
1599	 * Take the default quirk entry until we have inquiry
1600	 * data and can determine a better quirk to use.
1601	 */
1602	quirk = &ata_quirk_table[nitems(ata_quirk_table) - 1];
1603	device->quirk = (void *)quirk;
1604	device->mintags = 0;
1605	device->maxtags = 0;
1606	bzero(&device->inq_data, sizeof(device->inq_data));
1607	device->inq_flags = 0;
1608	device->queue_flags = 0;
1609	device->serial_num = NULL;
1610	device->serial_num_len = 0;
1611	return (device);
1612}
1613
1614static void
1615ata_device_transport(struct cam_path *path)
1616{
1617	struct ccb_pathinq cpi;
1618	struct ccb_trans_settings cts;
1619	struct scsi_inquiry_data *inq_buf = NULL;
1620	struct ata_params *ident_buf = NULL;
1621
1622	/* Get transport information from the SIM */
1623	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1624	cpi.ccb_h.func_code = XPT_PATH_INQ;
1625	xpt_action((union ccb *)&cpi);
1626
1627	path->device->transport = cpi.transport;
1628	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1629		inq_buf = &path->device->inq_data;
1630	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1631		ident_buf = &path->device->ident_data;
1632	if (path->device->protocol == PROTO_ATA) {
1633		path->device->protocol_version = ident_buf ?
1634		    ata_version(ident_buf->version_major) : cpi.protocol_version;
1635	} else if (path->device->protocol == PROTO_SCSI) {
1636		path->device->protocol_version = inq_buf ?
1637		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1638	}
1639	path->device->transport_version = ident_buf ?
1640	    ata_version(ident_buf->version_major) : cpi.transport_version;
1641
1642	/* Tell the controller what we think */
1643	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
1644	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1645	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1646	cts.transport = path->device->transport;
1647	cts.transport_version = path->device->transport_version;
1648	cts.protocol = path->device->protocol;
1649	cts.protocol_version = path->device->protocol_version;
1650	cts.proto_specific.valid = 0;
1651	if (ident_buf) {
1652		if (path->device->transport == XPORT_ATA) {
1653			cts.xport_specific.ata.atapi =
1654			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1655			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1656			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1657			cts.xport_specific.ata.valid = CTS_ATA_VALID_ATAPI;
1658		} else {
1659			cts.xport_specific.sata.atapi =
1660			    (ident_buf->config == ATA_PROTO_CFA) ? 0 :
1661			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_16) ? 16 :
1662			    ((ident_buf->config & ATA_PROTO_MASK) == ATA_PROTO_ATAPI_12) ? 12 : 0;
1663			cts.xport_specific.sata.valid = CTS_SATA_VALID_ATAPI;
1664		}
1665	} else
1666		cts.xport_specific.valid = 0;
1667	xpt_action((union ccb *)&cts);
1668}
1669
1670static void
1671ata_dev_advinfo(union ccb *start_ccb)
1672{
1673	struct cam_ed *device;
1674	struct ccb_dev_advinfo *cdai;
1675	off_t amt;
1676
1677	start_ccb->ccb_h.status = CAM_REQ_INVALID;
1678	device = start_ccb->ccb_h.path->device;
1679	cdai = &start_ccb->cdai;
1680	switch(cdai->buftype) {
1681	case CDAI_TYPE_SCSI_DEVID:
1682		if (cdai->flags & CDAI_FLAG_STORE)
1683			return;
1684		cdai->provsiz = device->device_id_len;
1685		if (device->device_id_len == 0)
1686			break;
1687		amt = device->device_id_len;
1688		if (cdai->provsiz > cdai->bufsiz)
1689			amt = cdai->bufsiz;
1690		memcpy(cdai->buf, device->device_id, amt);
1691		break;
1692	case CDAI_TYPE_SERIAL_NUM:
1693		if (cdai->flags & CDAI_FLAG_STORE)
1694			return;
1695		cdai->provsiz = device->serial_num_len;
1696		if (device->serial_num_len == 0)
1697			break;
1698		amt = device->serial_num_len;
1699		if (cdai->provsiz > cdai->bufsiz)
1700			amt = cdai->bufsiz;
1701		memcpy(cdai->buf, device->serial_num, amt);
1702		break;
1703	case CDAI_TYPE_PHYS_PATH:
1704		if (cdai->flags & CDAI_FLAG_STORE) {
1705			if (device->physpath != NULL)
1706				free(device->physpath, M_CAMXPT);
1707			device->physpath_len = cdai->bufsiz;
1708			/* Clear existing buffer if zero length */
1709			if (cdai->bufsiz == 0)
1710				break;
1711			device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
1712			if (device->physpath == NULL) {
1713				start_ccb->ccb_h.status = CAM_REQ_ABORTED;
1714				return;
1715			}
1716			memcpy(device->physpath, cdai->buf, cdai->bufsiz);
1717		} else {
1718			cdai->provsiz = device->physpath_len;
1719			if (device->physpath_len == 0)
1720				break;
1721			amt = device->physpath_len;
1722			if (cdai->provsiz > cdai->bufsiz)
1723				amt = cdai->bufsiz;
1724			memcpy(cdai->buf, device->physpath, amt);
1725		}
1726		break;
1727	default:
1728		return;
1729	}
1730	start_ccb->ccb_h.status = CAM_REQ_CMP;
1731
1732	if (cdai->flags & CDAI_FLAG_STORE) {
1733		xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
1734			  (void *)(uintptr_t)cdai->buftype);
1735	}
1736}
1737
1738static void
1739ata_action(union ccb *start_ccb)
1740{
1741
1742	switch (start_ccb->ccb_h.func_code) {
1743	case XPT_SET_TRAN_SETTINGS:
1744	{
1745		ata_set_transfer_settings(&start_ccb->cts,
1746					   start_ccb->ccb_h.path,
1747					   /*async_update*/FALSE);
1748		break;
1749	}
1750	case XPT_SCAN_BUS:
1751	case XPT_SCAN_TGT:
1752		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1753		break;
1754	case XPT_SCAN_LUN:
1755		ata_scan_lun(start_ccb->ccb_h.path->periph,
1756			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1757			      start_ccb);
1758		break;
1759	case XPT_GET_TRAN_SETTINGS:
1760	{
1761		ata_get_transfer_settings(&start_ccb->cts);
1762		break;
1763	}
1764	case XPT_SCSI_IO:
1765	{
1766		struct cam_ed *device;
1767		u_int	maxlen = 0;
1768
1769		device = start_ccb->ccb_h.path->device;
1770		if (device->protocol == PROTO_SCSI &&
1771		    (device->flags & CAM_DEV_IDENTIFY_DATA_VALID)) {
1772			uint16_t p =
1773			    device->ident_data.config & ATA_PROTO_MASK;
1774
1775			maxlen =
1776			    (device->ident_data.config == ATA_PROTO_CFA) ? 0 :
1777			    (p == ATA_PROTO_ATAPI_16) ? 16 :
1778			    (p == ATA_PROTO_ATAPI_12) ? 12 : 0;
1779		}
1780		if (start_ccb->csio.cdb_len > maxlen) {
1781			start_ccb->ccb_h.status = CAM_REQ_INVALID;
1782			xpt_done(start_ccb);
1783			break;
1784		}
1785		xpt_action_default(start_ccb);
1786		break;
1787	}
1788	case XPT_DEV_ADVINFO:
1789	{
1790		ata_dev_advinfo(start_ccb);
1791		break;
1792	}
1793	default:
1794		xpt_action_default(start_ccb);
1795		break;
1796	}
1797}
1798
1799static void
1800ata_get_transfer_settings(struct ccb_trans_settings *cts)
1801{
1802	struct	ccb_trans_settings_ata *ata;
1803	struct	ccb_trans_settings_scsi *scsi;
1804	struct	cam_ed *device;
1805
1806	device = cts->ccb_h.path->device;
1807	xpt_action_default((union ccb *)cts);
1808
1809	if (cts->protocol == PROTO_UNKNOWN ||
1810	    cts->protocol == PROTO_UNSPECIFIED) {
1811		cts->protocol = device->protocol;
1812		cts->protocol_version = device->protocol_version;
1813	}
1814
1815	if (cts->protocol == PROTO_ATA) {
1816		ata = &cts->proto_specific.ata;
1817		if ((ata->valid & CTS_ATA_VALID_TQ) == 0) {
1818			ata->valid |= CTS_ATA_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				ata->flags |= CTS_ATA_FLAGS_TAG_ENB;
1823		}
1824	}
1825	if (cts->protocol == PROTO_SCSI) {
1826		scsi = &cts->proto_specific.scsi;
1827		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1828			scsi->valid |= CTS_SCSI_VALID_TQ;
1829			if (cts->type == CTS_TYPE_USER_SETTINGS ||
1830			    (device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1831			    (device->inq_flags & SID_CmdQue) != 0)
1832				scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1833		}
1834	}
1835
1836	if (cts->transport == XPORT_UNKNOWN ||
1837	    cts->transport == XPORT_UNSPECIFIED) {
1838		cts->transport = device->transport;
1839		cts->transport_version = device->transport_version;
1840	}
1841}
1842
1843static void
1844ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
1845			   int async_update)
1846{
1847	struct	ccb_pathinq cpi;
1848	struct	ccb_trans_settings_ata *ata;
1849	struct	ccb_trans_settings_scsi *scsi;
1850	struct	ata_params *ident_data;
1851	struct	scsi_inquiry_data *inq_data;
1852	struct	cam_ed *device;
1853
1854	if (path == NULL || (device = path->device) == NULL) {
1855		cts->ccb_h.status = CAM_PATH_INVALID;
1856		xpt_done((union ccb *)cts);
1857		return;
1858	}
1859
1860	if (cts->protocol == PROTO_UNKNOWN
1861	 || cts->protocol == PROTO_UNSPECIFIED) {
1862		cts->protocol = device->protocol;
1863		cts->protocol_version = device->protocol_version;
1864	}
1865
1866	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1867	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1868		cts->protocol_version = device->protocol_version;
1869
1870	if (cts->protocol != device->protocol) {
1871		xpt_print(path, "Uninitialized Protocol %x:%x?\n",
1872		       cts->protocol, device->protocol);
1873		cts->protocol = device->protocol;
1874	}
1875
1876	if (cts->protocol_version > device->protocol_version) {
1877		if (bootverbose) {
1878			xpt_print(path, "Down reving Protocol "
1879			    "Version from %d to %d?\n", cts->protocol_version,
1880			    device->protocol_version);
1881		}
1882		cts->protocol_version = device->protocol_version;
1883	}
1884
1885	if (cts->transport == XPORT_UNKNOWN
1886	 || cts->transport == XPORT_UNSPECIFIED) {
1887		cts->transport = device->transport;
1888		cts->transport_version = device->transport_version;
1889	}
1890
1891	if (cts->transport_version == XPORT_VERSION_UNKNOWN
1892	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1893		cts->transport_version = device->transport_version;
1894
1895	if (cts->transport != device->transport) {
1896		xpt_print(path, "Uninitialized Transport %x:%x?\n",
1897		    cts->transport, device->transport);
1898		cts->transport = device->transport;
1899	}
1900
1901	if (cts->transport_version > device->transport_version) {
1902		if (bootverbose) {
1903			xpt_print(path, "Down reving Transport "
1904			    "Version from %d to %d?\n", cts->transport_version,
1905			    device->transport_version);
1906		}
1907		cts->transport_version = device->transport_version;
1908	}
1909
1910	ident_data = &device->ident_data;
1911	inq_data = &device->inq_data;
1912	if (cts->protocol == PROTO_ATA)
1913		ata = &cts->proto_specific.ata;
1914	else
1915		ata = NULL;
1916	if (cts->protocol == PROTO_SCSI)
1917		scsi = &cts->proto_specific.scsi;
1918	else
1919		scsi = NULL;
1920	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
1921	cpi.ccb_h.func_code = XPT_PATH_INQ;
1922	xpt_action((union ccb *)&cpi);
1923
1924	/* Sanity checking */
1925	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1926	 || (ata && (ident_data->satacapabilities & ATA_SUPPORT_NCQ) == 0)
1927	 || (scsi && (INQ_DATA_TQ_ENABLED(inq_data)) == 0)
1928	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1929	 || (device->mintags == 0)) {
1930		/*
1931		 * Can't tag on hardware that doesn't support tags,
1932		 * doesn't have it enabled, or has broken tag support.
1933		 */
1934		if (ata)
1935			ata->flags &= ~CTS_ATA_FLAGS_TAG_ENB;
1936		if (scsi)
1937			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1938	}
1939
1940	/* Start/stop tags use. */
1941	if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1942	    ((ata && (ata->valid & CTS_ATA_VALID_TQ) != 0) ||
1943	     (scsi && (scsi->valid & CTS_SCSI_VALID_TQ) != 0))) {
1944		int nowt, newt = 0;
1945
1946		nowt = ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 ||
1947			(device->inq_flags & SID_CmdQue) != 0);
1948		if (ata)
1949			newt = (ata->flags & CTS_ATA_FLAGS_TAG_ENB) != 0;
1950		if (scsi)
1951			newt = (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0;
1952
1953		if (newt && !nowt) {
1954			/*
1955			 * Delay change to use tags until after a
1956			 * few commands have gone to this device so
1957			 * the controller has time to perform transfer
1958			 * negotiations without tagged messages getting
1959			 * in the way.
1960			 */
1961			device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1962			device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1963		} else if (nowt && !newt)
1964			xpt_stop_tags(path);
1965	}
1966
1967	if (async_update == FALSE)
1968		xpt_action_default((union ccb *)cts);
1969}
1970
1971/*
1972 * Handle any per-device event notifications that require action by the XPT.
1973 */
1974static void
1975ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1976	      struct cam_ed *device, void *async_arg)
1977{
1978	cam_status status;
1979	struct cam_path newpath;
1980
1981	/*
1982	 * We only need to handle events for real devices.
1983	 */
1984	if (target->target_id == CAM_TARGET_WILDCARD
1985	 || device->lun_id == CAM_LUN_WILDCARD)
1986		return;
1987
1988	/*
1989	 * We need our own path with wildcards expanded to
1990	 * handle certain types of events.
1991	 */
1992	if ((async_code == AC_SENT_BDR)
1993	 || (async_code == AC_BUS_RESET)
1994	 || (async_code == AC_INQ_CHANGED))
1995		status = xpt_compile_path(&newpath, NULL,
1996					  bus->path_id,
1997					  target->target_id,
1998					  device->lun_id);
1999	else
2000		status = CAM_REQ_CMP_ERR;
2001
2002	if (status == CAM_REQ_CMP) {
2003		if (async_code == AC_INQ_CHANGED) {
2004			/*
2005			 * We've sent a start unit command, or
2006			 * something similar to a device that
2007			 * may have caused its inquiry data to
2008			 * change. So we re-scan the device to
2009			 * refresh the inquiry data for it.
2010			 */
2011			ata_scan_lun(newpath.periph, &newpath,
2012				     CAM_EXPECT_INQ_CHANGE, NULL);
2013		} else {
2014			/* We need to reinitialize device after reset. */
2015			ata_scan_lun(newpath.periph, &newpath,
2016				     0, NULL);
2017		}
2018		xpt_release_path(&newpath);
2019	} else if (async_code == AC_LOST_DEVICE &&
2020	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2021		device->flags |= CAM_DEV_UNCONFIGURED;
2022		xpt_release_device(device);
2023	} else if (async_code == AC_TRANSFER_NEG) {
2024		struct ccb_trans_settings *settings;
2025		struct cam_path path;
2026
2027		settings = (struct ccb_trans_settings *)async_arg;
2028		xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
2029				 device->lun_id);
2030		ata_set_transfer_settings(settings, &path,
2031					  /*async_update*/TRUE);
2032		xpt_release_path(&path);
2033	}
2034}
2035
2036static void
2037ata_announce_periph(struct cam_periph *periph)
2038{
2039	struct	ccb_pathinq cpi;
2040	struct	ccb_trans_settings cts;
2041	struct	cam_path *path = periph->path;
2042	u_int	speed;
2043	u_int	mb;
2044
2045	cam_periph_assert(periph, MA_OWNED);
2046
2047	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
2048	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2049	cts.type = CTS_TYPE_CURRENT_SETTINGS;
2050	xpt_action((union ccb*)&cts);
2051	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
2052		return;
2053	/* Ask the SIM for its base transfer speed */
2054	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
2055	cpi.ccb_h.func_code = XPT_PATH_INQ;
2056	xpt_action((union ccb *)&cpi);
2057	/* Report connection speed */
2058	speed = cpi.base_transfer_speed;
2059	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
2060		struct	ccb_trans_settings_pata *pata =
2061		    &cts.xport_specific.ata;
2062
2063		if (pata->valid & CTS_ATA_VALID_MODE)
2064			speed = ata_mode2speed(pata->mode);
2065	}
2066	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
2067		struct	ccb_trans_settings_sata *sata =
2068		    &cts.xport_specific.sata;
2069
2070		if (sata->valid & CTS_SATA_VALID_REVISION)
2071			speed = ata_revision2speed(sata->revision);
2072	}
2073	mb = speed / 1000;
2074	if (mb > 0)
2075		printf("%s%d: %d.%03dMB/s transfers",
2076		       periph->periph_name, periph->unit_number,
2077		       mb, speed % 1000);
2078	else
2079		printf("%s%d: %dKB/s transfers", periph->periph_name,
2080		       periph->unit_number, speed);
2081	/* Report additional information about connection */
2082	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_ATA) {
2083		struct ccb_trans_settings_pata *pata =
2084		    &cts.xport_specific.ata;
2085
2086		printf(" (");
2087		if (pata->valid & CTS_ATA_VALID_MODE)
2088			printf("%s, ", ata_mode2string(pata->mode));
2089		if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
2090			printf("ATAPI %dbytes, ", pata->atapi);
2091		if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
2092			printf("PIO %dbytes", pata->bytecount);
2093		printf(")");
2094	}
2095	if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
2096		struct ccb_trans_settings_sata *sata =
2097		    &cts.xport_specific.sata;
2098
2099		printf(" (");
2100		if (sata->valid & CTS_SATA_VALID_REVISION)
2101			printf("SATA %d.x, ", sata->revision);
2102		else
2103			printf("SATA, ");
2104		if (sata->valid & CTS_SATA_VALID_MODE)
2105			printf("%s, ", ata_mode2string(sata->mode));
2106		if ((sata->valid & CTS_ATA_VALID_ATAPI) && sata->atapi != 0)
2107			printf("ATAPI %dbytes, ", sata->atapi);
2108		if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
2109			printf("PIO %dbytes", sata->bytecount);
2110		printf(")");
2111	}
2112	printf("\n");
2113}
2114