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