]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/hv/StorVsc.c
staging: hv: Remove Ringbuffer from TODO line
[net-next-2.6.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"
731a7884 28#include "StorVscApi.h"
216260d8 29#include "VmbusPacketFormat.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{
068c5df2 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;
bef4a34a
HJ
140 ASSERT(storDevice);
141
f4888417
BP
142 atomic_dec(&storDevice->RefCount);
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;
bef4a34a
HJ
152 ASSERT(storDevice);
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;
bef4a34a
HJ
168 ASSERT(storDevice);
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();
bef4a34a
HJ
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,
068c5df2
GKH
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");
bef4a34a
HJ
221 goto Cleanup;
222 }
223
bfc30aae 224 osd_WaitEventWait(request->WaitEvent);
bef4a34a 225
068c5df2
GKH
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);
bef4a34a
HJ
231 goto Cleanup;
232 }
233
234 DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
235
454f18a9 236 /* reuse the packet for version range supported */
8c960e49 237 memset(vstorPacket, 0, sizeof(struct vstor_packet));
bef4a34a
HJ
238 vstorPacket->Operation = VStorOperationQueryProtocolVersion;
239 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
240
068c5df2
GKH
241 vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT;
242 FILL_VMSTOR_REVISION(vstorPacket->Version.Revision);
bef4a34a
HJ
243
244 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
068c5df2
GKH
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");
bef4a34a
HJ
253 goto Cleanup;
254 }
255
bfc30aae 256 osd_WaitEventWait(request->WaitEvent);
bef4a34a 257
454f18a9 258 /* TODO: Check returned version */
068c5df2
GKH
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);
bef4a34a
HJ
264 goto Cleanup;
265 }
266
454f18a9 267 /* Query channel properties */
bef4a34a
HJ
268 DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
269
8c960e49 270 memset(vstorPacket, 0, sizeof(struct vstor_packet));
068c5df2 271 vstorPacket->Operation = VStorOperationQueryProperties;
bef4a34a 272 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
068c5df2
GKH
273 vstorPacket->StorageChannelProperties.PortNumber =
274 storDevice->PortNumber;
bef4a34a
HJ
275
276 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
068c5df2
GKH
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");
bef4a34a
HJ
286 goto Cleanup;
287 }
288
bfc30aae 289 osd_WaitEventWait(request->WaitEvent);
bef4a34a 290
454f18a9 291 /* TODO: Check returned version */
068c5df2
GKH
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);
bef4a34a
HJ
297 goto Cleanup;
298 }
299
bef4a34a
HJ
300 storDevice->PathId = vstorPacket->StorageChannelProperties.PathId;
301 storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId;
302
068c5df2
GKH
303 DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x",
304 vstorPacket->StorageChannelProperties.Flags,
305 vstorPacket->StorageChannelProperties.MaxTransferBytes);
bef4a34a
HJ
306
307 DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
308
8c960e49 309 memset(vstorPacket, 0, sizeof(struct vstor_packet));
068c5df2 310 vstorPacket->Operation = VStorOperationEndInitialization;
bef4a34a
HJ
311 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
312
313 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
068c5df2
GKH
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");
bef4a34a
HJ
323 goto Cleanup;
324 }
325
bfc30aae 326 osd_WaitEventWait(request->WaitEvent);
bef4a34a 327
068c5df2
GKH
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);
bef4a34a
HJ
333 goto Cleanup;
334 }
335
336 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
337
338Cleanup:
068c5df2
GKH
339 kfree(request->WaitEvent);
340 request->WaitEvent = NULL;
bef4a34a
HJ
341
342 PutStorDevice(Device);
343
344 DPRINT_EXIT(STORVSC);
345 return ret;
346}
347
163680b4
GKH
348static 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
417static 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
438static 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
068c5df2 505static int StorVscConnectToVsp(struct hv_device *Device)
bef4a34a 506{
b3715ee4 507 struct vmstorage_channel_properties props;
068c5df2
GKH
508 struct storvsc_driver_object *storDriver;
509 int ret;
bef4a34a 510
068c5df2 511 storDriver = (struct storvsc_driver_object *)Device->Driver;
8c960e49 512 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
bef4a34a 513
454f18a9 514 /* Open the channel */
bef4a34a 515 ret = Device->Driver->VmbusChannelInterface.Open(Device,
068c5df2
GKH
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) {
bef4a34a
HJ
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
163680b4
GKH
536/**
537 * StorVscOnDeviceAdd - Callback when the device belonging to this driver is added
538 */
539static 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
582Cleanup:
583 DPRINT_EXIT(STORVSC);
584
585 return ret;
586}
bef4a34a 587
068c5df2
GKH
588/**
589 * StorVscOnDeviceRemove - Callback when the our device is being removed
590 */
591static int StorVscOnDeviceRemove(struct hv_device *Device)
bef4a34a 592{
7dd03fc4 593 struct storvsc_device *storDevice;
bef4a34a
HJ
594
595 DPRINT_ENTER(STORVSC);
596
068c5df2
GKH
597 DPRINT_INFO(STORVSC, "disabling storage device (%p)...",
598 Device->Extension);
bef4a34a
HJ
599
600 storDevice = ReleaseStorDevice(Device);
601
454f18a9
BP
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 */
068c5df2
GKH
607 while (atomic_read(&storDevice->NumOutstandingRequests)) {
608 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
609 atomic_read(&storDevice->NumOutstandingRequests));
b4362c9c 610 udelay(100);
bef4a34a
HJ
611 }
612
068c5df2
GKH
613 DPRINT_INFO(STORVSC, "removing storage device (%p)...",
614 Device->Extension);
bef4a34a
HJ
615
616 storDevice = FinalReleaseStorDevice(Device);
617
618 DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
619
454f18a9 620 /* Close the channel */
bef4a34a
HJ
621 Device->Driver->VmbusChannelInterface.Close(Device);
622
623 FreeStorDevice(storDevice);
624
625 DPRINT_EXIT(STORVSC);
068c5df2 626 return 0;
454f18a9 627}
bef4a34a 628
354b0a64 629int StorVscOnHostReset(struct hv_device *Device)
bef4a34a 630{
7dd03fc4
GKH
631 struct storvsc_device *storDevice;
632 struct storvsc_request_extension *request;
b3715ee4 633 struct vstor_packet *vstorPacket;
068c5df2 634 int ret;
bef4a34a
HJ
635
636 DPRINT_ENTER(STORVSC);
637
638 DPRINT_INFO(STORVSC, "resetting host adapter...");
639
640 storDevice = GetStorDevice(Device);
068c5df2
GKH
641 if (!storDevice) {
642 DPRINT_ERR(STORVSC, "unable to get stor device..."
643 "device being destroyed?");
bef4a34a
HJ
644 DPRINT_EXIT(STORVSC);
645 return -1;
646 }
647
648 request = &storDevice->ResetRequest;
649 vstorPacket = &request->VStorPacket;
650
bfc30aae 651 request->WaitEvent = osd_WaitEventCreate();
bef4a34a 652
068c5df2
GKH
653 vstorPacket->Operation = VStorOperationResetBus;
654 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
655 vstorPacket->VmSrb.PathId = storDevice->PathId;
bef4a34a
HJ
656
657 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
068c5df2
GKH
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);
bef4a34a
HJ
666 goto Cleanup;
667 }
668
454f18a9 669 /* FIXME: Add a timeout */
bfc30aae 670 osd_WaitEventWait(request->WaitEvent);
bef4a34a 671
420beac4 672 kfree(request->WaitEvent);
bef4a34a
HJ
673 DPRINT_INFO(STORVSC, "host adapter reset completed");
674
454f18a9
BP
675 /*
676 * At this point, all outstanding requests in the adapter
677 * should have been flushed out and return to us
678 */
bef4a34a
HJ
679
680Cleanup:
681 PutStorDevice(Device);
682 DPRINT_EXIT(STORVSC);
683 return ret;
684}
685
068c5df2
GKH
686/**
687 * StorVscOnIORequest - Callback to initiate an I/O request
688 */
689static int StorVscOnIORequest(struct hv_device *Device,
690 struct hv_storvsc_request *Request)
bef4a34a 691{
7dd03fc4 692 struct storvsc_device *storDevice;
068c5df2
GKH
693 struct storvsc_request_extension *requestExtension;
694 struct vstor_packet *vstorPacket;
695 int ret = 0;
bef4a34a
HJ
696
697 DPRINT_ENTER(STORVSC);
698
068c5df2
GKH
699 requestExtension =
700 (struct storvsc_request_extension *)Request->Extension;
701 vstorPacket = &requestExtension->VStorPacket;
bef4a34a
HJ
702 storDevice = GetStorDevice(Device);
703
068c5df2
GKH
704 DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, "
705 "Extension %p", Device, storDevice, Request,
706 requestExtension);
bef4a34a
HJ
707
708 DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
068c5df2
GKH
709 Request, Request->DataBuffer.Length, Request->Bus,
710 Request->TargetId, Request->LunId, Request->CdbLen);
bef4a34a 711
068c5df2
GKH
712 if (!storDevice) {
713 DPRINT_ERR(STORVSC, "unable to get stor device..."
714 "device being destroyed?");
bef4a34a
HJ
715 DPRINT_EXIT(STORVSC);
716 return -2;
717 }
718
068c5df2
GKH
719 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb,
720 * Request->CdbLen); */
bef4a34a
HJ
721
722 requestExtension->Request = Request;
723 requestExtension->Device = Device;
724
b3715ee4 725 memset(vstorPacket, 0 , sizeof(struct vstor_packet));
bef4a34a
HJ
726
727 vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
728
068c5df2 729 vstorPacket->VmSrb.Length = sizeof(struct vmscsi_request);
bef4a34a
HJ
730
731 vstorPacket->VmSrb.PortNumber = Request->Host;
068c5df2
GKH
732 vstorPacket->VmSrb.PathId = Request->Bus;
733 vstorPacket->VmSrb.TargetId = Request->TargetId;
734 vstorPacket->VmSrb.Lun = Request->LunId;
bef4a34a
HJ
735
736 vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
737
454f18a9 738 /* Copy over the scsi command descriptor block */
068c5df2 739 vstorPacket->VmSrb.CdbLength = Request->CdbLen;
bef4a34a
HJ
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
068c5df2
GKH
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,
bef4a34a
HJ
760 &requestExtension->Request->DataBuffer,
761 vstorPacket,
b3715ee4 762 sizeof(struct vstor_packet),
c4b0bc94 763 (unsigned long)requestExtension);
068c5df2 764 } else {
bef4a34a 765 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
068c5df2
GKH
766 vstorPacket,
767 sizeof(struct vstor_packet),
768 (unsigned long)requestExtension,
769 VmbusPacketTypeDataInBand,
770 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
bef4a34a
HJ
771 }
772
068c5df2
GKH
773 if (ret != 0) {
774 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d",
775 vstorPacket, ret);
bef4a34a
HJ
776 }
777
f4888417 778 atomic_inc(&storDevice->NumOutstandingRequests);
bef4a34a
HJ
779
780 PutStorDevice(Device);
781
782 DPRINT_EXIT(STORVSC);
783 return ret;
784}
785
068c5df2
GKH
786/**
787 * StorVscOnCleanup - Perform any cleanup when the driver is removed
788 */
789static void StorVscOnCleanup(struct hv_driver *Driver)
bef4a34a
HJ
790{
791 DPRINT_ENTER(STORVSC);
792 DPRINT_EXIT(STORVSC);
793}
794
163680b4
GKH
795/**
796 * StorVscInitialize - Main entry point
797 */
798int StorVscInitialize(struct hv_driver *Driver)
bef4a34a 799{
163680b4 800 struct storvsc_driver_object *storDriver;
bef4a34a
HJ
801
802 DPRINT_ENTER(STORVSC);
803
163680b4 804 storDriver = (struct storvsc_driver_object *)Driver;
bef4a34a 805
163680b4
GKH
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));
bef4a34a 814
163680b4
GKH
815 /* Make sure we are at least 2 pages since 1 page is used for control */
816 ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1));
bef4a34a 817
163680b4
GKH
818 Driver->name = gDriverName;
819 memcpy(&Driver->deviceType, &gStorVscDeviceType,
820 sizeof(struct hv_guid));
bef4a34a 821
163680b4 822 storDriver->RequestExtSize = sizeof(struct storvsc_request_extension);
bef4a34a 823
163680b4
GKH
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)));
bef4a34a 835
163680b4
GKH
836 DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
837 storDriver->MaxOutstandingRequestsPerChannel,
838 STORVSC_MAX_IO_REQUESTS);
bef4a34a 839
163680b4
GKH
840 /* Setup the dispatch table */
841 storDriver->Base.OnDeviceAdd = StorVscOnDeviceAdd;
842 storDriver->Base.OnDeviceRemove = StorVscOnDeviceRemove;
843 storDriver->Base.OnCleanup = StorVscOnCleanup;
bef4a34a 844
163680b4 845 storDriver->OnIORequest = StorVscOnIORequest;
bef4a34a
HJ
846
847 DPRINT_EXIT(STORVSC);
bef4a34a 848
163680b4 849 return 0;
bef4a34a 850}