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