xref: /freebsd/sys/cam/ata/ata_pmp.c (revision 409a390c3341fb4f162cd7de1fd595a323ebbfd8)
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_xpt_internal.h>
58 #include <cam/cam_sim.h>
59 
60 #include <cam/ata/ata_all.h>
61 
62 #ifdef _KERNEL
63 
64 typedef enum {
65 	PMP_STATE_NORMAL,
66 	PMP_STATE_PORTS,
67 	PMP_STATE_PRECONFIG,
68 	PMP_STATE_RESET,
69 	PMP_STATE_CONNECT,
70 	PMP_STATE_CHECK,
71 	PMP_STATE_CLEAR,
72 	PMP_STATE_CONFIG,
73 	PMP_STATE_SCAN
74 } pmp_state;
75 
76 typedef enum {
77 	PMP_FLAG_SCTX_INIT	= 0x200
78 } pmp_flags;
79 
80 typedef enum {
81 	PMP_CCB_PROBE		= 0x01,
82 } pmp_ccb_state;
83 
84 /* Offsets into our private area for storing information */
85 #define ccb_state	ppriv_field0
86 #define ccb_bp		ppriv_ptr1
87 
88 struct pmp_softc {
89 	SLIST_ENTRY(pmp_softc)	links;
90 	pmp_state		state;
91 	pmp_flags		flags;
92 	uint32_t		pm_pid;
93 	uint32_t		pm_prv;
94 	int			pm_ports;
95 	int			pm_step;
96 	int			pm_try;
97 	int			found;
98 	int			reset;
99 	int			frozen;
100 	int			restart;
101 	union			ccb saved_ccb;
102 	struct task		sysctl_task;
103 	struct sysctl_ctx_list	sysctl_ctx;
104 	struct sysctl_oid	*sysctl_tree;
105 };
106 
107 static	periph_init_t	pmpinit;
108 static	void		pmpasync(void *callback_arg, u_int32_t code,
109 				struct cam_path *path, void *arg);
110 static	void		pmpsysctlinit(void *context, int pending);
111 static	periph_ctor_t	pmpregister;
112 static	periph_dtor_t	pmpcleanup;
113 static	periph_start_t	pmpstart;
114 static	periph_oninv_t	pmponinvalidate;
115 static	void		pmpdone(struct cam_periph *periph,
116 			       union ccb *done_ccb);
117 
118 #ifndef PMP_DEFAULT_TIMEOUT
119 #define PMP_DEFAULT_TIMEOUT 30	/* Timeout in seconds */
120 #endif
121 
122 #ifndef	PMP_DEFAULT_RETRY
123 #define	PMP_DEFAULT_RETRY	1
124 #endif
125 
126 static int pmp_retry_count = PMP_DEFAULT_RETRY;
127 static int pmp_default_timeout = PMP_DEFAULT_TIMEOUT;
128 
129 SYSCTL_NODE(_kern_cam, OID_AUTO, pmp, CTLFLAG_RD, 0,
130             "CAM Direct Access Disk driver");
131 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, retry_count, CTLFLAG_RW,
132            &pmp_retry_count, 0, "Normal I/O retry count");
133 TUNABLE_INT("kern.cam.pmp.retry_count", &pmp_retry_count);
134 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, default_timeout, CTLFLAG_RW,
135            &pmp_default_timeout, 0, "Normal I/O timeout (in seconds)");
136 TUNABLE_INT("kern.cam.pmp.default_timeout", &pmp_default_timeout);
137 
138 static struct periph_driver pmpdriver =
139 {
140 	pmpinit, "pmp",
141 	TAILQ_HEAD_INITIALIZER(pmpdriver.units), /* generation */ 0,
142 	CAM_PERIPH_DRV_EARLY
143 };
144 
145 PERIPHDRIVER_DECLARE(pmp, pmpdriver);
146 
147 MALLOC_DEFINE(M_ATPMP, "ata_pmp", "ata_pmp buffers");
148 
149 static void
150 pmpinit(void)
151 {
152 	cam_status status;
153 
154 	/*
155 	 * Install a global async callback.  This callback will
156 	 * receive async callbacks like "new device found".
157 	 */
158 	status = xpt_register_async(AC_FOUND_DEVICE, pmpasync, NULL, NULL);
159 
160 	if (status != CAM_REQ_CMP) {
161 		printf("pmp: Failed to attach master async callback "
162 		       "due to status 0x%x!\n", status);
163 	}
164 }
165 
166 static void
167 pmpfreeze(struct cam_periph *periph, int mask)
168 {
169 	struct pmp_softc *softc = (struct pmp_softc *)periph->softc;
170 	struct cam_path *dpath;
171 	int i;
172 
173 	mask &= ~softc->frozen;
174 	for (i = 0; i < 15; i++) {
175 		if ((mask & (1 << i)) == 0)
176 			continue;
177 		if (xpt_create_path(&dpath, periph,
178 		    xpt_path_path_id(periph->path),
179 		    i, 0) == CAM_REQ_CMP) {
180 			softc->frozen |= (1 << i);
181 			xpt_acquire_device(dpath->device);
182 			cam_freeze_devq(dpath);
183 			xpt_free_path(dpath);
184 		}
185 	}
186 }
187 
188 static void
189 pmprelease(struct cam_periph *periph, int mask)
190 {
191 	struct pmp_softc *softc = (struct pmp_softc *)periph->softc;
192 	struct cam_path *dpath;
193 	int i;
194 
195 	mask &= softc->frozen;
196 	for (i = 0; i < 15; i++) {
197 		if ((mask & (1 << i)) == 0)
198 			continue;
199 		if (xpt_create_path(&dpath, periph,
200 		    xpt_path_path_id(periph->path),
201 		    i, 0) == CAM_REQ_CMP) {
202 			softc->frozen &= ~(1 << i);
203 			cam_release_devq(dpath, 0, 0, 0, FALSE);
204 			xpt_release_device(dpath->device);
205 			xpt_free_path(dpath);
206 		}
207 	}
208 }
209 
210 static void
211 pmponinvalidate(struct cam_periph *periph)
212 {
213 	struct pmp_softc *softc;
214 	struct cam_path *dpath;
215 	int i;
216 
217 	softc = (struct pmp_softc *)periph->softc;
218 
219 	/*
220 	 * De-register any async callbacks.
221 	 */
222 	xpt_register_async(0, pmpasync, periph, periph->path);
223 
224 	for (i = 0; i < 15; i++) {
225 		if (xpt_create_path(&dpath, periph,
226 		    xpt_path_path_id(periph->path),
227 		    i, 0) == CAM_REQ_CMP) {
228 			xpt_async(AC_LOST_DEVICE, dpath, NULL);
229 			xpt_free_path(dpath);
230 		}
231 	}
232 	pmprelease(periph, -1);
233 	xpt_print(periph->path, "lost device\n");
234 }
235 
236 static void
237 pmpcleanup(struct cam_periph *periph)
238 {
239 	struct pmp_softc *softc;
240 
241 	softc = (struct pmp_softc *)periph->softc;
242 
243 	xpt_print(periph->path, "removing device entry\n");
244 	cam_periph_unlock(periph);
245 
246 	/*
247 	 * If we can't free the sysctl tree, oh well...
248 	 */
249 	if ((softc->flags & PMP_FLAG_SCTX_INIT) != 0
250 	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
251 		xpt_print(periph->path, "can't remove sysctl context\n");
252 	}
253 
254 	free(softc, M_DEVBUF);
255 	cam_periph_lock(periph);
256 }
257 
258 static void
259 pmpasync(void *callback_arg, u_int32_t code,
260 	struct cam_path *path, void *arg)
261 {
262 	struct cam_periph *periph;
263 	struct pmp_softc *softc;
264 
265 	periph = (struct cam_periph *)callback_arg;
266 	switch (code) {
267 	case AC_FOUND_DEVICE:
268 	{
269 		struct ccb_getdev *cgd;
270 		cam_status status;
271 
272 		cgd = (struct ccb_getdev *)arg;
273 		if (cgd == NULL)
274 			break;
275 
276 		if (cgd->protocol != PROTO_SATAPM)
277 			break;
278 
279 		/*
280 		 * Allocate a peripheral instance for
281 		 * this device and start the probe
282 		 * process.
283 		 */
284 		status = cam_periph_alloc(pmpregister, pmponinvalidate,
285 					  pmpcleanup, pmpstart,
286 					  "pmp", CAM_PERIPH_BIO,
287 					  cgd->ccb_h.path, pmpasync,
288 					  AC_FOUND_DEVICE, cgd);
289 
290 		if (status != CAM_REQ_CMP
291 		 && status != CAM_REQ_INPROG)
292 			printf("pmpasync: Unable to attach to new device "
293 				"due to status 0x%x\n", status);
294 		break;
295 	}
296 	case AC_SCSI_AEN:
297 	case AC_SENT_BDR:
298 	case AC_BUS_RESET:
299 		softc = (struct pmp_softc *)periph->softc;
300 		cam_periph_async(periph, code, path, arg);
301 		if (code == AC_SCSI_AEN && softc->state != PMP_STATE_NORMAL &&
302 		    softc->state != PMP_STATE_SCAN)
303 			break;
304 		if (softc->state != PMP_STATE_SCAN)
305 			pmpfreeze(periph, softc->found);
306 		else
307 			pmpfreeze(periph, softc->found & ~(1 << softc->pm_step));
308 		if (code == AC_SENT_BDR || code == AC_BUS_RESET)
309 			softc->found = 0; /* We have to reset everything. */
310 		if (softc->state == PMP_STATE_NORMAL) {
311 			softc->state = PMP_STATE_PORTS;
312 			cam_periph_acquire(periph);
313 			xpt_schedule(periph, CAM_PRIORITY_BUS);
314 		} else
315 			softc->restart = 1;
316 		break;
317 	default:
318 		cam_periph_async(periph, code, path, arg);
319 		break;
320 	}
321 }
322 
323 static void
324 pmpsysctlinit(void *context, int pending)
325 {
326 	struct cam_periph *periph;
327 	struct pmp_softc *softc;
328 	char tmpstr[80], tmpstr2[80];
329 
330 	periph = (struct cam_periph *)context;
331 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
332 		return;
333 
334 	softc = (struct pmp_softc *)periph->softc;
335 	snprintf(tmpstr, sizeof(tmpstr), "CAM PMP unit %d", periph->unit_number);
336 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
337 
338 	sysctl_ctx_init(&softc->sysctl_ctx);
339 	softc->flags |= PMP_FLAG_SCTX_INIT;
340 	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
341 		SYSCTL_STATIC_CHILDREN(_kern_cam_pmp), OID_AUTO, tmpstr2,
342 		CTLFLAG_RD, 0, tmpstr);
343 	if (softc->sysctl_tree == NULL) {
344 		printf("pmpsysctlinit: unable to allocate sysctl tree\n");
345 		cam_periph_release(periph);
346 		return;
347 	}
348 
349 	cam_periph_release(periph);
350 }
351 
352 static cam_status
353 pmpregister(struct cam_periph *periph, void *arg)
354 {
355 	struct pmp_softc *softc;
356 	struct ccb_pathinq cpi;
357 	struct ccb_getdev *cgd;
358 
359 	cgd = (struct ccb_getdev *)arg;
360 	if (periph == NULL) {
361 		printf("pmpregister: periph was NULL!!\n");
362 		return(CAM_REQ_CMP_ERR);
363 	}
364 
365 	if (cgd == NULL) {
366 		printf("pmpregister: no getdev CCB, can't register device\n");
367 		return(CAM_REQ_CMP_ERR);
368 	}
369 
370 	softc = (struct pmp_softc *)malloc(sizeof(*softc), M_DEVBUF,
371 	    M_NOWAIT|M_ZERO);
372 
373 	if (softc == NULL) {
374 		printf("pmpregister: Unable to probe new device. "
375 		       "Unable to allocate softc\n");
376 		return(CAM_REQ_CMP_ERR);
377 	}
378 	periph->softc = softc;
379 
380 	softc->state = PMP_STATE_PORTS;
381 	softc->pm_pid = ((uint32_t *)&cgd->ident_data)[0];
382 	softc->pm_prv = ((uint32_t *)&cgd->ident_data)[1];
383 
384 	/* Check if the SIM does not want queued commands */
385 	bzero(&cpi, sizeof(cpi));
386 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
387 	cpi.ccb_h.func_code = XPT_PATH_INQ;
388 	xpt_action((union ccb *)&cpi);
389 
390 	TASK_INIT(&softc->sysctl_task, 0, pmpsysctlinit, periph);
391 
392 	xpt_announce_periph(periph, NULL);
393 
394 	/*
395 	 * Add async callbacks for bus reset and
396 	 * bus device reset calls.  I don't bother
397 	 * checking if this fails as, in most cases,
398 	 * the system will function just fine without
399 	 * them and the only alternative would be to
400 	 * not attach the device on failure.
401 	 */
402 	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
403 		AC_SCSI_AEN, pmpasync, periph, periph->path);
404 
405 	/*
406 	 * Take an exclusive refcount on the periph while pmpstart is called
407 	 * to finish the probe.  The reference will be dropped in pmpdone at
408 	 * the end of probe.
409 	 */
410 	(void)cam_periph_acquire(periph);
411 	xpt_schedule(periph, CAM_PRIORITY_BUS);
412 
413 	return(CAM_REQ_CMP);
414 }
415 
416 static void
417 pmpstart(struct cam_periph *periph, union ccb *start_ccb)
418 {
419 	struct ccb_ataio *ataio;
420 	struct pmp_softc *softc;
421 
422 	softc = (struct pmp_softc *)periph->softc;
423 	ataio = &start_ccb->ataio;
424 
425 	if (softc->restart) {
426 		softc->restart = 0;
427 		softc->state = PMP_STATE_PORTS;
428 	}
429 
430 	switch (softc->state) {
431 	case PMP_STATE_PORTS:
432 		cam_fill_ataio(ataio,
433 		      pmp_retry_count,
434 		      pmpdone,
435 		      /*flags*/CAM_DIR_NONE,
436 		      0,
437 		      /*data_ptr*/NULL,
438 		      /*dxfer_len*/0,
439 		      pmp_default_timeout * 1000);
440 		ata_pm_read_cmd(ataio, 2, 15);
441 		break;
442 	case PMP_STATE_PRECONFIG:
443 		cam_fill_ataio(ataio,
444 		      pmp_retry_count,
445 		      pmpdone,
446 		      /*flags*/CAM_DIR_NONE,
447 		      0,
448 		      /*data_ptr*/NULL,
449 		      /*dxfer_len*/0,
450 		      pmp_default_timeout * 1000);
451 		ata_pm_write_cmd(ataio, 0x60, 15, 0x0);
452 		break;
453 	case PMP_STATE_RESET:
454 		cam_fill_ataio(ataio,
455 		      pmp_retry_count,
456 		      pmpdone,
457 		      /*flags*/CAM_DIR_NONE,
458 		      0,
459 		      /*data_ptr*/NULL,
460 		      /*dxfer_len*/0,
461 		      pmp_default_timeout * 1000);
462 		ata_pm_write_cmd(ataio, 2, softc->pm_step,
463 		    (softc->found & (1 << softc->pm_step)) ? 0 : 1);
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("%s%d: %d fan-out ports\n",
588 		    periph->periph_name, periph->unit_number,
589 		    softc->pm_ports);
590 		softc->state = PMP_STATE_PRECONFIG;
591 		xpt_release_ccb(done_ccb);
592 		xpt_schedule(periph, priority);
593 		return;
594 	case PMP_STATE_PRECONFIG:
595 		softc->pm_step = 0;
596 		softc->state = PMP_STATE_RESET;
597 		softc->reset |= ~softc->found;
598 		xpt_release_ccb(done_ccb);
599 		xpt_schedule(periph, priority);
600 		return;
601 	case PMP_STATE_RESET:
602 		softc->pm_step++;
603 		if (softc->pm_step >= softc->pm_ports) {
604 			softc->pm_step = 0;
605 			cam_freeze_devq(periph->path);
606 			cam_release_devq(periph->path,
607 			    RELSIM_RELEASE_AFTER_TIMEOUT,
608 			    /*reduction*/0,
609 			    /*timeout*/5,
610 			    /*getcount_only*/0);
611 			softc->state = PMP_STATE_CONNECT;
612 		}
613 		xpt_release_ccb(done_ccb);
614 		xpt_schedule(periph, priority);
615 		return;
616 	case PMP_STATE_CONNECT:
617 		softc->pm_step++;
618 		if (softc->pm_step >= softc->pm_ports) {
619 			softc->pm_step = 0;
620 			softc->pm_try = 0;
621 			cam_freeze_devq(periph->path);
622 			cam_release_devq(periph->path,
623 			    RELSIM_RELEASE_AFTER_TIMEOUT,
624 			    /*reduction*/0,
625 			    /*timeout*/10,
626 			    /*getcount_only*/0);
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 			if (bootverbose) {
639 				printf("%s%d: port %d status: %08x\n",
640 				    periph->periph_name, periph->unit_number,
641 				    softc->pm_step, res);
642 			}
643 			/* Report device speed. */
644 			if (xpt_create_path(&dpath, periph,
645 			    xpt_path_path_id(periph->path),
646 			    softc->pm_step, 0) == CAM_REQ_CMP) {
647 				bzero(&cts, sizeof(cts));
648 				xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NORMAL);
649 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
650 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
651 				cts.xport_specific.sata.revision = (res & 0x0f0) >> 4;
652 				cts.xport_specific.sata.valid = CTS_SATA_VALID_REVISION;
653 				xpt_action((union ccb *)&cts);
654 				xpt_free_path(dpath);
655 			}
656 			softc->found |= (1 << softc->pm_step);
657 			softc->pm_step++;
658 		} else {
659 			if (softc->pm_try < 10) {
660 				cam_freeze_devq(periph->path);
661 				cam_release_devq(periph->path,
662 				    RELSIM_RELEASE_AFTER_TIMEOUT,
663 				    /*reduction*/0,
664 				    /*timeout*/10,
665 				    /*getcount_only*/0);
666 				softc->pm_try++;
667 			} else {
668 				if (bootverbose) {
669 					printf("%s%d: port %d status: %08x\n",
670 					    periph->periph_name, periph->unit_number,
671 					    softc->pm_step, res);
672 				}
673 				softc->found &= ~(1 << softc->pm_step);
674 				if (xpt_create_path(&dpath, periph,
675 				    done_ccb->ccb_h.path_id,
676 				    softc->pm_step, 0) == CAM_REQ_CMP) {
677 					xpt_async(AC_LOST_DEVICE, dpath, NULL);
678 					xpt_free_path(dpath);
679 				}
680 				softc->pm_step++;
681 			}
682 		}
683 		if (softc->pm_step >= softc->pm_ports) {
684 			if (softc->reset & softc->found) {
685 				cam_freeze_devq(periph->path);
686 				cam_release_devq(periph->path,
687 				    RELSIM_RELEASE_AFTER_TIMEOUT,
688 				    /*reduction*/0,
689 				    /*timeout*/1000,
690 				    /*getcount_only*/0);
691 			}
692 			softc->state = PMP_STATE_CLEAR;
693 			softc->pm_step = 0;
694 		}
695 		xpt_release_ccb(done_ccb);
696 		xpt_schedule(periph, priority);
697 		return;
698 	case PMP_STATE_CLEAR:
699 		softc->pm_step++;
700 		if (softc->pm_step >= softc->pm_ports) {
701 			softc->state = PMP_STATE_CONFIG;
702 			softc->pm_step = 0;
703 		}
704 		xpt_release_ccb(done_ccb);
705 		xpt_schedule(periph, priority);
706 		return;
707 	case PMP_STATE_CONFIG:
708 		if (softc->found) {
709 			softc->pm_step = 0;
710 			softc->state = PMP_STATE_SCAN;
711 			work_ccb = xpt_alloc_ccb_nowait();
712 			if (work_ccb != NULL)
713 				goto do_scan;
714 			xpt_release_ccb(done_ccb);
715 		}
716 		break;
717 	case PMP_STATE_SCAN:
718 		work_ccb = done_ccb;
719 		done_ccb = (union ccb*)work_ccb->ccb_h.ppriv_ptr0;
720 		/* Free the current request path- we're done with it. */
721 		xpt_free_path(work_ccb->ccb_h.path);
722 		softc->pm_step++;
723 do_scan:
724 		while (softc->pm_step < softc->pm_ports &&
725 		    (softc->found & (1 << softc->pm_step)) == 0) {
726 			softc->pm_step++;
727 		}
728 		if (softc->pm_step >= softc->pm_ports) {
729 			xpt_free_ccb(work_ccb);
730 			break;
731 		}
732 		if (xpt_create_path(&dpath, periph,
733 		    done_ccb->ccb_h.path_id,
734 		    softc->pm_step, 0) != CAM_REQ_CMP) {
735 			printf("pmpdone: xpt_create_path failed"
736 			    ", bus scan halted\n");
737 			xpt_free_ccb(work_ccb);
738 			break;
739 		}
740 		xpt_setup_ccb(&work_ccb->ccb_h, dpath,
741 		    done_ccb->ccb_h.pinfo.priority);
742 		work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
743 		work_ccb->ccb_h.cbfcnp = pmpdone;
744 		work_ccb->ccb_h.ppriv_ptr0 = done_ccb;
745 		work_ccb->crcn.flags = done_ccb->crcn.flags;
746 		xpt_action(work_ccb);
747 		pmprelease(periph, 1 << softc->pm_step);
748 		return;
749 	default:
750 		break;
751 	}
752 done:
753 	xpt_release_ccb(done_ccb);
754 	softc->state = PMP_STATE_NORMAL;
755 	pmprelease(periph, -1);
756 	cam_periph_release_locked(periph);
757 }
758 
759 #endif /* _KERNEL */
760