]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/hv/storvsc.c
staging: octeon: '&pointer[0]' to 'pointer' fix
[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"
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 return -1;
190 }
191
192 request = &storDevice->InitRequest;
193 vstorPacket = &request->VStorPacket;
194
068c5df2
GKH
195 /*
196 * Now, initiate the vsc/vsp initialization protocol on the open
197 * channel
198 */
8c960e49 199 memset(request, 0, sizeof(struct storvsc_request_extension));
bfc30aae 200 request->WaitEvent = osd_WaitEventCreate();
80d11b2a
BP
201 if (!request->WaitEvent) {
202 ret = -ENOMEM;
203 goto nomem;
204 }
bef4a34a
HJ
205
206 vstorPacket->Operation = VStorOperationBeginInitialization;
207 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
208
209 /*SpinlockAcquire(gDriverExt.packetListLock);
210 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
211 SpinlockRelease(gDriverExt.packetListLock);*/
212
213 DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
214
215 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
068c5df2
GKH
216 vstorPacket,
217 sizeof(struct vstor_packet),
218 (unsigned long)request,
219 VmbusPacketTypeDataInBand,
220 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
221 if (ret != 0) {
222 DPRINT_ERR(STORVSC,
223 "unable to send BEGIN_INITIALIZATION_OPERATION");
bef4a34a
HJ
224 goto Cleanup;
225 }
226
bfc30aae 227 osd_WaitEventWait(request->WaitEvent);
bef4a34a 228
068c5df2
GKH
229 if (vstorPacket->Operation != VStorOperationCompleteIo ||
230 vstorPacket->Status != 0) {
231 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed "
232 "(op %d status 0x%x)",
233 vstorPacket->Operation, vstorPacket->Status);
bef4a34a
HJ
234 goto Cleanup;
235 }
236
237 DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
238
454f18a9 239 /* reuse the packet for version range supported */
8c960e49 240 memset(vstorPacket, 0, sizeof(struct vstor_packet));
bef4a34a
HJ
241 vstorPacket->Operation = VStorOperationQueryProtocolVersion;
242 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
243
068c5df2
GKH
244 vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT;
245 FILL_VMSTOR_REVISION(vstorPacket->Version.Revision);
bef4a34a
HJ
246
247 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
068c5df2
GKH
248 vstorPacket,
249 sizeof(struct vstor_packet),
250 (unsigned long)request,
251 VmbusPacketTypeDataInBand,
252 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
253 if (ret != 0) {
254 DPRINT_ERR(STORVSC,
255 "unable to send BEGIN_INITIALIZATION_OPERATION");
bef4a34a
HJ
256 goto Cleanup;
257 }
258
bfc30aae 259 osd_WaitEventWait(request->WaitEvent);
bef4a34a 260
454f18a9 261 /* TODO: Check returned version */
068c5df2
GKH
262 if (vstorPacket->Operation != VStorOperationCompleteIo ||
263 vstorPacket->Status != 0) {
264 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed "
265 "(op %d status 0x%x)",
266 vstorPacket->Operation, vstorPacket->Status);
bef4a34a
HJ
267 goto Cleanup;
268 }
269
454f18a9 270 /* Query channel properties */
bef4a34a
HJ
271 DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
272
8c960e49 273 memset(vstorPacket, 0, sizeof(struct vstor_packet));
068c5df2 274 vstorPacket->Operation = VStorOperationQueryProperties;
bef4a34a 275 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
068c5df2
GKH
276 vstorPacket->StorageChannelProperties.PortNumber =
277 storDevice->PortNumber;
bef4a34a
HJ
278
279 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
068c5df2
GKH
280 vstorPacket,
281 sizeof(struct vstor_packet),
282 (unsigned long)request,
283 VmbusPacketTypeDataInBand,
284 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
285
286 if (ret != 0) {
287 DPRINT_ERR(STORVSC,
288 "unable to send QUERY_PROPERTIES_OPERATION");
bef4a34a
HJ
289 goto Cleanup;
290 }
291
bfc30aae 292 osd_WaitEventWait(request->WaitEvent);
bef4a34a 293
454f18a9 294 /* TODO: Check returned version */
068c5df2
GKH
295 if (vstorPacket->Operation != VStorOperationCompleteIo ||
296 vstorPacket->Status != 0) {
297 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed "
298 "(op %d status 0x%x)",
299 vstorPacket->Operation, vstorPacket->Status);
bef4a34a
HJ
300 goto Cleanup;
301 }
302
bef4a34a
HJ
303 storDevice->PathId = vstorPacket->StorageChannelProperties.PathId;
304 storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId;
305
068c5df2
GKH
306 DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x",
307 vstorPacket->StorageChannelProperties.Flags,
308 vstorPacket->StorageChannelProperties.MaxTransferBytes);
bef4a34a
HJ
309
310 DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
311
8c960e49 312 memset(vstorPacket, 0, sizeof(struct vstor_packet));
068c5df2 313 vstorPacket->Operation = VStorOperationEndInitialization;
bef4a34a
HJ
314 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
315
316 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
068c5df2
GKH
317 vstorPacket,
318 sizeof(struct vstor_packet),
319 (unsigned long)request,
320 VmbusPacketTypeDataInBand,
321 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
322
323 if (ret != 0) {
324 DPRINT_ERR(STORVSC,
325 "unable to send END_INITIALIZATION_OPERATION");
bef4a34a
HJ
326 goto Cleanup;
327 }
328
bfc30aae 329 osd_WaitEventWait(request->WaitEvent);
bef4a34a 330
068c5df2
GKH
331 if (vstorPacket->Operation != VStorOperationCompleteIo ||
332 vstorPacket->Status != 0) {
333 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed "
334 "(op %d status 0x%x)",
335 vstorPacket->Operation, vstorPacket->Status);
bef4a34a
HJ
336 goto Cleanup;
337 }
338
339 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
340
341Cleanup:
068c5df2
GKH
342 kfree(request->WaitEvent);
343 request->WaitEvent = NULL;
80d11b2a 344nomem:
bef4a34a 345 PutStorDevice(Device);
bef4a34a
HJ
346 return ret;
347}
348
163680b4
GKH
349static void StorVscOnIOCompletion(struct hv_device *Device,
350 struct vstor_packet *VStorPacket,
351 struct storvsc_request_extension *RequestExt)
352{
353 struct hv_storvsc_request *request;
354 struct storvsc_device *storDevice;
355
163680b4
GKH
356 storDevice = MustGetStorDevice(Device);
357 if (!storDevice) {
358 DPRINT_ERR(STORVSC, "unable to get stor device..."
359 "device being destroyed?");
163680b4
GKH
360 return;
361 }
362
363 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p "
364 "completed bytes xfer %u", RequestExt,
365 VStorPacket->VmSrb.DataTransferLength);
366
e2e64432
BP
367 /* ASSERT(RequestExt != NULL); */
368 /* ASSERT(RequestExt->Request != NULL); */
163680b4
GKH
369
370 request = RequestExt->Request;
371
e2e64432 372 /* ASSERT(request->OnIOCompletion != NULL); */
163680b4
GKH
373
374 /* Copy over the status...etc */
375 request->Status = VStorPacket->VmSrb.ScsiStatus;
376
377 if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1) {
378 DPRINT_WARN(STORVSC,
379 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
380 request->Cdb[0], VStorPacket->VmSrb.ScsiStatus,
381 VStorPacket->VmSrb.SrbStatus);
382 }
383
384 if ((request->Status & 0xFF) == 0x02) {
385 /* CHECK_CONDITION */
386 if (VStorPacket->VmSrb.SrbStatus & 0x80) {
387 /* autosense data available */
388 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
389 "valid - len %d\n", RequestExt,
390 VStorPacket->VmSrb.SenseInfoLength);
391
e2e64432
BP
392 /* ASSERT(VStorPacket->VmSrb.SenseInfoLength <= */
393 /* request->SenseBufferSize); */
163680b4
GKH
394 memcpy(request->SenseBuffer,
395 VStorPacket->VmSrb.SenseData,
396 VStorPacket->VmSrb.SenseInfoLength);
397
398 request->SenseBufferSize =
399 VStorPacket->VmSrb.SenseInfoLength;
400 }
401 }
402
403 /* TODO: */
404 request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
405
406 request->OnIOCompletion(request);
407
408 atomic_dec(&storDevice->NumOutstandingRequests);
409
410 PutStorDevice(Device);
163680b4
GKH
411}
412
413static void StorVscOnReceive(struct hv_device *Device,
414 struct vstor_packet *VStorPacket,
415 struct storvsc_request_extension *RequestExt)
416{
417 switch (VStorPacket->Operation) {
418 case VStorOperationCompleteIo:
419 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
420 StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
421 break;
422 case VStorOperationRemoveDevice:
423 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
424 /* TODO: */
425 break;
426
427 default:
428 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
429 VStorPacket->Operation);
430 break;
431 }
432}
433
434static void StorVscOnChannelCallback(void *context)
435{
436 struct hv_device *device = (struct hv_device *)context;
437 struct storvsc_device *storDevice;
438 u32 bytesRecvd;
439 u64 requestId;
440 unsigned char packet[ALIGN_UP(sizeof(struct vstor_packet), 8)];
441 struct storvsc_request_extension *request;
442 int ret;
443
e2e64432 444 /* ASSERT(device); */
163680b4
GKH
445
446 storDevice = MustGetStorDevice(device);
447 if (!storDevice) {
448 DPRINT_ERR(STORVSC, "unable to get stor device..."
449 "device being destroyed?");
163680b4
GKH
450 return;
451 }
452
453 do {
454 ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
455 packet,
456 ALIGN_UP(sizeof(struct vstor_packet), 8),
457 &bytesRecvd, &requestId);
458 if (ret == 0 && bytesRecvd > 0) {
459 DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx",
460 bytesRecvd, requestId);
461
462 /* ASSERT(bytesRecvd == sizeof(struct vstor_packet)); */
463
464 request = (struct storvsc_request_extension *)
465 (unsigned long)requestId;
e2e64432 466 /* ASSERT(request);c */
163680b4
GKH
467
468 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
469 if ((request == &storDevice->InitRequest) ||
470 (request == &storDevice->ResetRequest)) {
471 /* DPRINT_INFO(STORVSC,
472 * "reset completion - operation "
473 * "%u status %u",
474 * vstorPacket.Operation,
475 * vstorPacket.Status); */
476
477 memcpy(&request->VStorPacket, packet,
478 sizeof(struct vstor_packet));
479
480 osd_WaitEventSet(request->WaitEvent);
481 } else {
482 StorVscOnReceive(device,
483 (struct vstor_packet *)packet,
484 request);
485 }
486 } else {
487 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
488 break;
489 }
490 } while (1);
491
492 PutStorDevice(device);
163680b4
GKH
493 return;
494}
495
068c5df2 496static int StorVscConnectToVsp(struct hv_device *Device)
bef4a34a 497{
b3715ee4 498 struct vmstorage_channel_properties props;
068c5df2
GKH
499 struct storvsc_driver_object *storDriver;
500 int ret;
bef4a34a 501
068c5df2 502 storDriver = (struct storvsc_driver_object *)Device->Driver;
8c960e49 503 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
bef4a34a 504
454f18a9 505 /* Open the channel */
bef4a34a 506 ret = Device->Driver->VmbusChannelInterface.Open(Device,
068c5df2
GKH
507 storDriver->RingBufferSize,
508 storDriver->RingBufferSize,
509 (void *)&props,
510 sizeof(struct vmstorage_channel_properties),
511 StorVscOnChannelCallback,
512 Device);
513
514 DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d",
515 props.PathId, props.TargetId, props.MaxTransferBytes);
516
517 if (ret != 0) {
bef4a34a
HJ
518 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
519 return -1;
520 }
521
522 ret = StorVscChannelInit(Device);
523
524 return ret;
525}
526
3e189519 527/*
163680b4
GKH
528 * StorVscOnDeviceAdd - Callback when the device belonging to this driver is added
529 */
530static int StorVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
531{
532 struct storvsc_device *storDevice;
533 /* struct vmstorage_channel_properties *props; */
534 struct storvsc_device_info *deviceInfo;
535 int ret = 0;
536
163680b4
GKH
537 deviceInfo = (struct storvsc_device_info *)AdditionalInfo;
538 storDevice = AllocStorDevice(Device);
539 if (!storDevice) {
540 ret = -1;
541 goto Cleanup;
542 }
543
544 /* Save the channel properties to our storvsc channel */
545 /* props = (struct vmstorage_channel_properties *)
0686e4f4 546 * channel->offerMsg.Offer.u.Standard.UserDefined; */
163680b4
GKH
547
548 /* FIXME: */
549 /*
550 * If we support more than 1 scsi channel, we need to set the
551 * port number here to the scsi channel but how do we get the
552 * scsi channel prior to the bus scan
553 */
554
555 /* storChannel->PortNumber = 0;
556 storChannel->PathId = props->PathId;
557 storChannel->TargetId = props->TargetId; */
558
559 storDevice->PortNumber = deviceInfo->PortNumber;
560 /* Send it back up */
561 ret = StorVscConnectToVsp(Device);
562
563 /* deviceInfo->PortNumber = storDevice->PortNumber; */
564 deviceInfo->PathId = storDevice->PathId;
565 deviceInfo->TargetId = storDevice->TargetId;
566
567 DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n",
568 storDevice->PortNumber, storDevice->PathId,
569 storDevice->TargetId);
570
571Cleanup:
163680b4
GKH
572 return ret;
573}
bef4a34a 574
3e189519 575/*
068c5df2
GKH
576 * StorVscOnDeviceRemove - Callback when the our device is being removed
577 */
578static int StorVscOnDeviceRemove(struct hv_device *Device)
bef4a34a 579{
7dd03fc4 580 struct storvsc_device *storDevice;
bef4a34a 581
068c5df2
GKH
582 DPRINT_INFO(STORVSC, "disabling storage device (%p)...",
583 Device->Extension);
bef4a34a
HJ
584
585 storDevice = ReleaseStorDevice(Device);
586
454f18a9
BP
587 /*
588 * At this point, all outbound traffic should be disable. We
589 * only allow inbound traffic (responses) to proceed so that
590 * outstanding requests can be completed.
591 */
068c5df2
GKH
592 while (atomic_read(&storDevice->NumOutstandingRequests)) {
593 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
594 atomic_read(&storDevice->NumOutstandingRequests));
b4362c9c 595 udelay(100);
bef4a34a
HJ
596 }
597
068c5df2
GKH
598 DPRINT_INFO(STORVSC, "removing storage device (%p)...",
599 Device->Extension);
bef4a34a
HJ
600
601 storDevice = FinalReleaseStorDevice(Device);
602
603 DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
604
454f18a9 605 /* Close the channel */
bef4a34a
HJ
606 Device->Driver->VmbusChannelInterface.Close(Device);
607
608 FreeStorDevice(storDevice);
068c5df2 609 return 0;
454f18a9 610}
bef4a34a 611
354b0a64 612int StorVscOnHostReset(struct hv_device *Device)
bef4a34a 613{
7dd03fc4
GKH
614 struct storvsc_device *storDevice;
615 struct storvsc_request_extension *request;
b3715ee4 616 struct vstor_packet *vstorPacket;
068c5df2 617 int ret;
bef4a34a 618
bef4a34a
HJ
619 DPRINT_INFO(STORVSC, "resetting host adapter...");
620
621 storDevice = GetStorDevice(Device);
068c5df2
GKH
622 if (!storDevice) {
623 DPRINT_ERR(STORVSC, "unable to get stor device..."
624 "device being destroyed?");
bef4a34a
HJ
625 return -1;
626 }
627
628 request = &storDevice->ResetRequest;
629 vstorPacket = &request->VStorPacket;
630
bfc30aae 631 request->WaitEvent = osd_WaitEventCreate();
80d11b2a
BP
632 if (!request->WaitEvent) {
633 ret = -ENOMEM;
634 goto Cleanup;
635 }
bef4a34a 636
068c5df2
GKH
637 vstorPacket->Operation = VStorOperationResetBus;
638 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
639 vstorPacket->VmSrb.PathId = storDevice->PathId;
bef4a34a
HJ
640
641 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
068c5df2
GKH
642 vstorPacket,
643 sizeof(struct vstor_packet),
644 (unsigned long)&storDevice->ResetRequest,
645 VmbusPacketTypeDataInBand,
646 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
647 if (ret != 0) {
648 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d",
649 vstorPacket, ret);
bef4a34a
HJ
650 goto Cleanup;
651 }
652
454f18a9 653 /* FIXME: Add a timeout */
bfc30aae 654 osd_WaitEventWait(request->WaitEvent);
bef4a34a 655
420beac4 656 kfree(request->WaitEvent);
bef4a34a
HJ
657 DPRINT_INFO(STORVSC, "host adapter reset completed");
658
454f18a9
BP
659 /*
660 * At this point, all outstanding requests in the adapter
661 * should have been flushed out and return to us
662 */
bef4a34a
HJ
663
664Cleanup:
665 PutStorDevice(Device);
bef4a34a
HJ
666 return ret;
667}
668
3e189519 669/*
068c5df2
GKH
670 * StorVscOnIORequest - Callback to initiate an I/O request
671 */
672static int StorVscOnIORequest(struct hv_device *Device,
673 struct hv_storvsc_request *Request)
bef4a34a 674{
7dd03fc4 675 struct storvsc_device *storDevice;
068c5df2
GKH
676 struct storvsc_request_extension *requestExtension;
677 struct vstor_packet *vstorPacket;
678 int ret = 0;
bef4a34a 679
068c5df2
GKH
680 requestExtension =
681 (struct storvsc_request_extension *)Request->Extension;
682 vstorPacket = &requestExtension->VStorPacket;
bef4a34a
HJ
683 storDevice = GetStorDevice(Device);
684
068c5df2
GKH
685 DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, "
686 "Extension %p", Device, storDevice, Request,
687 requestExtension);
bef4a34a
HJ
688
689 DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
068c5df2
GKH
690 Request, Request->DataBuffer.Length, Request->Bus,
691 Request->TargetId, Request->LunId, Request->CdbLen);
bef4a34a 692
068c5df2
GKH
693 if (!storDevice) {
694 DPRINT_ERR(STORVSC, "unable to get stor device..."
695 "device being destroyed?");
bef4a34a
HJ
696 return -2;
697 }
698
068c5df2 699 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb,
0686e4f4 700 * Request->CdbLen); */
bef4a34a
HJ
701
702 requestExtension->Request = Request;
703 requestExtension->Device = Device;
704
b3715ee4 705 memset(vstorPacket, 0 , sizeof(struct vstor_packet));
bef4a34a
HJ
706
707 vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
708
068c5df2 709 vstorPacket->VmSrb.Length = sizeof(struct vmscsi_request);
bef4a34a
HJ
710
711 vstorPacket->VmSrb.PortNumber = Request->Host;
068c5df2
GKH
712 vstorPacket->VmSrb.PathId = Request->Bus;
713 vstorPacket->VmSrb.TargetId = Request->TargetId;
714 vstorPacket->VmSrb.Lun = Request->LunId;
bef4a34a
HJ
715
716 vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
717
454f18a9 718 /* Copy over the scsi command descriptor block */
068c5df2 719 vstorPacket->VmSrb.CdbLength = Request->CdbLen;
bef4a34a
HJ
720 memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen);
721
722 vstorPacket->VmSrb.DataIn = Request->Type;
723 vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length;
724
725 vstorPacket->Operation = VStorOperationExecuteSRB;
726
068c5df2
GKH
727 DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, "
728 "lun %d senselen %d cdblen %d",
729 vstorPacket->VmSrb.Length,
730 vstorPacket->VmSrb.PortNumber,
731 vstorPacket->VmSrb.PathId,
732 vstorPacket->VmSrb.TargetId,
733 vstorPacket->VmSrb.Lun,
734 vstorPacket->VmSrb.SenseInfoLength,
735 vstorPacket->VmSrb.CdbLength);
736
737 if (requestExtension->Request->DataBuffer.Length) {
738 ret = Device->Driver->VmbusChannelInterface.
739 SendPacketMultiPageBuffer(Device,
bef4a34a
HJ
740 &requestExtension->Request->DataBuffer,
741 vstorPacket,
b3715ee4 742 sizeof(struct vstor_packet),
c4b0bc94 743 (unsigned long)requestExtension);
068c5df2 744 } else {
bef4a34a 745 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
068c5df2
GKH
746 vstorPacket,
747 sizeof(struct vstor_packet),
748 (unsigned long)requestExtension,
749 VmbusPacketTypeDataInBand,
750 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
bef4a34a
HJ
751 }
752
068c5df2
GKH
753 if (ret != 0) {
754 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d",
755 vstorPacket, ret);
bef4a34a
HJ
756 }
757
f4888417 758 atomic_inc(&storDevice->NumOutstandingRequests);
bef4a34a
HJ
759
760 PutStorDevice(Device);
bef4a34a
HJ
761 return ret;
762}
763
3e189519 764/*
068c5df2
GKH
765 * StorVscOnCleanup - Perform any cleanup when the driver is removed
766 */
767static void StorVscOnCleanup(struct hv_driver *Driver)
bef4a34a 768{
bef4a34a
HJ
769}
770
3e189519 771/*
163680b4
GKH
772 * StorVscInitialize - Main entry point
773 */
774int StorVscInitialize(struct hv_driver *Driver)
bef4a34a 775{
163680b4 776 struct storvsc_driver_object *storDriver;
bef4a34a 777
163680b4 778 storDriver = (struct storvsc_driver_object *)Driver;
bef4a34a 779
163680b4
GKH
780 DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd "
781 "sizeof(struct storvsc_request_extension)=%zd "
782 "sizeof(struct vstor_packet)=%zd, "
783 "sizeof(struct vmscsi_request)=%zd",
784 sizeof(struct hv_storvsc_request),
785 sizeof(struct storvsc_request_extension),
786 sizeof(struct vstor_packet),
787 sizeof(struct vmscsi_request));
bef4a34a 788
163680b4 789 /* Make sure we are at least 2 pages since 1 page is used for control */
e2e64432 790 /* ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1)); */
bef4a34a 791
163680b4
GKH
792 Driver->name = gDriverName;
793 memcpy(&Driver->deviceType, &gStorVscDeviceType,
794 sizeof(struct hv_guid));
bef4a34a 795
163680b4 796 storDriver->RequestExtSize = sizeof(struct storvsc_request_extension);
bef4a34a 797
163680b4
GKH
798 /*
799 * Divide the ring buffer data size (which is 1 page less
800 * than the ring buffer size since that page is reserved for
801 * the ring buffer indices) by the max request size (which is
802 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + struct vstor_packet + u64)
803 */
804 storDriver->MaxOutstandingRequestsPerChannel =
805 ((storDriver->RingBufferSize - PAGE_SIZE) /
806 ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET +
807 sizeof(struct vstor_packet) + sizeof(u64),
808 sizeof(u64)));
bef4a34a 809
163680b4
GKH
810 DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
811 storDriver->MaxOutstandingRequestsPerChannel,
812 STORVSC_MAX_IO_REQUESTS);
bef4a34a 813
163680b4
GKH
814 /* Setup the dispatch table */
815 storDriver->Base.OnDeviceAdd = StorVscOnDeviceAdd;
816 storDriver->Base.OnDeviceRemove = StorVscOnDeviceRemove;
817 storDriver->Base.OnCleanup = StorVscOnCleanup;
bef4a34a 818
163680b4 819 storDriver->OnIORequest = StorVscOnIORequest;
bef4a34a 820
163680b4 821 return 0;
bef4a34a 822}