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 | /* |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 42 | * round down 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 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 57 | fifo->data = kmalloc(size * esize, gfp_mask); |
| 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 | |
Linus Torvalds | 1d06b24 | 2019-08-30 18:47:15 -0700 | [diff] [blame] | 85 | if (!is_power_of_2(size)) |
| 86 | size = rounddown_pow_of_two(size); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 87 | |
| 88 | fifo->in = 0; |
| 89 | fifo->out = 0; |
| 90 | fifo->esize = esize; |
| 91 | fifo->data = buffer; |
| 92 | |
| 93 | if (size < 2) { |
| 94 | fifo->mask = 0; |
| 95 | return -EINVAL; |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 96 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 97 | fifo->mask = size - 1; |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 98 | |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame] | 99 | return 0; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 100 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 101 | EXPORT_SYMBOL(__kfifo_init); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 102 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 103 | static void kfifo_copy_in(struct __kfifo *fifo, const void *src, |
| 104 | unsigned int len, unsigned int off) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 105 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 106 | unsigned int size = fifo->mask + 1; |
| 107 | unsigned int esize = fifo->esize; |
| 108 | unsigned int l; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 109 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 110 | off &= fifo->mask; |
| 111 | if (esize != 1) { |
| 112 | off *= esize; |
| 113 | size *= esize; |
| 114 | len *= esize; |
| 115 | } |
| 116 | l = min(len, size - off); |
| 117 | |
| 118 | memcpy(fifo->data + off, src, l); |
| 119 | memcpy(fifo->data, src + l, len - l); |
| 120 | /* |
| 121 | * make sure that the data in the fifo is up to date before |
| 122 | * incrementing the fifo->in index counter |
| 123 | */ |
| 124 | smp_wmb(); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 125 | } |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 126 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 127 | unsigned int __kfifo_in(struct __kfifo *fifo, |
| 128 | const void *buf, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 130 | unsigned int l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 132 | l = kfifo_unused(fifo); |
| 133 | if (len > l) |
| 134 | len = l; |
| 135 | |
| 136 | kfifo_copy_in(fifo, buf, len, fifo->in); |
| 137 | fifo->in += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return len; |
| 139 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 140 | EXPORT_SYMBOL(__kfifo_in); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 142 | static void kfifo_copy_out(struct __kfifo *fifo, void *dst, |
| 143 | unsigned int len, unsigned int off) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 144 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 145 | unsigned int size = fifo->mask + 1; |
| 146 | unsigned int esize = fifo->esize; |
| 147 | unsigned int l; |
| 148 | |
| 149 | off &= fifo->mask; |
| 150 | if (esize != 1) { |
| 151 | off *= esize; |
| 152 | size *= esize; |
| 153 | len *= esize; |
| 154 | } |
| 155 | l = min(len, size - off); |
| 156 | |
| 157 | memcpy(dst, fifo->data + off, l); |
| 158 | memcpy(dst + l, fifo->data, len - l); |
| 159 | /* |
| 160 | * make sure that the data is copied before |
| 161 | * incrementing the fifo->out index counter |
| 162 | */ |
| 163 | smp_wmb(); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 164 | } |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 165 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 166 | unsigned int __kfifo_out_peek(struct __kfifo *fifo, |
| 167 | void *buf, unsigned int len) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 168 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 169 | unsigned int l; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 170 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 171 | l = fifo->in - fifo->out; |
| 172 | if (len > l) |
| 173 | len = l; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 174 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 175 | kfifo_copy_out(fifo, buf, len, fifo->out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return len; |
| 177 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 178 | EXPORT_SYMBOL(__kfifo_out_peek); |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 179 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 180 | unsigned int __kfifo_out(struct __kfifo *fifo, |
| 181 | void *buf, unsigned int len) |
Andi Kleen | a5b9e2c | 2010-01-15 17:01:16 -0800 | [diff] [blame] | 182 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 183 | len = __kfifo_out_peek(fifo, buf, len); |
| 184 | fifo->out += len; |
Andi Kleen | a5b9e2c | 2010-01-15 17:01:16 -0800 | [diff] [blame] | 185 | return len; |
| 186 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 187 | EXPORT_SYMBOL(__kfifo_out); |
Andi Kleen | a5b9e2c | 2010-01-15 17:01:16 -0800 | [diff] [blame] | 188 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 189 | static unsigned long kfifo_copy_from_user(struct __kfifo *fifo, |
| 190 | const void __user *from, unsigned int len, unsigned int off, |
| 191 | unsigned int *copied) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 192 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 193 | unsigned int size = fifo->mask + 1; |
| 194 | unsigned int esize = fifo->esize; |
| 195 | unsigned int l; |
| 196 | unsigned long ret; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 197 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 198 | off &= fifo->mask; |
| 199 | if (esize != 1) { |
| 200 | off *= esize; |
| 201 | size *= esize; |
| 202 | len *= esize; |
| 203 | } |
| 204 | l = min(len, size - off); |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame] | 205 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 206 | ret = copy_from_user(fifo->data + off, from, l); |
| 207 | if (unlikely(ret)) |
| 208 | ret = DIV_ROUND_UP(ret + len - l, esize); |
| 209 | else { |
| 210 | ret = copy_from_user(fifo->data, from + l, len - l); |
| 211 | if (unlikely(ret)) |
| 212 | ret = DIV_ROUND_UP(ret, esize); |
| 213 | } |
| 214 | /* |
| 215 | * make sure that the data in the fifo is up to date before |
| 216 | * incrementing the fifo->in index counter |
| 217 | */ |
| 218 | smp_wmb(); |
Lars-Peter Clausen | a019e48 | 2013-11-14 14:32:16 -0800 | [diff] [blame] | 219 | *copied = len - ret * esize; |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 220 | /* return the number of elements which are not copied */ |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame] | 221 | return ret; |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 222 | } |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 223 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 224 | int __kfifo_from_user(struct __kfifo *fifo, const void __user *from, |
| 225 | unsigned long len, unsigned int *copied) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 226 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 227 | unsigned int l; |
| 228 | unsigned long ret; |
| 229 | unsigned int esize = fifo->esize; |
| 230 | int err; |
| 231 | |
| 232 | if (esize != 1) |
| 233 | len /= esize; |
| 234 | |
| 235 | l = kfifo_unused(fifo); |
| 236 | if (len > l) |
| 237 | len = l; |
| 238 | |
| 239 | ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied); |
| 240 | if (unlikely(ret)) { |
| 241 | len -= ret; |
| 242 | err = -EFAULT; |
| 243 | } else |
| 244 | err = 0; |
| 245 | fifo->in += len; |
| 246 | return err; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 247 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 248 | EXPORT_SYMBOL(__kfifo_from_user); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 249 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 250 | static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to, |
| 251 | unsigned int len, unsigned int off, unsigned int *copied) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 252 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 253 | unsigned int l; |
| 254 | unsigned long ret; |
| 255 | unsigned int size = fifo->mask + 1; |
| 256 | unsigned int esize = fifo->esize; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 257 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 258 | off &= fifo->mask; |
| 259 | if (esize != 1) { |
| 260 | off *= esize; |
| 261 | size *= esize; |
| 262 | len *= esize; |
| 263 | } |
| 264 | l = min(len, size - off); |
| 265 | |
| 266 | ret = copy_to_user(to, fifo->data + off, l); |
| 267 | if (unlikely(ret)) |
| 268 | ret = DIV_ROUND_UP(ret + len - l, esize); |
| 269 | else { |
| 270 | ret = copy_to_user(to + l, fifo->data, len - l); |
| 271 | if (unlikely(ret)) |
| 272 | ret = DIV_ROUND_UP(ret, esize); |
| 273 | } |
| 274 | /* |
| 275 | * make sure that the data is copied before |
| 276 | * incrementing the fifo->out index counter |
| 277 | */ |
| 278 | smp_wmb(); |
Lars-Peter Clausen | a019e48 | 2013-11-14 14:32:16 -0800 | [diff] [blame] | 279 | *copied = len - ret * esize; |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 280 | /* return the number of elements which are not copied */ |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | int __kfifo_to_user(struct __kfifo *fifo, void __user *to, |
| 285 | unsigned long len, unsigned int *copied) |
| 286 | { |
| 287 | unsigned int l; |
| 288 | unsigned long ret; |
| 289 | unsigned int esize = fifo->esize; |
| 290 | int err; |
| 291 | |
| 292 | if (esize != 1) |
| 293 | len /= esize; |
| 294 | |
| 295 | l = fifo->in - fifo->out; |
| 296 | if (len > l) |
| 297 | len = l; |
| 298 | ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied); |
| 299 | if (unlikely(ret)) { |
| 300 | len -= ret; |
| 301 | err = -EFAULT; |
| 302 | } else |
| 303 | err = 0; |
| 304 | fifo->out += len; |
| 305 | return err; |
| 306 | } |
| 307 | EXPORT_SYMBOL(__kfifo_to_user); |
| 308 | |
| 309 | static int setup_sgl_buf(struct scatterlist *sgl, void *buf, |
| 310 | int nents, unsigned int len) |
| 311 | { |
| 312 | int n; |
| 313 | unsigned int l; |
| 314 | unsigned int off; |
| 315 | struct page *page; |
| 316 | |
| 317 | if (!nents) |
| 318 | return 0; |
| 319 | |
| 320 | if (!len) |
| 321 | return 0; |
| 322 | |
| 323 | n = 0; |
| 324 | page = virt_to_page(buf); |
| 325 | off = offset_in_page(buf); |
| 326 | l = 0; |
| 327 | |
| 328 | while (len >= l + PAGE_SIZE - off) { |
| 329 | struct page *npage; |
| 330 | |
| 331 | l += PAGE_SIZE; |
| 332 | buf += PAGE_SIZE; |
| 333 | npage = virt_to_page(buf); |
| 334 | if (page_to_phys(page) != page_to_phys(npage) - l) { |
Stefani Seibold | d78a3ed | 2010-08-11 14:17:27 -0700 | [diff] [blame] | 335 | sg_set_page(sgl, page, l - off, off); |
| 336 | sgl = sg_next(sgl); |
| 337 | if (++n == nents || sgl == NULL) |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 338 | return n; |
| 339 | page = npage; |
| 340 | len -= l - off; |
| 341 | l = off = 0; |
| 342 | } |
| 343 | } |
Stefani Seibold | d78a3ed | 2010-08-11 14:17:27 -0700 | [diff] [blame] | 344 | sg_set_page(sgl, page, len, off); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 345 | return n + 1; |
| 346 | } |
| 347 | |
| 348 | static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl, |
| 349 | int nents, unsigned int len, unsigned int off) |
| 350 | { |
| 351 | unsigned int size = fifo->mask + 1; |
| 352 | unsigned int esize = fifo->esize; |
| 353 | unsigned int l; |
| 354 | unsigned int n; |
| 355 | |
| 356 | off &= fifo->mask; |
| 357 | if (esize != 1) { |
| 358 | off *= esize; |
| 359 | size *= esize; |
| 360 | len *= esize; |
| 361 | } |
| 362 | l = min(len, size - off); |
| 363 | |
Stefani Seibold | d78a3ed | 2010-08-11 14:17:27 -0700 | [diff] [blame] | 364 | n = setup_sgl_buf(sgl, fifo->data + off, nents, l); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 365 | n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l); |
| 366 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 367 | return n; |
| 368 | } |
| 369 | |
| 370 | unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo, |
| 371 | struct scatterlist *sgl, int nents, unsigned int len) |
| 372 | { |
| 373 | unsigned int l; |
| 374 | |
| 375 | l = kfifo_unused(fifo); |
| 376 | if (len > l) |
| 377 | len = l; |
| 378 | |
| 379 | return setup_sgl(fifo, sgl, nents, len, fifo->in); |
| 380 | } |
| 381 | EXPORT_SYMBOL(__kfifo_dma_in_prepare); |
| 382 | |
| 383 | unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo, |
| 384 | struct scatterlist *sgl, int nents, unsigned int len) |
| 385 | { |
| 386 | unsigned int l; |
| 387 | |
| 388 | l = fifo->in - fifo->out; |
| 389 | if (len > l) |
| 390 | len = l; |
| 391 | |
| 392 | return setup_sgl(fifo, sgl, nents, len, fifo->out); |
| 393 | } |
| 394 | EXPORT_SYMBOL(__kfifo_dma_out_prepare); |
| 395 | |
| 396 | unsigned int __kfifo_max_r(unsigned int len, size_t recsize) |
| 397 | { |
| 398 | unsigned int max = (1 << (recsize << 3)) - 1; |
| 399 | |
| 400 | if (len > max) |
| 401 | return max; |
| 402 | return len; |
| 403 | } |
Srinivas Kandagatla | 30059d9 | 2012-04-11 05:01:56 -0300 | [diff] [blame] | 404 | EXPORT_SYMBOL(__kfifo_max_r); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 405 | |
| 406 | #define __KFIFO_PEEK(data, out, mask) \ |
| 407 | ((data)[(out) & (mask)]) |
| 408 | /* |
| 409 | * __kfifo_peek_n internal helper function for determinate the length of |
| 410 | * the next record in the fifo |
| 411 | */ |
| 412 | static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize) |
| 413 | { |
| 414 | unsigned int l; |
| 415 | unsigned int mask = fifo->mask; |
| 416 | unsigned char *data = fifo->data; |
| 417 | |
| 418 | l = __KFIFO_PEEK(data, fifo->out, mask); |
| 419 | |
| 420 | if (--recsize) |
| 421 | l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8; |
| 422 | |
| 423 | return l; |
| 424 | } |
| 425 | |
| 426 | #define __KFIFO_POKE(data, in, mask, val) \ |
| 427 | ( \ |
| 428 | (data)[(in) & (mask)] = (unsigned char)(val) \ |
| 429 | ) |
| 430 | |
| 431 | /* |
| 432 | * __kfifo_poke_n internal helper function for storeing the length of |
| 433 | * the record into the fifo |
| 434 | */ |
| 435 | static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize) |
| 436 | { |
| 437 | unsigned int mask = fifo->mask; |
| 438 | unsigned char *data = fifo->data; |
| 439 | |
| 440 | __KFIFO_POKE(data, fifo->in, mask, n); |
| 441 | |
| 442 | if (recsize > 1) |
| 443 | __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8); |
| 444 | } |
| 445 | |
| 446 | unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize) |
| 447 | { |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 448 | return __kfifo_peek_n(fifo, recsize); |
| 449 | } |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 450 | EXPORT_SYMBOL(__kfifo_len_r); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 451 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 452 | unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf, |
| 453 | unsigned int len, size_t recsize) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 454 | { |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 455 | if (len + recsize > kfifo_unused(fifo)) |
| 456 | return 0; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 457 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 458 | __kfifo_poke_n(fifo, len, recsize); |
| 459 | |
| 460 | kfifo_copy_in(fifo, buf, len, fifo->in + recsize); |
| 461 | fifo->in += len + recsize; |
| 462 | return len; |
| 463 | } |
| 464 | EXPORT_SYMBOL(__kfifo_in_r); |
| 465 | |
| 466 | static unsigned int kfifo_out_copy_r(struct __kfifo *fifo, |
| 467 | void *buf, unsigned int len, size_t recsize, unsigned int *n) |
| 468 | { |
| 469 | *n = __kfifo_peek_n(fifo, recsize); |
| 470 | |
| 471 | if (len > *n) |
| 472 | len = *n; |
| 473 | |
| 474 | kfifo_copy_out(fifo, buf, len, fifo->out + recsize); |
| 475 | return len; |
| 476 | } |
| 477 | |
| 478 | unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf, |
| 479 | unsigned int len, size_t recsize) |
| 480 | { |
| 481 | unsigned int n; |
| 482 | |
| 483 | if (fifo->in == fifo->out) |
| 484 | return 0; |
| 485 | |
| 486 | return kfifo_out_copy_r(fifo, buf, len, recsize, &n); |
| 487 | } |
| 488 | EXPORT_SYMBOL(__kfifo_out_peek_r); |
| 489 | |
| 490 | unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf, |
| 491 | unsigned int len, size_t recsize) |
| 492 | { |
| 493 | unsigned int n; |
| 494 | |
| 495 | if (fifo->in == fifo->out) |
| 496 | return 0; |
| 497 | |
| 498 | len = kfifo_out_copy_r(fifo, buf, len, recsize, &n); |
| 499 | fifo->out += n + recsize; |
| 500 | return len; |
| 501 | } |
| 502 | EXPORT_SYMBOL(__kfifo_out_r); |
| 503 | |
Andrea Righi | b35de43 | 2010-08-19 14:13:27 -0700 | [diff] [blame] | 504 | void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize) |
| 505 | { |
| 506 | unsigned int n; |
| 507 | |
| 508 | n = __kfifo_peek_n(fifo, recsize); |
| 509 | fifo->out += n + recsize; |
| 510 | } |
| 511 | EXPORT_SYMBOL(__kfifo_skip_r); |
| 512 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 513 | int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from, |
| 514 | unsigned long len, unsigned int *copied, size_t recsize) |
| 515 | { |
| 516 | unsigned long ret; |
| 517 | |
| 518 | len = __kfifo_max_r(len, recsize); |
| 519 | |
| 520 | if (len + recsize > kfifo_unused(fifo)) { |
| 521 | *copied = 0; |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | __kfifo_poke_n(fifo, len, recsize); |
| 526 | |
| 527 | ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied); |
| 528 | if (unlikely(ret)) { |
| 529 | *copied = 0; |
| 530 | return -EFAULT; |
| 531 | } |
| 532 | fifo->in += len + recsize; |
| 533 | return 0; |
| 534 | } |
| 535 | EXPORT_SYMBOL(__kfifo_from_user_r); |
| 536 | |
| 537 | int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to, |
| 538 | unsigned long len, unsigned int *copied, size_t recsize) |
| 539 | { |
| 540 | unsigned long ret; |
| 541 | unsigned int n; |
| 542 | |
| 543 | if (fifo->in == fifo->out) { |
| 544 | *copied = 0; |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | n = __kfifo_peek_n(fifo, recsize); |
| 549 | if (len > n) |
| 550 | len = n; |
| 551 | |
| 552 | ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied); |
| 553 | if (unlikely(ret)) { |
| 554 | *copied = 0; |
| 555 | return -EFAULT; |
| 556 | } |
| 557 | fifo->out += n + recsize; |
| 558 | return 0; |
| 559 | } |
| 560 | EXPORT_SYMBOL(__kfifo_to_user_r); |
| 561 | |
| 562 | unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo, |
| 563 | struct scatterlist *sgl, int nents, unsigned int len, size_t recsize) |
| 564 | { |
Himangi Saraogi | 89b3ac6 | 2014-08-08 14:22:44 -0700 | [diff] [blame] | 565 | BUG_ON(!nents); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 566 | |
| 567 | len = __kfifo_max_r(len, recsize); |
| 568 | |
| 569 | if (len + recsize > kfifo_unused(fifo)) |
| 570 | return 0; |
| 571 | |
| 572 | return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize); |
| 573 | } |
| 574 | EXPORT_SYMBOL(__kfifo_dma_in_prepare_r); |
| 575 | |
| 576 | void __kfifo_dma_in_finish_r(struct __kfifo *fifo, |
| 577 | unsigned int len, size_t recsize) |
| 578 | { |
| 579 | len = __kfifo_max_r(len, recsize); |
| 580 | __kfifo_poke_n(fifo, len, recsize); |
| 581 | fifo->in += len + recsize; |
| 582 | } |
| 583 | EXPORT_SYMBOL(__kfifo_dma_in_finish_r); |
| 584 | |
| 585 | unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo, |
| 586 | struct scatterlist *sgl, int nents, unsigned int len, size_t recsize) |
| 587 | { |
Himangi Saraogi | 89b3ac6 | 2014-08-08 14:22:44 -0700 | [diff] [blame] | 588 | BUG_ON(!nents); |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 589 | |
| 590 | len = __kfifo_max_r(len, recsize); |
| 591 | |
| 592 | if (len + recsize > fifo->in - fifo->out) |
| 593 | return 0; |
| 594 | |
| 595 | return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize); |
| 596 | } |
| 597 | EXPORT_SYMBOL(__kfifo_dma_out_prepare_r); |
| 598 | |
| 599 | void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize) |
| 600 | { |
| 601 | unsigned int len; |
| 602 | |
| 603 | len = __kfifo_peek_n(fifo, recsize); |
| 604 | fifo->out += len + recsize; |
| 605 | } |
| 606 | EXPORT_SYMBOL(__kfifo_dma_out_finish_r); |