2 * Copyright (c) 1994 John S. Dyson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
16 * 4. Modifications may be freely made to this file if the above conditions
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.27 2008/08/22 08:47:56 swildner Exp $
23 #include <sys/param.h>
24 #include <sys/systm.h>
29 #include <sys/device.h>
30 #include <sys/thread2.h>
33 #include <vm/vm_extern.h>
36 physio(cdev_t dev
, struct uio
*uio
, int ioflag
)
48 saflags
= bp
->b_flags
;
51 /* XXX: sanity check */
52 if (dev
->si_iosize_max
< PAGE_SIZE
) {
53 kprintf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
54 devtoname(dev
), dev
->si_iosize_max
);
55 dev
->si_iosize_max
= DFLTPHYS
;
58 /* Must be a real uio */
59 KKASSERT(uio
->uio_segflg
!= UIO_NOCOPY
);
61 for (i
= 0; i
< uio
->uio_iovcnt
; i
++) {
62 while (uio
->uio_iov
[i
].iov_len
) {
63 if (uio
->uio_rw
== UIO_READ
)
64 bp
->b_cmd
= BUF_CMD_READ
;
66 bp
->b_cmd
= BUF_CMD_WRITE
;
67 bp
->b_flags
= saflags
;
68 bcount
= uio
->uio_iov
[i
].iov_len
;
70 reinitbufbio(bp
); /* clear translation cache */
71 bp
->b_bio1
.bio_offset
= uio
->uio_offset
;
72 bp
->b_bio1
.bio_done
= biodone_sync
;
73 bp
->b_bio1
.bio_flags
|= BIO_SYNC
;
76 * Setup for mapping the request into kernel memory.
78 * We can only write as much as fits in a pbuf,
79 * which is MAXPHYS, and no larger then the device's
82 * If not using bounce pages the base address of the
83 * user mapping into the pbuf may be offset, further
84 * reducing how much will actually fit in the pbuf.
86 if (bcount
> dev
->si_iosize_max
)
87 bcount
= dev
->si_iosize_max
;
89 ubase
= uio
->uio_iov
[i
].iov_base
;
90 bounceit
= (int)(((vm_offset_t
)ubase
) & 15);
91 iolen
= ((vm_offset_t
)ubase
) & PAGE_MASK
;
93 if (bcount
> bp
->b_kvasize
)
94 bcount
= bp
->b_kvasize
;
96 if ((bcount
+ iolen
) > bp
->b_kvasize
) {
97 bcount
= bp
->b_kvasize
;
104 * If we have to use a bounce buffer allocate kernel
105 * memory and copyin/copyout. Otherwise map the
106 * user buffer directly into kernel memory without
109 if (uio
->uio_segflg
== UIO_USERSPACE
) {
111 bp
->b_data
= bp
->b_kvabase
;
112 bp
->b_bcount
= bcount
;
113 vm_hold_load_pages(bp
, (vm_offset_t
)bp
->b_data
, (vm_offset_t
)bp
->b_data
+ bcount
);
114 if (uio
->uio_rw
== UIO_WRITE
) {
115 error
= copyin(ubase
, bp
->b_data
, bcount
);
117 vm_hold_free_pages(bp
, (vm_offset_t
)bp
->b_data
, (vm_offset_t
)bp
->b_data
+ bcount
);
121 } else if (vmapbuf(bp
, ubase
, bcount
) < 0) {
126 bp
->b_data
= uio
->uio_iov
[i
].iov_base
;
127 bp
->b_bcount
= bcount
;
129 dev_dstrategy(dev
, &bp
->b_bio1
);
130 biowait(&bp
->b_bio1
, "physstr");
132 iolen
= bp
->b_bcount
- bp
->b_resid
;
133 if (uio
->uio_segflg
== UIO_USERSPACE
) {
135 if (uio
->uio_rw
== UIO_READ
&& iolen
) {
136 error
= copyout(bp
->b_data
, ubase
, iolen
);
138 bp
->b_flags
|= B_ERROR
;
142 vm_hold_free_pages(bp
, (vm_offset_t
)bp
->b_data
, (vm_offset_t
)bp
->b_data
+ bcount
);
147 if (iolen
== 0 && !(bp
->b_flags
& B_ERROR
))
148 goto doerror
; /* EOF */
149 uio
->uio_iov
[i
].iov_len
-= iolen
;
150 uio
->uio_iov
[i
].iov_base
= (char *)uio
->uio_iov
[i
].iov_base
+ iolen
;
151 uio
->uio_resid
-= iolen
;
152 uio
->uio_offset
+= iolen
;
153 if (bp
->b_flags
& B_ERROR
) {
165 physread(struct dev_read_args
*ap
)
167 return(physio(ap
->a_head
.a_dev
, ap
->a_uio
, ap
->a_ioflag
));
171 physwrite(struct dev_write_args
*ap
)
173 return(physio(ap
->a_head
.a_dev
, ap
->a_uio
, ap
->a_ioflag
));