xref: /freebsd/sys/cam/ata/ata_pmp.c (revision 03836978bec158bdc0ecee7a4198962f91ce8298)
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 #ifndef	PMP_DEFAULT_HIDE_SPECIAL
130 #define	PMP_DEFAULT_HIDE_SPECIAL	1
131 #endif
132 
133 static int pmp_retry_count = PMP_DEFAULT_RETRY;
134 static int pmp_default_timeout = PMP_DEFAULT_TIMEOUT;
135 static int pmp_hide_special = PMP_DEFAULT_HIDE_SPECIAL;
136 
137 static SYSCTL_NODE(_kern_cam, OID_AUTO, pmp, CTLFLAG_RD, 0,
138             "CAM Direct Access Disk driver");
139 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, retry_count, CTLFLAG_RW,
140            &pmp_retry_count, 0, "Normal I/O retry count");
141 TUNABLE_INT("kern.cam.pmp.retry_count", &pmp_retry_count);
142 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, default_timeout, CTLFLAG_RW,
143            &pmp_default_timeout, 0, "Normal I/O timeout (in seconds)");
144 TUNABLE_INT("kern.cam.pmp.default_timeout", &pmp_default_timeout);
145 SYSCTL_INT(_kern_cam_pmp, OID_AUTO, hide_special, CTLFLAG_RW,
146            &pmp_hide_special, 0, "Hide extra ports");
147 TUNABLE_INT("kern.cam.pmp.hide_special", &pmp_hide_special);
148 
149 static struct periph_driver pmpdriver =
150 {
151 	pmpinit, "pmp",
152 	TAILQ_HEAD_INITIALIZER(pmpdriver.units), /* generation */ 0,
153 	CAM_PERIPH_DRV_EARLY
154 };
155 
156 PERIPHDRIVER_DECLARE(pmp, pmpdriver);
157 
158 static MALLOC_DEFINE(M_ATPMP, "ata_pmp", "ata_pmp buffers");
159 
160 static void
161 pmpinit(void)
162 {
163 	cam_status status;
164 
165 	/*
166 	 * Install a global async callback.  This callback will
167 	 * receive async callbacks like "new device found".
168 	 */
169 	status = xpt_register_async(AC_FOUND_DEVICE, pmpasync, NULL, NULL);
170 
171 	if (status != CAM_REQ_CMP) {
172 		printf("pmp: Failed to attach master async callback "
173 		       "due to status 0x%x!\n", status);
174 	}
175 }
176 
177 static void
178 pmpfreeze(struct cam_periph *periph, int mask)
179 {
180 	struct pmp_softc *softc = (struct pmp_softc *)periph->softc;
181 	struct cam_path *dpath;
182 	int i;
183 
184 	mask &= ~softc->frozen;
185 	for (i = 0; i < 15; i++) {
186 		if ((mask & (1 << i)) == 0)
187 			continue;
188 		if (xpt_create_path(&dpath, periph,
189 		    xpt_path_path_id(periph->path),
190 		    i, 0) == CAM_REQ_CMP) {
191 			softc->frozen |= (1 << i);
192 			xpt_acquire_device(dpath->device);
193 			cam_freeze_devq(dpath);
194 			xpt_free_path(dpath);
195 		}
196 	}
197 }
198 
199 static void
200 pmprelease(struct cam_periph *periph, int mask)
201 {
202 	struct pmp_softc *softc = (struct pmp_softc *)periph->softc;
203 	struct cam_path *dpath;
204 	int i;
205 
206 	mask &= softc->frozen;
207 	for (i = 0; i < 15; i++) {
208 		if ((mask & (1 << i)) == 0)
209 			continue;
210 		if (xpt_create_path(&dpath, periph,
211 		    xpt_path_path_id(periph->path),
212 		    i, 0) == CAM_REQ_CMP) {
213 			softc->frozen &= ~(1 << i);
214 			cam_release_devq(dpath, 0, 0, 0, FALSE);
215 			xpt_release_device(dpath->device);
216 			xpt_free_path(dpath);
217 		}
218 	}
219 }
220 
221 static void
222 pmponinvalidate(struct cam_periph *periph)
223 {
224 	struct cam_path *dpath;
225 	int i;
226 
227 	/*
228 	 * De-register any async callbacks.
229 	 */
230 	xpt_register_async(0, pmpasync, periph, periph->path);
231 
232 	for (i = 0; i < 15; i++) {
233 		if (xpt_create_path(&dpath, periph,
234 		    xpt_path_path_id(periph->path),
235 		    i, 0) == CAM_REQ_CMP) {
236 			xpt_async(AC_LOST_DEVICE, dpath, NULL);
237 			xpt_free_path(dpath);
238 		}
239 	}
240 	pmprelease(periph, -1);
241 	xpt_print(periph->path, "lost device\n");
242 }
243 
244 static void
245 pmpcleanup(struct cam_periph *periph)
246 {
247 	struct pmp_softc *softc;
248 
249 	softc = (struct pmp_softc *)periph->softc;
250 
251 	xpt_print(periph->path, "removing device entry\n");
252 	cam_periph_unlock(periph);
253 
254 	/*
255 	 * If we can't free the sysctl tree, oh well...
256 	 */
257 	if ((softc->flags & PMP_FLAG_SCTX_INIT) != 0
258 	    && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
259 		xpt_print(periph->path, "can't remove sysctl context\n");
260 	}
261 
262 	free(softc, M_DEVBUF);
263 	cam_periph_lock(periph);
264 }
265 
266 static void
267 pmpasync(void *callback_arg, u_int32_t code,
268 	struct cam_path *path, void *arg)
269 {
270 	struct cam_periph *periph;
271 	struct pmp_softc *softc;
272 
273 	periph = (struct cam_periph *)callback_arg;
274 	switch (code) {
275 	case AC_FOUND_DEVICE:
276 	{
277 		struct ccb_getdev *cgd;
278 		cam_status status;
279 
280 		cgd = (struct ccb_getdev *)arg;
281 		if (cgd == NULL)
282 			break;
283 
284 		if (cgd->protocol != PROTO_SATAPM)
285 			break;
286 
287 		/*
288 		 * Allocate a peripheral instance for
289 		 * this device and start the probe
290 		 * process.
291 		 */
292 		status = cam_periph_alloc(pmpregister, pmponinvalidate,
293 					  pmpcleanup, pmpstart,
294 					  "pmp", CAM_PERIPH_BIO,
295 					  cgd->ccb_h.path, pmpasync,
296 					  AC_FOUND_DEVICE, cgd);
297 
298 		if (status != CAM_REQ_CMP
299 		 && status != CAM_REQ_INPROG)
300 			printf("pmpasync: Unable to attach to new device "
301 				"due to status 0x%x\n", status);
302 		break;
303 	}
304 	case AC_SCSI_AEN:
305 	case AC_SENT_BDR:
306 	case AC_BUS_RESET:
307 		softc = (struct pmp_softc *)periph->softc;
308 		cam_periph_async(periph, code, path, arg);
309 		if (code == AC_SCSI_AEN)
310 			softc->events |= PMP_EV_RESCAN;
311 		else
312 			softc->events |= PMP_EV_RESET;
313 		if (code == AC_SCSI_AEN && softc->state != PMP_STATE_NORMAL)
314 			break;
315 		xpt_hold_boot();
316 		pmpfreeze(periph, softc->found);
317 		if (code == AC_SENT_BDR || code == AC_BUS_RESET)
318 			softc->found = 0; /* We have to reset everything. */
319 		if (softc->state == PMP_STATE_NORMAL) {
320 			softc->state = PMP_STATE_PRECONFIG;
321 			cam_periph_acquire(periph);
322 			xpt_schedule(periph, CAM_PRIORITY_DEV);
323 		} else
324 			softc->restart = 1;
325 		break;
326 	default:
327 		cam_periph_async(periph, code, path, arg);
328 		break;
329 	}
330 }
331 
332 static void
333 pmpsysctlinit(void *context, int pending)
334 {
335 	struct cam_periph *periph;
336 	struct pmp_softc *softc;
337 	char tmpstr[80], tmpstr2[80];
338 
339 	periph = (struct cam_periph *)context;
340 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
341 		return;
342 
343 	softc = (struct pmp_softc *)periph->softc;
344 	snprintf(tmpstr, sizeof(tmpstr), "CAM PMP unit %d", periph->unit_number);
345 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
346 
347 	sysctl_ctx_init(&softc->sysctl_ctx);
348 	softc->flags |= PMP_FLAG_SCTX_INIT;
349 	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
350 		SYSCTL_STATIC_CHILDREN(_kern_cam_pmp), OID_AUTO, tmpstr2,
351 		CTLFLAG_RD, 0, tmpstr);
352 	if (softc->sysctl_tree == NULL) {
353 		printf("pmpsysctlinit: unable to allocate sysctl tree\n");
354 		cam_periph_release(periph);
355 		return;
356 	}
357 
358 	cam_periph_release(periph);
359 }
360 
361 static cam_status
362 pmpregister(struct cam_periph *periph, void *arg)
363 {
364 	struct pmp_softc *softc;
365 	struct ccb_getdev *cgd;
366 
367 	cgd = (struct ccb_getdev *)arg;
368 	if (cgd == NULL) {
369 		printf("pmpregister: no getdev CCB, can't register device\n");
370 		return(CAM_REQ_CMP_ERR);
371 	}
372 
373 	softc = (struct pmp_softc *)malloc(sizeof(*softc), M_DEVBUF,
374 	    M_NOWAIT|M_ZERO);
375 
376 	if (softc == NULL) {
377 		printf("pmpregister: Unable to probe new device. "
378 		       "Unable to allocate softc\n");
379 		return(CAM_REQ_CMP_ERR);
380 	}
381 	periph->softc = softc;
382 
383 	softc->pm_pid = ((uint32_t *)&cgd->ident_data)[0];
384 	softc->pm_prv = ((uint32_t *)&cgd->ident_data)[1];
385 	TASK_INIT(&softc->sysctl_task, 0, pmpsysctlinit, periph);
386 
387 	xpt_announce_periph(periph, NULL);
388 
389 	/*
390 	 * Add async callbacks for bus reset and
391 	 * bus device reset calls.  I don't bother
392 	 * checking if this fails as, in most cases,
393 	 * the system will function just fine without
394 	 * them and the only alternative would be to
395 	 * not attach the device on failure.
396 	 */
397 	xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE |
398 		AC_SCSI_AEN, pmpasync, periph, periph->path);
399 
400 	/*
401 	 * Take an exclusive refcount on the periph while pmpstart is called
402 	 * to finish the probe.  The reference will be dropped in pmpdone at
403 	 * the end of probe.
404 	 */
405 	(void)cam_periph_acquire(periph);
406 	xpt_hold_boot();
407 	softc->state = PMP_STATE_PORTS;
408 	softc->events = PMP_EV_RESCAN;
409 	xpt_schedule(periph, CAM_PRIORITY_DEV);
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_trans_settings cts;
418 	struct ccb_ataio *ataio;
419 	struct pmp_softc *softc;
420 	struct cam_path *dpath;
421 	int revision = 0;
422 
423 	softc = (struct pmp_softc *)periph->softc;
424 	ataio = &start_ccb->ataio;
425 
426 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("pmpstart\n"));
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, 0x07 |
537 		    ((softc->caps & CTS_SATA_CAPS_H_AN) ? 0x08 : 0));
538 		break;
539 	default:
540 		break;
541 	}
542 	xpt_action(start_ccb);
543 }
544 
545 static void
546 pmpdone(struct cam_periph *periph, union ccb *done_ccb)
547 {
548 	struct ccb_trans_settings cts;
549 	struct pmp_softc *softc;
550 	struct ccb_ataio *ataio;
551 	struct cam_path *dpath;
552 	u_int32_t  priority, res;
553 	int i;
554 
555 	softc = (struct pmp_softc *)periph->softc;
556 	ataio = &done_ccb->ataio;
557 
558 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("pmpdone\n"));
559 
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 = (ataio->res.lba_high << 24) +
586 		    (ataio->res.lba_mid << 16) +
587 		    (ataio->res.lba_low << 8) +
588 		    ataio->res.sector_count;
589 		if (pmp_hide_special) {
590 			/*
591 			 * This PMP declares 6 ports, while only 5 of them
592 			 * are real. Port 5 is a SEMB port, probing which
593 			 * causes timeouts if external SEP is not connected
594 			 * to PMP over I2C.
595 			 */
596 			if ((softc->pm_pid == 0x37261095 ||
597 			     softc->pm_pid == 0x38261095) &&
598 			    softc->pm_ports == 6)
599 				softc->pm_ports = 5;
600 
601 			/*
602 			 * This PMP declares 7 ports, while only 5 of them
603 			 * are real. Port 5 is a fake "Config  Disk" with
604 			 * 640 sectors size. Port 6 is a SEMB port.
605 			 */
606 			if (softc->pm_pid == 0x47261095 && softc->pm_ports == 7)
607 				softc->pm_ports = 5;
608 
609 			/*
610 			 * These PMPs have extra configuration port.
611 			 */
612 			if (softc->pm_pid == 0x57231095 ||
613 			    softc->pm_pid == 0x57331095 ||
614 			    softc->pm_pid == 0x57341095 ||
615 			    softc->pm_pid == 0x57441095)
616 				softc->pm_ports--;
617 		}
618 		printf("%s%d: %d fan-out ports\n",
619 		    periph->periph_name, periph->unit_number,
620 		    softc->pm_ports);
621 		softc->state = PMP_STATE_PRECONFIG;
622 		xpt_release_ccb(done_ccb);
623 		xpt_schedule(periph, priority);
624 		return;
625 	case PMP_STATE_PRECONFIG:
626 		softc->pm_step = 0;
627 		softc->state = PMP_STATE_RESET;
628 		softc->reset |= ~softc->found;
629 		xpt_release_ccb(done_ccb);
630 		xpt_schedule(periph, priority);
631 		return;
632 	case PMP_STATE_RESET:
633 		softc->pm_step++;
634 		if (softc->pm_step >= softc->pm_ports) {
635 			softc->pm_step = 0;
636 			cam_freeze_devq(periph->path);
637 			cam_release_devq(periph->path,
638 			    RELSIM_RELEASE_AFTER_TIMEOUT,
639 			    /*reduction*/0,
640 			    /*timeout*/5,
641 			    /*getcount_only*/0);
642 			softc->state = PMP_STATE_CONNECT;
643 		}
644 		xpt_release_ccb(done_ccb);
645 		xpt_schedule(periph, priority);
646 		return;
647 	case PMP_STATE_CONNECT:
648 		softc->pm_step++;
649 		if (softc->pm_step >= softc->pm_ports) {
650 			softc->pm_step = 0;
651 			softc->pm_try = 0;
652 			cam_freeze_devq(periph->path);
653 			cam_release_devq(periph->path,
654 			    RELSIM_RELEASE_AFTER_TIMEOUT,
655 			    /*reduction*/0,
656 			    /*timeout*/10,
657 			    /*getcount_only*/0);
658 			softc->state = PMP_STATE_CHECK;
659 		}
660 		xpt_release_ccb(done_ccb);
661 		xpt_schedule(periph, priority);
662 		return;
663 	case PMP_STATE_CHECK:
664 		res = (ataio->res.lba_high << 24) +
665 		    (ataio->res.lba_mid << 16) +
666 		    (ataio->res.lba_low << 8) +
667 		    ataio->res.sector_count;
668 		if (((res & 0xf0f) == 0x103 && (res & 0x0f0) != 0) ||
669 		    (res & 0x600) != 0) {
670 			if (bootverbose) {
671 				printf("%s%d: port %d status: %08x\n",
672 				    periph->periph_name, periph->unit_number,
673 				    softc->pm_step, res);
674 			}
675 			/* Report device speed if it is online. */
676 			if ((res & 0xf0f) == 0x103 &&
677 			    xpt_create_path(&dpath, periph,
678 			    xpt_path_path_id(periph->path),
679 			    softc->pm_step, 0) == CAM_REQ_CMP) {
680 				bzero(&cts, sizeof(cts));
681 				xpt_setup_ccb(&cts.ccb_h, dpath, CAM_PRIORITY_NONE);
682 				cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
683 				cts.type = CTS_TYPE_CURRENT_SETTINGS;
684 				cts.xport_specific.sata.revision = (res & 0x0f0) >> 4;
685 				cts.xport_specific.sata.valid = CTS_SATA_VALID_REVISION;
686 				cts.xport_specific.sata.caps = softc->caps &
687 				    (CTS_SATA_CAPS_H_PMREQ |
688 				     CTS_SATA_CAPS_H_DMAAA |
689 				     CTS_SATA_CAPS_H_AN);
690 				cts.xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
691 				xpt_action((union ccb *)&cts);
692 				xpt_free_path(dpath);
693 			}
694 			softc->found |= (1 << softc->pm_step);
695 			softc->pm_step++;
696 		} else {
697 			if (softc->pm_try < 10) {
698 				cam_freeze_devq(periph->path);
699 				cam_release_devq(periph->path,
700 				    RELSIM_RELEASE_AFTER_TIMEOUT,
701 				    /*reduction*/0,
702 				    /*timeout*/10,
703 				    /*getcount_only*/0);
704 				softc->pm_try++;
705 			} else {
706 				if (bootverbose) {
707 					printf("%s%d: port %d status: %08x\n",
708 					    periph->periph_name, periph->unit_number,
709 					    softc->pm_step, res);
710 				}
711 				softc->found &= ~(1 << softc->pm_step);
712 				if (xpt_create_path(&dpath, periph,
713 				    done_ccb->ccb_h.path_id,
714 				    softc->pm_step, 0) == CAM_REQ_CMP) {
715 					xpt_async(AC_LOST_DEVICE, dpath, NULL);
716 					xpt_free_path(dpath);
717 				}
718 				softc->pm_step++;
719 			}
720 		}
721 		if (softc->pm_step >= softc->pm_ports) {
722 			if (softc->reset & softc->found) {
723 				cam_freeze_devq(periph->path);
724 				cam_release_devq(periph->path,
725 				    RELSIM_RELEASE_AFTER_TIMEOUT,
726 				    /*reduction*/0,
727 				    /*timeout*/1000,
728 				    /*getcount_only*/0);
729 			}
730 			softc->state = PMP_STATE_CLEAR;
731 			softc->pm_step = 0;
732 		}
733 		xpt_release_ccb(done_ccb);
734 		xpt_schedule(periph, priority);
735 		return;
736 	case PMP_STATE_CLEAR:
737 		softc->pm_step++;
738 		if (softc->pm_step >= softc->pm_ports) {
739 			softc->state = PMP_STATE_CONFIG;
740 			softc->pm_step = 0;
741 		}
742 		xpt_release_ccb(done_ccb);
743 		xpt_schedule(periph, priority);
744 		return;
745 	case PMP_STATE_CONFIG:
746 		for (i = 0; i < softc->pm_ports; i++) {
747 			union ccb *ccb;
748 
749 			if ((softc->found & (1 << i)) == 0)
750 				continue;
751 			if (xpt_create_path(&dpath, periph,
752 			    xpt_path_path_id(periph->path),
753 			    i, 0) != CAM_REQ_CMP) {
754 				printf("pmpdone: xpt_create_path failed\n");
755 				continue;
756 			}
757 			/* If we did hard reset to this device, inform XPT. */
758 			if ((softc->reset & softc->found & (1 << i)) != 0)
759 				xpt_async(AC_SENT_BDR, dpath, NULL);
760 			/* If rescan requested, scan this device. */
761 			if (softc->events & PMP_EV_RESCAN) {
762 				ccb = xpt_alloc_ccb_nowait();
763 				if (ccb == NULL) {
764 					xpt_free_path(dpath);
765 					goto done;
766 				}
767 				xpt_setup_ccb(&ccb->ccb_h, dpath, CAM_PRIORITY_XPT);
768 				xpt_rescan(ccb);
769 			} else
770 				xpt_free_path(dpath);
771 		}
772 		break;
773 	default:
774 		break;
775 	}
776 done:
777 	xpt_release_ccb(done_ccb);
778 	softc->state = PMP_STATE_NORMAL;
779 	softc->events = 0;
780 	xpt_release_boot();
781 	pmprelease(periph, -1);
782 	cam_periph_release_locked(periph);
783 }
784 
785 #endif /* _KERNEL */
786