Home | History | Annotate | Line # | Download | only in dmover
      1  1.9  jakllsch /*	$NetBSD: dmover_backend.c,v 1.9 2011/05/14 18:24:47 jakllsch 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.9  jakllsch __KERNEL_RCSID(0, "$NetBSD: dmover_backend.c,v 1.9 2011/05/14 18:24:47 jakllsch Exp $");
     44  1.1   thorpej 
     45  1.1   thorpej #include <sys/param.h>
     46  1.6        ad #include <sys/mutex.h>
     47  1.1   thorpej #include <sys/systm.h>
     48  1.9  jakllsch #include <sys/once.h>
     49  1.1   thorpej 
     50  1.1   thorpej #include <dev/dmover/dmovervar.h>
     51  1.1   thorpej 
     52  1.1   thorpej TAILQ_HEAD(, dmover_backend) dmover_backend_list;
     53  1.6        ad kmutex_t dmover_backend_list_lock;
     54  1.9  jakllsch static bool initialized;
     55  1.1   thorpej 
     56  1.9  jakllsch static int
     57  1.1   thorpej initialize(void)
     58  1.1   thorpej {
     59  1.1   thorpej 
     60  1.9  jakllsch 	KASSERT(initialized == false);
     61  1.1   thorpej 
     62  1.9  jakllsch 	TAILQ_INIT(&dmover_backend_list);
     63  1.9  jakllsch 	mutex_init(&dmover_backend_list_lock, MUTEX_DEFAULT, IPL_VM);
     64  1.9  jakllsch 
     65  1.9  jakllsch 	/* Initialize the other bits of dmover. */
     66  1.9  jakllsch 	dmover_session_initialize();
     67  1.9  jakllsch 	dmover_request_initialize();
     68  1.9  jakllsch 	dmover_process_initialize();
     69  1.9  jakllsch 
     70  1.9  jakllsch 	initialized = true;
     71  1.9  jakllsch 
     72  1.9  jakllsch 	return 0;
     73  1.1   thorpej }
     74  1.1   thorpej 
     75  1.1   thorpej /*
     76  1.1   thorpej  * dmover_backend_register:	[back-end interface function]
     77  1.1   thorpej  *
     78  1.1   thorpej  *	Register a back-end with dmover-api.
     79  1.1   thorpej  */
     80  1.1   thorpej void
     81  1.1   thorpej dmover_backend_register(struct dmover_backend *dmb)
     82  1.1   thorpej {
     83  1.9  jakllsch 	static ONCE_DECL(control);
     84  1.9  jakllsch 
     85  1.9  jakllsch 	RUN_ONCE(&control, initialize);
     86  1.1   thorpej 
     87  1.9  jakllsch 	KASSERT(initialized == true);
     88  1.1   thorpej 
     89  1.1   thorpej 	LIST_INIT(&dmb->dmb_sessions);
     90  1.1   thorpej 	dmb->dmb_nsessions = 0;
     91  1.1   thorpej 
     92  1.1   thorpej 	TAILQ_INIT(&dmb->dmb_pendreqs);
     93  1.1   thorpej 	dmb->dmb_npendreqs = 0;
     94  1.1   thorpej 
     95  1.6        ad 	mutex_enter(&dmover_backend_list_lock);
     96  1.1   thorpej 	TAILQ_INSERT_TAIL(&dmover_backend_list, dmb, dmb_list);
     97  1.6        ad 	mutex_exit(&dmover_backend_list_lock);
     98  1.1   thorpej }
     99  1.1   thorpej 
    100  1.1   thorpej /*
    101  1.1   thorpej  * dmover_backend_unregister:	[back-end interface function]
    102  1.1   thorpej  *
    103  1.1   thorpej  *	Un-register a back-end from dmover-api.
    104  1.1   thorpej  */
    105  1.1   thorpej void
    106  1.1   thorpej dmover_backend_unregister(struct dmover_backend *dmb)
    107  1.1   thorpej {
    108  1.1   thorpej 
    109  1.9  jakllsch 	KASSERT(initialized == true);
    110  1.1   thorpej 
    111  1.1   thorpej 	/* XXX */
    112  1.1   thorpej 	if (dmb->dmb_nsessions)
    113  1.1   thorpej 		panic("dmover_backend_unregister");
    114  1.1   thorpej 
    115  1.6        ad 	mutex_enter(&dmover_backend_list_lock);
    116  1.1   thorpej 	TAILQ_REMOVE(&dmover_backend_list, dmb, dmb_list);
    117  1.6        ad 	mutex_exit(&dmover_backend_list_lock);
    118  1.1   thorpej }
    119  1.1   thorpej 
    120  1.1   thorpej /*
    121  1.1   thorpej  * dmover_backend_alloc:
    122  1.1   thorpej  *
    123  1.1   thorpej  *	Allocate and return a back-end on behalf of a session.
    124  1.1   thorpej  */
    125  1.1   thorpej int
    126  1.1   thorpej dmover_backend_alloc(struct dmover_session *dses, const char *type)
    127  1.1   thorpej {
    128  1.1   thorpej 	struct dmover_backend *dmb, *best_dmb = NULL;
    129  1.1   thorpej 	const struct dmover_algdesc *algdesc, *best_algdesc = NULL;
    130  1.1   thorpej 
    131  1.9  jakllsch 	if (__predict_false(initialized == false)) {
    132  1.9  jakllsch 		return (ESRCH);
    133  1.1   thorpej 	}
    134  1.1   thorpej 
    135  1.6        ad 	mutex_enter(&dmover_backend_list_lock);
    136  1.1   thorpej 
    137  1.1   thorpej 	/* First, find a back-end that can handle the session parts. */
    138  1.1   thorpej 	for (dmb = TAILQ_FIRST(&dmover_backend_list); dmb != NULL;
    139  1.1   thorpej 	     dmb = TAILQ_NEXT(dmb, dmb_list)) {
    140  1.1   thorpej 		/*
    141  1.1   thorpej 		 * First, check to see if the back-end supports the
    142  1.1   thorpej 		 * function we wish to perform.
    143  1.1   thorpej 		 */
    144  1.1   thorpej 		algdesc = dmover_algdesc_lookup(dmb->dmb_algdescs,
    145  1.1   thorpej 		    dmb->dmb_nalgdescs, type);
    146  1.1   thorpej 		if (algdesc == NULL)
    147  1.1   thorpej 			continue;
    148  1.1   thorpej 
    149  1.1   thorpej 		if (best_dmb == NULL) {
    150  1.1   thorpej 			best_dmb = dmb;
    151  1.1   thorpej 			best_algdesc = algdesc;
    152  1.1   thorpej 			continue;
    153  1.1   thorpej 		}
    154  1.1   thorpej 
    155  1.1   thorpej 		/*
    156  1.1   thorpej 		 * XXX All the stuff from here on should be shot in
    157  1.1   thorpej 		 * XXX the head.  Instead, we should build a list
    158  1.1   thorpej 		 * XXX of candidates, and select the best back-end
    159  1.1   thorpej 		 * XXX when a request is scheduled for processing.
    160  1.1   thorpej 		 */
    161  1.1   thorpej 
    162  1.1   thorpej 		if (dmb->dmb_speed >= best_dmb->dmb_speed) {
    163  1.1   thorpej 			/*
    164  1.1   thorpej 			 * If the current best match is slower than
    165  1.1   thorpej 			 * this back-end, then this one is the new
    166  1.1   thorpej 			 * best match.
    167  1.1   thorpej 			 */
    168  1.1   thorpej 			if (dmb->dmb_speed > best_dmb->dmb_speed) {
    169  1.1   thorpej 				best_dmb = dmb;
    170  1.1   thorpej 				best_algdesc = algdesc;
    171  1.1   thorpej 				continue;
    172  1.1   thorpej 			}
    173  1.1   thorpej 
    174  1.1   thorpej 			/*
    175  1.1   thorpej 			 * If this back-end has fewer sessions allocated
    176  1.1   thorpej 			 * to it than the current best match, then this
    177  1.1   thorpej 			 * one is now the best match.
    178  1.1   thorpej 			 */
    179  1.1   thorpej 			if (best_dmb->dmb_nsessions > dmb->dmb_nsessions) {
    180  1.1   thorpej 				best_dmb = dmb;
    181  1.1   thorpej 				best_algdesc = algdesc;
    182  1.1   thorpej 				continue;
    183  1.1   thorpej 			}
    184  1.1   thorpej 		}
    185  1.1   thorpej 	}
    186  1.1   thorpej 	if (best_dmb == NULL) {
    187  1.6        ad 		mutex_exit(&dmover_backend_list_lock);
    188  1.1   thorpej 		return (ESRCH);
    189  1.1   thorpej 	}
    190  1.1   thorpej 
    191  1.1   thorpej 	KASSERT(best_algdesc != NULL);
    192  1.1   thorpej 
    193  1.1   thorpej 	/* Plug the back-end into the static (XXX) assignment. */
    194  1.1   thorpej 	dses->__dses_assignment.das_backend = best_dmb;
    195  1.1   thorpej 	dses->__dses_assignment.das_algdesc = best_algdesc;
    196  1.1   thorpej 
    197  1.5    briggs 	dses->dses_ninputs = best_algdesc->dad_ninputs;
    198  1.1   thorpej 
    199  1.1   thorpej 	LIST_INSERT_HEAD(&best_dmb->dmb_sessions, dses, __dses_list);
    200  1.1   thorpej 	best_dmb->dmb_nsessions++;
    201  1.1   thorpej 
    202  1.6        ad 	mutex_exit(&dmover_backend_list_lock);
    203  1.1   thorpej 
    204  1.1   thorpej 	return (0);
    205  1.1   thorpej }
    206  1.1   thorpej 
    207  1.1   thorpej /*
    208  1.1   thorpej  * dmover_backend_release:
    209  1.1   thorpej  *
    210  1.1   thorpej  *	Release the back-end from the specified session.
    211  1.1   thorpej  */
    212  1.1   thorpej void
    213  1.1   thorpej dmover_backend_release(struct dmover_session *dses)
    214  1.1   thorpej {
    215  1.1   thorpej 	struct dmover_backend *dmb;
    216  1.1   thorpej 
    217  1.6        ad 	mutex_enter(&dmover_backend_list_lock);
    218  1.1   thorpej 
    219  1.1   thorpej 	/* XXX Clear out the static assignment. */
    220  1.1   thorpej 	dmb = dses->__dses_assignment.das_backend;
    221  1.1   thorpej 	dses->__dses_assignment.das_backend = NULL;
    222  1.1   thorpej 	dses->__dses_assignment.das_algdesc = NULL;
    223  1.1   thorpej 
    224  1.1   thorpej 	LIST_REMOVE(dses, __dses_list);
    225  1.1   thorpej 	dmb->dmb_nsessions--;
    226  1.1   thorpej 
    227  1.6        ad 	mutex_exit(&dmover_backend_list_lock);
    228  1.1   thorpej }
    229