Home | History | Annotate | Line # | Download | only in dmover
dmover_backend.c revision 1.5
      1  1.5   briggs /*	$NetBSD: dmover_backend.c,v 1.5 2003/04/26 04:44:18 briggs Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 2002 Wasabi Systems, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  1.1  thorpej  *
      9  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     10  1.1  thorpej  * modification, are permitted provided that the following conditions
     11  1.1  thorpej  * are met:
     12  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     14  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     17  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     18  1.1  thorpej  *    must display the following acknowledgement:
     19  1.1  thorpej  *	This product includes software developed for the NetBSD Project by
     20  1.1  thorpej  *	Wasabi Systems, Inc.
     21  1.1  thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.1  thorpej  *    or promote products derived from this software without specific prior
     23  1.1  thorpej  *    written permission.
     24  1.1  thorpej  *
     25  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1  thorpej  */
     37  1.1  thorpej 
     38  1.1  thorpej /*
     39  1.1  thorpej  * dmover_backend.c: Backend management functions for dmover-api.
     40  1.1  thorpej  */
     41  1.1  thorpej 
     42  1.1  thorpej #include <sys/cdefs.h>
     43  1.5   briggs __KERNEL_RCSID(0, "$NetBSD: dmover_backend.c,v 1.5 2003/04/26 04:44:18 briggs Exp $");
     44  1.1  thorpej 
     45  1.1  thorpej #include <sys/param.h>
     46  1.1  thorpej #include <sys/lock.h>
     47  1.1  thorpej #include <sys/systm.h>
     48  1.1  thorpej 
     49  1.1  thorpej #include <dev/dmover/dmovervar.h>
     50  1.1  thorpej 
     51  1.1  thorpej TAILQ_HEAD(, dmover_backend) dmover_backend_list;
     52  1.1  thorpej struct lock dmover_backend_list_lock;
     53  1.1  thorpej 
     54  1.1  thorpej #define	BACKEND_LIST_LOCK_READ()					\
     55  1.1  thorpej do {									\
     56  1.3  thorpej 	(void) spinlockmgr(&dmover_backend_list_lock, LK_SHARED, NULL);	\
     57  1.1  thorpej } while (/*CONSTCOND*/0)
     58  1.1  thorpej 
     59  1.1  thorpej #define	BACKEND_LIST_UNLOCK_READ()					\
     60  1.1  thorpej do {									\
     61  1.1  thorpej 	(void) spinlockmgr(&dmover_backend_list_lock, LK_RELEASE, NULL);\
     62  1.1  thorpej } while (/*CONSTCOND*/0)
     63  1.1  thorpej 
     64  1.1  thorpej #define	BACKEND_LIST_LOCK_WRITE(s)					\
     65  1.1  thorpej do {									\
     66  1.1  thorpej 	(s) = splbio();							\
     67  1.1  thorpej 	(void) spinlockmgr(&dmover_backend_list_lock, LK_EXCLUSIVE, NULL); \
     68  1.1  thorpej } while (/*CONSTCOND*/0)
     69  1.1  thorpej 
     70  1.1  thorpej #define	BACKEND_LIST_UNLOCK_WRITE(s)					\
     71  1.1  thorpej do {									\
     72  1.1  thorpej 	(void) spinlockmgr(&dmover_backend_list_lock, LK_RELEASE, NULL);\
     73  1.1  thorpej 	splx((s));							\
     74  1.1  thorpej } while (/*CONSTCOND*/0)
     75  1.1  thorpej 
     76  1.1  thorpej static int initialized;
     77  1.1  thorpej static struct simplelock initialized_slock = SIMPLELOCK_INITIALIZER;
     78  1.1  thorpej 
     79  1.1  thorpej static void
     80  1.1  thorpej initialize(void)
     81  1.1  thorpej {
     82  1.1  thorpej 
     83  1.1  thorpej 	simple_lock(&initialized_slock);
     84  1.1  thorpej 	if (__predict_true(initialized == 0)) {
     85  1.1  thorpej 		TAILQ_INIT(&dmover_backend_list);
     86  1.1  thorpej 		spinlockinit(&dmover_backend_list_lock, "dmbelk", 0);
     87  1.1  thorpej 
     88  1.1  thorpej 		/* Initialize the other bits of dmover. */
     89  1.1  thorpej 		dmover_session_initialize();
     90  1.1  thorpej 		dmover_request_initialize();
     91  1.1  thorpej 		dmover_process_initialize();
     92  1.1  thorpej 
     93  1.1  thorpej 		initialized = 1;
     94  1.1  thorpej 	}
     95  1.1  thorpej 	simple_unlock(&initialized_slock);
     96  1.1  thorpej }
     97  1.1  thorpej 
     98  1.1  thorpej /*
     99  1.1  thorpej  * dmover_backend_register:	[back-end interface function]
    100  1.1  thorpej  *
    101  1.1  thorpej  *	Register a back-end with dmover-api.
    102  1.1  thorpej  */
    103  1.1  thorpej void
    104  1.1  thorpej dmover_backend_register(struct dmover_backend *dmb)
    105  1.1  thorpej {
    106  1.1  thorpej 	int s;
    107  1.1  thorpej 
    108  1.1  thorpej 	if (__predict_false(initialized == 0))
    109  1.1  thorpej 		initialize();
    110  1.1  thorpej 
    111  1.1  thorpej 	LIST_INIT(&dmb->dmb_sessions);
    112  1.1  thorpej 	dmb->dmb_nsessions = 0;
    113  1.1  thorpej 
    114  1.1  thorpej 	TAILQ_INIT(&dmb->dmb_pendreqs);
    115  1.1  thorpej 	dmb->dmb_npendreqs = 0;
    116  1.1  thorpej 
    117  1.1  thorpej 	BACKEND_LIST_LOCK_WRITE(s);
    118  1.1  thorpej 	TAILQ_INSERT_TAIL(&dmover_backend_list, dmb, dmb_list);
    119  1.1  thorpej 	BACKEND_LIST_UNLOCK_WRITE(s);
    120  1.1  thorpej }
    121  1.1  thorpej 
    122  1.1  thorpej /*
    123  1.1  thorpej  * dmover_backend_unregister:	[back-end interface function]
    124  1.1  thorpej  *
    125  1.1  thorpej  *	Un-register a back-end from dmover-api.
    126  1.1  thorpej  */
    127  1.1  thorpej void
    128  1.1  thorpej dmover_backend_unregister(struct dmover_backend *dmb)
    129  1.1  thorpej {
    130  1.1  thorpej 	int s;
    131  1.1  thorpej 
    132  1.1  thorpej #ifdef DIAGNOSTIC
    133  1.1  thorpej 	if (__predict_false(initialized == 0)) {
    134  1.1  thorpej 		int croak;
    135  1.1  thorpej 
    136  1.1  thorpej 		simple_lock(&initialized_slock);
    137  1.1  thorpej 		croak = (initialized == 0);
    138  1.1  thorpej 		simple_unlock(&initialized_slock);
    139  1.1  thorpej 
    140  1.1  thorpej 		if (croak)
    141  1.1  thorpej 			panic("dmover_backend_unregister: not initialized");
    142  1.1  thorpej 	}
    143  1.1  thorpej #endif
    144  1.1  thorpej 
    145  1.1  thorpej 	/* XXX */
    146  1.1  thorpej 	if (dmb->dmb_nsessions)
    147  1.1  thorpej 		panic("dmover_backend_unregister");
    148  1.1  thorpej 
    149  1.1  thorpej 	BACKEND_LIST_LOCK_WRITE(s);
    150  1.1  thorpej 	TAILQ_REMOVE(&dmover_backend_list, dmb, dmb_list);
    151  1.1  thorpej 	BACKEND_LIST_UNLOCK_WRITE(s);
    152  1.1  thorpej }
    153  1.1  thorpej 
    154  1.1  thorpej /*
    155  1.1  thorpej  * dmover_backend_alloc:
    156  1.1  thorpej  *
    157  1.1  thorpej  *	Allocate and return a back-end on behalf of a session.
    158  1.1  thorpej  */
    159  1.1  thorpej int
    160  1.1  thorpej dmover_backend_alloc(struct dmover_session *dses, const char *type)
    161  1.1  thorpej {
    162  1.1  thorpej 	struct dmover_backend *dmb, *best_dmb = NULL;
    163  1.1  thorpej 	const struct dmover_algdesc *algdesc, *best_algdesc = NULL;
    164  1.1  thorpej 
    165  1.1  thorpej 	if (__predict_false(initialized == 0)) {
    166  1.1  thorpej 		int fail;
    167  1.1  thorpej 
    168  1.1  thorpej 		simple_lock(&initialized_slock);
    169  1.1  thorpej 		fail = (initialized == 0);
    170  1.1  thorpej 		simple_unlock(&initialized_slock);
    171  1.1  thorpej 
    172  1.1  thorpej 		if (fail)
    173  1.1  thorpej 			return (ESRCH);
    174  1.1  thorpej 	}
    175  1.1  thorpej 
    176  1.1  thorpej 	BACKEND_LIST_LOCK_READ();
    177  1.1  thorpej 
    178  1.1  thorpej 	/* First, find a back-end that can handle the session parts. */
    179  1.1  thorpej 	for (dmb = TAILQ_FIRST(&dmover_backend_list); dmb != NULL;
    180  1.1  thorpej 	     dmb = TAILQ_NEXT(dmb, dmb_list)) {
    181  1.1  thorpej 		/*
    182  1.1  thorpej 		 * First, check to see if the back-end supports the
    183  1.1  thorpej 		 * function we wish to perform.
    184  1.1  thorpej 		 */
    185  1.1  thorpej 		algdesc = dmover_algdesc_lookup(dmb->dmb_algdescs,
    186  1.1  thorpej 		    dmb->dmb_nalgdescs, type);
    187  1.1  thorpej 		if (algdesc == NULL)
    188  1.1  thorpej 			continue;
    189  1.1  thorpej 
    190  1.1  thorpej 		if (best_dmb == NULL) {
    191  1.1  thorpej 			best_dmb = dmb;
    192  1.1  thorpej 			best_algdesc = algdesc;
    193  1.1  thorpej 			continue;
    194  1.1  thorpej 		}
    195  1.1  thorpej 
    196  1.1  thorpej 		/*
    197  1.1  thorpej 		 * XXX All the stuff from here on should be shot in
    198  1.1  thorpej 		 * XXX the head.  Instead, we should build a list
    199  1.1  thorpej 		 * XXX of candidates, and select the best back-end
    200  1.1  thorpej 		 * XXX when a request is scheduled for processing.
    201  1.1  thorpej 		 */
    202  1.1  thorpej 
    203  1.1  thorpej 		if (dmb->dmb_speed >= best_dmb->dmb_speed) {
    204  1.1  thorpej 			/*
    205  1.1  thorpej 			 * If the current best match is slower than
    206  1.1  thorpej 			 * this back-end, then this one is the new
    207  1.1  thorpej 			 * best match.
    208  1.1  thorpej 			 */
    209  1.1  thorpej 			if (dmb->dmb_speed > best_dmb->dmb_speed) {
    210  1.1  thorpej 				best_dmb = dmb;
    211  1.1  thorpej 				best_algdesc = algdesc;
    212  1.1  thorpej 				continue;
    213  1.1  thorpej 			}
    214  1.1  thorpej 
    215  1.1  thorpej 			/*
    216  1.1  thorpej 			 * If this back-end has fewer sessions allocated
    217  1.1  thorpej 			 * to it than the current best match, then this
    218  1.1  thorpej 			 * one is now the best match.
    219  1.1  thorpej 			 */
    220  1.1  thorpej 			if (best_dmb->dmb_nsessions > dmb->dmb_nsessions) {
    221  1.1  thorpej 				best_dmb = dmb;
    222  1.1  thorpej 				best_algdesc = algdesc;
    223  1.1  thorpej 				continue;
    224  1.1  thorpej 			}
    225  1.1  thorpej 		}
    226  1.1  thorpej 	}
    227  1.1  thorpej 	if (best_dmb == NULL) {
    228  1.1  thorpej 		BACKEND_LIST_UNLOCK_READ();
    229  1.1  thorpej 		return (ESRCH);
    230  1.1  thorpej 	}
    231  1.1  thorpej 
    232  1.1  thorpej 	KASSERT(best_algdesc != NULL);
    233  1.1  thorpej 
    234  1.1  thorpej 	/* Plug the back-end into the static (XXX) assignment. */
    235  1.1  thorpej 	dses->__dses_assignment.das_backend = best_dmb;
    236  1.1  thorpej 	dses->__dses_assignment.das_algdesc = best_algdesc;
    237  1.1  thorpej 
    238  1.5   briggs 	dses->dses_ninputs = best_algdesc->dad_ninputs;
    239  1.1  thorpej 
    240  1.1  thorpej 	LIST_INSERT_HEAD(&best_dmb->dmb_sessions, dses, __dses_list);
    241  1.1  thorpej 	best_dmb->dmb_nsessions++;
    242  1.1  thorpej 
    243  1.1  thorpej 	BACKEND_LIST_UNLOCK_READ();
    244  1.1  thorpej 
    245  1.1  thorpej 	return (0);
    246  1.1  thorpej }
    247  1.1  thorpej 
    248  1.1  thorpej /*
    249  1.1  thorpej  * dmover_backend_release:
    250  1.1  thorpej  *
    251  1.1  thorpej  *	Release the back-end from the specified session.
    252  1.1  thorpej  */
    253  1.1  thorpej void
    254  1.1  thorpej dmover_backend_release(struct dmover_session *dses)
    255  1.1  thorpej {
    256  1.1  thorpej 	struct dmover_backend *dmb;
    257  1.1  thorpej 
    258  1.4   briggs 	BACKEND_LIST_LOCK_READ();
    259  1.1  thorpej 
    260  1.1  thorpej 	/* XXX Clear out the static assignment. */
    261  1.1  thorpej 	dmb = dses->__dses_assignment.das_backend;
    262  1.1  thorpej 	dses->__dses_assignment.das_backend = NULL;
    263  1.1  thorpej 	dses->__dses_assignment.das_algdesc = NULL;
    264  1.1  thorpej 
    265  1.1  thorpej 	LIST_REMOVE(dses, __dses_list);
    266  1.1  thorpej 	dmb->dmb_nsessions--;
    267  1.1  thorpej 
    268  1.1  thorpej 	BACKEND_LIST_UNLOCK_READ();
    269  1.1  thorpej }
    270