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