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