Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 2 | * A generic kernel FIFO implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 4 | * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Stefani Seibold | d78a3ed | 2010-08-11 14:17:27 -0700 | [diff] [blame] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kernel.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 23 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/err.h> |
vignesh babu | f84d5a7 | 2007-07-15 23:41:34 -0700 | [diff] [blame] | 26 | #include <linux/log2.h> |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 28 | #include <linux/kfifo.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 30 | /* |
| 31 | * internal helper to calculate the unused elements in a fifo |
| 32 | */ |
| 33 | static inline unsigned int kfifo_unused(struct __kfifo *fifo) |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 34 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 35 | return (fifo->mask + 1) - (fifo->in - fifo->out); |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 38 | int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, |
| 39 | size_t esize, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* |
Martin Kelly | 75a24b8 | 2018-03-26 11:18:23 -0700 | [diff] [blame] | 42 | * round up to the next power of 2, since our 'let the indices |
Robert P. J. Day | b33112d | 2009-06-16 15:33:34 -0700 | [diff] [blame] | 43 | * wrap' technique works only in this case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | */ |
Stefani Seibold | dfe2a77 | 2013-02-27 17:05:51 -0800 | [diff] [blame] | 45 | size = roundup_pow_of_two(size); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 46 | |
| 47 | fifo->in = 0; |
| 48 | fifo->out = 0; |
| 49 | fifo->esize = esize; |
| 50 | |
| 51 | if (size < 2) { |
| 52 | fifo->data = NULL; |
| 53 | fifo->mask = 0; |
| 54 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 57 | fifo->data = kmalloc_array(esize, size, gfp_mask); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 58 | |
| 59 | if (!fifo->data) { |
| 60 | fifo->mask = 0; |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 61 | return -ENOMEM; |
| 62 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 63 | fifo->mask = size - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 65 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 67 | EXPORT_SYMBOL(__kfifo_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 69 | void __kfifo_free(struct __kfifo *fifo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 71 | kfree(fifo->data); |
| 72 | fifo->in = 0; |
| 73 | fifo->out = 0; |
| 74 | fifo->esize = 0; |
| 75 | fifo->data = NULL; |
| 76 | fifo->mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 78 | EXPORT_SYMBOL(__kfifo_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 80 | int __kfifo_init(struct __kfifo *fifo, void *buffer, |
| 81 | unsigned int size, size_t esize) |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 82 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 83 | size /= esize; |
| 84 | |
Stefani Seibold | dfe2a77 | 2013-02-27 17:05:51 -0800 | [diff] [blame] | 85 | size = roundup_pow_of_two(size); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 86 | |
| 87 | fifo->in = 0; |
| 88 | fifo->out = 0; |
| 89 | fifo->esize = esize; |
| 90 | fifo->data = buffer; |
| 91 | |
| 92 | if (size < 2) { |
| 93 | fifo->mask = 0; |
| 94 | return -EINVAL; |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 95 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 96 | fifo->mask = size - 1; |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 97 | |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame] | 98 | return 0; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 99 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 100 | EXPORT_SYMBOL(__kfifo_init); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 101 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 102 | static void kfifo_copy_in(struct __kfifo *fifo, const void *src, |
| 103 | unsigned int len, unsigned int off) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 104 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 105 | unsigned int size = fifo->mask + 1; |
| 106 | unsigned int esize = fifo->esize; |
| 107 | unsigned int l; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 108 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 109 | off &= fifo->mask; |
| 110 | if (esize != 1) { |
| 111 | off *= esize; |
| 112 | size *= esize; |
| 113 | len *= esize; |
| 114 | } |
| 115 | l = min(len, size - off); |
| 116 | |
| 117 | memcpy(fifo->data + off, src, l); |
| 118 | memcpy(fifo->data, src + l, len - l); |
| 119 | /* |
| 120 | * make sure that the data in the fifo is up to date before |
| 121 | * incrementing the fifo->in index counter |
| 122 | */ |
| 123 | smp_wmb(); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 124 | } |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 125 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 126 | unsigned int __kfifo_in(struct __kfifo *fifo, |
| 127 | const void *buf, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 129 | unsigned int l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 131 | l = kfifo_unused(fifo); |
| 132 | if (len > l) |
| 133 | len = l; |
| 134 | |
| 135 | kfifo_copy_in(fifo, buf, len, fifo->in); |
| 136 | fifo->in += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return len; |
| 138 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 139 | EXPORT_SYMBOL(__kfifo_in); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 141 | static void kfifo_copy_out(struct __kfifo *fifo, void *dst, |
| 142 | unsigned int len, unsigned int off) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 143 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 144 | unsigned int size = fifo->mask + 1; |
| 145 | unsigned int esize = fifo->esize; |
| 146 | unsigned int l; |
| 147 | |
| 148 | off &= fifo->mask; |
| 149 | if (esize != 1) { |
| 150 | off *= esize; |
| 151 | size *= esize; |
| 152 | len *= esize; |
| 153 | } |
| 154 | l = min(len, size - off); |
| 155 | |
| 156 | memcpy(dst, fifo->data + off, l); |
| 157 | memcpy(dst + l, fifo->data, len - l); |
| 158 | /* |
| 159 | * make sure that the data is copied before |
| 160 | * incrementing the fifo->out index counter |
| 161 | */ |
| 162 | smp_wmb(); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 163 | } |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 164 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 165 | unsigned int __kfifo_out_peek(struct __kfifo *fifo, |
| 166 | void *buf, unsigned int len) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 167 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 168 | unsigned int l; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 169 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 170 | l = fifo->in - fifo->out; |
| 171 | if (len > l) |
| 172 | len = l; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 173 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 174 | kfifo_copy_out(fifo, buf, len, fifo->out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return len; |
| 176 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 177 | EXPORT_SYMBOL(__kfifo_out_peek); |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 178 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 179 | unsigned int __kfifo_out(struct __kfifo *fifo, |
| 180 | void *buf, unsigned int len) |
Andi Kleen | a5b9e2c | 2010-01-15 17:01:16 -0800 | [diff] [blame] | 181 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 182 | len = __kfifo_out_peek(fifo, buf, len); |
| 183 | fifo->out += len; |
Andi Kleen | a5b9e2c | 2010-01-15 17:01:16 -0800 | [diff] [blame] | 184 | return len; |
| 185 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 186 | EXPORT_SYMBOL(__kfifo_out); |
Andi Kleen | a5b9e2c | 2010-01-15 17:01:16 -0800 | [diff] [blame] | 187 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 188 | static unsigned long kfifo_copy_from_user(struct __kfifo *fifo, |
| 189 | const void __user *from, unsigned int len, unsigned int off, |
| 190 | unsigned int *copied) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 191 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 192 | unsigned int size = fifo->mask + 1; |
| 193 | unsigned int esize = fifo->esize; |
| 194 | unsigned int l; |
| 195 | unsigned long ret; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 196 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 197 | off &= fifo->mask; |
| 198 | if (esize != 1) { |
| 199 | off *= esize; |
| 200 | size *= esize; |
| 201 | len *= esize; |
| 202 | } |
| 203 | l = min(len, size - off); |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame] | 204 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 205 | ret = copy_from_user(fifo->data + off, from, l); |
| 206 | if (unlikely(ret)) |
| 207 | ret = DIV_ROUND_UP(ret + len - l, esize); |
| 208 | else { |
| 209 | ret = copy_from_user(fifo->data, from + l, len - l); |
| 210 | if (unlikely(ret)) |
| 211 | ret = DIV_ROUND_UP(ret, esize); |
| 212 | } |
| 213 | /* |
| 214 | * make sure that the data in the fifo is up to date before |
| 215 | * incrementing the fifo->in index counter |
| 216 | */ |
| 217 | smp_wmb(); |
Lars-Peter Clausen | a019e48 | 2013-11-14 14:32:16 -0800 | [diff] [blame] | 218 | *copied = len - ret * esize; |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 219 | /* return the number of elements which are not copied */ |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame] | 220 | return ret; |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 221 | } |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 222 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 223 | int __kfifo_from_user(struct __kfifo *fifo, const void __user *from, |
| 224 | unsigned long len, unsigned int *copied) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 225 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 226 | unsigned int l; |
| 227 | unsigned long ret; |
| 228 | unsigned int esize = fifo->esize; |
| 229 | int err; |
| 230 | |
| 231 | if (esize != 1) |
| 232 | len /= esize; |
| 233 | |
| 234 | l = kfifo_unused(fifo); |
| 235 | if (len > l) |
| 236 | len = l; |
| 237 | |
| 238 | ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied); |
| 239 | if (unlikely(ret)) { |
| 240 | len -= ret; |
| 241 | err = -EFAULT; |
| 242 | } else |
| 243 | err = 0; |
| 244 | fifo->in += len; |
| 245 | return err; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 246 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 247 | EXPORT_SYMBOL(__kfifo_from_user); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 248 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 249 | static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to, |
| 250 | unsigned int len, unsigned int off, unsigned int *copied) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 251 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 252 | unsigned int l; |
| 253 | unsigned long ret; |
| 254 | unsigned int size = fifo->mask + 1; |
| 255 | unsigned int esize = fifo->esize; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 256 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 257 | off &= fifo->mask; |
| 258 | if (esize != 1) { |
| 259 | off *= esize; |
| 260 | size *= esize; |
| 261 | len *= esize; |
| 262 | } |
| 263 | l = min(len, size - off); |
| 264 | |
| 265 | ret = copy_to_user(to, fifo->data + off, l); |
| 266 | if (unlikely(ret)) |
| 267 | ret = DIV_ROUND_UP(ret + len - l, esize); |
| 268 | else { |
| 269 | ret = copy_to_user(to + l, fifo->data, len - l); |
| 270 | if (unlikely(ret)) |
| 271 | ret = DIV_ROUND_UP(ret, esize); |
| 272 | } |
| 273 | /* |
| 274 | * make sure that the data is copied before |
| 275 | * incrementing the fifo->out index counter |
| 276 | */ |
| 277 | smp_wmb(); |
Lars-Peter Clausen | a019e48 | 2013-11-14 14:32:16 -0800 | [diff] [blame] | 278 | *copied = len - ret * esize; |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 279 | /* return the number of elements which are not copied */ |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | int __kfifo_to_user(struct __kfifo *fifo, void __user *to, |
| 284 | unsigned long len, unsigned int *copied) |
| 285 | { |
| 286 | unsigned int l; |
| 287 | unsigned long ret; |
| 288 | unsigned int esize = fifo->esize; |
| 289 | int err; |
| 290 | |
| 291 | if (esize != 1) |
| 292 | len /= esize; |
| 293 | |
| 294 | l = fifo->in - fifo->out; |
| 295 | if (len > l) |
| 296 | len = l; |
| 297 | ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied); |
| 298 | if (unlikely(ret)) { |
| 299 | len -= ret; |
| 300 | err = -EFAULT; |
| 301 | } else |
| 302 | err = 0; |
| 303 | fifo->out += len; |
| 304 | return err; |
| 305 | } |
| 306 | EXPORT_SYMBOL(__kfifo_to_user); |
| 307 | |
| 308 | static int setup_sgl_buf(struct scatterlist *sgl, void *buf, |
| 309 | int nents, unsigned int len) |
| 310 | { |
| 311 | int n; |
| 312 | unsigned int l; |
| 313 | unsigned int off; |
| 314 | struct page *page; |
| 315 | |
| 316 | if (!nents) |
| 317 | return 0; |
| 318 | |
| 319 | if (!len) |
| 320 | return 0; |
| 321 | |
| 322 | n = 0; |
| 323 | page = virt_to_page(buf); |
| 324 | off = offset_in_page(buf); |
| 325 | l = 0; |
| 326 | |
| 327 | while (len >= l + PAGE_SIZE - off) { |
| 328 | struct page *npage; |
| 329 | |
| 330 | l += PAGE_SIZE; |
| 331 | buf += PAGE_SIZE; |
| 332 | npage = virt_to_page(buf); |
| 333 | if (page_to_phys(page) != page_to_phys(npage) - l) { |
Stefani Seibold | d78a3ed | 2010-08-11 14:17:27 -0700 | [diff] [blame] | 334 | sg_set_page(sgl, page, l - off, off); |
| 335 | sgl = sg_next(sgl); |
| 336 | if (++n == nents || sgl == NULL) |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 337 | return n; |
| 338 | page = npage; |
| 339 | len -= l - off; |
| 340 | l = off = 0; |
| 341 | } |
| 342 | } |
Stefani Seibold | d78a3ed | 2010-08-11 14:17:27 -0700 | [diff] [blame] | 343 | sg_set_page(sgl, page, len, off); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 344 | return n + 1; |
| 345 | } |
| 346 | |
| 347 | static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl, |
| 348 | int nents, unsigned int len, unsigned int off) |
| 349 | { |
| 350 | unsigned int size = fifo->mask + 1; |
| 351 | unsigned int esize = fifo->esize; |
| 352 | unsigned int l; |
| 353 | unsigned int n; |
| 354 | |
| 355 | off &= fifo->mask; |
| 356 | if (esize != 1) { |
| 357 | off *= esize; |
| 358 | size *= esize; |
| 359 | len *= esize; |
| 360 | } |
| 361 | l = min(len, size - off); |
| 362 | |
Stefani Seibold | d78a3ed | 2010-08-11 14:17:27 -0700 | [diff] [blame] | 363 | n = setup_sgl_buf(sgl, fifo->data + off, nents, l); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 364 | n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l); |
| 365 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 366 | return n; |
| 367 | } |
| 368 | |
| 369 | unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo, |
| 370 | struct scatterlist *sgl, int nents, unsigned int len) |
| 371 | { |
| 372 | unsigned int l; |
| 373 | |
| 374 | l = kfifo_unused(fifo); |
| 375 | if (len > l) |
| 376 | len = l; |
| 377 | |
| 378 | return setup_sgl(fifo, sgl, nents, len, fifo->in); |
| 379 | } |
| 380 | EXPORT_SYMBOL(__kfifo_dma_in_prepare); |
| 381 | |
| 382 | unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo, |
| 383 | struct scatterlist *sgl, int nents, unsigned int len) |
| 384 | { |
| 385 | unsigned int l; |
| 386 | |
| 387 | l = fifo->in - fifo->out; |
| 388 | if (len > l) |
| 389 | len = l; |
| 390 | |
| 391 | return setup_sgl(fifo, sgl, nents, len, fifo->out); |
| 392 | } |
| 393 | EXPORT_SYMBOL(__kfifo_dma_out_prepare); |
| 394 | |
| 395 | unsigned int __kfifo_max_r(unsigned int len, size_t recsize) |
| 396 | { |
| 397 | unsigned int max = (1 << (recsize << 3)) - 1; |
| 398 | |
| 399 | if (len > max) |
| 400 | return max; |
| 401 | return len; |
| 402 | } |
Srinivas Kandagatla | 30059d9 | 2012-04-11 05:01:56 -0300 | [diff] [blame] | 403 | EXPORT_SYMBOL(__kfifo_max_r); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 404 | |
| 405 | #define __KFIFO_PEEK(data, out, mask) \ |
| 406 | ((data)[(out) & (mask)]) |
| 407 | /* |
| 408 | * __kfifo_peek_n internal helper function for determinate the length of |
| 409 | * the next record in the fifo |
| 410 | */ |
| 411 | static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize) |
| 412 | { |
| 413 | unsigned int l; |
| 414 | unsigned int mask = fifo->mask; |
| 415 | unsigned char *data = fifo->data; |
| 416 | |
| 417 | l = __KFIFO_PEEK(data, fifo->out, mask); |
| 418 | |
| 419 | if (--recsize) |
| 420 | l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8; |
| 421 | |
| 422 | return l; |
| 423 | } |
| 424 | |
| 425 | #define __KFIFO_POKE(data, in, mask, val) \ |
| 426 | ( \ |
| 427 | (data)[(in) & (mask)] = (unsigned char)(val) \ |
| 428 | ) |
| 429 | |
| 430 | /* |
| 431 | * __kfifo_poke_n internal helper function for storeing the length of |
| 432 | * the record into the fifo |
| 433 | */ |
| 434 | static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize) |
| 435 | { |
| 436 | unsigned int mask = fifo->mask; |
| 437 | unsigned char *data = fifo->data; |
| 438 | |
| 439 | __KFIFO_POKE(data, fifo->in, mask, n); |
| 440 | |
| 441 | if (recsize > 1) |
| 442 | __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8); |
| 443 | } |
| 444 | |
| 445 | unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize) |
| 446 | { |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 447 | return __kfifo_peek_n(fifo, recsize); |
| 448 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 449 | EXPORT_SYMBOL(__kfifo_len_r); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 450 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 451 | unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf, |
| 452 | unsigned int len, size_t recsize) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 453 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 454 | if (len + recsize > kfifo_unused(fifo)) |
| 455 | return 0; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 456 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 457 | __kfifo_poke_n(fifo, len, recsize); |
| 458 | |
| 459 | kfifo_copy_in(fifo, buf, len, fifo->in + recsize); |
| 460 | fifo->in += len + recsize; |
| 461 | return len; |
| 462 | } |
| 463 | EXPORT_SYMBOL(__kfifo_in_r); |
| 464 | |
| 465 | static unsigned int kfifo_out_copy_r(struct __kfifo *fifo, |
| 466 | void *buf, unsigned int len, size_t recsize, unsigned int *n) |
| 467 | { |
| 468 | *n = __kfifo_peek_n(fifo, recsize); |
| 469 | |
| 470 | if (len > *n) |
| 471 | len = *n; |
| 472 | |
| 473 | kfifo_copy_out(fifo, buf, len, fifo->out + recsize); |
| 474 | return len; |
| 475 | } |
| 476 | |
| 477 | unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf, |
| 478 | unsigned int len, size_t recsize) |
| 479 | { |
| 480 | unsigned int n; |
| 481 | |
| 482 | if (fifo->in == fifo->out) |
| 483 | return 0; |
| 484 | |
| 485 | return kfifo_out_copy_r(fifo, buf, len, recsize, &n); |
| 486 | } |
| 487 | EXPORT_SYMBOL(__kfifo_out_peek_r); |
| 488 | |
| 489 | unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf, |
| 490 | unsigned int len, size_t recsize) |
| 491 | { |
| 492 | unsigned int n; |
| 493 | |
| 494 | if (fifo->in == fifo->out) |
| 495 | return 0; |
| 496 | |
| 497 | len = kfifo_out_copy_r(fifo, buf, len, recsize, &n); |
| 498 | fifo->out += n + recsize; |
| 499 | return len; |
| 500 | } |
| 501 | EXPORT_SYMBOL(__kfifo_out_r); |
| 502 | |
Andrea Righi | b35de43 | 2010-08-19 14:13:27 -0700 | [diff] [blame] | 503 | void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize) |
| 504 | { |
| 505 | unsigned int n; |
| 506 | |
| 507 | n = __kfifo_peek_n(fifo, recsize); |
| 508 | fifo->out += n + recsize; |
| 509 | } |
| 510 | EXPORT_SYMBOL(__kfifo_skip_r); |
| 511 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 512 | int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from, |
| 513 | unsigned long len, unsigned int *copied, size_t recsize) |
| 514 | { |
| 515 | unsigned long ret; |
| 516 | |
| 517 | len = __kfifo_max_r(len, recsize); |
| 518 | |
| 519 | if (len + recsize > kfifo_unused(fifo)) { |
| 520 | *copied = 0; |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | __kfifo_poke_n(fifo, len, recsize); |
| 525 | |
| 526 | ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied); |
| 527 | if (unlikely(ret)) { |
| 528 | *copied = 0; |
| 529 | return -EFAULT; |
| 530 | } |
| 531 | fifo->in += len + recsize; |
| 532 | return 0; |
| 533 | } |
| 534 | EXPORT_SYMBOL(__kfifo_from_user_r); |
| 535 | |
| 536 | int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to, |
| 537 | unsigned long len, unsigned int *copied, size_t recsize) |
| 538 | { |
| 539 | unsigned long ret; |
| 540 | unsigned int n; |
| 541 | |
| 542 | if (fifo->in == fifo->out) { |
| 543 | *copied = 0; |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | n = __kfifo_peek_n(fifo, recsize); |
| 548 | if (len > n) |
| 549 | len = n; |
| 550 | |
| 551 | ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied); |
| 552 | if (unlikely(ret)) { |
| 553 | *copied = 0; |
| 554 | return -EFAULT; |
| 555 | } |
| 556 | fifo->out += n + recsize; |
| 557 | return 0; |
| 558 | } |
| 559 | EXPORT_SYMBOL(__kfifo_to_user_r); |
| 560 | |
| 561 | unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo, |
| 562 | struct scatterlist *sgl, int nents, unsigned int len, size_t recsize) |
| 563 | { |
Himangi Saraogi | 89b3ac6 | 2014-08-08 14:22:44 -0700 | [diff] [blame] | 564 | BUG_ON(!nents); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 565 | |
| 566 | len = __kfifo_max_r(len, recsize); |
| 567 | |
| 568 | if (len + recsize > kfifo_unused(fifo)) |
| 569 | return 0; |
| 570 | |
| 571 | return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize); |
| 572 | } |
| 573 | EXPORT_SYMBOL(__kfifo_dma_in_prepare_r); |
| 574 | |
| 575 | void __kfifo_dma_in_finish_r(struct __kfifo *fifo, |
| 576 | unsigned int len, size_t recsize) |
| 577 | { |
| 578 | len = __kfifo_max_r(len, recsize); |
| 579 | __kfifo_poke_n(fifo, len, recsize); |
| 580 | fifo->in += len + recsize; |
| 581 | } |
| 582 | EXPORT_SYMBOL(__kfifo_dma_in_finish_r); |
| 583 | |
| 584 | unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo, |
| 585 | struct scatterlist *sgl, int nents, unsigned int len, size_t recsize) |
| 586 | { |
Himangi Saraogi | 89b3ac6 | 2014-08-08 14:22:44 -0700 | [diff] [blame] | 587 | BUG_ON(!nents); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 588 | |
| 589 | len = __kfifo_max_r(len, recsize); |
| 590 | |
| 591 | if (len + recsize > fifo->in - fifo->out) |
| 592 | return 0; |
| 593 | |
| 594 | return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize); |
| 595 | } |
| 596 | EXPORT_SYMBOL(__kfifo_dma_out_prepare_r); |
| 597 | |
| 598 | void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize) |
| 599 | { |
| 600 | unsigned int len; |
| 601 | |
| 602 | len = __kfifo_peek_n(fifo, recsize); |
| 603 | fifo->out += len + recsize; |
| 604 | } |
| 605 | EXPORT_SYMBOL(__kfifo_dma_out_finish_r); |