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