Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) 2009, Microsoft Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 16 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 17 | * |
| 18 | * Authors: |
| 19 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 20 | * Hank Janssen <hjanssen@microsoft.com> |
| 21 | * |
| 22 | */ |
| 23 | |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 24 | #include <linux/kernel.h> |
| 25 | #include <linux/mm.h> |
Greg Kroah-Hartman | 4983b39 | 2009-08-19 16:14:47 -0700 | [diff] [blame] | 26 | #include "osd.h" |
Greg Kroah-Hartman | 645954c | 2009-08-28 16:22:59 -0700 | [diff] [blame] | 27 | #include "logging.h" |
Greg Kroah-Hartman | 8f078ca | 2010-05-05 22:22:35 -0700 | [diff] [blame] | 28 | #include "ring_buffer.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 29 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 30 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 31 | /* #defines */ |
| 32 | |
| 33 | |
| 34 | /* Amount of space to write to */ |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame] | 35 | #define BYTES_AVAIL_TO_WRITE(r, w, z) ((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w)) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | /*++ |
| 39 | |
| 40 | Name: |
| 41 | GetRingBufferAvailBytes() |
| 42 | |
| 43 | Description: |
| 44 | Get number of bytes available to read and to write to |
| 45 | for the specified ring buffer |
| 46 | |
| 47 | --*/ |
| 48 | static inline void |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 49 | GetRingBufferAvailBytes(RING_BUFFER_INFO *rbi, u32 *read, u32 *write) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 50 | { |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 51 | u32 read_loc, write_loc; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 52 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 53 | /* Capture the read/write indices before they changed */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 54 | read_loc = rbi->RingBuffer->ReadIndex; |
| 55 | write_loc = rbi->RingBuffer->WriteIndex; |
| 56 | |
| 57 | *write = BYTES_AVAIL_TO_WRITE(read_loc, write_loc, rbi->RingDataSize); |
| 58 | *read = rbi->RingDataSize - *write; |
| 59 | } |
| 60 | |
| 61 | /*++ |
| 62 | |
| 63 | Name: |
| 64 | GetNextWriteLocation() |
| 65 | |
| 66 | Description: |
| 67 | Get the next write location for the specified ring buffer |
| 68 | |
| 69 | --*/ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 70 | static inline u32 |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 71 | GetNextWriteLocation(RING_BUFFER_INFO *RingInfo) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 72 | { |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 73 | u32 next = RingInfo->RingBuffer->WriteIndex; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 74 | |
Bill Pemberton | 1bbdd7a | 2010-05-05 15:27:48 -0400 | [diff] [blame] | 75 | /* ASSERT(next < RingInfo->RingDataSize); */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 76 | |
| 77 | return next; |
| 78 | } |
| 79 | |
| 80 | /*++ |
| 81 | |
| 82 | Name: |
| 83 | SetNextWriteLocation() |
| 84 | |
| 85 | Description: |
| 86 | Set the next write location for the specified ring buffer |
| 87 | |
| 88 | --*/ |
| 89 | static inline void |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 90 | SetNextWriteLocation(RING_BUFFER_INFO *RingInfo, u32 NextWriteLocation) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 91 | { |
| 92 | RingInfo->RingBuffer->WriteIndex = NextWriteLocation; |
| 93 | } |
| 94 | |
| 95 | /*++ |
| 96 | |
| 97 | Name: |
| 98 | GetNextReadLocation() |
| 99 | |
| 100 | Description: |
| 101 | Get the next read location for the specified ring buffer |
| 102 | |
| 103 | --*/ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 104 | static inline u32 |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 105 | GetNextReadLocation(RING_BUFFER_INFO *RingInfo) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 106 | { |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 107 | u32 next = RingInfo->RingBuffer->ReadIndex; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 108 | |
Bill Pemberton | 1bbdd7a | 2010-05-05 15:27:48 -0400 | [diff] [blame] | 109 | /* ASSERT(next < RingInfo->RingDataSize); */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 110 | |
| 111 | return next; |
| 112 | } |
| 113 | |
| 114 | /*++ |
| 115 | |
| 116 | Name: |
| 117 | GetNextReadLocationWithOffset() |
| 118 | |
| 119 | Description: |
| 120 | Get the next read location + offset for the specified ring buffer. |
| 121 | This allows the caller to skip |
| 122 | |
| 123 | --*/ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 124 | static inline u32 |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 125 | GetNextReadLocationWithOffset(RING_BUFFER_INFO *RingInfo, u32 Offset) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 126 | { |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 127 | u32 next = RingInfo->RingBuffer->ReadIndex; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 128 | |
Bill Pemberton | 1bbdd7a | 2010-05-05 15:27:48 -0400 | [diff] [blame] | 129 | /* ASSERT(next < RingInfo->RingDataSize); */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 130 | next += Offset; |
| 131 | next %= RingInfo->RingDataSize; |
| 132 | |
| 133 | return next; |
| 134 | } |
| 135 | |
| 136 | /*++ |
| 137 | |
| 138 | Name: |
| 139 | SetNextReadLocation() |
| 140 | |
| 141 | Description: |
| 142 | Set the next read location for the specified ring buffer |
| 143 | |
| 144 | --*/ |
| 145 | static inline void |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 146 | SetNextReadLocation(RING_BUFFER_INFO *RingInfo, u32 NextReadLocation) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 147 | { |
| 148 | RingInfo->RingBuffer->ReadIndex = NextReadLocation; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /*++ |
| 153 | |
| 154 | Name: |
| 155 | GetRingBuffer() |
| 156 | |
| 157 | Description: |
| 158 | Get the start of the ring buffer |
| 159 | |
| 160 | --*/ |
Greg Kroah-Hartman | 8282c40 | 2009-07-14 15:06:28 -0700 | [diff] [blame] | 161 | static inline void * |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 162 | GetRingBuffer(RING_BUFFER_INFO *RingInfo) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 163 | { |
Greg Kroah-Hartman | 8282c40 | 2009-07-14 15:06:28 -0700 | [diff] [blame] | 164 | return (void *)RingInfo->RingBuffer->Buffer; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
| 168 | /*++ |
| 169 | |
| 170 | Name: |
| 171 | GetRingBufferSize() |
| 172 | |
| 173 | Description: |
| 174 | Get the size of the ring buffer |
| 175 | |
| 176 | --*/ |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 177 | static inline u32 |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 178 | GetRingBufferSize(RING_BUFFER_INFO *RingInfo) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 179 | { |
| 180 | return RingInfo->RingDataSize; |
| 181 | } |
| 182 | |
| 183 | /*++ |
| 184 | |
| 185 | Name: |
| 186 | GetRingBufferIndices() |
| 187 | |
| 188 | Description: |
Greg Kroah-Hartman | 5947143 | 2009-07-14 15:10:26 -0700 | [diff] [blame] | 189 | Get the read and write indices as u64 of the specified ring buffer |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 190 | |
| 191 | --*/ |
Greg Kroah-Hartman | 5947143 | 2009-07-14 15:10:26 -0700 | [diff] [blame] | 192 | static inline u64 |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 193 | GetRingBufferIndices(RING_BUFFER_INFO *RingInfo) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 194 | { |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 195 | return ((u64)RingInfo->RingBuffer->WriteIndex << 32) |
| 196 | || RingInfo->RingBuffer->ReadIndex; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | |
| 200 | /*++ |
| 201 | |
| 202 | Name: |
| 203 | DumpRingInfo() |
| 204 | |
| 205 | Description: |
| 206 | Dump out to console the ring buffer info |
| 207 | |
| 208 | --*/ |
Greg Kroah-Hartman | 3523a80 | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 209 | void DumpRingInfo(RING_BUFFER_INFO *RingInfo, char *Prefix) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 210 | { |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 211 | u32 bytesAvailToWrite; |
| 212 | u32 bytesAvailToRead; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 213 | |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 214 | GetRingBufferAvailBytes(RingInfo, |
| 215 | &bytesAvailToRead, |
| 216 | &bytesAvailToWrite); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 217 | |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 218 | DPRINT(VMBUS, |
| 219 | DEBUG_RING_LVL, |
| 220 | "%s <<ringinfo %p buffer %p avail write %u " |
| 221 | "avail read %u read idx %u write idx %u>>", |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 222 | Prefix, |
| 223 | RingInfo, |
| 224 | RingInfo->RingBuffer->Buffer, |
| 225 | bytesAvailToWrite, |
| 226 | bytesAvailToRead, |
| 227 | RingInfo->RingBuffer->ReadIndex, |
| 228 | RingInfo->RingBuffer->WriteIndex); |
| 229 | } |
| 230 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 231 | |
| 232 | /* Internal routines */ |
| 233 | |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 234 | static u32 |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 235 | CopyToRingBuffer( |
| 236 | RING_BUFFER_INFO *RingInfo, |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 237 | u32 StartWriteOffset, |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 238 | void *Src, |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 239 | u32 SrcLen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 240 | |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 241 | static u32 |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 242 | CopyFromRingBuffer( |
| 243 | RING_BUFFER_INFO *RingInfo, |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 244 | void *Dest, |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 245 | u32 DestLen, |
| 246 | u32 StartReadOffset); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 247 | |
| 248 | |
| 249 | |
| 250 | /*++ |
| 251 | |
| 252 | Name: |
| 253 | RingBufferGetDebugInfo() |
| 254 | |
| 255 | Description: |
| 256 | Get various debug metrics for the specified ring buffer |
| 257 | |
| 258 | --*/ |
Greg Kroah-Hartman | 3523a80 | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 259 | void RingBufferGetDebugInfo(RING_BUFFER_INFO *RingInfo, |
Greg Kroah-Hartman | 80682b7 | 2010-07-27 11:37:32 -0700 | [diff] [blame^] | 260 | struct hv_ring_buffer_debug_info *debug_info) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 261 | { |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 262 | u32 bytesAvailToWrite; |
| 263 | u32 bytesAvailToRead; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 264 | |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 265 | if (RingInfo->RingBuffer) { |
| 266 | GetRingBufferAvailBytes(RingInfo, |
| 267 | &bytesAvailToRead, |
| 268 | &bytesAvailToWrite); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 269 | |
Greg Kroah-Hartman | 80682b7 | 2010-07-27 11:37:32 -0700 | [diff] [blame^] | 270 | debug_info->BytesAvailToRead = bytesAvailToRead; |
| 271 | debug_info->BytesAvailToWrite = bytesAvailToWrite; |
| 272 | debug_info->CurrentReadIndex = RingInfo->RingBuffer->ReadIndex; |
| 273 | debug_info->CurrentWriteIndex = RingInfo->RingBuffer->WriteIndex; |
| 274 | debug_info->CurrentInterruptMask = RingInfo->RingBuffer->InterruptMask; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | |
| 279 | /*++ |
| 280 | |
| 281 | Name: |
| 282 | GetRingBufferInterruptMask() |
| 283 | |
| 284 | Description: |
| 285 | Get the interrupt mask for the specified ring buffer |
| 286 | |
| 287 | --*/ |
Greg Kroah-Hartman | 3523a80 | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 288 | u32 GetRingBufferInterruptMask(RING_BUFFER_INFO *rbi) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 289 | { |
| 290 | return rbi->RingBuffer->InterruptMask; |
| 291 | } |
| 292 | |
| 293 | /*++ |
| 294 | |
| 295 | Name: |
| 296 | RingBufferInit() |
| 297 | |
| 298 | Description: |
| 299 | Initialize the ring buffer |
| 300 | |
| 301 | --*/ |
Greg Kroah-Hartman | 3523a80 | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 302 | int RingBufferInit(RING_BUFFER_INFO *RingInfo, void *Buffer, u32 BufferLen) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 303 | { |
Bill Pemberton | 3324fb4 | 2010-05-05 15:27:49 -0400 | [diff] [blame] | 304 | if (sizeof(RING_BUFFER) != PAGE_SIZE) |
| 305 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 306 | |
| 307 | memset(RingInfo, 0, sizeof(RING_BUFFER_INFO)); |
| 308 | |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 309 | RingInfo->RingBuffer = (RING_BUFFER *)Buffer; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 310 | RingInfo->RingBuffer->ReadIndex = RingInfo->RingBuffer->WriteIndex = 0; |
| 311 | |
| 312 | RingInfo->RingSize = BufferLen; |
| 313 | RingInfo->RingDataSize = BufferLen - sizeof(RING_BUFFER); |
| 314 | |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 315 | spin_lock_init(&RingInfo->ring_lock); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /*++ |
| 321 | |
| 322 | Name: |
| 323 | RingBufferCleanup() |
| 324 | |
| 325 | Description: |
| 326 | Cleanup the ring buffer |
| 327 | |
| 328 | --*/ |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 329 | void RingBufferCleanup(RING_BUFFER_INFO *RingInfo) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 330 | { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /*++ |
| 334 | |
| 335 | Name: |
| 336 | RingBufferWrite() |
| 337 | |
| 338 | Description: |
| 339 | Write to the ring buffer |
| 340 | |
| 341 | --*/ |
Greg Kroah-Hartman | 3523a80 | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 342 | int RingBufferWrite(RING_BUFFER_INFO *OutRingInfo, |
| 343 | struct scatterlist *sglist, u32 sgcount) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 344 | { |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 345 | int i = 0; |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 346 | u32 byteAvailToWrite; |
| 347 | u32 byteAvailToRead; |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 348 | u32 totalBytesToWrite = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 349 | |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 350 | struct scatterlist *sg; |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 351 | volatile u32 nextWriteLocation; |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 352 | u64 prevIndices = 0; |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 353 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 354 | |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 355 | for_each_sg(sglist, sg, sgcount, i) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 356 | { |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 357 | totalBytesToWrite += sg->length; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Greg Kroah-Hartman | 5947143 | 2009-07-14 15:10:26 -0700 | [diff] [blame] | 360 | totalBytesToWrite += sizeof(u64); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 361 | |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 362 | spin_lock_irqsave(&OutRingInfo->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 363 | |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 364 | GetRingBufferAvailBytes(OutRingInfo, |
| 365 | &byteAvailToRead, |
| 366 | &byteAvailToWrite); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 367 | |
| 368 | DPRINT_DBG(VMBUS, "Writing %u bytes...", totalBytesToWrite); |
| 369 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 370 | /* DumpRingInfo(OutRingInfo, "BEFORE "); */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 371 | |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 372 | /* If there is only room for the packet, assume it is full. */ |
| 373 | /* Otherwise, the next time around, we think the ring buffer */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 374 | /* is empty since the read index == write index */ |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 375 | if (byteAvailToWrite <= totalBytesToWrite) { |
| 376 | DPRINT_DBG(VMBUS, |
| 377 | "No more space left on outbound ring buffer " |
| 378 | "(needed %u, avail %u)", |
| 379 | totalBytesToWrite, |
| 380 | byteAvailToWrite); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 381 | |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 382 | spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 383 | return -1; |
| 384 | } |
| 385 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 386 | /* Write to the ring buffer */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 387 | nextWriteLocation = GetNextWriteLocation(OutRingInfo); |
| 388 | |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 389 | for_each_sg(sglist, sg, sgcount, i) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 390 | { |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 391 | nextWriteLocation = CopyToRingBuffer(OutRingInfo, |
| 392 | nextWriteLocation, |
| 393 | sg_virt(sg), |
| 394 | sg->length); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 397 | /* Set previous packet start */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 398 | prevIndices = GetRingBufferIndices(OutRingInfo); |
| 399 | |
| 400 | nextWriteLocation = CopyToRingBuffer(OutRingInfo, |
Nicolas Palix | b219b3f | 2009-07-30 17:37:23 +0200 | [diff] [blame] | 401 | nextWriteLocation, |
| 402 | &prevIndices, |
| 403 | sizeof(u64)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 404 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 405 | /* Make sure we flush all writes before updating the writeIndex */ |
Greg Kroah-Hartman | 28b6ca9 | 2009-07-16 12:34:20 -0700 | [diff] [blame] | 406 | mb(); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 407 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 408 | /* Now, update the write location */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 409 | SetNextWriteLocation(OutRingInfo, nextWriteLocation); |
| 410 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 411 | /* DumpRingInfo(OutRingInfo, "AFTER "); */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 412 | |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 413 | spin_unlock_irqrestore(&OutRingInfo->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | /*++ |
| 419 | |
| 420 | Name: |
| 421 | RingBufferPeek() |
| 422 | |
| 423 | Description: |
| 424 | Read without advancing the read index |
| 425 | |
| 426 | --*/ |
Greg Kroah-Hartman | 3523a80 | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 427 | int RingBufferPeek(RING_BUFFER_INFO *InRingInfo, void *Buffer, u32 BufferLen) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 428 | { |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 429 | u32 bytesAvailToWrite; |
| 430 | u32 bytesAvailToRead; |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 431 | u32 nextReadLocation = 0; |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 432 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 433 | |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 434 | spin_lock_irqsave(&InRingInfo->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 435 | |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 436 | GetRingBufferAvailBytes(InRingInfo, |
| 437 | &bytesAvailToRead, |
| 438 | &bytesAvailToWrite); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 439 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 440 | /* Make sure there is something to read */ |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 441 | if (bytesAvailToRead < BufferLen) { |
| 442 | /* DPRINT_DBG(VMBUS, |
| 443 | "got callback but not enough to read " |
| 444 | "<avail to read %d read size %d>!!", |
| 445 | bytesAvailToRead, |
| 446 | BufferLen); */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 447 | |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 448 | spin_unlock_irqrestore(&InRingInfo->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 449 | |
| 450 | return -1; |
| 451 | } |
| 452 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 453 | /* Convert to byte offset */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 454 | nextReadLocation = GetNextReadLocation(InRingInfo); |
| 455 | |
| 456 | nextReadLocation = CopyFromRingBuffer(InRingInfo, |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 457 | Buffer, |
| 458 | BufferLen, |
| 459 | nextReadLocation); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 460 | |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 461 | spin_unlock_irqrestore(&InRingInfo->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | /*++ |
| 468 | |
| 469 | Name: |
| 470 | RingBufferRead() |
| 471 | |
| 472 | Description: |
| 473 | Read and advance the read index |
| 474 | |
| 475 | --*/ |
Greg Kroah-Hartman | 3523a80 | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 476 | int RingBufferRead(RING_BUFFER_INFO *InRingInfo, void *Buffer, |
| 477 | u32 BufferLen, u32 Offset) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 478 | { |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 479 | u32 bytesAvailToWrite; |
| 480 | u32 bytesAvailToRead; |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 481 | u32 nextReadLocation = 0; |
| 482 | u64 prevIndices = 0; |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 483 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 484 | |
Bill Pemberton | a16e148 | 2010-05-05 15:27:50 -0400 | [diff] [blame] | 485 | if (BufferLen <= 0) |
| 486 | return -EINVAL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 487 | |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 488 | spin_lock_irqsave(&InRingInfo->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 489 | |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 490 | GetRingBufferAvailBytes(InRingInfo, |
| 491 | &bytesAvailToRead, |
| 492 | &bytesAvailToWrite); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 493 | |
| 494 | DPRINT_DBG(VMBUS, "Reading %u bytes...", BufferLen); |
| 495 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 496 | /* DumpRingInfo(InRingInfo, "BEFORE "); */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 497 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 498 | /* Make sure there is something to read */ |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 499 | if (bytesAvailToRead < BufferLen) { |
| 500 | DPRINT_DBG(VMBUS, |
| 501 | "got callback but not enough to read " |
| 502 | "<avail to read %d read size %d>!!", |
| 503 | bytesAvailToRead, |
| 504 | BufferLen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 505 | |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 506 | spin_unlock_irqrestore(&InRingInfo->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 507 | |
| 508 | return -1; |
| 509 | } |
| 510 | |
| 511 | nextReadLocation = GetNextReadLocationWithOffset(InRingInfo, Offset); |
| 512 | |
| 513 | nextReadLocation = CopyFromRingBuffer(InRingInfo, |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 514 | Buffer, |
| 515 | BufferLen, |
| 516 | nextReadLocation); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 517 | |
| 518 | nextReadLocation = CopyFromRingBuffer(InRingInfo, |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 519 | &prevIndices, |
| 520 | sizeof(u64), |
| 521 | nextReadLocation); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 522 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 523 | /* Make sure all reads are done before we update the read index since */ |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 524 | /* the writer may start writing to the read area once the read index */ |
| 525 | /*is updated */ |
Greg Kroah-Hartman | 28b6ca9 | 2009-07-16 12:34:20 -0700 | [diff] [blame] | 526 | mb(); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 527 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 528 | /* Update the read index */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 529 | SetNextReadLocation(InRingInfo, nextReadLocation); |
| 530 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 531 | /* DumpRingInfo(InRingInfo, "AFTER "); */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 532 | |
Greg Kroah-Hartman | a98f96e | 2009-07-15 14:55:14 -0700 | [diff] [blame] | 533 | spin_unlock_irqrestore(&InRingInfo->ring_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | /*++ |
| 540 | |
| 541 | Name: |
| 542 | CopyToRingBuffer() |
| 543 | |
| 544 | Description: |
| 545 | Helper routine to copy from source to ring buffer. |
| 546 | Assume there is enough room. Handles wrap-around in dest case only!! |
| 547 | |
| 548 | --*/ |
Greg Kroah-Hartman | bd1de70 | 2009-07-29 09:04:51 -0700 | [diff] [blame] | 549 | static u32 |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 550 | CopyToRingBuffer( |
| 551 | RING_BUFFER_INFO *RingInfo, |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 552 | u32 StartWriteOffset, |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 553 | void *Src, |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 554 | u32 SrcLen) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 555 | { |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 556 | void *ringBuffer = GetRingBuffer(RingInfo); |
| 557 | u32 ringBufferSize = GetRingBufferSize(RingInfo); |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 558 | u32 fragLen; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 559 | |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 560 | /* wrap-around detected! */ |
| 561 | if (SrcLen > ringBufferSize - StartWriteOffset) { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 562 | DPRINT_DBG(VMBUS, "wrap-around detected!"); |
| 563 | |
| 564 | fragLen = ringBufferSize - StartWriteOffset; |
| 565 | memcpy(ringBuffer + StartWriteOffset, Src, fragLen); |
| 566 | memcpy(ringBuffer, Src + fragLen, SrcLen - fragLen); |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 567 | } else |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 568 | memcpy(ringBuffer + StartWriteOffset, Src, SrcLen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 569 | |
| 570 | StartWriteOffset += SrcLen; |
| 571 | StartWriteOffset %= ringBufferSize; |
| 572 | |
| 573 | return StartWriteOffset; |
| 574 | } |
| 575 | |
| 576 | |
| 577 | /*++ |
| 578 | |
| 579 | Name: |
| 580 | CopyFromRingBuffer() |
| 581 | |
| 582 | Description: |
| 583 | Helper routine to copy to source from ring buffer. |
| 584 | Assume there is enough room. Handles wrap-around in src case only!! |
| 585 | |
| 586 | --*/ |
Greg Kroah-Hartman | bd1de70 | 2009-07-29 09:04:51 -0700 | [diff] [blame] | 587 | static u32 |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 588 | CopyFromRingBuffer( |
| 589 | RING_BUFFER_INFO *RingInfo, |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 590 | void *Dest, |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 591 | u32 DestLen, |
| 592 | u32 StartReadOffset) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 593 | { |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 594 | void *ringBuffer = GetRingBuffer(RingInfo); |
| 595 | u32 ringBufferSize = GetRingBufferSize(RingInfo); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 596 | |
Greg Kroah-Hartman | 4d64311 | 2009-07-14 15:09:36 -0700 | [diff] [blame] | 597 | u32 fragLen; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 598 | |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 599 | /* wrap-around detected at the src */ |
| 600 | if (DestLen > ringBufferSize - StartReadOffset) { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 601 | DPRINT_DBG(VMBUS, "src wrap-around detected!"); |
| 602 | |
| 603 | fragLen = ringBufferSize - StartReadOffset; |
| 604 | |
| 605 | memcpy(Dest, ringBuffer + StartReadOffset, fragLen); |
| 606 | memcpy(Dest + fragLen, ringBuffer, DestLen - fragLen); |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 607 | } else |
| 608 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 609 | memcpy(Dest, ringBuffer + StartReadOffset, DestLen); |
C. Bartlett | 4408f53 | 2010-02-03 15:34:27 +0000 | [diff] [blame] | 610 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 611 | |
| 612 | StartReadOffset += DestLen; |
| 613 | StartReadOffset %= ringBufferSize; |
| 614 | |
| 615 | return StartReadOffset; |
| 616 | } |
| 617 | |
| 618 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 619 | /* eof */ |