ata_xpt.c revision 200218
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 200218 2009-12-07 16:23:25Z mav $");
29
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/endian.h>
33#include <sys/systm.h>
34#include <sys/types.h>
35#include <sys/malloc.h>
36#include <sys/kernel.h>
37#include <sys/time.h>
38#include <sys/conf.h>
39#include <sys/fcntl.h>
40#include <sys/md5.h>
41#include <sys/interrupt.h>
42#include <sys/sbuf.h>
43
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/sysctl.h>
47
48#ifdef PC98
49#include <pc98/pc98/pc98_machdep.h>	/* geometry translation */
50#endif
51
52#include <cam/cam.h>
53#include <cam/cam_ccb.h>
54#include <cam/cam_queue.h>
55#include <cam/cam_periph.h>
56#include <cam/cam_sim.h>
57#include <cam/cam_xpt.h>
58#include <cam/cam_xpt_sim.h>
59#include <cam/cam_xpt_periph.h>
60#include <cam/cam_xpt_internal.h>
61#include <cam/cam_debug.h>
62
63#include <cam/scsi/scsi_all.h>
64#include <cam/scsi/scsi_message.h>
65#include <cam/ata/ata_all.h>
66#include <machine/stdarg.h>	/* for xpt_print below */
67#include "opt_cam.h"
68
69struct ata_quirk_entry {
70	struct scsi_inquiry_pattern inq_pat;
71	u_int8_t quirks;
72#define	CAM_QUIRK_MAXTAGS	0x01
73	u_int maxtags;
74};
75
76static periph_init_t probe_periph_init;
77
78static struct periph_driver probe_driver =
79{
80	probe_periph_init, "aprobe",
81	TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
82	CAM_PERIPH_DRV_EARLY
83};
84
85PERIPHDRIVER_DECLARE(aprobe, probe_driver);
86
87typedef enum {
88	PROBE_RESET,
89	PROBE_IDENTIFY,
90	PROBE_SETMODE,
91	PROBE_SET_MULTI,
92	PROBE_INQUIRY,
93	PROBE_FULL_INQUIRY,
94	PROBE_PM_PID,
95	PROBE_PM_PRV,
96	PROBE_INVALID
97} probe_action;
98
99static char *probe_action_text[] = {
100	"PROBE_RESET",
101	"PROBE_IDENTIFY",
102	"PROBE_SETMODE",
103	"PROBE_SET_MULTI",
104	"PROBE_INQUIRY",
105	"PROBE_FULL_INQUIRY",
106	"PROBE_PM_PID",
107	"PROBE_PM_PRV",
108	"PROBE_INVALID"
109};
110
111#define PROBE_SET_ACTION(softc, newaction)	\
112do {									\
113	char **text;							\
114	text = probe_action_text;					\
115	CAM_DEBUG((softc)->periph->path, CAM_DEBUG_INFO,		\
116	    ("Probe %s to %s\n", text[(softc)->action],			\
117	    text[(newaction)]));					\
118	(softc)->action = (newaction);					\
119} while(0)
120
121typedef enum {
122	PROBE_NO_ANNOUNCE	= 0x04
123} probe_flags;
124
125typedef struct {
126	TAILQ_HEAD(, ccb_hdr) request_ccbs;
127	probe_action	action;
128	union ccb	saved_ccb;
129	probe_flags	flags;
130	u_int8_t	digest[16];
131	uint32_t	pm_pid;
132	uint32_t	pm_prv;
133	struct cam_periph *periph;
134} probe_softc;
135
136static struct ata_quirk_entry ata_quirk_table[] =
137{
138	{
139		/* Default tagged queuing parameters for all devices */
140		{
141		  T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
142		  /*vendor*/"*", /*product*/"*", /*revision*/"*"
143		},
144		/*quirks*/0, /*maxtags*/0
145	},
146};
147
148static const int ata_quirk_table_size =
149	sizeof(ata_quirk_table) / sizeof(*ata_quirk_table);
150
151static cam_status	proberegister(struct cam_periph *periph,
152				      void *arg);
153static void	 probeschedule(struct cam_periph *probe_periph);
154static void	 probestart(struct cam_periph *periph, union ccb *start_ccb);
155//static void	 proberequestdefaultnegotiation(struct cam_periph *periph);
156//static int       proberequestbackoff(struct cam_periph *periph,
157//				     struct cam_ed *device);
158static void	 probedone(struct cam_periph *periph, union ccb *done_ccb);
159static void	 probecleanup(struct cam_periph *periph);
160static void	 ata_find_quirk(struct cam_ed *device);
161static void	 ata_scan_bus(struct cam_periph *periph, union ccb *ccb);
162static void	 ata_scan_lun(struct cam_periph *periph,
163			       struct cam_path *path, cam_flags flags,
164			       union ccb *ccb);
165static void	 xptscandone(struct cam_periph *periph, union ccb *done_ccb);
166static struct cam_ed *
167		 ata_alloc_device(struct cam_eb *bus, struct cam_et *target,
168				   lun_id_t lun_id);
169static void	 ata_device_transport(struct cam_path *path);
170static void	 ata_set_transfer_settings(struct ccb_trans_settings *cts,
171					    struct cam_ed *device,
172					    int async_update);
173static void	 ata_dev_async(u_int32_t async_code,
174				struct cam_eb *bus,
175				struct cam_et *target,
176				struct cam_ed *device,
177				void *async_arg);
178static void	 ata_action(union ccb *start_ccb);
179
180static struct xpt_xport ata_xport = {
181	.alloc_device = ata_alloc_device,
182	.action = ata_action,
183	.async = ata_dev_async,
184};
185
186struct xpt_xport *
187ata_get_xport(void)
188{
189	return (&ata_xport);
190}
191
192static void
193probe_periph_init()
194{
195}
196
197static cam_status
198proberegister(struct cam_periph *periph, void *arg)
199{
200	union ccb *request_ccb;	/* CCB representing the probe request */
201	cam_status status;
202	probe_softc *softc;
203
204	request_ccb = (union ccb *)arg;
205	if (periph == NULL) {
206		printf("proberegister: periph was NULL!!\n");
207		return(CAM_REQ_CMP_ERR);
208	}
209
210	if (request_ccb == NULL) {
211		printf("proberegister: no probe CCB, "
212		       "can't register device\n");
213		return(CAM_REQ_CMP_ERR);
214	}
215
216	softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
217
218	if (softc == NULL) {
219		printf("proberegister: Unable to probe new device. "
220		       "Unable to allocate softc\n");
221		return(CAM_REQ_CMP_ERR);
222	}
223	TAILQ_INIT(&softc->request_ccbs);
224	TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
225			  periph_links.tqe);
226	softc->flags = 0;
227	periph->softc = softc;
228	softc->periph = periph;
229	softc->action = PROBE_INVALID;
230	status = cam_periph_acquire(periph);
231	if (status != CAM_REQ_CMP) {
232		return (status);
233	}
234
235
236	/*
237	 * Ensure we've waited at least a bus settle
238	 * delay before attempting to probe the device.
239	 * For HBAs that don't do bus resets, this won't make a difference.
240	 */
241	cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
242				      scsi_delay);
243	probeschedule(periph);
244	return(CAM_REQ_CMP);
245}
246
247static void
248probeschedule(struct cam_periph *periph)
249{
250	struct ccb_pathinq cpi;
251	union ccb *ccb;
252	probe_softc *softc;
253
254	softc = (probe_softc *)periph->softc;
255	ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
256
257	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
258	cpi.ccb_h.func_code = XPT_PATH_INQ;
259	xpt_action((union ccb *)&cpi);
260
261	if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) ||
262	    periph->path->device->protocol == PROTO_SATAPM)
263		PROBE_SET_ACTION(softc, PROBE_RESET);
264	else
265		PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
266
267	if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
268		softc->flags |= PROBE_NO_ANNOUNCE;
269	else
270		softc->flags &= ~PROBE_NO_ANNOUNCE;
271
272	xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
273}
274
275static void
276probestart(struct cam_periph *periph, union ccb *start_ccb)
277{
278	struct ccb_trans_settings cts;
279	struct ccb_ataio *ataio;
280	struct ccb_scsiio *csio;
281	probe_softc *softc;
282	struct cam_path *path;
283	struct ata_params *ident_buf;
284
285	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
286
287	softc = (probe_softc *)periph->softc;
288	path = start_ccb->ccb_h.path;
289	ataio = &start_ccb->ataio;
290	csio = &start_ccb->csio;
291	ident_buf = &periph->path->device->ident_data;
292
293	switch (softc->action) {
294	case PROBE_RESET:
295		cam_fill_ataio(ataio,
296		      0,
297		      probedone,
298		      /*flags*/CAM_DIR_NONE,
299		      0,
300		      /*data_ptr*/NULL,
301		      /*dxfer_len*/0,
302		      (start_ccb->ccb_h.target_id == 15 ? 3 : 15) * 1000);
303		ata_reset_cmd(ataio);
304		break;
305	case PROBE_IDENTIFY:
306		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
307			/* Prepare check that it is the same device. */
308			MD5_CTX context;
309
310			MD5Init(&context);
311			MD5Update(&context,
312			    (unsigned char *)ident_buf->model,
313			    sizeof(ident_buf->model));
314			MD5Update(&context,
315			    (unsigned char *)ident_buf->revision,
316			    sizeof(ident_buf->revision));
317			MD5Update(&context,
318			    (unsigned char *)ident_buf->serial,
319			    sizeof(ident_buf->serial));
320			MD5Final(softc->digest, &context);
321		}
322		cam_fill_ataio(ataio,
323		      1,
324		      probedone,
325		      /*flags*/CAM_DIR_IN,
326		      0,
327		      /*data_ptr*/(u_int8_t *)ident_buf,
328		      /*dxfer_len*/sizeof(struct ata_params),
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_SETMODE:
336	{
337		int mode, wantmode;
338
339		mode = 0;
340		/* Fetch user modes from SIM. */
341		bzero(&cts, sizeof(cts));
342		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
343		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
344		cts.type = CTS_TYPE_USER_SETTINGS;
345		xpt_action((union ccb *)&cts);
346		if (path->device->transport == XPORT_ATA) {
347			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
348				mode = cts.xport_specific.ata.mode;
349		} else {
350			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_MODE)
351				mode = cts.xport_specific.sata.mode;
352		}
353negotiate:
354		/* Honor device capabilities. */
355		wantmode = mode = ata_max_mode(ident_buf, mode);
356		/* Report modes to SIM. */
357		bzero(&cts, sizeof(cts));
358		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
359		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
360		cts.type = CTS_TYPE_CURRENT_SETTINGS;
361		if (path->device->transport == XPORT_ATA) {
362			cts.xport_specific.ata.mode = mode;
363			cts.xport_specific.ata.valid = CTS_ATA_VALID_MODE;
364		} else {
365			cts.xport_specific.sata.mode = mode;
366			cts.xport_specific.sata.valid = CTS_SATA_VALID_MODE;
367		}
368		xpt_action((union ccb *)&cts);
369		/* Fetch current modes from SIM. */
370		bzero(&cts, sizeof(cts));
371		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
372		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
373		cts.type = CTS_TYPE_CURRENT_SETTINGS;
374		xpt_action((union ccb *)&cts);
375		if (path->device->transport == XPORT_ATA) {
376			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_MODE)
377				mode = cts.xport_specific.ata.mode;
378		} else {
379			if (cts.xport_specific.ata.valid & CTS_SATA_VALID_MODE)
380				mode = cts.xport_specific.sata.mode;
381		}
382		/* If SIM disagree - renegotiate. */
383		if (mode != wantmode)
384			goto negotiate;
385		cam_fill_ataio(ataio,
386		      1,
387		      probedone,
388		      /*flags*/CAM_DIR_NONE,
389		      0,
390		      /*data_ptr*/NULL,
391		      /*dxfer_len*/0,
392		      30 * 1000);
393		ata_28bit_cmd(ataio, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
394		break;
395	}
396	case PROBE_SET_MULTI:
397	{
398		u_int sectors, bytecount;
399
400		bytecount = 8192;	/* SATA maximum */
401		/* Fetch user bytecount from SIM. */
402		bzero(&cts, sizeof(cts));
403		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
404		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
405		cts.type = CTS_TYPE_USER_SETTINGS;
406		xpt_action((union ccb *)&cts);
407		if (path->device->transport == XPORT_ATA) {
408			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
409				bytecount = cts.xport_specific.ata.bytecount;
410		} else {
411			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
412				bytecount = cts.xport_specific.sata.bytecount;
413		}
414		/* Honor device capabilities. */
415		sectors = max(1, min(ident_buf->sectors_intr & 0xff,
416		    bytecount / ata_logical_sector_size(ident_buf)));
417		/* Report bytecount to SIM. */
418		bzero(&cts, sizeof(cts));
419		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
420		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
421		cts.type = CTS_TYPE_CURRENT_SETTINGS;
422		if (path->device->transport == XPORT_ATA) {
423			cts.xport_specific.ata.bytecount = sectors *
424			    ata_logical_sector_size(ident_buf);
425			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
426		} else {
427			cts.xport_specific.sata.bytecount = sectors *
428			    ata_logical_sector_size(ident_buf);
429			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
430		}
431		xpt_action((union ccb *)&cts);
432		/* Fetch current bytecount from SIM. */
433		bzero(&cts, sizeof(cts));
434		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
435		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
436		cts.type = CTS_TYPE_CURRENT_SETTINGS;
437		xpt_action((union ccb *)&cts);
438		if (path->device->transport == XPORT_ATA) {
439			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
440				bytecount = cts.xport_specific.ata.bytecount;
441		} else {
442			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
443				bytecount = cts.xport_specific.sata.bytecount;
444		}
445		sectors = bytecount / ata_logical_sector_size(ident_buf);
446
447		cam_fill_ataio(ataio,
448		    1,
449		    probedone,
450		    CAM_DIR_NONE,
451		    0,
452		    NULL,
453		    0,
454		    30*1000);
455		ata_28bit_cmd(ataio, ATA_SET_MULTI, 0, 0, sectors);
456		break;
457	}
458	case PROBE_INQUIRY:
459	{
460		u_int bytecount;
461
462		bytecount = 8192;	/* SATA maximum */
463		/* Fetch user bytecount from SIM. */
464		bzero(&cts, sizeof(cts));
465		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
466		cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
467		cts.type = CTS_TYPE_USER_SETTINGS;
468		xpt_action((union ccb *)&cts);
469		if (path->device->transport == XPORT_ATA) {
470			if (cts.xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
471				bytecount = cts.xport_specific.ata.bytecount;
472		} else {
473			if (cts.xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
474				bytecount = cts.xport_specific.sata.bytecount;
475		}
476		/* Honor device capabilities. */
477		bytecount &= ~1;
478		bytecount = max(2, min(65534, bytecount));
479		if (ident_buf->satacapabilities != 0x0000 &&
480		    ident_buf->satacapabilities != 0xffff) {
481			bytecount = min(8192, bytecount);
482		}
483		/* Report bytecount to SIM. */
484		bzero(&cts, sizeof(cts));
485		xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
486		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
487		cts.type = CTS_TYPE_CURRENT_SETTINGS;
488		if (path->device->transport == XPORT_ATA) {
489			cts.xport_specific.ata.bytecount = bytecount;
490			cts.xport_specific.ata.valid = CTS_ATA_VALID_BYTECOUNT;
491		} else {
492			cts.xport_specific.sata.bytecount = bytecount;
493			cts.xport_specific.sata.valid = CTS_SATA_VALID_BYTECOUNT;
494		}
495		xpt_action((union ccb *)&cts);
496		/* FALLTHROUGH */
497	}
498	case PROBE_FULL_INQUIRY:
499	{
500		u_int inquiry_len;
501		struct scsi_inquiry_data *inq_buf =
502		    &periph->path->device->inq_data;
503
504		if (softc->action == PROBE_INQUIRY)
505			inquiry_len = SHORT_INQUIRY_LENGTH;
506		else
507			inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
508		/*
509		 * Some parallel SCSI devices fail to send an
510		 * ignore wide residue message when dealing with
511		 * odd length inquiry requests.  Round up to be
512		 * safe.
513		 */
514		inquiry_len = roundup2(inquiry_len, 2);
515		scsi_inquiry(csio,
516			     /*retries*/1,
517			     probedone,
518			     MSG_SIMPLE_Q_TAG,
519			     (u_int8_t *)inq_buf,
520			     inquiry_len,
521			     /*evpd*/FALSE,
522			     /*page_code*/0,
523			     SSD_MIN_SIZE,
524			     /*timeout*/60 * 1000);
525		break;
526	}
527	case PROBE_PM_PID:
528		cam_fill_ataio(ataio,
529		      1,
530		      probedone,
531		      /*flags*/CAM_DIR_NONE,
532		      0,
533		      /*data_ptr*/NULL,
534		      /*dxfer_len*/0,
535		      10 * 1000);
536		ata_pm_read_cmd(ataio, 0, 15);
537		break;
538	case PROBE_PM_PRV:
539		cam_fill_ataio(ataio,
540		      1,
541		      probedone,
542		      /*flags*/CAM_DIR_NONE,
543		      0,
544		      /*data_ptr*/NULL,
545		      /*dxfer_len*/0,
546		      10 * 1000);
547		ata_pm_read_cmd(ataio, 1, 15);
548		break;
549	case PROBE_INVALID:
550		CAM_DEBUG(path, CAM_DEBUG_INFO,
551		    ("probestart: invalid action state\n"));
552	default:
553		break;
554	}
555	xpt_action(start_ccb);
556}
557#if 0
558static void
559proberequestdefaultnegotiation(struct cam_periph *periph)
560{
561	struct ccb_trans_settings cts;
562
563	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
564	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
565	cts.type = CTS_TYPE_USER_SETTINGS;
566	xpt_action((union ccb *)&cts);
567	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
568		return;
569	}
570	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
571	cts.type = CTS_TYPE_CURRENT_SETTINGS;
572	xpt_action((union ccb *)&cts);
573}
574
575/*
576 * Backoff Negotiation Code- only pertinent for SPI devices.
577 */
578static int
579proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
580{
581	struct ccb_trans_settings cts;
582	struct ccb_trans_settings_spi *spi;
583
584	memset(&cts, 0, sizeof (cts));
585	xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
586	cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
587	cts.type = CTS_TYPE_CURRENT_SETTINGS;
588	xpt_action((union ccb *)&cts);
589	if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
590		if (bootverbose) {
591			xpt_print(periph->path,
592			    "failed to get current device settings\n");
593		}
594		return (0);
595	}
596	if (cts.transport != XPORT_SPI) {
597		if (bootverbose) {
598			xpt_print(periph->path, "not SPI transport\n");
599		}
600		return (0);
601	}
602	spi = &cts.xport_specific.spi;
603
604	/*
605	 * We cannot renegotiate sync rate if we don't have one.
606	 */
607	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
608		if (bootverbose) {
609			xpt_print(periph->path, "no sync rate known\n");
610		}
611		return (0);
612	}
613
614	/*
615	 * We'll assert that we don't have to touch PPR options- the
616	 * SIM will see what we do with period and offset and adjust
617	 * the PPR options as appropriate.
618	 */
619
620	/*
621	 * A sync rate with unknown or zero offset is nonsensical.
622	 * A sync period of zero means Async.
623	 */
624	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
625	 || spi->sync_offset == 0 || spi->sync_period == 0) {
626		if (bootverbose) {
627			xpt_print(periph->path, "no sync rate available\n");
628		}
629		return (0);
630	}
631
632	if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
633		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
634		    ("hit async: giving up on DV\n"));
635		return (0);
636	}
637
638
639	/*
640	 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
641	 * We don't try to remember 'last' settings to see if the SIM actually
642	 * gets into the speed we want to set. We check on the SIM telling
643	 * us that a requested speed is bad, but otherwise don't try and
644	 * check the speed due to the asynchronous and handshake nature
645	 * of speed setting.
646	 */
647	spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
648	for (;;) {
649		spi->sync_period++;
650		if (spi->sync_period >= 0xf) {
651			spi->sync_period = 0;
652			spi->sync_offset = 0;
653			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
654			    ("setting to async for DV\n"));
655			/*
656			 * Once we hit async, we don't want to try
657			 * any more settings.
658			 */
659			device->flags |= CAM_DEV_DV_HIT_BOTTOM;
660		} else if (bootverbose) {
661			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
662			    ("DV: period 0x%x\n", spi->sync_period));
663			printf("setting period to 0x%x\n", spi->sync_period);
664		}
665		cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
666		cts.type = CTS_TYPE_CURRENT_SETTINGS;
667		xpt_action((union ccb *)&cts);
668		if ((cts.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
669			break;
670		}
671		CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
672		    ("DV: failed to set period 0x%x\n", spi->sync_period));
673		if (spi->sync_period == 0) {
674			return (0);
675		}
676	}
677	return (1);
678}
679#endif
680static void
681probedone(struct cam_periph *periph, union ccb *done_ccb)
682{
683	struct ccb_trans_settings cts;
684	struct ata_params *ident_buf;
685	probe_softc *softc;
686	struct cam_path *path;
687	u_int32_t  priority;
688	int found = 1;
689
690	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
691
692	softc = (probe_softc *)periph->softc;
693	path = done_ccb->ccb_h.path;
694	priority = done_ccb->ccb_h.pinfo.priority;
695	ident_buf = &path->device->ident_data;
696
697	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
698device_fail:	if (cam_periph_error(done_ccb, 0, 0,
699		    &softc->saved_ccb) == ERESTART) {
700			return;
701		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
702			/* Don't wedge the queue */
703			xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
704					 /*run_queue*/TRUE);
705		}
706		/* Old PIO2 devices may not support mode setting. */
707		if (softc->action == PROBE_SETMODE &&
708		    ata_max_pmode(ident_buf) <= ATA_PIO2 &&
709		    (ident_buf->capabilities1 & ATA_SUPPORT_IORDY) == 0)
710			goto noerror;
711		/*
712		 * If we get to this point, we got an error status back
713		 * from the inquiry and the error status doesn't require
714		 * automatically retrying the command.  Therefore, the
715		 * inquiry failed.  If we had inquiry information before
716		 * for this device, but this latest inquiry command failed,
717		 * the device has probably gone away.  If this device isn't
718		 * already marked unconfigured, notify the peripheral
719		 * drivers that this device is no more.
720		 */
721		if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
722			xpt_async(AC_LOST_DEVICE, path, NULL);
723		found = 0;
724		goto done;
725	}
726noerror:
727	switch (softc->action) {
728	case PROBE_RESET:
729	{
730		int sign = (done_ccb->ataio.res.lba_high << 8) +
731		    done_ccb->ataio.res.lba_mid;
732		if (bootverbose)
733			xpt_print(path, "SIGNATURE: %04x\n", sign);
734		if (sign == 0x0000 &&
735		    done_ccb->ccb_h.target_id != 15) {
736			path->device->protocol = PROTO_ATA;
737			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
738		} else if (sign == 0x9669 &&
739		    done_ccb->ccb_h.target_id == 15) {
740			/* Report SIM that PM is present. */
741			bzero(&cts, sizeof(cts));
742			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
743			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
744			cts.type = CTS_TYPE_CURRENT_SETTINGS;
745			cts.xport_specific.sata.pm_present = 1;
746			cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
747			xpt_action((union ccb *)&cts);
748			path->device->protocol = PROTO_SATAPM;
749			PROBE_SET_ACTION(softc, PROBE_PM_PID);
750		} else if (sign == 0xeb14 &&
751		    done_ccb->ccb_h.target_id != 15) {
752			path->device->protocol = PROTO_SCSI;
753			PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
754		} else {
755			if (done_ccb->ccb_h.target_id != 15) {
756				xpt_print(path,
757				    "Unexpected signature 0x%04x\n", sign);
758			}
759			goto device_fail;
760		}
761		xpt_release_ccb(done_ccb);
762		xpt_schedule(periph, priority);
763		return;
764	}
765	case PROBE_IDENTIFY:
766	{
767		int16_t *ptr;
768
769		for (ptr = (int16_t *)ident_buf;
770		     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
771			*ptr = le16toh(*ptr);
772		}
773		if (strncmp(ident_buf->model, "FX", 2) &&
774		    strncmp(ident_buf->model, "NEC", 3) &&
775		    strncmp(ident_buf->model, "Pioneer", 7) &&
776		    strncmp(ident_buf->model, "SHARP", 5)) {
777			ata_bswap(ident_buf->model, sizeof(ident_buf->model));
778			ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
779			ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
780		}
781		ata_btrim(ident_buf->model, sizeof(ident_buf->model));
782		ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
783		ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
784		ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
785		ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
786		ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
787
788		if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
789			/* Check that it is the same device. */
790			MD5_CTX context;
791			u_int8_t digest[16];
792
793			MD5Init(&context);
794			MD5Update(&context,
795			    (unsigned char *)ident_buf->model,
796			    sizeof(ident_buf->model));
797			MD5Update(&context,
798			    (unsigned char *)ident_buf->revision,
799			    sizeof(ident_buf->revision));
800			MD5Update(&context,
801			    (unsigned char *)ident_buf->serial,
802			    sizeof(ident_buf->serial));
803			MD5Final(digest, &context);
804			if (bcmp(digest, softc->digest, sizeof(digest))) {
805				/* Device changed. */
806				xpt_async(AC_LOST_DEVICE, path, NULL);
807			}
808		} else {
809			/* Clean up from previous instance of this device */
810			if (path->device->serial_num != NULL) {
811				free(path->device->serial_num, M_CAMXPT);
812				path->device->serial_num = NULL;
813				path->device->serial_num_len = 0;
814			}
815			path->device->serial_num =
816				(u_int8_t *)malloc((sizeof(ident_buf->serial) + 1),
817					   M_CAMXPT, M_NOWAIT);
818			if (path->device->serial_num != NULL) {
819				bcopy(ident_buf->serial,
820				      path->device->serial_num,
821				      sizeof(ident_buf->serial));
822				path->device->serial_num[sizeof(ident_buf->serial)]
823				    = '\0';
824				path->device->serial_num_len =
825				    strlen(path->device->serial_num);
826			}
827
828			path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
829		}
830		if (ident_buf->satacapabilities & ATA_SUPPORT_NCQ) {
831			path->device->mintags = path->device->maxtags =
832			    ATA_QUEUE_LEN(ident_buf->queue) + 1;
833		}
834		ata_find_quirk(path->device);
835		if (path->device->mintags != 0 &&
836		    path->bus->sim->max_tagged_dev_openings != 0) {
837			/* Report SIM which tags are allowed. */
838			bzero(&cts, sizeof(cts));
839			xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
840			cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
841			cts.type = CTS_TYPE_CURRENT_SETTINGS;
842			cts.xport_specific.sata.tags = path->device->maxtags;
843			cts.xport_specific.sata.valid = CTS_SATA_VALID_TAGS;
844			xpt_action((union ccb *)&cts);
845			/* Reconfigure queues for tagged queueing. */
846			xpt_start_tags(path);
847		}
848		ata_device_transport(path);
849		PROBE_SET_ACTION(softc, PROBE_SETMODE);
850		xpt_release_ccb(done_ccb);
851		xpt_schedule(periph, priority);
852		return;
853	}
854	case PROBE_SETMODE:
855		if (path->device->protocol == PROTO_ATA) {
856			PROBE_SET_ACTION(softc, PROBE_SET_MULTI);
857		} else {
858			PROBE_SET_ACTION(softc, PROBE_INQUIRY);
859		}
860		xpt_release_ccb(done_ccb);
861		xpt_schedule(periph, priority);
862		return;
863	case PROBE_SET_MULTI:
864		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
865			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
866			xpt_acquire_device(path->device);
867			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
868			xpt_action(done_ccb);
869			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
870			    done_ccb);
871		}
872		break;
873	case PROBE_INQUIRY:
874	case PROBE_FULL_INQUIRY:
875	{
876		struct scsi_inquiry_data *inq_buf;
877		u_int8_t periph_qual, len;
878
879		path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
880		inq_buf = &path->device->inq_data;
881
882		periph_qual = SID_QUAL(inq_buf);
883
884		if (periph_qual != SID_QUAL_LU_CONNECTED)
885			break;
886
887		/*
888		 * We conservatively request only
889		 * SHORT_INQUIRY_LEN bytes of inquiry
890		 * information during our first try
891		 * at sending an INQUIRY. If the device
892		 * has more information to give,
893		 * perform a second request specifying
894		 * the amount of information the device
895		 * is willing to give.
896		 */
897		len = inq_buf->additional_length
898		    + offsetof(struct scsi_inquiry_data, additional_length) + 1;
899		if (softc->action == PROBE_INQUIRY
900		    && len > SHORT_INQUIRY_LENGTH) {
901			PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
902			xpt_release_ccb(done_ccb);
903			xpt_schedule(periph, priority);
904			return;
905		}
906
907		ata_device_transport(path);
908		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
909			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
910			xpt_acquire_device(path->device);
911			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
912			xpt_action(done_ccb);
913			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path, done_ccb);
914		}
915		break;
916	}
917	case PROBE_PM_PID:
918		if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) == 0)
919			bzero(ident_buf, sizeof(*ident_buf));
920		softc->pm_pid = (done_ccb->ataio.res.lba_high << 24) +
921		    (done_ccb->ataio.res.lba_mid << 16) +
922		    (done_ccb->ataio.res.lba_low << 8) +
923		    done_ccb->ataio.res.sector_count;
924		((uint32_t *)ident_buf)[0] = softc->pm_pid;
925		snprintf(ident_buf->model, sizeof(ident_buf->model),
926		    "Port Multiplier %08x", softc->pm_pid);
927		PROBE_SET_ACTION(softc, PROBE_PM_PRV);
928		xpt_release_ccb(done_ccb);
929		xpt_schedule(periph, priority);
930		return;
931	case PROBE_PM_PRV:
932		softc->pm_prv = (done_ccb->ataio.res.lba_high << 24) +
933		    (done_ccb->ataio.res.lba_mid << 16) +
934		    (done_ccb->ataio.res.lba_low << 8) +
935		    done_ccb->ataio.res.sector_count;
936		((uint32_t *)ident_buf)[1] = softc->pm_prv;
937		snprintf(ident_buf->revision, sizeof(ident_buf->revision),
938		    "%04x", softc->pm_prv);
939		path->device->flags |= CAM_DEV_IDENTIFY_DATA_VALID;
940		if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
941			path->device->flags &= ~CAM_DEV_UNCONFIGURED;
942			xpt_acquire_device(path->device);
943			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
944			xpt_action(done_ccb);
945			xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
946			    done_ccb);
947		} else {
948			done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
949			xpt_action(done_ccb);
950			xpt_async(AC_SCSI_AEN, done_ccb->ccb_h.path, done_ccb);
951		}
952		break;
953	case PROBE_INVALID:
954		CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_INFO,
955		    ("probedone: invalid action state\n"));
956	default:
957		break;
958	}
959done:
960	xpt_release_ccb(done_ccb);
961	done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
962	TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
963	done_ccb->ccb_h.status = CAM_REQ_CMP;
964	done_ccb->ccb_h.ppriv_field1 = found;
965	xpt_done(done_ccb);
966	if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
967		cam_periph_invalidate(periph);
968		cam_periph_release_locked(periph);
969	} else {
970		probeschedule(periph);
971	}
972}
973
974static void
975probecleanup(struct cam_periph *periph)
976{
977	free(periph->softc, M_CAMXPT);
978}
979
980static void
981ata_find_quirk(struct cam_ed *device)
982{
983	struct ata_quirk_entry *quirk;
984	caddr_t	match;
985
986	match = cam_quirkmatch((caddr_t)&device->ident_data,
987			       (caddr_t)ata_quirk_table,
988			       ata_quirk_table_size,
989			       sizeof(*ata_quirk_table), ata_identify_match);
990
991	if (match == NULL)
992		panic("xpt_find_quirk: device didn't match wildcard entry!!");
993
994	quirk = (struct ata_quirk_entry *)match;
995	device->quirk = quirk;
996	if (quirk->quirks & CAM_QUIRK_MAXTAGS)
997		device->mintags = device->maxtags = quirk->maxtags;
998}
999
1000typedef struct {
1001	union	ccb *request_ccb;
1002	struct 	ccb_pathinq *cpi;
1003	int	counter;
1004	int	found;
1005} ata_scan_bus_info;
1006
1007/*
1008 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1009 * As the scan progresses, xpt_scan_bus is used as the
1010 * callback on completion function.
1011 */
1012static void
1013ata_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1014{
1015	struct	cam_path *path;
1016	ata_scan_bus_info *scan_info;
1017	union	ccb *work_ccb;
1018	cam_status status;
1019
1020	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1021		  ("xpt_scan_bus\n"));
1022	switch (request_ccb->ccb_h.func_code) {
1023	case XPT_SCAN_BUS:
1024		/* Find out the characteristics of the bus */
1025		work_ccb = xpt_alloc_ccb_nowait();
1026		if (work_ccb == NULL) {
1027			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1028			xpt_done(request_ccb);
1029			return;
1030		}
1031		xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1032			      request_ccb->ccb_h.pinfo.priority);
1033		work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1034		xpt_action(work_ccb);
1035		if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1036			request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1037			xpt_free_ccb(work_ccb);
1038			xpt_done(request_ccb);
1039			return;
1040		}
1041
1042		/* Save some state for use while we probe for devices */
1043		scan_info = (ata_scan_bus_info *)
1044		    malloc(sizeof(ata_scan_bus_info), M_CAMXPT, M_NOWAIT);
1045		if (scan_info == NULL) {
1046			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1047			xpt_done(request_ccb);
1048			return;
1049		}
1050		scan_info->request_ccb = request_ccb;
1051		scan_info->cpi = &work_ccb->cpi;
1052		if (scan_info->cpi->transport == XPORT_ATA)
1053			scan_info->found = 0x0003;
1054		else
1055			scan_info->found = 0x8001;
1056		scan_info->counter = 0;
1057		/* If PM supported, probe it first. */
1058		if (scan_info->cpi->hba_inquiry & PI_SATAPM)
1059			scan_info->counter = 15;
1060
1061		work_ccb = xpt_alloc_ccb_nowait();
1062		if (work_ccb == NULL) {
1063			free(scan_info, M_CAMXPT);
1064			request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1065			xpt_done(request_ccb);
1066			break;
1067		}
1068		goto scan_next;
1069	case XPT_SCAN_LUN:
1070		work_ccb = request_ccb;
1071		/* Reuse the same CCB to query if a device was really found */
1072		scan_info = (ata_scan_bus_info *)work_ccb->ccb_h.ppriv_ptr0;
1073		/* Free the current request path- we're done with it. */
1074		xpt_free_path(work_ccb->ccb_h.path);
1075		/* If there is PMP... */
1076		if (scan_info->counter == 15) {
1077			if (work_ccb->ccb_h.ppriv_field1 != 0) {
1078				/* everything else willbe probed by it */
1079				scan_info->found = 0x8000;
1080			} else {
1081				struct ccb_trans_settings cts;
1082
1083				/* Report SIM that PM is absent. */
1084				bzero(&cts, sizeof(cts));
1085				xpt_setup_ccb(&cts.ccb_h,
1086				    scan_info->request_ccb->ccb_h.path, 1);
1087				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1088				cts.type = CTS_TYPE_CURRENT_SETTINGS;
1089				cts.xport_specific.sata.pm_present = 0;
1090				cts.xport_specific.sata.valid = CTS_SATA_VALID_PM;
1091				xpt_action((union ccb *)&cts);
1092			}
1093		}
1094take_next:
1095		/* Take next device. Wrap from 15 (PM) to 0. */
1096		scan_info->counter = (scan_info->counter + 1 ) & 0x0f;
1097		if (scan_info->counter > scan_info->cpi->max_target -
1098		    ((scan_info->cpi->hba_inquiry & PI_SATAPM) ? 1 : 0)) {
1099			xpt_free_ccb(work_ccb);
1100			xpt_free_ccb((union ccb *)scan_info->cpi);
1101			request_ccb = scan_info->request_ccb;
1102			free(scan_info, M_CAMXPT);
1103			request_ccb->ccb_h.status = CAM_REQ_CMP;
1104			xpt_done(request_ccb);
1105			break;
1106		}
1107scan_next:
1108		if ((scan_info->found & (1 << scan_info->counter)) == 0)
1109			goto take_next;
1110		status = xpt_create_path(&path, xpt_periph,
1111		    scan_info->request_ccb->ccb_h.path_id,
1112		    scan_info->counter, 0);
1113		if (status != CAM_REQ_CMP) {
1114			printf("xpt_scan_bus: xpt_create_path failed"
1115			    " with status %#x, bus scan halted\n",
1116			    status);
1117			xpt_free_ccb(work_ccb);
1118			xpt_free_ccb((union ccb *)scan_info->cpi);
1119			request_ccb = scan_info->request_ccb;
1120			free(scan_info, M_CAMXPT);
1121			request_ccb->ccb_h.status = status;
1122			xpt_done(request_ccb);
1123			break;
1124		}
1125		xpt_setup_ccb(&work_ccb->ccb_h, path,
1126		    scan_info->request_ccb->ccb_h.pinfo.priority);
1127		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1128		work_ccb->ccb_h.cbfcnp = ata_scan_bus;
1129		work_ccb->ccb_h.ppriv_ptr0 = scan_info;
1130		work_ccb->crcn.flags = scan_info->request_ccb->crcn.flags;
1131		xpt_action(work_ccb);
1132		break;
1133	default:
1134		break;
1135	}
1136}
1137
1138static void
1139ata_scan_lun(struct cam_periph *periph, struct cam_path *path,
1140	     cam_flags flags, union ccb *request_ccb)
1141{
1142	struct ccb_pathinq cpi;
1143	cam_status status;
1144	struct cam_path *new_path;
1145	struct cam_periph *old_periph;
1146
1147	CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1148		  ("xpt_scan_lun\n"));
1149
1150	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1151	cpi.ccb_h.func_code = XPT_PATH_INQ;
1152	xpt_action((union ccb *)&cpi);
1153
1154	if (cpi.ccb_h.status != CAM_REQ_CMP) {
1155		if (request_ccb != NULL) {
1156			request_ccb->ccb_h.status = cpi.ccb_h.status;
1157			xpt_done(request_ccb);
1158		}
1159		return;
1160	}
1161
1162	if (request_ccb == NULL) {
1163		request_ccb = malloc(sizeof(union ccb), M_CAMXPT, M_NOWAIT);
1164		if (request_ccb == NULL) {
1165			xpt_print(path, "xpt_scan_lun: can't allocate CCB, "
1166			    "can't continue\n");
1167			return;
1168		}
1169		new_path = malloc(sizeof(*new_path), M_CAMXPT, M_NOWAIT);
1170		if (new_path == NULL) {
1171			xpt_print(path, "xpt_scan_lun: can't allocate path, "
1172			    "can't continue\n");
1173			free(request_ccb, M_CAMXPT);
1174			return;
1175		}
1176		status = xpt_compile_path(new_path, xpt_periph,
1177					  path->bus->path_id,
1178					  path->target->target_id,
1179					  path->device->lun_id);
1180
1181		if (status != CAM_REQ_CMP) {
1182			xpt_print(path, "xpt_scan_lun: can't compile path, "
1183			    "can't continue\n");
1184			free(request_ccb, M_CAMXPT);
1185			free(new_path, M_CAMXPT);
1186			return;
1187		}
1188		xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_NORMAL);
1189		request_ccb->ccb_h.cbfcnp = xptscandone;
1190		request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
1191		request_ccb->crcn.flags = flags;
1192	}
1193
1194	if ((old_periph = cam_periph_find(path, "aprobe")) != NULL) {
1195		probe_softc *softc;
1196
1197		softc = (probe_softc *)old_periph->softc;
1198		TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
1199				  periph_links.tqe);
1200	} else {
1201		status = cam_periph_alloc(proberegister, NULL, probecleanup,
1202					  probestart, "aprobe",
1203					  CAM_PERIPH_BIO,
1204					  request_ccb->ccb_h.path, NULL, 0,
1205					  request_ccb);
1206
1207		if (status != CAM_REQ_CMP) {
1208			xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
1209			    "returned an error, can't continue probe\n");
1210			request_ccb->ccb_h.status = status;
1211			xpt_done(request_ccb);
1212		}
1213	}
1214}
1215
1216static void
1217xptscandone(struct cam_periph *periph, union ccb *done_ccb)
1218{
1219	xpt_release_path(done_ccb->ccb_h.path);
1220	free(done_ccb->ccb_h.path, M_CAMXPT);
1221	free(done_ccb, M_CAMXPT);
1222}
1223
1224static struct cam_ed *
1225ata_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
1226{
1227	struct cam_path path;
1228	struct ata_quirk_entry *quirk;
1229	struct cam_ed *device;
1230	struct cam_ed *cur_device;
1231
1232	device = xpt_alloc_device(bus, target, lun_id);
1233	if (device == NULL)
1234		return (NULL);
1235
1236	/*
1237	 * Take the default quirk entry until we have inquiry
1238	 * data and can determine a better quirk to use.
1239	 */
1240	quirk = &ata_quirk_table[ata_quirk_table_size - 1];
1241	device->quirk = (void *)quirk;
1242	device->mintags = 0;
1243	device->maxtags = 0;
1244	bzero(&device->inq_data, sizeof(device->inq_data));
1245	device->inq_flags = 0;
1246	device->queue_flags = 0;
1247	device->serial_num = NULL;
1248	device->serial_num_len = 0;
1249
1250	/*
1251	 * XXX should be limited by number of CCBs this bus can
1252	 * do.
1253	 */
1254	bus->sim->max_ccbs += device->ccbq.devq_openings;
1255	/* Insertion sort into our target's device list */
1256	cur_device = TAILQ_FIRST(&target->ed_entries);
1257	while (cur_device != NULL && cur_device->lun_id < lun_id)
1258		cur_device = TAILQ_NEXT(cur_device, links);
1259	if (cur_device != NULL) {
1260		TAILQ_INSERT_BEFORE(cur_device, device, links);
1261	} else {
1262		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
1263	}
1264	target->generation++;
1265	if (lun_id != CAM_LUN_WILDCARD) {
1266		xpt_compile_path(&path,
1267				 NULL,
1268				 bus->path_id,
1269				 target->target_id,
1270				 lun_id);
1271		ata_device_transport(&path);
1272		xpt_release_path(&path);
1273	}
1274
1275	return (device);
1276}
1277
1278static void
1279ata_device_transport(struct cam_path *path)
1280{
1281	struct ccb_pathinq cpi;
1282	struct ccb_trans_settings cts;
1283	struct scsi_inquiry_data *inq_buf = NULL;
1284	struct ata_params *ident_buf = NULL;
1285
1286	/* Get transport information from the SIM */
1287	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1288	cpi.ccb_h.func_code = XPT_PATH_INQ;
1289	xpt_action((union ccb *)&cpi);
1290
1291	path->device->transport = cpi.transport;
1292	if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
1293		inq_buf = &path->device->inq_data;
1294	if ((path->device->flags & CAM_DEV_IDENTIFY_DATA_VALID) != 0)
1295		ident_buf = &path->device->ident_data;
1296	if (path->device->protocol == PROTO_ATA) {
1297		path->device->protocol_version = ident_buf ?
1298		    ata_version(ident_buf->version_major) : cpi.protocol_version;
1299	} else if (path->device->protocol == PROTO_SCSI) {
1300		path->device->protocol_version = inq_buf ?
1301		    SID_ANSI_REV(inq_buf) : cpi.protocol_version;
1302	}
1303	path->device->transport_version = ident_buf ?
1304	    ata_version(ident_buf->version_major) : cpi.transport_version;
1305
1306	/* Tell the controller what we think */
1307	xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1308	cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1309	cts.type = CTS_TYPE_CURRENT_SETTINGS;
1310	cts.transport = path->device->transport;
1311	cts.transport_version = path->device->transport_version;
1312	cts.protocol = path->device->protocol;
1313	cts.protocol_version = path->device->protocol_version;
1314	cts.proto_specific.valid = 0;
1315	cts.xport_specific.valid = 0;
1316	xpt_action((union ccb *)&cts);
1317}
1318
1319static void
1320ata_action(union ccb *start_ccb)
1321{
1322
1323	switch (start_ccb->ccb_h.func_code) {
1324	case XPT_SET_TRAN_SETTINGS:
1325	{
1326		ata_set_transfer_settings(&start_ccb->cts,
1327					   start_ccb->ccb_h.path->device,
1328					   /*async_update*/FALSE);
1329		break;
1330	}
1331	case XPT_SCAN_BUS:
1332		ata_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
1333		break;
1334	case XPT_SCAN_LUN:
1335		ata_scan_lun(start_ccb->ccb_h.path->periph,
1336			      start_ccb->ccb_h.path, start_ccb->crcn.flags,
1337			      start_ccb);
1338		break;
1339	case XPT_GET_TRAN_SETTINGS:
1340	{
1341		struct cam_sim *sim;
1342
1343		sim = start_ccb->ccb_h.path->bus->sim;
1344		(*(sim->sim_action))(sim, start_ccb);
1345		break;
1346	}
1347	default:
1348		xpt_action_default(start_ccb);
1349		break;
1350	}
1351}
1352
1353static void
1354ata_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
1355			   int async_update)
1356{
1357	struct	ccb_pathinq cpi;
1358	struct	ccb_trans_settings cur_cts;
1359	struct	ccb_trans_settings_scsi *scsi;
1360	struct	ccb_trans_settings_scsi *cur_scsi;
1361	struct	cam_sim *sim;
1362	struct	scsi_inquiry_data *inq_data;
1363
1364	if (device == NULL) {
1365		cts->ccb_h.status = CAM_PATH_INVALID;
1366		xpt_done((union ccb *)cts);
1367		return;
1368	}
1369
1370	if (cts->protocol == PROTO_UNKNOWN
1371	 || cts->protocol == PROTO_UNSPECIFIED) {
1372		cts->protocol = device->protocol;
1373		cts->protocol_version = device->protocol_version;
1374	}
1375
1376	if (cts->protocol_version == PROTO_VERSION_UNKNOWN
1377	 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
1378		cts->protocol_version = device->protocol_version;
1379
1380	if (cts->protocol != device->protocol) {
1381		xpt_print(cts->ccb_h.path, "Uninitialized Protocol %x:%x?\n",
1382		       cts->protocol, device->protocol);
1383		cts->protocol = device->protocol;
1384	}
1385
1386	if (cts->protocol_version > device->protocol_version) {
1387		if (bootverbose) {
1388			xpt_print(cts->ccb_h.path, "Down reving Protocol "
1389			    "Version from %d to %d?\n", cts->protocol_version,
1390			    device->protocol_version);
1391		}
1392		cts->protocol_version = device->protocol_version;
1393	}
1394
1395	if (cts->transport == XPORT_UNKNOWN
1396	 || cts->transport == XPORT_UNSPECIFIED) {
1397		cts->transport = device->transport;
1398		cts->transport_version = device->transport_version;
1399	}
1400
1401	if (cts->transport_version == XPORT_VERSION_UNKNOWN
1402	 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
1403		cts->transport_version = device->transport_version;
1404
1405	if (cts->transport != device->transport) {
1406		xpt_print(cts->ccb_h.path, "Uninitialized Transport %x:%x?\n",
1407		    cts->transport, device->transport);
1408		cts->transport = device->transport;
1409	}
1410
1411	if (cts->transport_version > device->transport_version) {
1412		if (bootverbose) {
1413			xpt_print(cts->ccb_h.path, "Down reving Transport "
1414			    "Version from %d to %d?\n", cts->transport_version,
1415			    device->transport_version);
1416		}
1417		cts->transport_version = device->transport_version;
1418	}
1419
1420	sim = cts->ccb_h.path->bus->sim;
1421
1422	/*
1423	 * Nothing more of interest to do unless
1424	 * this is a device connected via the
1425	 * SCSI protocol.
1426	 */
1427	if (cts->protocol != PROTO_SCSI) {
1428		if (async_update == FALSE)
1429			(*(sim->sim_action))(sim, (union ccb *)cts);
1430		return;
1431	}
1432
1433	inq_data = &device->inq_data;
1434	scsi = &cts->proto_specific.scsi;
1435	xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
1436	cpi.ccb_h.func_code = XPT_PATH_INQ;
1437	xpt_action((union ccb *)&cpi);
1438
1439	/* SCSI specific sanity checking */
1440	if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
1441	 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
1442	 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
1443	 || (device->mintags == 0)) {
1444		/*
1445		 * Can't tag on hardware that doesn't support tags,
1446		 * doesn't have it enabled, or has broken tag support.
1447		 */
1448		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1449	}
1450
1451	if (async_update == FALSE) {
1452		/*
1453		 * Perform sanity checking against what the
1454		 * controller and device can do.
1455		 */
1456		xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, CAM_PRIORITY_NORMAL);
1457		cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1458		cur_cts.type = cts->type;
1459		xpt_action((union ccb *)&cur_cts);
1460		if ((cur_cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1461			return;
1462		}
1463		cur_scsi = &cur_cts.proto_specific.scsi;
1464		if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
1465			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1466			scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
1467		}
1468		if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
1469			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1470	}
1471
1472	if (cts->type == CTS_TYPE_CURRENT_SETTINGS
1473	 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
1474		int device_tagenb;
1475
1476		/*
1477		 * If we are transitioning from tags to no-tags or
1478		 * vice-versa, we need to carefully freeze and restart
1479		 * the queue so that we don't overlap tagged and non-tagged
1480		 * commands.  We also temporarily stop tags if there is
1481		 * a change in transfer negotiation settings to allow
1482		 * "tag-less" negotiation.
1483		 */
1484		if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
1485		 || (device->inq_flags & SID_CmdQue) != 0)
1486			device_tagenb = TRUE;
1487		else
1488			device_tagenb = FALSE;
1489
1490		if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
1491		  && device_tagenb == FALSE)
1492		 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
1493		  && device_tagenb == TRUE)) {
1494
1495			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
1496				/*
1497				 * Delay change to use tags until after a
1498				 * few commands have gone to this device so
1499				 * the controller has time to perform transfer
1500				 * negotiations without tagged messages getting
1501				 * in the way.
1502				 */
1503				device->tag_delay_count = CAM_TAG_DELAY_COUNT;
1504				device->flags |= CAM_DEV_TAG_AFTER_COUNT;
1505			} else {
1506				xpt_stop_tags(cts->ccb_h.path);
1507			}
1508		}
1509	}
1510	if (async_update == FALSE)
1511		(*(sim->sim_action))(sim, (union ccb *)cts);
1512}
1513
1514/*
1515 * Handle any per-device event notifications that require action by the XPT.
1516 */
1517static void
1518ata_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
1519	      struct cam_ed *device, void *async_arg)
1520{
1521	cam_status status;
1522	struct cam_path newpath;
1523
1524	/*
1525	 * We only need to handle events for real devices.
1526	 */
1527	if (target->target_id == CAM_TARGET_WILDCARD
1528	 || device->lun_id == CAM_LUN_WILDCARD)
1529		return;
1530
1531	/*
1532	 * We need our own path with wildcards expanded to
1533	 * handle certain types of events.
1534	 */
1535	if ((async_code == AC_SENT_BDR)
1536	 || (async_code == AC_BUS_RESET)
1537	 || (async_code == AC_INQ_CHANGED))
1538		status = xpt_compile_path(&newpath, NULL,
1539					  bus->path_id,
1540					  target->target_id,
1541					  device->lun_id);
1542	else
1543		status = CAM_REQ_CMP_ERR;
1544
1545	if (status == CAM_REQ_CMP) {
1546		if (async_code == AC_INQ_CHANGED) {
1547			/*
1548			 * We've sent a start unit command, or
1549			 * something similar to a device that
1550			 * may have caused its inquiry data to
1551			 * change. So we re-scan the device to
1552			 * refresh the inquiry data for it.
1553			 */
1554			ata_scan_lun(newpath.periph, &newpath,
1555				     CAM_EXPECT_INQ_CHANGE, NULL);
1556		}
1557		xpt_release_path(&newpath);
1558	} else if (async_code == AC_LOST_DEVICE &&
1559	    (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1560		device->flags |= CAM_DEV_UNCONFIGURED;
1561		xpt_release_device(device);
1562	} else if (async_code == AC_TRANSFER_NEG) {
1563		struct ccb_trans_settings *settings;
1564
1565		settings = (struct ccb_trans_settings *)async_arg;
1566		ata_set_transfer_settings(settings, device,
1567					  /*async_update*/TRUE);
1568	}
1569}
1570
1571