xref: /freebsd/sys/cam/ata/ata_pmp.c (revision 5022f21bd974c740b9052f149fb31745dc602965)
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$");
29 
30 #include <sys/param.h>
31 
32 #ifdef _KERNEL
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bio.h>
36 #include <sys/sysctl.h>
37 #include <sys/taskqueue.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/conf.h>
41 #include <sys/devicestat.h>
42 #include <sys/eventhandler.h>
43 #include <sys/malloc.h>
44 #include <sys/cons.h>
45 #include <geom/geom_disk.h>
46 #endif /* _KERNEL */
47 
48 #ifndef _KERNEL
49 #include <stdio.h>
50 #include <string.h>
51 #endif /* _KERNEL */
52 
53 #include <cam/cam.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_sim.h>
58 
59 #include <cam/ata/ata_all.h>
60 
61 #ifdef _KERNEL
62 
63 typedef enum {
64 	PMP_STATE_NORMAL,
65 	PMP_STATE_PORTS,
66 	PMP_STATE_PRECONFIG,
67 	PMP_STATE_RESET,
68 	PMP_STATE_CONNECT,
69 	PMP_STATE_CHECK,
70 	PMP_STATE_CLEAR,
71 	PMP_STATE_CONFIG,
72 	PMP_STATE_SCAN
73 } pmp_state;
74 
75 typedef enum {
76 	PMP_FLAG_SCTX_INIT	= 0x200
77 } pmp_flags;
78 
79 typedef enum {
80 	PMP_CCB_PROBE		= 0x01,
81 } pmp_ccb_state;
82 
83 /* Offsets into our private area for storing information */
84 #define ccb_state	ppriv_field0
85 #define ccb_bp		ppriv_ptr1
86 
87 struct pmp_softc {
88 	SLIST_ENTRY(pmp_softc)	links;
89 	pmp_state		state;
90 	pmp_flags		flags;
91 	uint32_t		pm_pid;
92 	uint32_t		pm_prv;
93 	int			pm_ports;
94 	int			pm_step;
95 	int			pm_try;
96 	int			found;
97 	int			reset;
98 	int			frozen;
99 	int			restart;
100 	union			ccb saved_ccb;
101 	struct task		sysctl_task;
102 	struct sysctl_ctx_list	sysctl_ctx;
103 	struct sysctl_oid	*sysctl_tree;
104 };
105 
106 static	periph_init_t	pmpinit;
107 static	void		pmpasync(void *callback_arg, u_int32_t code,
108 				struct cam_path *path, void *arg);
109 static	void		pmpsysctlinit(void *context, int pending);
110 static	periph_ctor_t	pmpregister;
111 static	periph_dtor_t	pmpcleanup;
112 static	periph_start_t	pmpstart;
113 static	periph_oninv_t	pmponinvalidate;
114 static	void		pmpdone(struct cam_periph *periph,
115 			       union ccb *done_ccb);
116 
117 #ifndef PMP_DEFAULT_TIMEOUT
118 #define PMP_DEFAULT_TIMEOUT 30	/* Timeout in seconds */
119 #endif
120 
121 #ifndef	PMP_DEFAULT_RETRY
122 #define	PMP_DEFAULT_RETRY	1
123 #endif
124 
125 static int pmp_retry_count = PMP_DEFAULT_RETRY;
126 static int pmp_default_timeout = PMP_DEFAULT_TIMEOUT;
127 
128 SYSCTL_NODE(_kern_cam, OID_AUTO, pmp, CTLFLAG_RD, 0,
129             "CAM Direct Access Disk driver");
130 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, retry_count, CTLFLAG_RW,
131            &pmp_retry_count, 0, "Normal I/O retry count");
132 TUNABLE_INT("kern.cam.pmp.retry_count", &pmp_retry_count);
133 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, default_timeout, CTLFLAG_RW,
134            &pmp_default_timeout, 0, "Normal I/O timeout (in seconds)");
135 TUNABLE_INT("kern.cam.pmp.default_timeout", &pmp_default_timeout);
136 
137 static struct periph_driver pmpdriver =
138 {
139 	pmpinit, "pmp",
140 	TAILQ_HEAD_INITIALIZER(pmpdriver.units), /* generation */ 0,
141 	CAM_PERIPH_DRV_EARLY
142 };
143 
144 PERIPHDRIVER_DECLARE(pmp, pmpdriver);
145 
146 MALLOC_DEFINE(M_ATPMP, "ata_pmp", "ata_pmp buffers");
147 
148 static void
149 pmpinit(void)
150 {
151 	cam_status status;
152 
153 	/*
154 	 * Install a global async callback.  This callback will
155 	 * receive async callbacks like "new device found".
156 	 */
157 	status = xpt_register_async(AC_FOUND_DEVICE, pmpasync, NULL, NULL);
158 
159 	if (status != CAM_REQ_CMP) {
160 		printf("pmp: Failed to attach master async callback "
161 		       "due to status 0x%x!\n", status);
162 	}
163 }
164 
165 static void
166 pmpfreeze(struct cam_periph *periph, int mask)
167 {
168 	struct pmp_softc *softc = (struct pmp_softc *)periph->softc;
169 	struct cam_path *dpath;
170 	int i;
171 
172 	mask &= ~softc->frozen;
173 	for (i = 0; i < 15; i++) {
174 		if ((mask & (1 << i)) == 0)
175 			continue;
176 		if (xpt_create_path(&dpath, periph,
177 		    xpt_path_path_id(periph->path),
178 		    i, 0) == CAM_REQ_CMP) {
179 printf("PMP freeze: %d\n", i);
180 			softc->frozen |= (1 << i);
181 			cam_freeze_devq(dpath);
182 			xpt_free_path(dpath);
183 		}
184 	}
185 }
186 
187 static void
188 pmprelease(struct cam_periph *periph, int mask)
189 {
190 	struct pmp_softc *softc = (struct pmp_softc *)periph->softc;
191 	struct cam_path *dpath;
192 	int i;
193 
194 	mask &= softc->frozen;
195 	for (i = 0; i < 15; i++) {
196 		if ((mask & (1 << i)) == 0)
197 			continue;
198 		if (xpt_create_path(&dpath, periph,
199 		    xpt_path_path_id(periph->path),
200 		    i, 0) == CAM_REQ_CMP) {
201 printf("PMP release: %d\n", i);
202 			softc->frozen &= ~(1 << i);
203 			cam_release_devq(dpath, 0, 0, 0, FALSE);
204 			xpt_free_path(dpath);
205 		}
206 	}
207 }
208 
209 static void
210 pmponinvalidate(struct cam_periph *periph)
211 {
212 	struct pmp_softc *softc;
213 	struct cam_path *dpath;
214 	int i;
215 
216 	softc = (struct pmp_softc *)periph->softc;
217 
218 	/*
219 	 * De-register any async callbacks.
220 	 */
221 	xpt_register_async(0, pmpasync, periph, periph->path);
222 
223 	for (i = 0; i < 15; i++) {
224 		if (xpt_create_path(&dpath, periph,
225 		    xpt_path_path_id(periph->path),
226 		    i, 0) == CAM_REQ_CMP) {
227 			xpt_async(AC_LOST_DEVICE, dpath, NULL);
228 			xpt_free_path(dpath);
229 		}
230 	}
231 	xpt_print(periph->path, "lost device\n");
232 }
233 
234 static void
235 pmpcleanup(struct cam_periph *periph)
236 {
237 	struct pmp_softc *softc;
238 
239 	softc = (struct pmp_softc *)periph->softc;
240 
241 	xpt_print(periph->path, "removing device entry\n");
242 	cam_periph_unlock(periph);
243 
244 	/*
245 	 * If we can't free the sysctl tree, oh well...
246 	 */
247 	if ((softc->flags & PMP_FLAG_SCTX_INIT) != 0
248 	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
249 		xpt_print(periph->path, "can't remove sysctl context\n");
250 	}
251 
252 	free(softc, M_DEVBUF);
253 	cam_periph_lock(periph);
254 }
255 
256 static void
257 pmpasync(void *callback_arg, u_int32_t code,
258 	struct cam_path *path, void *arg)
259 {
260 	struct cam_periph *periph;
261 	struct pmp_softc *softc;
262 
263 	periph = (struct cam_periph *)callback_arg;
264 	switch (code) {
265 	case AC_FOUND_DEVICE:
266 	{
267 		struct ccb_getdev *cgd;
268 		cam_status status;
269 
270 		cgd = (struct ccb_getdev *)arg;
271 		if (cgd == NULL)
272 			break;
273 
274 		if (cgd->protocol != PROTO_SATAPM)
275 			break;
276 
277 		/*
278 		 * Allocate a peripheral instance for
279 		 * this device and start the probe
280 		 * process.
281 		 */
282 		status = cam_periph_alloc(pmpregister, pmponinvalidate,
283 					  pmpcleanup, pmpstart,
284 					  "pmp", CAM_PERIPH_BIO,
285 					  cgd->ccb_h.path, pmpasync,
286 					  AC_FOUND_DEVICE, cgd);
287 
288 		if (status != CAM_REQ_CMP
289 		 && status != CAM_REQ_INPROG)
290 			printf("pmpasync: Unable to attach to new device "
291 				"due to status 0x%x\n", status);
292 		break;
293 	}
294 	case AC_SCSI_AEN:
295 	case AC_SENT_BDR:
296 	case AC_BUS_RESET:
297 		softc = (struct pmp_softc *)periph->softc;
298 		cam_periph_async(periph, code, path, arg);
299 		if (code == AC_SCSI_AEN && softc->state != PMP_STATE_NORMAL &&
300 		    softc->state != PMP_STATE_SCAN)
301 			break;
302 		if (softc->state != PMP_STATE_SCAN)
303 			pmpfreeze(periph, softc->found);
304 		else
305 			pmpfreeze(periph, softc->found & ~(1 << softc->pm_step));
306 		if (code == AC_SENT_BDR || code == AC_BUS_RESET)
307 			softc->found = 0; /* We have to reset everything. */
308 		if (softc->state == PMP_STATE_NORMAL) {
309 			softc->state = PMP_STATE_PORTS;
310 			cam_periph_acquire(periph);
311 			xpt_schedule(periph, CAM_PRIORITY_BUS);
312 		} else
313 			softc->restart = 1;
314 		break;
315 	default:
316 		cam_periph_async(periph, code, path, arg);
317 		break;
318 	}
319 }
320 
321 static void
322 pmpsysctlinit(void *context, int pending)
323 {
324 	struct cam_periph *periph;
325 	struct pmp_softc *softc;
326 	char tmpstr[80], tmpstr2[80];
327 
328 	periph = (struct cam_periph *)context;
329 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
330 		return;
331 
332 	softc = (struct pmp_softc *)periph->softc;
333 	snprintf(tmpstr, sizeof(tmpstr), "CAM PMP unit %d", periph->unit_number);
334 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
335 
336 	sysctl_ctx_init(&softc->sysctl_ctx);
337 	softc->flags |= PMP_FLAG_SCTX_INIT;
338 	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
339 		SYSCTL_STATIC_CHILDREN(_kern_cam_pmp), OID_AUTO, tmpstr2,
340 		CTLFLAG_RD, 0, tmpstr);
341 	if (softc->sysctl_tree == NULL) {
342 		printf("pmpsysctlinit: unable to allocate sysctl tree\n");
343 		cam_periph_release(periph);
344 		return;
345 	}
346 
347 	cam_periph_release(periph);
348 }
349 
350 static cam_status
351 pmpregister(struct cam_periph *periph, void *arg)
352 {
353 	struct pmp_softc *softc;
354 	struct ccb_pathinq cpi;
355 	struct ccb_getdev *cgd;
356 
357 	cgd = (struct ccb_getdev *)arg;
358 	if (periph == NULL) {
359 		printf("pmpregister: periph was NULL!!\n");
360 		return(CAM_REQ_CMP_ERR);
361 	}
362 
363 	if (cgd == NULL) {
364 		printf("pmpregister: no getdev CCB, can't register device\n");
365 		return(CAM_REQ_CMP_ERR);
366 	}
367 
368 	softc = (struct pmp_softc *)malloc(sizeof(*softc), M_DEVBUF,
369 	    M_NOWAIT|M_ZERO);
370 
371 	if (softc == NULL) {
372 		printf("pmpregister: Unable to probe new device. "
373 		       "Unable to allocate softc\n");
374 		return(CAM_REQ_CMP_ERR);
375 	}
376 	periph->softc = softc;
377 
378 	softc->state = PMP_STATE_PORTS;
379 	softc->pm_pid = ((uint32_t *)&cgd->ident_data)[0];
380 	softc->pm_prv = ((uint32_t *)&cgd->ident_data)[1];
381 
382 	/* Check if the SIM does not want queued commands */
383 	bzero(&cpi, sizeof(cpi));
384 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
385 	cpi.ccb_h.func_code = XPT_PATH_INQ;
386 	xpt_action((union ccb *)&cpi);
387 
388 	TASK_INIT(&softc->sysctl_task, 0, pmpsysctlinit, periph);
389 
390 	xpt_announce_periph(periph, NULL);
391 
392 	/*
393 	 * Add async callbacks for bus reset and
394 	 * bus device reset calls.  I don't bother
395 	 * checking if this fails as, in most cases,
396 	 * the system will function just fine without
397 	 * them and the only alternative would be to
398 	 * not attach the device on failure.
399 	 */
400 	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
401 		AC_SCSI_AEN, pmpasync, periph, periph->path);
402 
403 	/*
404 	 * Take an exclusive refcount on the periph while pmpstart is called
405 	 * to finish the probe.  The reference will be dropped in pmpdone at
406 	 * the end of probe.
407 	 */
408 	(void)cam_periph_acquire(periph);
409 	xpt_schedule(periph, CAM_PRIORITY_BUS);
410 
411 	return(CAM_REQ_CMP);
412 }
413 
414 static void
415 pmpstart(struct cam_periph *periph, union ccb *start_ccb)
416 {
417 	struct ccb_ataio *ataio;
418 	struct pmp_softc *softc;
419 
420 	softc = (struct pmp_softc *)periph->softc;
421 	ataio = &start_ccb->ataio;
422 
423 	if (softc->restart) {
424 		softc->restart = 0;
425 		softc->state = PMP_STATE_PORTS;
426 	}
427 
428 	switch (softc->state) {
429 	case PMP_STATE_PORTS:
430 		cam_fill_ataio(ataio,
431 		      pmp_retry_count,
432 		      pmpdone,
433 		      /*flags*/CAM_DIR_NONE,
434 		      0,
435 		      /*data_ptr*/NULL,
436 		      /*dxfer_len*/0,
437 		      pmp_default_timeout * 1000);
438 		ata_pm_read_cmd(ataio, 2, 15);
439 		break;
440 	case PMP_STATE_PRECONFIG:
441 		cam_fill_ataio(ataio,
442 		      pmp_retry_count,
443 		      pmpdone,
444 		      /*flags*/CAM_DIR_NONE,
445 		      0,
446 		      /*data_ptr*/NULL,
447 		      /*dxfer_len*/0,
448 		      pmp_default_timeout * 1000);
449 		ata_pm_write_cmd(ataio, 0x60, 15, 0x0);
450 		break;
451 	case PMP_STATE_RESET:
452 		cam_fill_ataio(ataio,
453 		      pmp_retry_count,
454 		      pmpdone,
455 		      /*flags*/CAM_DIR_NONE,
456 		      0,
457 		      /*data_ptr*/NULL,
458 		      /*dxfer_len*/0,
459 		      pmp_default_timeout * 1000);
460 		ata_pm_write_cmd(ataio, 2, softc->pm_step,
461 		    (softc->found & (1 << softc->pm_step)) ? 0 : 1);
462 printf("PM RESET %d%s\n", softc->pm_step,
463     (softc->found & (1 << softc->pm_step)) ? " skipping" : "");
464 		break;
465 	case PMP_STATE_CONNECT:
466 		cam_fill_ataio(ataio,
467 		      pmp_retry_count,
468 		      pmpdone,
469 		      /*flags*/CAM_DIR_NONE,
470 		      0,
471 		      /*data_ptr*/NULL,
472 		      /*dxfer_len*/0,
473 		      pmp_default_timeout * 1000);
474 		ata_pm_write_cmd(ataio, 2, softc->pm_step, 0);
475 		break;
476 	case PMP_STATE_CHECK:
477 		cam_fill_ataio(ataio,
478 		      pmp_retry_count,
479 		      pmpdone,
480 		      /*flags*/CAM_DIR_NONE,
481 		      0,
482 		      /*data_ptr*/NULL,
483 		      /*dxfer_len*/0,
484 		      pmp_default_timeout * 1000);
485 		ata_pm_read_cmd(ataio, 0, softc->pm_step);
486 		break;
487 	case PMP_STATE_CLEAR:
488 		softc->reset = 0;
489 		cam_fill_ataio(ataio,
490 		      pmp_retry_count,
491 		      pmpdone,
492 		      /*flags*/CAM_DIR_NONE,
493 		      0,
494 		      /*data_ptr*/NULL,
495 		      /*dxfer_len*/0,
496 		      pmp_default_timeout * 1000);
497 		ata_pm_write_cmd(ataio, 1, softc->pm_step, 0xFFFFFFFF);
498 		break;
499 	case PMP_STATE_CONFIG:
500 		cam_fill_ataio(ataio,
501 		      pmp_retry_count,
502 		      pmpdone,
503 		      /*flags*/CAM_DIR_NONE,
504 		      0,
505 		      /*data_ptr*/NULL,
506 		      /*dxfer_len*/0,
507 		      pmp_default_timeout * 1000);
508 		ata_pm_write_cmd(ataio, 0x60, 15, 0xf);
509 		break;
510 	default:
511 		break;
512 	}
513 	xpt_action(start_ccb);
514 }
515 
516 static void
517 pmpdone(struct cam_periph *periph, union ccb *done_ccb)
518 {
519 	struct ccb_trans_settings cts;
520 	struct pmp_softc *softc;
521 	struct ccb_ataio *ataio;
522 	union ccb *work_ccb;
523 	struct cam_path *path, *dpath;
524 	u_int32_t  priority, res;
525 
526 	softc = (struct pmp_softc *)periph->softc;
527 	ataio = &done_ccb->ataio;
528 
529 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("pmpdone\n"));
530 
531 	path = done_ccb->ccb_h.path;
532 	priority = done_ccb->ccb_h.pinfo.priority;
533 
534 	if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
535 		if (cam_periph_error(done_ccb, 0, 0,
536 		    &softc->saved_ccb) == ERESTART) {
537 			return;
538 		} else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
539 			cam_release_devq(done_ccb->ccb_h.path,
540 			    /*relsim_flags*/0,
541 			    /*reduction*/0,
542 			    /*timeout*/0,
543 			    /*getcount_only*/0);
544 		}
545 		goto done;
546 	}
547 
548 	if (softc->restart) {
549 		softc->restart = 0;
550 		if (softc->state == PMP_STATE_SCAN) {
551 			pmpfreeze(periph, 1 << softc->pm_step);
552 			work_ccb = done_ccb;
553 			done_ccb = (union ccb*)work_ccb->ccb_h.ppriv_ptr0;
554 			/* Free the current request path- we're done with it. */
555 		    	xpt_free_path(work_ccb->ccb_h.path);
556 			xpt_free_ccb(work_ccb);
557 		}
558 		xpt_release_ccb(done_ccb);
559 		softc->state = PMP_STATE_PORTS;
560 		xpt_schedule(periph, priority);
561 		return;
562 	}
563 
564 	switch (softc->state) {
565 	case PMP_STATE_PORTS:
566 		softc->pm_ports = (done_ccb->ataio.res.lba_high << 24) +
567 		    (done_ccb->ataio.res.lba_mid << 16) +
568 		    (done_ccb->ataio.res.lba_low << 8) +
569 		    done_ccb->ataio.res.sector_count;
570 		/* This PMP declares 6 ports, while only 5 of them are real.
571 		 * Port 5 is enclosure management bridge port, which has implementation
572 		 * problems, causing probe faults. Hide it for now. */
573 		if (softc->pm_pid == 0x37261095 && softc->pm_ports == 6)
574 			softc->pm_ports = 5;
575 		/* This PMP declares 7 ports, while only 5 of them are real.
576 		 * Port 5 is some fake "Config  Disk" with 640 sectors size,
577 		 * port 6 is enclosure management bridge port.
578 		 * Both fake ports has implementation problems, causing
579 		 * probe faults. Hide them for now. */
580 		if (softc->pm_pid == 0x47261095 && softc->pm_ports == 7)
581 			softc->pm_ports = 5;
582 		/* These PMPs declare one more port then actually have,
583 		 * for configuration purposes. Hide it for now. */
584 		if (softc->pm_pid == 0x57231095 || softc->pm_pid == 0x57331095 ||
585 		    softc->pm_pid == 0x57341095 || softc->pm_pid == 0x57441095)
586 			softc->pm_ports--;
587 		printf("PM ports: %d\n", softc->pm_ports);
588 		softc->state = PMP_STATE_PRECONFIG;
589 		xpt_release_ccb(done_ccb);
590 		xpt_schedule(periph, priority);
591 		return;
592 	case PMP_STATE_PRECONFIG:
593 		softc->pm_step = 0;
594 		softc->state = PMP_STATE_RESET;
595 		softc->reset |= ~softc->found;
596 		xpt_release_ccb(done_ccb);
597 		xpt_schedule(periph, priority);
598 		return;
599 	case PMP_STATE_RESET:
600 		softc->pm_step++;
601 		if (softc->pm_step >= softc->pm_ports) {
602 			softc->pm_step = 0;
603 			cam_freeze_devq(periph->path);
604 			cam_release_devq(periph->path,
605 			    RELSIM_RELEASE_AFTER_TIMEOUT,
606 			    /*reduction*/0,
607 			    /*timeout*/5,
608 			    /*getcount_only*/0);
609 			printf("PM reset done\n");
610 			softc->state = PMP_STATE_CONNECT;
611 		}
612 		xpt_release_ccb(done_ccb);
613 		xpt_schedule(periph, priority);
614 		return;
615 	case PMP_STATE_CONNECT:
616 		softc->pm_step++;
617 		if (softc->pm_step >= softc->pm_ports) {
618 			softc->pm_step = 0;
619 			softc->pm_try = 0;
620 			cam_freeze_devq(periph->path);
621 			cam_release_devq(periph->path,
622 			    RELSIM_RELEASE_AFTER_TIMEOUT,
623 			    /*reduction*/0,
624 			    /*timeout*/10,
625 			    /*getcount_only*/0);
626 			printf("PM connect done\n");
627 			softc->state = PMP_STATE_CHECK;
628 		}
629 		xpt_release_ccb(done_ccb);
630 		xpt_schedule(periph, priority);
631 		return;
632 	case PMP_STATE_CHECK:
633 		res = (done_ccb->ataio.res.lba_high << 24) +
634 		    (done_ccb->ataio.res.lba_mid << 16) +
635 		    (done_ccb->ataio.res.lba_low << 8) +
636 		    done_ccb->ataio.res.sector_count;
637 		if ((res & 0xf0f) == 0x103 && (res & 0x0f0) != 0) {
638 			printf("PM status: %d - %08x\n", softc->pm_step, res);
639 			/* Report device speed. */
640 			if (xpt_create_path(&dpath, periph,
641 			    xpt_path_path_id(periph->path),
642 			    softc->pm_step, 0) == CAM_REQ_CMP) {
643 				bzero(&cts, sizeof(cts));
644 				xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NORMAL);
645 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
646 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
647 				cts.xport_specific.sata.revision = (res & 0x0f0) >> 4;
648 				cts.xport_specific.sata.valid = CTS_SATA_VALID_REVISION;
649 				xpt_action((union ccb *)&cts);
650 				xpt_free_path(dpath);
651 			}
652 			softc->found |= (1 << softc->pm_step);
653 			softc->pm_step++;
654 		} else {
655 			if (softc->pm_try < 10) {
656 				cam_freeze_devq(periph->path);
657 				cam_release_devq(periph->path,
658 				    RELSIM_RELEASE_AFTER_TIMEOUT,
659 				    /*reduction*/0,
660 				    /*timeout*/10,
661 				    /*getcount_only*/0);
662 				softc->pm_try++;
663 			} else {
664 				printf("PM status: %d - %08x\n", softc->pm_step, res);
665 				softc->found &= ~(1 << softc->pm_step);
666 				if (xpt_create_path(&dpath, periph,
667 				    done_ccb->ccb_h.path_id,
668 				    softc->pm_step, 0) == CAM_REQ_CMP) {
669 					xpt_async(AC_LOST_DEVICE, dpath, NULL);
670 					xpt_free_path(dpath);
671 				}
672 				softc->pm_step++;
673 			}
674 		}
675 		if (softc->pm_step >= softc->pm_ports) {
676 			if (softc->reset & softc->found) {
677 				cam_freeze_devq(periph->path);
678 				cam_release_devq(periph->path,
679 				    RELSIM_RELEASE_AFTER_TIMEOUT,
680 				    /*reduction*/0,
681 				    /*timeout*/1000,
682 				    /*getcount_only*/0);
683 			}
684 			softc->state = PMP_STATE_CLEAR;
685 			softc->pm_step = 0;
686 		}
687 		xpt_release_ccb(done_ccb);
688 		xpt_schedule(periph, priority);
689 		return;
690 	case PMP_STATE_CLEAR:
691 		softc->pm_step++;
692 		if (softc->pm_step >= softc->pm_ports) {
693 			softc->state = PMP_STATE_CONFIG;
694 			softc->pm_step = 0;
695 		}
696 		xpt_release_ccb(done_ccb);
697 		xpt_schedule(periph, priority);
698 		return;
699 	case PMP_STATE_CONFIG:
700 		if (softc->found) {
701 			softc->pm_step = 0;
702 			softc->state = PMP_STATE_SCAN;
703 			work_ccb = xpt_alloc_ccb_nowait();
704 			if (work_ccb != NULL)
705 				goto do_scan;
706 			xpt_release_ccb(done_ccb);
707 		}
708 		break;
709 	case PMP_STATE_SCAN:
710 		work_ccb = done_ccb;
711 		done_ccb = (union ccb*)work_ccb->ccb_h.ppriv_ptr0;
712 		/* Free the current request path- we're done with it. */
713 		xpt_free_path(work_ccb->ccb_h.path);
714 		softc->pm_step++;
715 do_scan:
716 		while (softc->pm_step < softc->pm_ports &&
717 		    (softc->found & (1 << softc->pm_step)) == 0) {
718 			softc->pm_step++;
719 		}
720 		if (softc->pm_step >= softc->pm_ports) {
721 			xpt_free_ccb(work_ccb);
722 			break;
723 		}
724 		if (xpt_create_path(&dpath, periph,
725 		    done_ccb->ccb_h.path_id,
726 		    softc->pm_step, 0) != CAM_REQ_CMP) {
727 			printf("pmpdone: xpt_create_path failed"
728 			    ", bus scan halted\n");
729 			xpt_free_ccb(work_ccb);
730 			break;
731 		}
732 		xpt_setup_ccb(&work_ccb->ccb_h, dpath,
733 		    done_ccb->ccb_h.pinfo.priority);
734 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
735 		work_ccb->ccb_h.cbfcnp = pmpdone;
736 		work_ccb->ccb_h.ppriv_ptr0 = done_ccb;
737 		work_ccb->crcn.flags = done_ccb->crcn.flags;
738 		xpt_action(work_ccb);
739 		pmprelease(periph, 1 << softc->pm_step);
740 		return;
741 	default:
742 		break;
743 	}
744 done:
745 	xpt_release_ccb(done_ccb);
746 	softc->state = PMP_STATE_NORMAL;
747 	pmprelease(periph, -1);
748 	cam_periph_release_locked(periph);
749 }
750 
751 #endif /* _KERNEL */
752