summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/rsm/rsmops.c
blob: e69af1ad80649626e01243df1850f3b311fce4cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <sys/modctl.h>
#include <sys/stat.h>
#include <sys/proc.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/kmem.h>
#include <sys/file.h>
#include <sys/rsm/rsm_common.h>
#include <sys/rsm/rsmpi.h>
#include <sys/rsm/rsmpi_driver.h>

/* lint -w2 */
static struct modlmisc modlmisc = {
	&mod_miscops, "RSMOPS module %I%",
};

static struct modlinkage modlinkage = {
	MODREV_1, (void *)&modlmisc, NULL
};

static kmutex_t rsmops_lock;

static rsmops_drv_t *rsmops_drv_head = NULL;

static int rsmops_threads_started = 0;

int
_init(void)
{
	int	err;

	mutex_init(&rsmops_lock, NULL, MUTEX_DEFAULT, NULL);

	if ((err = mod_install(&modlinkage)) != 0)
		mutex_destroy(&rsmops_lock);

	return (err);
}

int
_fini(void)
{
	int	err;

	mutex_enter(&rsmops_lock);
	if (rsmops_drv_head) {
		/* Somebody is still registered with us - we cannot unload */
		mutex_exit(&rsmops_lock);
		return (EBUSY);
	}
	if (rsmops_threads_started) {
		/*
		 * Some threads have been started.  We do not have any
		 * well-supported way of checking whether they have all
		 * exited.  For now, fail attempt to unload if we have
		 * ever started any threads.  This is overkill, but ...
		 */
		mutex_exit(&rsmops_lock);
		return (EBUSY);
	}
	mutex_exit(&rsmops_lock);

	if ((err = mod_remove(&modlinkage)) == 0)
		mutex_destroy(&rsmops_lock);
	return (err);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

static void
rsmops_thread_entry(rsmops_drv_t *p_drv)
{
	/* p_drv->ctrl_cnt has already been increased by the time we get here */
	ASSERT(p_drv->drv.rsm_thread_entry_pt);

	/* call the driver with the thread */
	(*(p_drv->drv.rsm_thread_entry_pt))(p_drv->drv.drv_name);

	/* thread has returned */
	mutex_enter(&rsmops_lock);
	p_drv->ctrl_cnt--;
	mutex_exit(&rsmops_lock);
}

/* This is expected to be called from the driver's init function */
int
rsm_register_driver(rsmops_registry_t *p_registry)
{
	rsmops_drv_t **pp_tail;
	rsmops_drv_t *p;

	if (p_registry->rsm_version > RSM_VERSION) {
		/* The driver is up-rev than me.  Fail attempt to register */
		return (RSMERR_BAD_DRIVER_VERSION);
	}

	/*
	 * RSM_VERSION: Since this is the first version, there cannot be any
	 * down-rev drivers - this will be an issue in the future
	 */
	if (p_registry->rsm_version != RSM_VERSION)
		return (RSMERR_BAD_DRIVER_VERSION);

	mutex_enter(&rsmops_lock);
	/* First, search that this driver is not already registered */
	pp_tail = &rsmops_drv_head;
	while (*pp_tail) {
		if (strcmp((*pp_tail)->drv.drv_name, p_registry->drv_name)
		    == 0) {
			mutex_exit(&rsmops_lock);
			return (RSMERR_DRIVER_NAME_IN_USE);
		}
		pp_tail = &((*pp_tail)->next);
	}

	p = kmem_alloc(sizeof (rsmops_drv_t), KM_SLEEP);
	p->drv = *p_registry;	/* copy entire rsmops_registry_t structure */
	p->next = NULL;
	p->ctrl_cnt = 0;
	p->ctrl_head = NULL;

	if (p->drv.rsm_thread_entry_pt) {
		/* thread entry point is defined - we need to create a thread */
		extern  pri_t   minclsyspri;

		p->ctrl_cnt++;	/* bump up the count right now */
		p->thread_id = thread_create(NULL, 0, rsmops_thread_entry,
		    p, 0, &p0, TS_RUN, minclsyspri);
		rsmops_threads_started++;
	} else
		p->thread_id = NULL;

	*pp_tail = p;
	mutex_exit(&rsmops_lock);
	return (RSM_SUCCESS);
}

/*
 * This is expected to be called from the driver's fini function
 * if this function returns EBUSY, the driver is supposed to fail
 * its own fini operation
 */
int
rsm_unregister_driver(rsmops_registry_t *p_registry)
{
	rsmops_drv_t **pp_tail;
	rsmops_drv_t *p;

	mutex_enter(&rsmops_lock);

	/* Search for the driver */
	pp_tail = &rsmops_drv_head;
	while (*pp_tail) {
		if (strcmp((*pp_tail)->drv.drv_name, p_registry->drv_name)) {
			pp_tail = &((*pp_tail)->next);
			continue;
		}
		/* check ref count - if somebody is using it, return EBUSY */
		if ((*pp_tail)->ctrl_cnt) {
			mutex_exit(&rsmops_lock);
			return (RSMERR_CTLRS_REGISTERED);
		}
		/* Nobody is using it - we can allow the unregister to happen */
		p = *pp_tail;

		/* Stomp the guy out of our linked list */
		*pp_tail = (*pp_tail)->next;

		/* release the memory */
		kmem_free(p, sizeof (rsmops_drv_t));

		mutex_exit(&rsmops_lock);
		return (RSM_SUCCESS);
	}

	/* Could not find the guy */
	mutex_exit(&rsmops_lock);
	return (RSMERR_DRIVER_NOT_REGISTERED);
}

/* Should be called holding the rsmops_lock mutex */
static rsmops_drv_t *
find_rsmpi_driver(const char *name)
{
	rsmops_drv_t *p_rsmops_list;

	ASSERT(MUTEX_HELD(&rsmops_lock));
	/* the name is of the form "sci", "wci" etc */

	for (p_rsmops_list = rsmops_drv_head; p_rsmops_list != NULL;
	    p_rsmops_list = p_rsmops_list->next) {

		if (strcmp(name, p_rsmops_list->drv.drv_name) == 0) {
			return (p_rsmops_list);
		}
	}
	return (NULL);
}


/* Should be called holding the rsmops_lock mutex */
static rsmops_ctrl_t *
find_rsmpi_controller(const char *name, uint_t number)
{
	rsmops_drv_t *p_drv;
	rsmops_ctrl_t *p;

	ASSERT(MUTEX_HELD(&rsmops_lock));

	if ((p_drv = find_rsmpi_driver(name)) == NULL)
		return (NULL);

	for (p = p_drv->ctrl_head; p != NULL; p = p->next) {
		ASSERT(p->p_drv == p_drv);
		if (p->number == number)
			return (p);
	}
	return (NULL);
}

/* Should be called holding the rsmops_lock mutex */
static rsmops_ctrl_t *
find_rsmpi_controller_handle(rsm_controller_handle_t cntlr_handle)
{
	rsmops_drv_t *p_drv;
	rsmops_ctrl_t *p;

	ASSERT(MUTEX_HELD(&rsmops_lock));

	for (p_drv = rsmops_drv_head; p_drv != NULL; p_drv = p_drv->next) {
		for (p = p_drv->ctrl_head; p != NULL; p = p->next) {
			if (p->handle == cntlr_handle)
				return (p);
		}
	}

	return (NULL);
}

static vnode_t *
rsmops_device_open(const char *major_name, const minor_t minor_num);

int
rsm_get_controller(const char *name, uint_t number,
    rsm_controller_object_t *controller, uint_t version)
{
	rsmops_ctrl_t *p_ctrl;
	rsmops_drv_t *p_drv;
	vnode_t *vp;
	int error;
	int (*rsm_get_controller_handler)
	    (const char *name, uint_t number,
	    rsm_controller_object_t *pcontroller, uint_t version);

	mutex_enter(&rsmops_lock);

	/* check if the controller is already registered */
	if ((p_ctrl = find_rsmpi_controller(name, number)) == NULL) {
		/*
		 * controller is not registered.  We should try to load it
		 * First check if the driver is registered
		 */
		if ((p_drv = find_rsmpi_driver(name)) == NULL) {
			/* Cannot find the driver.  Try to load him */
			mutex_exit(&rsmops_lock);
			if ((error = modload("drv", (char *)name)) == -1) {
				return (RSMERR_CTLR_NOT_PRESENT);
			}
			mutex_enter(&rsmops_lock);
			if ((p_drv = find_rsmpi_driver(name)) == NULL) {
				mutex_exit(&rsmops_lock);
				/*
				 * Cannot find yet - maybe the driver we loaded
				 * was not a RSMPI driver at all.  We'll just
				 * fail this call.
				 */
				return (RSMERR_CTLR_NOT_PRESENT);
			}
		}
		ASSERT(p_drv);
		p_ctrl = find_rsmpi_controller(name, number);
		if (p_ctrl == NULL) {
			/*
			 * controller is not registered.
			 * try to do a VOP_OPEN to force it to get registered
			 */
			mutex_exit(&rsmops_lock);
			vp = rsmops_device_open(name, number);
			mutex_enter(&rsmops_lock);
			if (vp != NULL) {
				(void) VOP_CLOSE(vp, FREAD|FWRITE, 0, 0,
				    CRED());
				VN_RELE(vp);
			}
			p_ctrl = find_rsmpi_controller(name, number);
			if (p_ctrl == NULL) {
				mutex_exit(&rsmops_lock);
				return (RSMERR_CTLR_NOT_PRESENT);
			}
		}
		ASSERT(p_ctrl);
	} else {
		p_drv = p_ctrl->p_drv;
	}
	ASSERT(p_drv);
	ASSERT(p_drv == p_ctrl->p_drv);

	rsm_get_controller_handler = p_drv->drv.rsm_get_controller_handler;
	/*
	 * Increase the refcnt right now, so that attempts to deregister
	 * while we are using this entry will fail
	 */
	p_ctrl->refcnt++;
	mutex_exit(&rsmops_lock);

	error = (*rsm_get_controller_handler)(name, number, controller,
	    version);
	if (error != RSM_SUCCESS) {
		/* We failed - drop the refcnt back */
		mutex_enter(&rsmops_lock);
		/*
		 * Even though we had released the global lock, we can
		 * guarantee that p_ctrl is still meaningful (and has not
		 * been deregistered, freed whatever) because we were holding
		 * refcnt on it.  So, it is okay to just use p_ctrl here
		 * after re-acquiring the global lock
		 */
		p_ctrl->refcnt--;
		mutex_exit(&rsmops_lock);
	} else {
		/*
		 * Initialize the controller handle field
		 */
		mutex_enter(&rsmops_lock);
		if ((p_ctrl = find_rsmpi_controller(name, number)) == NULL) {
			mutex_exit(&rsmops_lock);
			return (RSMERR_CTLR_NOT_PRESENT);
		}

		p_ctrl->handle = controller->handle;
		mutex_exit(&rsmops_lock);
	}
	return (error);
}

int
rsm_release_controller(const char *name, uint_t number,
    rsm_controller_object_t *controller)
{
	rsmops_ctrl_t *p_ctrl;
	rsmops_drv_t *p_drv;
	int error;
	int (*releaser)(const char *name, uint_t number,
	    rsm_controller_object_t *controller);

	mutex_enter(&rsmops_lock);

	if ((p_ctrl = find_rsmpi_controller(name, number)) == NULL) {
		mutex_exit(&rsmops_lock);
		return (RSMERR_CTLR_NOT_PRESENT);
	}
	p_drv = find_rsmpi_driver(name);
	ASSERT(p_drv);	/* If we found controller, there MUST be a driver */

	/* Found the appropriate driver.  Forward the call to it */
	releaser = p_drv->drv.rsm_release_controller_handler;
	mutex_exit(&rsmops_lock);

	error = (*releaser)(name, number, controller);
	if (error == RSM_SUCCESS) {
		mutex_enter(&rsmops_lock);
		p_ctrl->refcnt--;
		mutex_exit(&rsmops_lock);
	}
	return (error);
}

/* This is expected to be called from the driver's attach function */
int
rsm_register_controller(const char *name, uint_t number,
    rsm_controller_attr_t *attrp)
{
	rsmops_drv_t *p_drv;
	rsmops_ctrl_t *p_ctrl;

	if (strlen(name) > MAX_DRVNAME)
		return (RSMERR_NAME_TOO_LONG);

	mutex_enter(&rsmops_lock);

	/* Check if the driver is registered with us */
	p_drv = find_rsmpi_driver(name);
	if (p_drv == NULL) {
		/*
		 * Hey! Driver is not registered, but we are getting a
		 * controller ??
		 */
		mutex_exit(&rsmops_lock);
		return (RSMERR_DRIVER_NOT_REGISTERED);
	}

	/* Check if the controller is already registered with us */
	p_ctrl = find_rsmpi_controller(name, number);
	if (p_ctrl) {
		/* already registered */
		mutex_exit(&rsmops_lock);
		return (RSMERR_CTLR_ALREADY_REGISTERED);
	}

	/* WAIT: sanity check - verify that the dip matches up to name,number */

	p_ctrl = kmem_alloc(sizeof (rsmops_ctrl_t), KM_SLEEP);

	/* bump up controller count on the driver */
	p_drv->ctrl_cnt++;

	p_ctrl->p_drv = p_drv;	/* setup the back pointer */
	p_ctrl->number = number;
	p_ctrl->refcnt = 0;
	p_ctrl->attrp = attrp;
	p_ctrl->handle = NULL;

	/* Now link to head of list */
	p_ctrl->next = p_drv->ctrl_head;
	p_drv->ctrl_head = p_ctrl;

	mutex_exit(&rsmops_lock);

	return (RSM_SUCCESS);
}

/*
 * This is expected to be called from the driver's detach function
 * if this function returns EBUSY, the driver is supposed to fail
 * his own detach operation
 */
int
rsm_unregister_controller(const char *name, uint_t number)
{
	rsmops_drv_t *p_drv;
	rsmops_ctrl_t **p_prev;
	rsmops_ctrl_t *found;

	mutex_enter(&rsmops_lock);

	/* Check if the driver is registered with us */
	p_drv = find_rsmpi_driver(name);
	if (p_drv == NULL) {
		/* Hey!  Driver is not registered */
		mutex_exit(&rsmops_lock);
		return (RSMERR_DRIVER_NOT_REGISTERED);
	}

	/* Search for the controller in the list */
	for (p_prev = &p_drv->ctrl_head; *p_prev; p_prev = &((*p_prev)->next)) {
		if ((*p_prev)->number == number) {
			/* Found the controller.  Check if it is busy */
			found = *p_prev;

			if (found->refcnt) {
				/* Controller is busy -  handles outstanding */
				mutex_exit(&rsmops_lock);
				return (RSMERR_CTLR_IN_USE);
			}
			/* unlink it out */
			*p_prev = found->next;
			/* bump down controller count on the driver */
			p_drv->ctrl_cnt--;

			mutex_exit(&rsmops_lock);
			kmem_free(found, sizeof (rsmops_ctrl_t));
			return (RSM_SUCCESS);
		}
	}
	mutex_exit(&rsmops_lock);
	/* Could not find the right controller */
	return (RSMERR_CTLR_NOT_REGISTERED);
}


/*
 * This opens and closes the appropriate device with minor number -
 * hopefully, it will cause the driver to attach and register a controller
 * with us
 */
static vnode_t *
rsmops_device_open(const char *major_name, const minor_t minor_num)
{
	major_t maj;
	vnode_t *vp;
	int ret;

	if (minor_num == (minor_t)-1) {
		return (NULL);
	}

	maj = ddi_name_to_major((char *)major_name);
	if (maj == (major_t)-1) {
		return (NULL);
	}

	vp = makespecvp(makedevice(maj, minor_num), VCHR);

	ret = VOP_OPEN(&vp, FREAD|FWRITE, CRED());
	if (ret == 0) {
		return (vp);
	} else {
		VN_RELE(vp);
		return (NULL);
	}
}

/*
 * Attributes for controller identified by the handle are returned
 * via *attrp. Modifications of attributes is prohibited by client!
 */
int
rsm_get_controller_attr(rsm_controller_handle_t handle,
    rsm_controller_attr_t **attrp)
{

	rsmops_ctrl_t *p_ctrl;

	if (handle == NULL)
		return (RSMERR_BAD_CTLR_HNDL);

	mutex_enter(&rsmops_lock);

	/* find controller */
	if ((p_ctrl = find_rsmpi_controller_handle(handle)) == NULL) {
		/* can't supply attributes for invalid controller */
		mutex_exit(&rsmops_lock);
		return (RSMERR_BAD_CTLR_HNDL);
	}
	*attrp =  p_ctrl->attrp;
	mutex_exit(&rsmops_lock);

	return (RSM_SUCCESS);
}