Staging: hv: TODO: add some more items
[deliverable/linux.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>
4983b39a 28#include "osd.h"
645954c5 29#include "logging.h"
731a7884 30#include "StorVscApi.h"
216260d8 31#include "VmbusPacketFormat.h"
2dd88b51 32#include "vstorage.h"
bef4a34a
HJ
33
34
bef4a34a 35
454f18a9
BP
36/* #defines */
37
38
39
40/* Data types */
7dd03fc4 41struct storvsc_request_extension {
454f18a9 42 /* LIST_ENTRY ListEntry; */
bef4a34a 43
0b3f6834 44 struct hv_storvsc_request *Request;
3d3b5518 45 struct hv_device *Device;
bef4a34a 46
454f18a9 47 /* Synchronize the request/response if needed */
aedb444a 48 struct osd_waitevent *WaitEvent;
bef4a34a 49
b3715ee4 50 struct vstor_packet VStorPacket;
7dd03fc4 51};
bef4a34a
HJ
52
53
454f18a9 54/* A storvsc device is a device object that contains a vmbus channel */
7dd03fc4 55struct storvsc_device {
3d3b5518 56 struct hv_device *Device;
bef4a34a 57
f4888417 58 atomic_t RefCount; /* 0 indicates the device is being destroyed */
bef4a34a 59
f4888417 60 atomic_t NumOutstandingRequests;
bef4a34a 61
454f18a9
BP
62 /*
63 * Each unique Port/Path/Target represents 1 channel ie scsi
64 * controller. In reality, the pathid, targetid is always 0
65 * and the port is set by us
66 */
3abcbc45 67 unsigned int PortNumber;
59471438
GKH
68 unsigned char PathId;
69 unsigned char TargetId;
bef4a34a 70
454f18a9
BP
71 /* LIST_ENTRY OutstandingRequestList; */
72 /* HANDLE OutstandingRequestLock; */
bef4a34a 73
454f18a9 74 /* Used for vsc/vsp channel reset process */
7dd03fc4
GKH
75 struct storvsc_request_extension InitRequest;
76 struct storvsc_request_extension ResetRequest;
bef4a34a 77
7dd03fc4 78};
bef4a34a
HJ
79
80
454f18a9
BP
81
82/* Globals */
83
bef4a34a
HJ
84static const char* gDriverName="storvsc";
85
454f18a9 86/* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
caf26a31
GKH
87static const struct hv_guid gStorVscDeviceType = {
88 .data = {
89 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
90 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
91 }
bef4a34a
HJ
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,
b3715ee4 132 struct vstor_packet *VStorPacket,
7dd03fc4 133 struct storvsc_request_extension *RequestExt
bef4a34a
HJ
134 );
135
136static void
137StorVscOnReceive(
3d3b5518 138 struct hv_device *Device,
b3715ee4 139 struct vstor_packet *VStorPacket,
7dd03fc4 140 struct storvsc_request_extension *RequestExt
bef4a34a
HJ
141 );
142
143static int
144StorVscConnectToVsp(
3d3b5518 145 struct hv_device *Device
bef4a34a
HJ
146 );
147
7dd03fc4 148static inline struct storvsc_device *AllocStorDevice(struct hv_device *Device)
bef4a34a 149{
7dd03fc4 150 struct storvsc_device *storDevice;
bef4a34a 151
7dd03fc4 152 storDevice = kzalloc(sizeof(struct 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
7dd03fc4 166static inline void FreeStorDevice(struct storvsc_device *Device)
bef4a34a 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 */
7dd03fc4 173static inline struct storvsc_device* GetStorDevice(struct hv_device *Device)
bef4a34a 174{
7dd03fc4 175 struct storvsc_device *storDevice;
bef4a34a 176
7dd03fc4 177 storDevice = (struct 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 */
7dd03fc4 187static inline struct storvsc_device *MustGetStorDevice(struct hv_device *Device)
bef4a34a 188{
7dd03fc4 189 struct storvsc_device *storDevice;
bef4a34a 190
7dd03fc4 191 storDevice = (struct 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 201{
7dd03fc4 202 struct storvsc_device *storDevice;
bef4a34a 203
7dd03fc4 204 storDevice = (struct storvsc_device *)Device->Extension;
bef4a34a
HJ
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() */
7dd03fc4 212static inline struct storvsc_device *ReleaseStorDevice(struct hv_device *Device)
bef4a34a 213{
7dd03fc4 214 struct storvsc_device *storDevice;
bef4a34a 215
7dd03fc4 216 storDevice = (struct storvsc_device *)Device->Extension;
bef4a34a
HJ
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. */
7dd03fc4 229static inline struct storvsc_device *FinalReleaseStorDevice(struct hv_device *Device)
bef4a34a 230{
7dd03fc4 231 struct storvsc_device *storDevice;
bef4a34a 232
7dd03fc4 233 storDevice = (struct storvsc_device *)Device->Extension;
bef4a34a
HJ
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{
9f0c7d2c 261 struct storvsc_driver_object *storDriver = (struct storvsc_driver_object *)Driver;
bef4a34a
HJ
262 int ret=0;
263
264 DPRINT_ENTER(STORVSC);
265
7dd03fc4
GKH
266 DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd sizeof(struct storvsc_request_extension)=%zd sizeof(struct vstor_packet)=%zd, sizeof(struct vmscsi_request)=%zd",
267 sizeof(struct hv_storvsc_request), sizeof(struct storvsc_request_extension), sizeof(struct vstor_packet), sizeof(struct 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;
caf26a31 273 memcpy(&Driver->deviceType, &gStorVscDeviceType, sizeof(struct hv_guid));
bef4a34a 274
7dd03fc4 275 storDriver->RequestExtSize = sizeof(struct storvsc_request_extension);
bef4a34a 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
b3715ee4 281 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + struct vstor_packet + u64)
454f18a9 282 */
bef4a34a 283 storDriver->MaxOutstandingRequestsPerChannel =
b3715ee4 284 ((storDriver->RingBufferSize - PAGE_SIZE) / ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET + sizeof(struct 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;
7dd03fc4 317 struct storvsc_device *storDevice;
b3715ee4 318 /* struct vmstorage_channel_properties *props; */
9f0c7d2c 319 struct storvsc_device_info *deviceInfo = (struct storvsc_device_info *)AdditionalInfo;
bef4a34a
HJ
320
321 DPRINT_ENTER(STORVSC);
322
323 storDevice = AllocStorDevice(Device);
324 if (!storDevice)
325 {
326 ret = -1;
327 goto Cleanup;
328 }
329
454f18a9 330 /* Save the channel properties to our storvsc channel */
b3715ee4 331 /* props = (struct vmstorage_channel_properties *) channel->offerMsg.Offer.u.Standard.UserDefined; */
454f18a9
BP
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;
7dd03fc4
GKH
363 struct storvsc_device *storDevice;
364 struct storvsc_request_extension *request;
b3715ee4 365 struct vstor_packet *vstorPacket;
bef4a34a
HJ
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 379
7dd03fc4 380 memset(request, sizeof(struct storvsc_request_extension), 0);
bfc30aae 381 request->WaitEvent = osd_WaitEventCreate();
bef4a34a
HJ
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,
b3715ee4 394 sizeof(struct 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
bfc30aae 404 osd_WaitEventWait(request->WaitEvent);
bef4a34a
HJ
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 */
b3715ee4 415 memset(vstorPacket, sizeof(struct vstor_packet), 0);
bef4a34a
HJ
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,
b3715ee4 424 sizeof(struct 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
bfc30aae 434 osd_WaitEventWait(request->WaitEvent);
bef4a34a 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
b3715ee4 446 memset(vstorPacket, sizeof(struct vstor_packet), 0);
bef4a34a
HJ
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,
b3715ee4 453 sizeof(struct 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
bfc30aae 464 osd_WaitEventWait(request->WaitEvent);
bef4a34a 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
b3715ee4 481 memset(vstorPacket, sizeof(struct vstor_packet), 0);
bef4a34a
HJ
482 vstorPacket->Operation = VStorOperationEndInitialization;
483 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
484
485 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
486 vstorPacket,
b3715ee4 487 sizeof(struct 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
bfc30aae 498 osd_WaitEventWait(request->WaitEvent);
bef4a34a
HJ
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;
b3715ee4 528 struct vmstorage_channel_properties props;
bef4a34a 529
9f0c7d2c 530 struct storvsc_driver_object *storDriver = (struct storvsc_driver_object *)Device->Driver;;
bef4a34a 531
b3715ee4 532 memset(&props, sizeof(struct vmstorage_channel_properties), 0);
bef4a34a 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,
b3715ee4 539 sizeof(struct vmstorage_channel_properties),
bef4a34a
HJ
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{
7dd03fc4 572 struct storvsc_device *storDevice;
bef4a34a
HJ
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;
9f0c7d2c 615struct storvsc_driver_object *storDriver;
bef4a34a 616
454f18a9
BP
617DPRINT_ENTER(STORVSC);
618
9f0c7d2c 619storDriver = (struct storvsc_driver_object*) device->Driver;
454f18a9
BP
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
7dd03fc4
GKH
633 struct storvsc_device *storDevice;
634 struct storvsc_request_extension *request;
b3715ee4 635 struct vstor_packet *vstorPacket;
bef4a34a
HJ
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
bfc30aae 652 request->WaitEvent = osd_WaitEventCreate();
bef4a34a
HJ
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,
b3715ee4 660 sizeof(struct 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 */
bfc30aae 671 osd_WaitEventWait(request->WaitEvent);
bef4a34a 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{
7dd03fc4
GKH
702 struct storvsc_device *storDevice;
703 struct storvsc_request_extension* requestExtension = (struct storvsc_request_extension*) Request->Extension;
b3715ee4 704 struct vstor_packet *vstorPacket =&requestExtension->VStorPacket;
bef4a34a
HJ
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
b3715ee4 729 memset(vstorPacket, 0 , sizeof(struct vstor_packet));
bef4a34a
HJ
730
731 vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
732
b3715ee4 733 vstorPacket->VmSrb.Length = sizeof(struct vmscsi_request);
bef4a34a
HJ
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,
b3715ee4 765 sizeof(struct vstor_packet),
c4b0bc94 766 (unsigned long)requestExtension);
bef4a34a
HJ
767 }
768 else
769 {
770 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
771 vstorPacket,
b3715ee4 772 sizeof(struct 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,
b3715ee4 813 struct vstor_packet *VStorPacket,
7dd03fc4 814 struct storvsc_request_extension *RequestExt
bef4a34a
HJ
815 )
816{
0b3f6834 817 struct hv_storvsc_request *request;
7dd03fc4 818 struct storvsc_device *storDevice;
bef4a34a
HJ
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,
b3715ee4 883 struct vstor_packet *VStorPacket,
7dd03fc4 884 struct storvsc_request_extension *RequestExt
bef4a34a
HJ
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;
7dd03fc4 921 struct storvsc_device *storDevice;
4d643114 922 u32 bytesRecvd;
59471438 923 u64 requestId;
b3715ee4 924 unsigned char packet[ALIGN_UP(sizeof(struct vstor_packet),8)];
7dd03fc4 925 struct storvsc_request_extension *request;
bef4a34a
HJ
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,
b3715ee4 943 ALIGN_UP(sizeof(struct vstor_packet),8),
bef4a34a
HJ
944 &bytesRecvd,
945 &requestId);
946 if (ret == 0 && bytesRecvd > 0)
947 {
948 DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx", bytesRecvd, requestId);
949
b3715ee4 950 /* ASSERT(bytesRecvd == sizeof(struct vstor_packet)); */
bef4a34a 951
7dd03fc4 952 request = (struct 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 959
b3715ee4 960 memcpy(&request->VStorPacket, packet, sizeof(struct vstor_packet));
bef4a34a 961
bfc30aae 962 osd_WaitEventSet(request->WaitEvent);
bef4a34a
HJ
963 }
964 else
965 {
b3715ee4 966 StorVscOnReceive(device, (struct vstor_packet *)packet, request);
bef4a34a
HJ
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}
This page took 0.088649 seconds and 5 git commands to generate.