Commit | Line | Data |
---|---|---|
bef4a34a HJ |
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 | ||
5654e932 | 24 | #include <linux/kernel.h> |
0ffa63b0 GKH |
25 | #include <linux/string.h> |
26 | #include <linux/mm.h> | |
b4362c9c | 27 | #include <linux/delay.h> |
97f4ee3d | 28 | #include "include/logging.h" |
bef4a34a | 29 | |
97f4ee3d GKH |
30 | #include "include/StorVscApi.h" |
31 | #include "include/VmbusPacketFormat.h" | |
32 | #include "include/vstorage.h" | |
bef4a34a HJ |
33 | |
34 | ||
bef4a34a | 35 | |
454f18a9 BP |
36 | /* #defines */ |
37 | ||
38 | ||
39 | ||
40 | /* Data types */ | |
41 | ||
bef4a34a HJ |
42 | |
43 | typedef struct _STORVSC_REQUEST_EXTENSION { | |
454f18a9 | 44 | /* LIST_ENTRY ListEntry; */ |
bef4a34a HJ |
45 | |
46 | STORVSC_REQUEST *Request; | |
3d3b5518 | 47 | struct hv_device *Device; |
bef4a34a | 48 | |
454f18a9 | 49 | /* Synchronize the request/response if needed */ |
aedb444a | 50 | struct osd_waitevent *WaitEvent; |
bef4a34a HJ |
51 | |
52 | VSTOR_PACKET VStorPacket; | |
53 | } STORVSC_REQUEST_EXTENSION; | |
54 | ||
55 | ||
454f18a9 | 56 | /* A storvsc device is a device object that contains a vmbus channel */ |
bef4a34a | 57 | typedef struct _STORVSC_DEVICE{ |
3d3b5518 | 58 | struct hv_device *Device; |
bef4a34a | 59 | |
454f18a9 | 60 | int RefCount; /* 0 indicates the device is being destroyed */ |
bef4a34a HJ |
61 | |
62 | int NumOutstandingRequests; | |
63 | ||
454f18a9 BP |
64 | /* |
65 | * Each unique Port/Path/Target represents 1 channel ie scsi | |
66 | * controller. In reality, the pathid, targetid is always 0 | |
67 | * and the port is set by us | |
68 | */ | |
3abcbc45 | 69 | unsigned int PortNumber; |
59471438 GKH |
70 | unsigned char PathId; |
71 | unsigned char TargetId; | |
bef4a34a | 72 | |
454f18a9 BP |
73 | /* LIST_ENTRY OutstandingRequestList; */ |
74 | /* HANDLE OutstandingRequestLock; */ | |
bef4a34a | 75 | |
454f18a9 | 76 | /* Used for vsc/vsp channel reset process */ |
bef4a34a HJ |
77 | STORVSC_REQUEST_EXTENSION InitRequest; |
78 | ||
79 | STORVSC_REQUEST_EXTENSION ResetRequest; | |
80 | ||
81 | } STORVSC_DEVICE; | |
82 | ||
83 | ||
454f18a9 BP |
84 | |
85 | /* Globals */ | |
86 | ||
bef4a34a HJ |
87 | static const char* gDriverName="storvsc"; |
88 | ||
454f18a9 | 89 | /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */ |
bef4a34a HJ |
90 | static const GUID gStorVscDeviceType={ |
91 | .Data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f} | |
92 | }; | |
93 | ||
454f18a9 BP |
94 | |
95 | /* Internal routines */ | |
96 | ||
bef4a34a HJ |
97 | static int |
98 | StorVscOnDeviceAdd( | |
3d3b5518 | 99 | struct hv_device *Device, |
bef4a34a HJ |
100 | void *AdditionalInfo |
101 | ); | |
102 | ||
103 | static int | |
104 | StorVscOnDeviceRemove( | |
3d3b5518 | 105 | struct hv_device *Device |
bef4a34a HJ |
106 | ); |
107 | ||
108 | static int | |
109 | StorVscOnIORequest( | |
3d3b5518 | 110 | struct hv_device *Device, |
bef4a34a HJ |
111 | STORVSC_REQUEST *Request |
112 | ); | |
113 | ||
114 | static int | |
115 | StorVscOnHostReset( | |
3d3b5518 | 116 | struct hv_device *Device |
bef4a34a HJ |
117 | ); |
118 | ||
119 | static void | |
120 | StorVscOnCleanup( | |
775ef25e | 121 | struct hv_driver *Device |
bef4a34a HJ |
122 | ); |
123 | ||
124 | static void | |
125 | StorVscOnChannelCallback( | |
8282c400 | 126 | void * Context |
bef4a34a HJ |
127 | ); |
128 | ||
129 | static void | |
130 | StorVscOnIOCompletion( | |
3d3b5518 | 131 | struct hv_device *Device, |
bef4a34a HJ |
132 | VSTOR_PACKET *VStorPacket, |
133 | STORVSC_REQUEST_EXTENSION *RequestExt | |
134 | ); | |
135 | ||
136 | static void | |
137 | StorVscOnReceive( | |
3d3b5518 | 138 | struct hv_device *Device, |
bef4a34a HJ |
139 | VSTOR_PACKET *VStorPacket, |
140 | STORVSC_REQUEST_EXTENSION *RequestExt | |
141 | ); | |
142 | ||
143 | static int | |
144 | StorVscConnectToVsp( | |
3d3b5518 | 145 | struct hv_device *Device |
bef4a34a HJ |
146 | ); |
147 | ||
3d3b5518 | 148 | static inline STORVSC_DEVICE* AllocStorDevice(struct hv_device *Device) |
bef4a34a HJ |
149 | { |
150 | STORVSC_DEVICE *storDevice; | |
151 | ||
e276a3a5 | 152 | storDevice = kzalloc(sizeof(STORVSC_DEVICE), GFP_KERNEL); |
bef4a34a HJ |
153 | if (!storDevice) |
154 | return NULL; | |
155 | ||
454f18a9 BP |
156 | /* Set to 2 to allow both inbound and outbound traffics */ |
157 | /* (ie GetStorDevice() and MustGetStorDevice()) to proceed. */ | |
bef4a34a HJ |
158 | InterlockedCompareExchange(&storDevice->RefCount, 2, 0); |
159 | ||
160 | storDevice->Device = Device; | |
161 | Device->Extension = storDevice; | |
162 | ||
163 | return storDevice; | |
164 | } | |
165 | ||
166 | static inline void FreeStorDevice(STORVSC_DEVICE *Device) | |
167 | { | |
168 | ASSERT(Device->RefCount == 0); | |
8c69f52a | 169 | kfree(Device); |
bef4a34a HJ |
170 | } |
171 | ||
454f18a9 | 172 | /* Get the stordevice object iff exists and its refcount > 1 */ |
3d3b5518 | 173 | static inline STORVSC_DEVICE* GetStorDevice(struct hv_device *Device) |
bef4a34a HJ |
174 | { |
175 | STORVSC_DEVICE *storDevice; | |
176 | ||
177 | storDevice = (STORVSC_DEVICE*)Device->Extension; | |
178 | if (storDevice && storDevice->RefCount > 1) | |
179 | { | |
180 | InterlockedIncrement(&storDevice->RefCount); | |
181 | } | |
182 | else | |
183 | { | |
184 | storDevice = NULL; | |
185 | } | |
186 | ||
187 | return storDevice; | |
188 | } | |
189 | ||
454f18a9 | 190 | /* Get the stordevice object iff exists and its refcount > 0 */ |
3d3b5518 | 191 | static inline STORVSC_DEVICE* MustGetStorDevice(struct hv_device *Device) |
bef4a34a HJ |
192 | { |
193 | STORVSC_DEVICE *storDevice; | |
194 | ||
195 | storDevice = (STORVSC_DEVICE*)Device->Extension; | |
196 | if (storDevice && storDevice->RefCount) | |
197 | { | |
198 | InterlockedIncrement(&storDevice->RefCount); | |
199 | } | |
200 | else | |
201 | { | |
202 | storDevice = NULL; | |
203 | } | |
204 | ||
205 | return storDevice; | |
206 | } | |
207 | ||
3d3b5518 | 208 | static inline void PutStorDevice(struct hv_device *Device) |
bef4a34a HJ |
209 | { |
210 | STORVSC_DEVICE *storDevice; | |
211 | ||
212 | storDevice = (STORVSC_DEVICE*)Device->Extension; | |
213 | ASSERT(storDevice); | |
214 | ||
215 | InterlockedDecrement(&storDevice->RefCount); | |
216 | ASSERT(storDevice->RefCount); | |
217 | } | |
218 | ||
454f18a9 | 219 | /* Drop ref count to 1 to effectively disable GetStorDevice() */ |
3d3b5518 | 220 | static inline STORVSC_DEVICE* ReleaseStorDevice(struct hv_device *Device) |
bef4a34a HJ |
221 | { |
222 | STORVSC_DEVICE *storDevice; | |
223 | ||
224 | storDevice = (STORVSC_DEVICE*)Device->Extension; | |
225 | ASSERT(storDevice); | |
226 | ||
454f18a9 | 227 | /* Busy wait until the ref drop to 2, then set it to 1 */ |
bef4a34a HJ |
228 | while (InterlockedCompareExchange(&storDevice->RefCount, 1, 2) != 2) |
229 | { | |
b4362c9c | 230 | udelay(100); |
bef4a34a HJ |
231 | } |
232 | ||
233 | return storDevice; | |
234 | } | |
235 | ||
454f18a9 | 236 | /* Drop ref count to 0. No one can use StorDevice object. */ |
3d3b5518 | 237 | static inline STORVSC_DEVICE* FinalReleaseStorDevice(struct hv_device *Device) |
bef4a34a HJ |
238 | { |
239 | STORVSC_DEVICE *storDevice; | |
240 | ||
241 | storDevice = (STORVSC_DEVICE*)Device->Extension; | |
242 | ASSERT(storDevice); | |
243 | ||
454f18a9 | 244 | /* Busy wait until the ref drop to 1, then set it to 0 */ |
bef4a34a HJ |
245 | while (InterlockedCompareExchange(&storDevice->RefCount, 0, 1) != 1) |
246 | { | |
b4362c9c | 247 | udelay(100); |
bef4a34a HJ |
248 | } |
249 | ||
250 | Device->Extension = NULL; | |
251 | return storDevice; | |
252 | } | |
253 | ||
254 | /*++; | |
255 | ||
256 | ||
257 | Name: | |
258 | StorVscInitialize() | |
259 | ||
260 | Description: | |
261 | Main entry point | |
262 | ||
263 | --*/ | |
264 | int | |
265 | StorVscInitialize( | |
775ef25e | 266 | struct hv_driver *Driver |
bef4a34a HJ |
267 | ) |
268 | { | |
269 | STORVSC_DRIVER_OBJECT* storDriver = (STORVSC_DRIVER_OBJECT*)Driver; | |
270 | int ret=0; | |
271 | ||
272 | DPRINT_ENTER(STORVSC); | |
273 | ||
b7d7ae6f | 274 | DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd sizeof(STORVSC_REQUEST_EXTENSION)=%zd sizeof(VSTOR_PACKET)=%zd, sizeof(VMSCSI_REQUEST)=%zd", |
bef4a34a HJ |
275 | sizeof(STORVSC_REQUEST), sizeof(STORVSC_REQUEST_EXTENSION), sizeof(VSTOR_PACKET), sizeof(VMSCSI_REQUEST)); |
276 | ||
454f18a9 | 277 | /* Make sure we are at least 2 pages since 1 page is used for control */ |
bef4a34a HJ |
278 | ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1)); |
279 | ||
280 | Driver->name = gDriverName; | |
281 | memcpy(&Driver->deviceType, &gStorVscDeviceType, sizeof(GUID)); | |
282 | ||
283 | storDriver->RequestExtSize = sizeof(STORVSC_REQUEST_EXTENSION); | |
284 | ||
454f18a9 BP |
285 | /* |
286 | * Divide the ring buffer data size (which is 1 page less | |
287 | * than the ring buffer size since that page is reserved for | |
288 | * the ring buffer indices) by the max request size (which is | |
289 | * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + VSTOR_PACKET + u64) | |
290 | */ | |
bef4a34a | 291 | storDriver->MaxOutstandingRequestsPerChannel = |
59471438 | 292 | ((storDriver->RingBufferSize - PAGE_SIZE) / ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET + sizeof(VSTOR_PACKET) + sizeof(u64),sizeof(u64))); |
bef4a34a HJ |
293 | |
294 | DPRINT_INFO(STORVSC, "max io %u, currently %u\n", storDriver->MaxOutstandingRequestsPerChannel, STORVSC_MAX_IO_REQUESTS); | |
295 | ||
454f18a9 | 296 | /* Setup the dispatch table */ |
bef4a34a HJ |
297 | storDriver->Base.OnDeviceAdd = StorVscOnDeviceAdd; |
298 | storDriver->Base.OnDeviceRemove = StorVscOnDeviceRemove; | |
299 | storDriver->Base.OnCleanup = StorVscOnCleanup; | |
300 | ||
301 | storDriver->OnIORequest = StorVscOnIORequest; | |
302 | storDriver->OnHostReset = StorVscOnHostReset; | |
303 | ||
304 | DPRINT_EXIT(STORVSC); | |
305 | ||
306 | return ret; | |
307 | } | |
308 | ||
309 | /*++ | |
310 | ||
311 | Name: | |
312 | StorVscOnDeviceAdd() | |
313 | ||
314 | Description: | |
315 | Callback when the device belonging to this driver is added | |
316 | ||
317 | --*/ | |
318 | int | |
319 | StorVscOnDeviceAdd( | |
3d3b5518 | 320 | struct hv_device *Device, |
bef4a34a HJ |
321 | void *AdditionalInfo |
322 | ) | |
323 | { | |
324 | int ret=0; | |
325 | STORVSC_DEVICE *storDevice; | |
454f18a9 | 326 | /* VMSTORAGE_CHANNEL_PROPERTIES *props; */ |
bef4a34a HJ |
327 | STORVSC_DEVICE_INFO *deviceInfo = (STORVSC_DEVICE_INFO*)AdditionalInfo; |
328 | ||
329 | DPRINT_ENTER(STORVSC); | |
330 | ||
331 | storDevice = AllocStorDevice(Device); | |
332 | if (!storDevice) | |
333 | { | |
334 | ret = -1; | |
335 | goto Cleanup; | |
336 | } | |
337 | ||
454f18a9 BP |
338 | /* Save the channel properties to our storvsc channel */ |
339 | /* props = (VMSTORAGE_CHANNEL_PROPERTIES*) channel->offerMsg.Offer.u.Standard.UserDefined; */ | |
340 | ||
341 | /* FIXME: */ | |
342 | /* | |
343 | * If we support more than 1 scsi channel, we need to set the | |
344 | * port number here to the scsi channel but how do we get the | |
345 | * scsi channel prior to the bus scan | |
346 | */ | |
bef4a34a | 347 | |
454f18a9 | 348 | /* storChannel->PortNumber = 0; |
bef4a34a | 349 | storChannel->PathId = props->PathId; |
454f18a9 | 350 | storChannel->TargetId = props->TargetId; */ |
bef4a34a HJ |
351 | |
352 | storDevice->PortNumber = deviceInfo->PortNumber; | |
454f18a9 | 353 | /* Send it back up */ |
bef4a34a HJ |
354 | ret = StorVscConnectToVsp(Device); |
355 | ||
454f18a9 | 356 | /* deviceInfo->PortNumber = storDevice->PortNumber; */ |
bef4a34a HJ |
357 | deviceInfo->PathId = storDevice->PathId; |
358 | deviceInfo->TargetId = storDevice->TargetId; | |
359 | ||
78cd2003 | 360 | DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n", storDevice->PortNumber, storDevice->PathId, storDevice->TargetId); |
bef4a34a HJ |
361 | |
362 | Cleanup: | |
363 | DPRINT_EXIT(STORVSC); | |
364 | ||
365 | return ret; | |
366 | } | |
367 | ||
3d3b5518 | 368 | static int StorVscChannelInit(struct hv_device *Device) |
bef4a34a HJ |
369 | { |
370 | int ret=0; | |
371 | STORVSC_DEVICE *storDevice; | |
372 | STORVSC_REQUEST_EXTENSION *request; | |
373 | VSTOR_PACKET *vstorPacket; | |
374 | ||
375 | storDevice = GetStorDevice(Device); | |
376 | if (!storDevice) | |
377 | { | |
378 | DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?"); | |
379 | DPRINT_EXIT(STORVSC); | |
380 | return -1; | |
381 | } | |
382 | ||
383 | request = &storDevice->InitRequest; | |
384 | vstorPacket = &request->VStorPacket; | |
385 | ||
454f18a9 | 386 | /* Now, initiate the vsc/vsp initialization protocol on the open channel */ |
bef4a34a HJ |
387 | |
388 | memset(request, sizeof(STORVSC_REQUEST_EXTENSION), 0); | |
389 | request->WaitEvent = WaitEventCreate(); | |
390 | ||
391 | vstorPacket->Operation = VStorOperationBeginInitialization; | |
392 | vstorPacket->Flags = REQUEST_COMPLETION_FLAG; | |
393 | ||
394 | /*SpinlockAcquire(gDriverExt.packetListLock); | |
395 | INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry); | |
396 | SpinlockRelease(gDriverExt.packetListLock);*/ | |
397 | ||
398 | DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION..."); | |
399 | ||
400 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
401 | vstorPacket, | |
402 | sizeof(VSTOR_PACKET), | |
c4b0bc94 | 403 | (unsigned long)request, |
bef4a34a HJ |
404 | VmbusPacketTypeDataInBand, |
405 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
406 | if ( ret != 0) | |
407 | { | |
408 | DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION"); | |
409 | goto Cleanup; | |
410 | } | |
411 | ||
412 | WaitEventWait(request->WaitEvent); | |
413 | ||
414 | if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0) | |
415 | { | |
78cd2003 | 416 | DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status); |
bef4a34a HJ |
417 | goto Cleanup; |
418 | } | |
419 | ||
420 | DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION..."); | |
421 | ||
454f18a9 | 422 | /* reuse the packet for version range supported */ |
bef4a34a HJ |
423 | memset(vstorPacket, sizeof(VSTOR_PACKET), 0); |
424 | vstorPacket->Operation = VStorOperationQueryProtocolVersion; | |
425 | vstorPacket->Flags = REQUEST_COMPLETION_FLAG; | |
426 | ||
427 | vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT; | |
428 | FILL_VMSTOR_REVISION(vstorPacket->Version.Revision); | |
429 | ||
430 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
431 | vstorPacket, | |
432 | sizeof(VSTOR_PACKET), | |
c4b0bc94 | 433 | (unsigned long)request, |
bef4a34a HJ |
434 | VmbusPacketTypeDataInBand, |
435 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
436 | if ( ret != 0) | |
437 | { | |
438 | DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION"); | |
439 | goto Cleanup; | |
440 | } | |
441 | ||
442 | WaitEventWait(request->WaitEvent); | |
443 | ||
454f18a9 | 444 | /* TODO: Check returned version */ |
bef4a34a HJ |
445 | if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0) |
446 | { | |
78cd2003 | 447 | DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status); |
bef4a34a HJ |
448 | goto Cleanup; |
449 | } | |
450 | ||
454f18a9 | 451 | /* Query channel properties */ |
bef4a34a HJ |
452 | DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION..."); |
453 | ||
454 | memset(vstorPacket, sizeof(VSTOR_PACKET), 0); | |
455 | vstorPacket->Operation = VStorOperationQueryProperties; | |
456 | vstorPacket->Flags = REQUEST_COMPLETION_FLAG; | |
457 | vstorPacket->StorageChannelProperties.PortNumber = storDevice->PortNumber; | |
458 | ||
459 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
460 | vstorPacket, | |
461 | sizeof(VSTOR_PACKET), | |
c4b0bc94 | 462 | (unsigned long)request, |
bef4a34a HJ |
463 | VmbusPacketTypeDataInBand, |
464 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
465 | ||
466 | if ( ret != 0) | |
467 | { | |
468 | DPRINT_ERR(STORVSC, "unable to send QUERY_PROPERTIES_OPERATION"); | |
469 | goto Cleanup; | |
470 | } | |
471 | ||
472 | WaitEventWait(request->WaitEvent); | |
473 | ||
454f18a9 | 474 | /* TODO: Check returned version */ |
bef4a34a HJ |
475 | if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0) |
476 | { | |
78cd2003 | 477 | DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status); |
bef4a34a HJ |
478 | goto Cleanup; |
479 | } | |
480 | ||
454f18a9 | 481 | /* storDevice->PortNumber = vstorPacket->StorageChannelProperties.PortNumber; */ |
bef4a34a HJ |
482 | storDevice->PathId = vstorPacket->StorageChannelProperties.PathId; |
483 | storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId; | |
484 | ||
78cd2003 | 485 | DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x", vstorPacket->StorageChannelProperties.Flags, vstorPacket->StorageChannelProperties.MaxTransferBytes); |
bef4a34a HJ |
486 | |
487 | DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION..."); | |
488 | ||
489 | memset(vstorPacket, sizeof(VSTOR_PACKET), 0); | |
490 | vstorPacket->Operation = VStorOperationEndInitialization; | |
491 | vstorPacket->Flags = REQUEST_COMPLETION_FLAG; | |
492 | ||
493 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
494 | vstorPacket, | |
495 | sizeof(VSTOR_PACKET), | |
c4b0bc94 | 496 | (unsigned long)request, |
bef4a34a HJ |
497 | VmbusPacketTypeDataInBand, |
498 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
499 | ||
500 | if ( ret != 0) | |
501 | { | |
502 | DPRINT_ERR(STORVSC, "unable to send END_INITIALIZATION_OPERATION"); | |
503 | goto Cleanup; | |
504 | } | |
505 | ||
506 | WaitEventWait(request->WaitEvent); | |
507 | ||
508 | if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0) | |
509 | { | |
78cd2003 | 510 | DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status); |
bef4a34a HJ |
511 | goto Cleanup; |
512 | } | |
513 | ||
514 | DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****"); | |
515 | ||
516 | Cleanup: | |
517 | if (request->WaitEvent) | |
518 | { | |
519 | WaitEventClose(request->WaitEvent); | |
520 | request->WaitEvent = NULL; | |
521 | } | |
522 | ||
523 | PutStorDevice(Device); | |
524 | ||
525 | DPRINT_EXIT(STORVSC); | |
526 | return ret; | |
527 | } | |
528 | ||
529 | ||
530 | int | |
531 | StorVscConnectToVsp( | |
3d3b5518 | 532 | struct hv_device *Device |
bef4a34a HJ |
533 | ) |
534 | { | |
535 | int ret=0; | |
536 | VMSTORAGE_CHANNEL_PROPERTIES props; | |
537 | ||
538 | STORVSC_DRIVER_OBJECT *storDriver = (STORVSC_DRIVER_OBJECT*) Device->Driver;; | |
539 | ||
540 | memset(&props, sizeof(VMSTORAGE_CHANNEL_PROPERTIES), 0); | |
541 | ||
454f18a9 | 542 | /* Open the channel */ |
bef4a34a HJ |
543 | ret = Device->Driver->VmbusChannelInterface.Open(Device, |
544 | storDriver->RingBufferSize, | |
545 | storDriver->RingBufferSize, | |
8282c400 | 546 | (void *)&props, |
bef4a34a HJ |
547 | sizeof(VMSTORAGE_CHANNEL_PROPERTIES), |
548 | StorVscOnChannelCallback, | |
549 | Device | |
550 | ); | |
551 | ||
78cd2003 | 552 | DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d", props.PathId, props.TargetId, props.MaxTransferBytes); |
bef4a34a HJ |
553 | |
554 | if (ret != 0) | |
555 | { | |
556 | DPRINT_ERR(STORVSC, "unable to open channel: %d", ret); | |
557 | return -1; | |
558 | } | |
559 | ||
560 | ret = StorVscChannelInit(Device); | |
561 | ||
562 | return ret; | |
563 | } | |
564 | ||
565 | ||
566 | /*++ | |
567 | ||
568 | Name: | |
569 | StorVscOnDeviceRemove() | |
570 | ||
571 | Description: | |
572 | Callback when the our device is being removed | |
573 | ||
574 | --*/ | |
575 | int | |
576 | StorVscOnDeviceRemove( | |
3d3b5518 | 577 | struct hv_device *Device |
bef4a34a HJ |
578 | ) |
579 | { | |
580 | STORVSC_DEVICE *storDevice; | |
581 | int ret=0; | |
582 | ||
583 | DPRINT_ENTER(STORVSC); | |
584 | ||
585 | DPRINT_INFO(STORVSC, "disabling storage device (%p)...", Device->Extension); | |
586 | ||
587 | storDevice = ReleaseStorDevice(Device); | |
588 | ||
454f18a9 BP |
589 | /* |
590 | * At this point, all outbound traffic should be disable. We | |
591 | * only allow inbound traffic (responses) to proceed so that | |
592 | * outstanding requests can be completed. | |
593 | */ | |
bef4a34a HJ |
594 | while (storDevice->NumOutstandingRequests) |
595 | { | |
596 | DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", storDevice->NumOutstandingRequests); | |
597 | ||
b4362c9c | 598 | udelay(100); |
bef4a34a HJ |
599 | } |
600 | ||
601 | DPRINT_INFO(STORVSC, "removing storage device (%p)...", Device->Extension); | |
602 | ||
603 | storDevice = FinalReleaseStorDevice(Device); | |
604 | ||
605 | DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice); | |
606 | ||
454f18a9 | 607 | /* Close the channel */ |
bef4a34a HJ |
608 | Device->Driver->VmbusChannelInterface.Close(Device); |
609 | ||
610 | FreeStorDevice(storDevice); | |
611 | ||
612 | DPRINT_EXIT(STORVSC); | |
613 | return ret; | |
614 | } | |
615 | ||
454f18a9 BP |
616 | /* *************** |
617 | static void | |
618 | StorVscOnTargetRescan( | |
619 | void *Context | |
620 | ) | |
621 | { | |
3d3b5518 | 622 | struct hv_device *device=(struct hv_device *)Context; |
454f18a9 | 623 | STORVSC_DRIVER_OBJECT *storDriver; |
bef4a34a | 624 | |
454f18a9 BP |
625 | DPRINT_ENTER(STORVSC); |
626 | ||
627 | storDriver = (STORVSC_DRIVER_OBJECT*) device->Driver; | |
628 | storDriver->OnHostRescan(device); | |
629 | ||
630 | DPRINT_EXIT(STORVSC); | |
631 | } | |
632 | *********** */ | |
bef4a34a HJ |
633 | |
634 | int | |
635 | StorVscOnHostReset( | |
3d3b5518 | 636 | struct hv_device *Device |
bef4a34a HJ |
637 | ) |
638 | { | |
639 | int ret=0; | |
640 | ||
641 | STORVSC_DEVICE *storDevice; | |
642 | STORVSC_REQUEST_EXTENSION *request; | |
643 | VSTOR_PACKET *vstorPacket; | |
644 | ||
645 | DPRINT_ENTER(STORVSC); | |
646 | ||
647 | DPRINT_INFO(STORVSC, "resetting host adapter..."); | |
648 | ||
649 | storDevice = GetStorDevice(Device); | |
650 | if (!storDevice) | |
651 | { | |
652 | DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?"); | |
653 | DPRINT_EXIT(STORVSC); | |
654 | return -1; | |
655 | } | |
656 | ||
657 | request = &storDevice->ResetRequest; | |
658 | vstorPacket = &request->VStorPacket; | |
659 | ||
660 | request->WaitEvent = WaitEventCreate(); | |
661 | ||
662 | vstorPacket->Operation = VStorOperationResetBus; | |
663 | vstorPacket->Flags = REQUEST_COMPLETION_FLAG; | |
664 | vstorPacket->VmSrb.PathId = storDevice->PathId; | |
665 | ||
666 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
667 | vstorPacket, | |
668 | sizeof(VSTOR_PACKET), | |
c4b0bc94 | 669 | (unsigned long)&storDevice->ResetRequest, |
bef4a34a HJ |
670 | VmbusPacketTypeDataInBand, |
671 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
672 | if (ret != 0) | |
673 | { | |
674 | DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d", vstorPacket, ret); | |
675 | goto Cleanup; | |
676 | } | |
677 | ||
454f18a9 | 678 | /* FIXME: Add a timeout */ |
bef4a34a HJ |
679 | WaitEventWait(request->WaitEvent); |
680 | ||
681 | WaitEventClose(request->WaitEvent); | |
682 | DPRINT_INFO(STORVSC, "host adapter reset completed"); | |
683 | ||
454f18a9 BP |
684 | /* |
685 | * At this point, all outstanding requests in the adapter | |
686 | * should have been flushed out and return to us | |
687 | */ | |
bef4a34a HJ |
688 | |
689 | Cleanup: | |
690 | PutStorDevice(Device); | |
691 | DPRINT_EXIT(STORVSC); | |
692 | return ret; | |
693 | } | |
694 | ||
695 | /*++ | |
696 | ||
697 | Name: | |
698 | StorVscOnIORequest() | |
699 | ||
700 | Description: | |
701 | Callback to initiate an I/O request | |
702 | ||
703 | --*/ | |
704 | int | |
705 | StorVscOnIORequest( | |
3d3b5518 | 706 | struct hv_device *Device, |
bef4a34a HJ |
707 | STORVSC_REQUEST *Request |
708 | ) | |
709 | { | |
710 | STORVSC_DEVICE *storDevice; | |
711 | STORVSC_REQUEST_EXTENSION* requestExtension = (STORVSC_REQUEST_EXTENSION*) Request->Extension; | |
712 | VSTOR_PACKET* vstorPacket =&requestExtension->VStorPacket; | |
713 | int ret=0; | |
714 | ||
715 | DPRINT_ENTER(STORVSC); | |
716 | ||
717 | storDevice = GetStorDevice(Device); | |
718 | ||
719 | DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, Extension %p", | |
720 | Device, storDevice, Request, requestExtension); | |
721 | ||
722 | DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d", | |
723 | Request, Request->DataBuffer.Length, Request->Bus, Request->TargetId, Request->LunId, Request->CdbLen); | |
724 | ||
725 | if (!storDevice) | |
726 | { | |
727 | DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?"); | |
728 | DPRINT_EXIT(STORVSC); | |
729 | return -2; | |
730 | } | |
731 | ||
04f50c4d | 732 | /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb, Request->CdbLen); */ |
bef4a34a HJ |
733 | |
734 | requestExtension->Request = Request; | |
735 | requestExtension->Device = Device; | |
736 | ||
737 | memset(vstorPacket, 0 , sizeof(VSTOR_PACKET)); | |
738 | ||
739 | vstorPacket->Flags |= REQUEST_COMPLETION_FLAG; | |
740 | ||
741 | vstorPacket->VmSrb.Length = sizeof(VMSCSI_REQUEST); | |
742 | ||
743 | vstorPacket->VmSrb.PortNumber = Request->Host; | |
744 | vstorPacket->VmSrb.PathId = Request->Bus; | |
745 | vstorPacket->VmSrb.TargetId = Request->TargetId; | |
746 | vstorPacket->VmSrb.Lun = Request->LunId; | |
747 | ||
748 | vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE; | |
749 | ||
454f18a9 | 750 | /* Copy over the scsi command descriptor block */ |
bef4a34a HJ |
751 | vstorPacket->VmSrb.CdbLength = Request->CdbLen; |
752 | memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen); | |
753 | ||
754 | vstorPacket->VmSrb.DataIn = Request->Type; | |
755 | vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length; | |
756 | ||
757 | vstorPacket->Operation = VStorOperationExecuteSRB; | |
758 | ||
759 | DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, lun %d senselen %d cdblen %d", | |
760 | vstorPacket->VmSrb.Length, | |
761 | vstorPacket->VmSrb.PortNumber, | |
762 | vstorPacket->VmSrb.PathId, | |
763 | vstorPacket->VmSrb.TargetId, | |
764 | vstorPacket->VmSrb.Lun, | |
765 | vstorPacket->VmSrb.SenseInfoLength, | |
766 | vstorPacket->VmSrb.CdbLength); | |
767 | ||
768 | if (requestExtension->Request->DataBuffer.Length) | |
769 | { | |
770 | ret = Device->Driver->VmbusChannelInterface.SendPacketMultiPageBuffer(Device, | |
771 | &requestExtension->Request->DataBuffer, | |
772 | vstorPacket, | |
773 | sizeof(VSTOR_PACKET), | |
c4b0bc94 | 774 | (unsigned long)requestExtension); |
bef4a34a HJ |
775 | } |
776 | else | |
777 | { | |
778 | ret = Device->Driver->VmbusChannelInterface.SendPacket(Device, | |
779 | vstorPacket, | |
780 | sizeof(VSTOR_PACKET), | |
c4b0bc94 | 781 | (unsigned long)requestExtension, |
bef4a34a HJ |
782 | VmbusPacketTypeDataInBand, |
783 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
784 | } | |
785 | ||
786 | if (ret != 0) | |
787 | { | |
788 | DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d", vstorPacket, ret); | |
789 | } | |
790 | ||
791 | InterlockedIncrement(&storDevice->NumOutstandingRequests); | |
792 | ||
793 | PutStorDevice(Device); | |
794 | ||
795 | DPRINT_EXIT(STORVSC); | |
796 | return ret; | |
797 | } | |
798 | ||
799 | /*++ | |
800 | ||
801 | Name: | |
802 | StorVscOnCleanup() | |
803 | ||
804 | Description: | |
805 | Perform any cleanup when the driver is removed | |
806 | ||
807 | --*/ | |
808 | void | |
809 | StorVscOnCleanup( | |
775ef25e | 810 | struct hv_driver *Driver |
bef4a34a HJ |
811 | ) |
812 | { | |
813 | DPRINT_ENTER(STORVSC); | |
814 | DPRINT_EXIT(STORVSC); | |
815 | } | |
816 | ||
817 | ||
818 | static void | |
819 | StorVscOnIOCompletion( | |
3d3b5518 | 820 | struct hv_device *Device, |
bef4a34a HJ |
821 | VSTOR_PACKET *VStorPacket, |
822 | STORVSC_REQUEST_EXTENSION *RequestExt | |
823 | ) | |
824 | { | |
825 | STORVSC_REQUEST *request; | |
826 | STORVSC_DEVICE *storDevice; | |
827 | ||
828 | DPRINT_ENTER(STORVSC); | |
829 | ||
830 | storDevice = MustGetStorDevice(Device); | |
831 | if (!storDevice) | |
832 | { | |
833 | DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?"); | |
834 | DPRINT_EXIT(STORVSC); | |
835 | return; | |
836 | } | |
837 | ||
78cd2003 | 838 | DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p completed bytes xfer %u", |
bef4a34a HJ |
839 | RequestExt, VStorPacket->VmSrb.DataTransferLength); |
840 | ||
841 | ASSERT(RequestExt != NULL); | |
842 | ASSERT(RequestExt->Request != NULL); | |
843 | ||
844 | request = RequestExt->Request; | |
845 | ||
846 | ASSERT(request->OnIOCompletion != NULL); | |
847 | ||
454f18a9 | 848 | /* Copy over the status...etc */ |
bef4a34a HJ |
849 | request->Status = VStorPacket->VmSrb.ScsiStatus; |
850 | ||
851 | if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1) | |
852 | { | |
853 | DPRINT_WARN(STORVSC, "cmd 0x%x scsi status 0x%x srb status 0x%x\n", | |
854 | request->Cdb[0], | |
855 | VStorPacket->VmSrb.ScsiStatus, | |
856 | VStorPacket->VmSrb.SrbStatus); | |
857 | } | |
858 | ||
454f18a9 | 859 | if ((request->Status & 0xFF) == 0x02) /* CHECK_CONDITION */ |
bef4a34a | 860 | { |
454f18a9 | 861 | if (VStorPacket->VmSrb.SrbStatus & 0x80) /* autosense data available */ |
bef4a34a HJ |
862 | { |
863 | DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data valid - len %d\n", | |
864 | RequestExt, VStorPacket->VmSrb.SenseInfoLength); | |
865 | ||
866 | ASSERT(VStorPacket->VmSrb.SenseInfoLength <= request->SenseBufferSize); | |
867 | memcpy(request->SenseBuffer, | |
868 | VStorPacket->VmSrb.SenseData, | |
869 | VStorPacket->VmSrb.SenseInfoLength); | |
870 | ||
871 | request->SenseBufferSize = VStorPacket->VmSrb.SenseInfoLength; | |
872 | } | |
873 | } | |
874 | ||
454f18a9 | 875 | /* TODO: */ |
bef4a34a HJ |
876 | request->BytesXfer = VStorPacket->VmSrb.DataTransferLength; |
877 | ||
878 | request->OnIOCompletion(request); | |
879 | ||
880 | InterlockedDecrement(&storDevice->NumOutstandingRequests); | |
881 | ||
882 | PutStorDevice(Device); | |
883 | ||
884 | DPRINT_EXIT(STORVSC); | |
885 | } | |
886 | ||
887 | ||
888 | static void | |
889 | StorVscOnReceive( | |
3d3b5518 | 890 | struct hv_device *Device, |
bef4a34a HJ |
891 | VSTOR_PACKET *VStorPacket, |
892 | STORVSC_REQUEST_EXTENSION *RequestExt | |
893 | ) | |
894 | { | |
895 | switch(VStorPacket->Operation) | |
896 | { | |
897 | case VStorOperationCompleteIo: | |
898 | ||
899 | DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION"); | |
900 | StorVscOnIOCompletion(Device, VStorPacket, RequestExt); | |
901 | break; | |
902 | ||
454f18a9 | 903 | /* case ENUMERATE_DEVICE_OPERATION: */ |
bef4a34a | 904 | |
454f18a9 | 905 | /* DPRINT_INFO(STORVSC, "ENUMERATE_DEVICE_OPERATION"); */ |
bef4a34a | 906 | |
454f18a9 BP |
907 | /* StorVscOnTargetRescan(Device); */ |
908 | /* break; */ | |
bef4a34a | 909 | |
454f18a9 | 910 | case VStorOperationRemoveDevice: |
bef4a34a HJ |
911 | |
912 | DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION"); | |
454f18a9 | 913 | /* TODO: */ |
bef4a34a HJ |
914 | break; |
915 | ||
916 | default: | |
917 | DPRINT_INFO(STORVSC, "Unknown operation received - %d", VStorPacket->Operation); | |
918 | break; | |
919 | } | |
920 | } | |
921 | ||
922 | void | |
923 | StorVscOnChannelCallback( | |
8282c400 | 924 | void * Context |
bef4a34a HJ |
925 | ) |
926 | { | |
927 | int ret=0; | |
3d3b5518 | 928 | struct hv_device *device = (struct hv_device*)Context; |
bef4a34a | 929 | STORVSC_DEVICE *storDevice; |
4d643114 | 930 | u32 bytesRecvd; |
59471438 GKH |
931 | u64 requestId; |
932 | unsigned char packet[ALIGN_UP(sizeof(VSTOR_PACKET),8)]; | |
bef4a34a HJ |
933 | STORVSC_REQUEST_EXTENSION *request; |
934 | ||
935 | DPRINT_ENTER(STORVSC); | |
936 | ||
937 | ASSERT(device); | |
938 | ||
939 | storDevice = MustGetStorDevice(device); | |
940 | if (!storDevice) | |
941 | { | |
942 | DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?"); | |
943 | DPRINT_EXIT(STORVSC); | |
944 | return; | |
945 | } | |
946 | ||
947 | do | |
948 | { | |
949 | ret = device->Driver->VmbusChannelInterface.RecvPacket(device, | |
950 | packet, | |
951 | ALIGN_UP(sizeof(VSTOR_PACKET),8), | |
952 | &bytesRecvd, | |
953 | &requestId); | |
954 | if (ret == 0 && bytesRecvd > 0) | |
955 | { | |
956 | DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx", bytesRecvd, requestId); | |
957 | ||
454f18a9 | 958 | /* ASSERT(bytesRecvd == sizeof(VSTOR_PACKET)); */ |
bef4a34a | 959 | |
c4b0bc94 | 960 | request = (STORVSC_REQUEST_EXTENSION*)(unsigned long)requestId; |
bef4a34a HJ |
961 | ASSERT(request); |
962 | ||
454f18a9 | 963 | /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */ |
bef4a34a HJ |
964 | if ((request == &storDevice->InitRequest) || (request == &storDevice->ResetRequest)) |
965 | { | |
454f18a9 | 966 | /* DPRINT_INFO(STORVSC, "reset completion - operation %u status %u", vstorPacket.Operation, vstorPacket.Status); */ |
bef4a34a HJ |
967 | |
968 | memcpy(&request->VStorPacket, packet, sizeof(VSTOR_PACKET)); | |
969 | ||
970 | WaitEventSet(request->WaitEvent); | |
971 | } | |
972 | else | |
973 | { | |
974 | StorVscOnReceive(device, (VSTOR_PACKET*)packet, request); | |
975 | } | |
976 | } | |
977 | else | |
978 | { | |
454f18a9 | 979 | /* DPRINT_DBG(STORVSC, "nothing else to read..."); */ |
bef4a34a HJ |
980 | break; |
981 | } | |
982 | } while (1); | |
983 | ||
984 | PutStorDevice(device); | |
985 | ||
986 | DPRINT_EXIT(STORVSC); | |
987 | return; | |
988 | } |