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