summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4/io/efcode/fc_physio.c
blob: 17890a7163a9d6b49f88a61a1b00c62b897f7fd0 (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
/*
 * 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 (c) 1999-2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

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

#include <sys/types.h>
#include <sys/debug.h>
#include <sys/cmn_err.h>
#include <sys/kmem.h>
#include <sys/sysmacros.h>
#include <sys/buf.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/vmsystm.h>
#include <sys/mman.h>
#include <sys/vm.h>
#include <sys/ddi.h>

#include <vm/as.h>
#include <vm/page.h>

/*
 * Prepare for raw I/O request - derived from default_physio()
 * This is the 'setup' portion of physio, limited to dealing
 * with unstructured access to a single range of user space addresses.
 *
 * This is quite limited in functionality compared to physio().
 *
 * 1. allocate and return buf header
 * 2. lock down user pages and verify access protections
 *
 * Use B_READ (S_WRITE) for unstructured access. (We don't know the
 * direction of the transfer, so use the safest.)
 */

int
fc_physio_setup(struct buf **bpp, void *io_base, size_t io_len)
{
	struct proc *procp;
	struct as *asp;
	int error = 0;
	page_t **pplist;
	struct buf *bp = *bpp;

	bp = getrbuf(KM_SLEEP);
	bp->b_iodone = NULL;
	bp->b_resid = 0;
	*bpp = bp;

	/* segflg *is always* UIO_USERSPACE for us */
	procp = ttoproc(curthread);
	asp = procp->p_as;

	ASSERT(SEMA_HELD(&bp->b_sem));

	bp->b_error = 0;
	bp->b_proc = procp;

	bp->b_flags = B_BUSY | B_PHYS | B_READ;
	bp->b_edev = 0;
	bp->b_lblkno = 0;

	/*
	 * Don't count on b_addr remaining untouched by the
	 * code below (it may be reset because someone does
	 * a bp_mapin on the buffer).
	 */
	bp->b_un.b_addr = io_base;
	bp->b_bcount = io_len;

	error = as_pagelock(asp, &pplist, io_base, io_len, S_WRITE);

	if (error != 0) {
		bp->b_flags |= B_ERROR;
		bp->b_error = error;
		bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
		freerbuf(bp);
		*bpp = NULL;
		return (error);
	}

	bp->b_shadow = pplist;
	if (pplist != NULL) {
		bp->b_flags |= B_SHADOW;
	}
	return (0);
}

/*
 * unlock the pages and free the buf header, if we allocated it.
 */
void
fc_physio_free(struct buf **bpp, void *io_base, size_t io_len)
{
	struct buf *bp = *bpp;
	page_t **pplist = NULL;

	/*
	 * unlock the pages
	 */

	if (bp->b_flags & B_SHADOW)
		pplist = bp->b_shadow;

	as_pageunlock(bp->b_proc->p_as, pplist, io_base, io_len, S_WRITE);

	bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_SHADOW);

	freerbuf(bp);
	*bpp = NULL;
}