]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/hv/storvsc.c
Staging: hv: storvsc: call vmbus_recvpacket directly
[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"
50ea95df 31#include "channel.h"
bef4a34a
HJ
32
33
7dd03fc4 34struct storvsc_request_extension {
068c5df2 35 /* LIST_ENTRY ListEntry; */
bef4a34a 36
0b3f6834 37 struct hv_storvsc_request *Request;
3d3b5518 38 struct hv_device *Device;
bef4a34a 39
454f18a9 40 /* Synchronize the request/response if needed */
aedb444a 41 struct osd_waitevent *WaitEvent;
bef4a34a 42
b3715ee4 43 struct vstor_packet VStorPacket;
7dd03fc4 44};
bef4a34a 45
454f18a9 46/* A storvsc device is a device object that contains a vmbus channel */
7dd03fc4 47struct storvsc_device {
3d3b5518 48 struct hv_device *Device;
bef4a34a 49
068c5df2
GKH
50 /* 0 indicates the device is being destroyed */
51 atomic_t RefCount;
bef4a34a 52
f4888417 53 atomic_t NumOutstandingRequests;
bef4a34a 54
454f18a9
BP
55 /*
56 * Each unique Port/Path/Target represents 1 channel ie scsi
57 * controller. In reality, the pathid, targetid is always 0
58 * and the port is set by us
59 */
068c5df2
GKH
60 unsigned int PortNumber;
61 unsigned char PathId;
62 unsigned char TargetId;
bef4a34a 63
068c5df2
GKH
64 /* LIST_ENTRY OutstandingRequestList; */
65 /* HANDLE OutstandingRequestLock; */
bef4a34a 66
454f18a9 67 /* Used for vsc/vsp channel reset process */
7dd03fc4
GKH
68 struct storvsc_request_extension InitRequest;
69 struct storvsc_request_extension ResetRequest;
7dd03fc4 70};
bef4a34a
HJ
71
72
068c5df2 73static const char *gDriverName = "storvsc";
bef4a34a 74
454f18a9 75/* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
caf26a31
GKH
76static const struct hv_guid gStorVscDeviceType = {
77 .data = {
78 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
79 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
80 }
bef4a34a
HJ
81};
82
454f18a9 83
7dd03fc4 84static inline struct storvsc_device *AllocStorDevice(struct hv_device *Device)
bef4a34a 85{
7dd03fc4 86 struct storvsc_device *storDevice;
bef4a34a 87
7dd03fc4 88 storDevice = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
bef4a34a
HJ
89 if (!storDevice)
90 return NULL;
91
454f18a9
BP
92 /* Set to 2 to allow both inbound and outbound traffics */
93 /* (ie GetStorDevice() and MustGetStorDevice()) to proceed. */
f4888417 94 atomic_cmpxchg(&storDevice->RefCount, 0, 2);
bef4a34a
HJ
95
96 storDevice->Device = Device;
97 Device->Extension = storDevice;
98
99 return storDevice;
100}
101
7dd03fc4 102static inline void FreeStorDevice(struct storvsc_device *Device)
bef4a34a 103{
e2e64432 104 /* ASSERT(atomic_read(&Device->RefCount) == 0); */
8c69f52a 105 kfree(Device);
bef4a34a
HJ
106}
107
454f18a9 108/* Get the stordevice object iff exists and its refcount > 1 */
068c5df2 109static inline struct storvsc_device *GetStorDevice(struct hv_device *Device)
bef4a34a 110{
7dd03fc4 111 struct storvsc_device *storDevice;
bef4a34a 112
7dd03fc4 113 storDevice = (struct storvsc_device *)Device->Extension;
f4888417
BP
114 if (storDevice && atomic_read(&storDevice->RefCount) > 1)
115 atomic_inc(&storDevice->RefCount);
bef4a34a 116 else
bef4a34a 117 storDevice = NULL;
bef4a34a
HJ
118
119 return storDevice;
120}
121
454f18a9 122/* Get the stordevice object iff exists and its refcount > 0 */
7dd03fc4 123static inline struct storvsc_device *MustGetStorDevice(struct hv_device *Device)
bef4a34a 124{
7dd03fc4 125 struct storvsc_device *storDevice;
bef4a34a 126
7dd03fc4 127 storDevice = (struct storvsc_device *)Device->Extension;
f4888417
BP
128 if (storDevice && atomic_read(&storDevice->RefCount))
129 atomic_inc(&storDevice->RefCount);
bef4a34a 130 else
bef4a34a 131 storDevice = NULL;
bef4a34a
HJ
132
133 return storDevice;
134}
135
3d3b5518 136static inline void PutStorDevice(struct hv_device *Device)
bef4a34a 137{
7dd03fc4 138 struct storvsc_device *storDevice;
bef4a34a 139
7dd03fc4 140 storDevice = (struct storvsc_device *)Device->Extension;
e2e64432 141 /* ASSERT(storDevice); */
bef4a34a 142
f4888417 143 atomic_dec(&storDevice->RefCount);
e2e64432 144 /* ASSERT(atomic_read(&storDevice->RefCount)); */
bef4a34a
HJ
145}
146
454f18a9 147/* Drop ref count to 1 to effectively disable GetStorDevice() */
7dd03fc4 148static inline struct storvsc_device *ReleaseStorDevice(struct hv_device *Device)
bef4a34a 149{
7dd03fc4 150 struct storvsc_device *storDevice;
bef4a34a 151
7dd03fc4 152 storDevice = (struct storvsc_device *)Device->Extension;
e2e64432 153 /* ASSERT(storDevice); */
bef4a34a 154
454f18a9 155 /* Busy wait until the ref drop to 2, then set it to 1 */
f4888417 156 while (atomic_cmpxchg(&storDevice->RefCount, 2, 1) != 2)
b4362c9c 157 udelay(100);
bef4a34a
HJ
158
159 return storDevice;
160}
161
454f18a9 162/* Drop ref count to 0. No one can use StorDevice object. */
068c5df2
GKH
163static inline struct storvsc_device *FinalReleaseStorDevice(
164 struct hv_device *Device)
bef4a34a 165{
7dd03fc4 166 struct storvsc_device *storDevice;
bef4a34a 167
7dd03fc4 168 storDevice = (struct storvsc_device *)Device->Extension;
e2e64432 169 /* ASSERT(storDevice); */
bef4a34a 170
454f18a9 171 /* Busy wait until the ref drop to 1, then set it to 0 */
f4888417 172 while (atomic_cmpxchg(&storDevice->RefCount, 1, 0) != 1)
b4362c9c 173 udelay(100);
bef4a34a
HJ
174
175 Device->Extension = NULL;
176 return storDevice;
177}
178
3d3b5518 179static int StorVscChannelInit(struct hv_device *Device)
bef4a34a 180{
7dd03fc4
GKH
181 struct storvsc_device *storDevice;
182 struct storvsc_request_extension *request;
b3715ee4 183 struct vstor_packet *vstorPacket;
068c5df2 184 int ret;
bef4a34a
HJ
185
186 storDevice = GetStorDevice(Device);
068c5df2
GKH
187 if (!storDevice) {
188 DPRINT_ERR(STORVSC, "unable to get stor device..."
189 "device being destroyed?");
bef4a34a
HJ
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 346 PutStorDevice(Device);
bef4a34a
HJ
347 return ret;
348}
349
163680b4
GKH
350static void StorVscOnIOCompletion(struct hv_device *Device,
351 struct vstor_packet *VStorPacket,
352 struct storvsc_request_extension *RequestExt)
353{
354 struct hv_storvsc_request *request;
355 struct storvsc_device *storDevice;
356
163680b4
GKH
357 storDevice = MustGetStorDevice(Device);
358 if (!storDevice) {
359 DPRINT_ERR(STORVSC, "unable to get stor device..."
360 "device being destroyed?");
163680b4
GKH
361 return;
362 }
363
364 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p "
365 "completed bytes xfer %u", RequestExt,
366 VStorPacket->VmSrb.DataTransferLength);
367
e2e64432
BP
368 /* ASSERT(RequestExt != NULL); */
369 /* ASSERT(RequestExt->Request != NULL); */
163680b4
GKH
370
371 request = RequestExt->Request;
372
e2e64432 373 /* ASSERT(request->OnIOCompletion != NULL); */
163680b4
GKH
374
375 /* Copy over the status...etc */
376 request->Status = VStorPacket->VmSrb.ScsiStatus;
377
378 if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1) {
379 DPRINT_WARN(STORVSC,
380 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
381 request->Cdb[0], VStorPacket->VmSrb.ScsiStatus,
382 VStorPacket->VmSrb.SrbStatus);
383 }
384
385 if ((request->Status & 0xFF) == 0x02) {
386 /* CHECK_CONDITION */
387 if (VStorPacket->VmSrb.SrbStatus & 0x80) {
388 /* autosense data available */
389 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
390 "valid - len %d\n", RequestExt,
391 VStorPacket->VmSrb.SenseInfoLength);
392
e2e64432
BP
393 /* ASSERT(VStorPacket->VmSrb.SenseInfoLength <= */
394 /* request->SenseBufferSize); */
163680b4
GKH
395 memcpy(request->SenseBuffer,
396 VStorPacket->VmSrb.SenseData,
397 VStorPacket->VmSrb.SenseInfoLength);
398
399 request->SenseBufferSize =
400 VStorPacket->VmSrb.SenseInfoLength;
401 }
402 }
403
404 /* TODO: */
405 request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
406
407 request->OnIOCompletion(request);
408
409 atomic_dec(&storDevice->NumOutstandingRequests);
410
411 PutStorDevice(Device);
163680b4
GKH
412}
413
414static void StorVscOnReceive(struct hv_device *Device,
415 struct vstor_packet *VStorPacket,
416 struct storvsc_request_extension *RequestExt)
417{
418 switch (VStorPacket->Operation) {
419 case VStorOperationCompleteIo:
420 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
421 StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
422 break;
423 case VStorOperationRemoveDevice:
424 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
425 /* TODO: */
426 break;
427
428 default:
429 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
430 VStorPacket->Operation);
431 break;
432 }
433}
434
435static void StorVscOnChannelCallback(void *context)
436{
437 struct hv_device *device = (struct hv_device *)context;
438 struct storvsc_device *storDevice;
439 u32 bytesRecvd;
440 u64 requestId;
441 unsigned char packet[ALIGN_UP(sizeof(struct vstor_packet), 8)];
442 struct storvsc_request_extension *request;
443 int ret;
444
e2e64432 445 /* ASSERT(device); */
163680b4
GKH
446
447 storDevice = MustGetStorDevice(device);
448 if (!storDevice) {
449 DPRINT_ERR(STORVSC, "unable to get stor device..."
450 "device being destroyed?");
163680b4
GKH
451 return;
452 }
453
454 do {
50ea95df
GKH
455 ret = vmbus_recvpacket(device->channel, packet,
456 ALIGN_UP(sizeof(struct vstor_packet), 8),
457 &bytesRecvd, &requestId);
163680b4
GKH
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
430a8e9a 802 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
163680b4
GKH
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}