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