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