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