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