xref: /netbsd-src/sys/dev/dmover/dmover_process.c (revision 34c3adecb24455f927f306fcbeee892ac68c96be)
1*34c3adecSjakllsch /*	$NetBSD: dmover_process.c,v 1.5 2011/05/14 14:49:19 jakllsch Exp $	*/
25d06c0e8Sthorpej 
35d06c0e8Sthorpej /*
45d06c0e8Sthorpej  * Copyright (c) 2002 Wasabi Systems, Inc.
55d06c0e8Sthorpej  * All rights reserved.
65d06c0e8Sthorpej  *
75d06c0e8Sthorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
85d06c0e8Sthorpej  *
95d06c0e8Sthorpej  * Redistribution and use in source and binary forms, with or without
105d06c0e8Sthorpej  * modification, are permitted provided that the following conditions
115d06c0e8Sthorpej  * are met:
125d06c0e8Sthorpej  * 1. Redistributions of source code must retain the above copyright
135d06c0e8Sthorpej  *    notice, this list of conditions and the following disclaimer.
145d06c0e8Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
155d06c0e8Sthorpej  *    notice, this list of conditions and the following disclaimer in the
165d06c0e8Sthorpej  *    documentation and/or other materials provided with the distribution.
175d06c0e8Sthorpej  * 3. All advertising materials mentioning features or use of this software
185d06c0e8Sthorpej  *    must display the following acknowledgement:
195d06c0e8Sthorpej  *	This product includes software developed for the NetBSD Project by
205d06c0e8Sthorpej  *	Wasabi Systems, Inc.
215d06c0e8Sthorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
225d06c0e8Sthorpej  *    or promote products derived from this software without specific prior
235d06c0e8Sthorpej  *    written permission.
245d06c0e8Sthorpej  *
255d06c0e8Sthorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
265d06c0e8Sthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
275d06c0e8Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
285d06c0e8Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
295d06c0e8Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
305d06c0e8Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
315d06c0e8Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
325d06c0e8Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
335d06c0e8Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
345d06c0e8Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
355d06c0e8Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
365d06c0e8Sthorpej  */
375d06c0e8Sthorpej 
385d06c0e8Sthorpej /*
395d06c0e8Sthorpej  * dmover_process.c: Processing engine for dmover-api.
405d06c0e8Sthorpej  */
415d06c0e8Sthorpej 
425d06c0e8Sthorpej #include <sys/cdefs.h>
43*34c3adecSjakllsch __KERNEL_RCSID(0, "$NetBSD: dmover_process.c,v 1.5 2011/05/14 14:49:19 jakllsch Exp $");
445d06c0e8Sthorpej 
455d06c0e8Sthorpej #include <sys/param.h>
465d06c0e8Sthorpej #include <sys/systm.h>
475d06c0e8Sthorpej #include <sys/proc.h>
4846ed8f7dSad #include <sys/intr.h>
49*34c3adecSjakllsch #include <sys/mutex.h>
505d06c0e8Sthorpej 
515d06c0e8Sthorpej #include <dev/dmover/dmovervar.h>
525d06c0e8Sthorpej 
535d06c0e8Sthorpej TAILQ_HEAD(, dmover_request) dmover_completed_q;
54*34c3adecSjakllsch kmutex_t dmover_completed_q_lock;
555d06c0e8Sthorpej 
565d06c0e8Sthorpej void	*dmover_completed_si;
575d06c0e8Sthorpej 
585d06c0e8Sthorpej void	dmover_complete(void *);
595d06c0e8Sthorpej 
605d06c0e8Sthorpej /*
615d06c0e8Sthorpej  * dmover_process_init:
625d06c0e8Sthorpej  *
635d06c0e8Sthorpej  *	Initialize the processing engine.
645d06c0e8Sthorpej  */
655d06c0e8Sthorpej void
dmover_process_initialize(void)665d06c0e8Sthorpej dmover_process_initialize(void)
675d06c0e8Sthorpej {
685d06c0e8Sthorpej 
695d06c0e8Sthorpej 	TAILQ_INIT(&dmover_completed_q);
70*34c3adecSjakllsch 	mutex_init(&dmover_completed_q_lock, MUTEX_DEFAULT, IPL_BIO);
715d06c0e8Sthorpej 
7246ed8f7dSad 	dmover_completed_si = softint_establish(SOFTINT_CLOCK,
735d06c0e8Sthorpej 	    dmover_complete, NULL);
745d06c0e8Sthorpej }
755d06c0e8Sthorpej 
765d06c0e8Sthorpej /*
775d06c0e8Sthorpej  * dmover_process:	[client interface function]
785d06c0e8Sthorpej  *
795d06c0e8Sthorpej  *	Submit a tranform request for processing.
805d06c0e8Sthorpej  */
815d06c0e8Sthorpej void
dmover_process(struct dmover_request * dreq)825d06c0e8Sthorpej dmover_process(struct dmover_request *dreq)
835d06c0e8Sthorpej {
845d06c0e8Sthorpej 	struct dmover_session *dses = dreq->dreq_session;
855d06c0e8Sthorpej 	struct dmover_assignment *das;
865d06c0e8Sthorpej 	struct dmover_backend *dmb;
875d06c0e8Sthorpej 	int s;
885d06c0e8Sthorpej 
895d06c0e8Sthorpej #ifdef DIAGNOSTIC
905d06c0e8Sthorpej 	if ((dreq->dreq_flags & DMOVER_REQ_WAIT) != 0 &&
915d06c0e8Sthorpej 	    dreq->dreq_callback != NULL)
925d06c0e8Sthorpej 		panic("dmover_process: WAIT used with callback");
935d06c0e8Sthorpej #endif
945d06c0e8Sthorpej 
955d06c0e8Sthorpej 	/* Clear unwanted flag bits. */
965d06c0e8Sthorpej 	dreq->dreq_flags &= __DMOVER_REQ_FLAGS_PRESERVE;
975d06c0e8Sthorpej 
985d06c0e8Sthorpej 	s = splbio();
995d06c0e8Sthorpej 
1005d06c0e8Sthorpej 	/* XXXLOCK */
1015d06c0e8Sthorpej 
1025d06c0e8Sthorpej 	/* XXX Right now, the back-end is statically assigned. */
1035d06c0e8Sthorpej 	das = &dses->__dses_assignment;
1045d06c0e8Sthorpej 
1055d06c0e8Sthorpej 	dmb = das->das_backend;
1065d06c0e8Sthorpej 	dreq->dreq_assignment = das;
1075d06c0e8Sthorpej 
1085d06c0e8Sthorpej 	dmover_session_insque(dses, dreq);
1095d06c0e8Sthorpej 	dmover_backend_insque(dmb, dreq);
1105d06c0e8Sthorpej 
1115d06c0e8Sthorpej 	/* XXXUNLOCK */
1125d06c0e8Sthorpej 
1135d06c0e8Sthorpej 	splx(s);
1145d06c0e8Sthorpej 
1155d06c0e8Sthorpej 	/* Kick the back-end into action. */
1165d06c0e8Sthorpej 	(*dmb->dmb_process)(das->das_backend);
1175d06c0e8Sthorpej 
1185d06c0e8Sthorpej 	if (dreq->dreq_flags & DMOVER_REQ_WAIT) {
1195d06c0e8Sthorpej 		s = splbio();
1205d06c0e8Sthorpej 		/* XXXLOCK */
1215d06c0e8Sthorpej 		while ((dreq->dreq_flags & DMOVER_REQ_DONE) == 0)
1225d06c0e8Sthorpej 			(void) tsleep(dreq, PRIBIO, "dmover", 0);
1235d06c0e8Sthorpej 		/* XXXUNLOCK */
1245d06c0e8Sthorpej 		splx(s);
1255d06c0e8Sthorpej 	}
1265d06c0e8Sthorpej }
1275d06c0e8Sthorpej 
1285d06c0e8Sthorpej /*
1295d06c0e8Sthorpej  * dmover_done:		[back-end interface function]
1305d06c0e8Sthorpej  *
1315d06c0e8Sthorpej  *	Back-end notification that the dmover is done.
1325d06c0e8Sthorpej  */
1335d06c0e8Sthorpej void
dmover_done(struct dmover_request * dreq)1345d06c0e8Sthorpej dmover_done(struct dmover_request *dreq)
1355d06c0e8Sthorpej {
1365d06c0e8Sthorpej 	struct dmover_session *dses = dreq->dreq_session;
1375d06c0e8Sthorpej 	int s;
1385d06c0e8Sthorpej 
1395d06c0e8Sthorpej 	s = splbio();
1405d06c0e8Sthorpej 
1415d06c0e8Sthorpej 	/* XXXLOCK */
1425d06c0e8Sthorpej 
1435d06c0e8Sthorpej 	dmover_session_remque(dses, dreq);
1445d06c0e8Sthorpej 	/* backend has removed it from its queue */
1455d06c0e8Sthorpej 
1465d06c0e8Sthorpej 	/* XXXUNLOCK */
1475d06c0e8Sthorpej 
1485d06c0e8Sthorpej 	dreq->dreq_flags |= DMOVER_REQ_DONE;
1495d06c0e8Sthorpej 	dreq->dreq_flags &= ~DMOVER_REQ_RUNNING;
1505d06c0e8Sthorpej 	dreq->dreq_assignment = NULL;
1515d06c0e8Sthorpej 
1525d06c0e8Sthorpej 	if (dreq->dreq_callback != NULL) {
153*34c3adecSjakllsch 		mutex_enter(&dmover_completed_q_lock);
1545d06c0e8Sthorpej 		TAILQ_INSERT_TAIL(&dmover_completed_q, dreq, dreq_dmbq);
155*34c3adecSjakllsch 		mutex_exit(&dmover_completed_q_lock);
15646ed8f7dSad 		softint_schedule(dmover_completed_si);
1575d06c0e8Sthorpej 	} else if (dreq->dreq_flags & DMOVER_REQ_WAIT)
1585d06c0e8Sthorpej 		wakeup(dreq);
1595d06c0e8Sthorpej 
1605d06c0e8Sthorpej 	splx(s);
1615d06c0e8Sthorpej }
1625d06c0e8Sthorpej 
1635d06c0e8Sthorpej /*
1645d06c0e8Sthorpej  * dmover_complete:
1655d06c0e8Sthorpej  *
1665d06c0e8Sthorpej  *	Complete a request by invoking the callback.
1675d06c0e8Sthorpej  */
1685d06c0e8Sthorpej void
dmover_complete(void * arg)1695d06c0e8Sthorpej dmover_complete(void *arg)
1705d06c0e8Sthorpej {
1715d06c0e8Sthorpej 	struct dmover_request *dreq;
1725d06c0e8Sthorpej 
1735d06c0e8Sthorpej 	for (;;) {
174*34c3adecSjakllsch 		mutex_enter(&dmover_completed_q_lock);
1755d06c0e8Sthorpej 		if ((dreq = TAILQ_FIRST(&dmover_completed_q)) != NULL)
1765d06c0e8Sthorpej 			TAILQ_REMOVE(&dmover_completed_q, dreq, dreq_dmbq);
177*34c3adecSjakllsch 		mutex_exit(&dmover_completed_q_lock);
1785d06c0e8Sthorpej 
1795d06c0e8Sthorpej 		if (dreq == NULL)
1805d06c0e8Sthorpej 			return;
1815d06c0e8Sthorpej 
1825d06c0e8Sthorpej 		(*dreq->dreq_callback)(dreq);
1835d06c0e8Sthorpej 	}
1845d06c0e8Sthorpej }
185