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