d8aed754e83f5e2cbf1e0e95a57e417508267b3e
[dragonfly.git] / sys / kern / kern_physio.c
1 /*
2  * Copyright (c) 1994 John S. Dyson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice immediately at the beginning of the file, without modification,
10  *    this list of conditions, and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Absolutely no warranty of function or purpose is made by the author
15  *    John S. Dyson.
16  * 4. Modifications may be freely made to this file if the above conditions
17  *    are met.
18  *
19  * $FreeBSD: src/sys/kern/kern_physio.c,v 1.46.2.4 2003/11/14 09:51:47 simokawa Exp $
20  * $DragonFly: src/sys/kern/kern_physio.c,v 1.25 2007/08/21 17:26:45 dillon Exp $
21  */
22
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/buf.h>
26 #include <sys/conf.h>
27 #include <sys/proc.h>
28 #include <sys/uio.h>
29 #include <sys/device.h>
30 #include <sys/thread2.h>
31
32 #include <vm/vm.h>
33 #include <vm/vm_extern.h>
34
35 static void
36 physwakeup(struct bio *bio)
37 {
38         bio->bio_buf->b_cmd = BUF_CMD_DONE;
39         wakeup(bio);
40 }
41
42 static int
43 physio(cdev_t dev, struct uio *uio, int ioflag)
44 {
45         int i;
46         int error;
47         int chk_blockno;
48         int saflags;
49         int iolen;
50         int bcount;
51         struct buf *bp;
52
53         bp = getpbuf(NULL);
54         saflags = bp->b_flags;
55         error = 0;
56
57         /* XXX: sanity check */
58         if(dev->si_iosize_max < PAGE_SIZE) {
59                 kprintf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
60                     devtoname(dev), dev->si_iosize_max);
61                 dev->si_iosize_max = DFLTPHYS;
62         }
63
64         /* Must be a real uio */
65         KKASSERT(uio->uio_segflg != UIO_NOCOPY);
66
67         /* Don't check block number overflow for D_MEM */
68         if ((dev_dflags(dev) & D_TYPEMASK) == D_MEM)
69                 chk_blockno = 0;
70         else
71                 chk_blockno = 1;
72
73         for (i = 0; i < uio->uio_iovcnt; i++) {
74                 while (uio->uio_iov[i].iov_len) {
75                         if (uio->uio_rw == UIO_READ)
76                                 bp->b_cmd = BUF_CMD_READ;
77                         else 
78                                 bp->b_cmd = BUF_CMD_WRITE;
79                         bp->b_flags = saflags;
80                         bcount = uio->uio_iov[i].iov_len;
81
82                         reinitbufbio(bp);       /* clear translation cache */
83                         bp->b_bio1.bio_offset = uio->uio_offset;
84                         bp->b_bio1.bio_done = physwakeup;
85
86                         /* Don't exceed drivers iosize limit */
87                         if (bcount > dev->si_iosize_max)
88                                 bcount = dev->si_iosize_max;
89
90                         /* 
91                          * Make sure the pbuf can map the request
92                          * XXX: The pbuf has kvasize = MAXPHYS so a request
93                          * XXX: larger than MAXPHYS - PAGE_SIZE must be
94                          * XXX: page aligned or it will be fragmented.
95                          */
96                         iolen = ((vm_offset_t) uio->uio_iov[i].iov_base) &
97                                 PAGE_MASK;
98                         if ((bcount + iolen) > bp->b_kvasize) {
99                                 bcount = bp->b_kvasize;
100                                 if (iolen != 0)
101                                         bcount -= PAGE_SIZE;
102                         }
103                         if (uio->uio_segflg == UIO_USERSPACE) {
104                                 if (vmapbuf(bp, uio->uio_iov[i].iov_base, bcount) < 0) {
105                                         error = EFAULT;
106                                         goto doerror;
107                                 }
108                         } else {
109                                 bp->b_data = uio->uio_iov[i].iov_base;
110                                 bp->b_bcount = bcount;
111                         }
112                         dev_dstrategy(dev, &bp->b_bio1);
113                         crit_enter();
114                         while (bp->b_cmd != BUF_CMD_DONE)
115                                 tsleep(&bp->b_bio1, 0, "physstr", 0);
116                         crit_exit();
117
118                         if (uio->uio_segflg == UIO_USERSPACE)
119                                 vunmapbuf(bp);
120                         iolen = bp->b_bcount - bp->b_resid;
121                         if (iolen == 0 && !(bp->b_flags & B_ERROR))
122                                 goto doerror;   /* EOF */
123                         uio->uio_iov[i].iov_len -= iolen;
124                         uio->uio_iov[i].iov_base += iolen;
125                         uio->uio_resid -= iolen;
126                         uio->uio_offset += iolen;
127                         if( bp->b_flags & B_ERROR) {
128                                 error = bp->b_error;
129                                 goto doerror;
130                         }
131                 }
132         }
133 doerror:
134         relpbuf(bp, NULL);
135         return (error);
136 }
137
138 int
139 physread(struct dev_read_args *ap)
140 {
141         return(physio(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
142 }
143
144 int
145 physwrite(struct dev_write_args *ap)
146 {
147         return(physio(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
148 }
149