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