-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathscsi_driver_extensions.c
475 lines (403 loc) · 14.6 KB
/
scsi_driver_extensions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* Copyright (c) 2019 SUSE LLC
*
* Licensed under LGPL-2.1 (see LICENSE)
*/
#include "debug.h"
#include "driver.h"
#include "options.h"
#include "srb_helper.h"
#include "scsi_driver_extensions.h"
#include "scsi_function.h"
#include "scsi_trace.h"
#include "util.h"
#include "userspace.h"
PWNBD_EXTENSION GlobalExt;
VOID
WnbdScsiAdapterSupportControlTypes(PSCSI_SUPPORTED_CONTROL_TYPE_LIST List)
{
ASSERT(List);
ULONG Iterator;
for (Iterator = 0; Iterator < List->MaxControlType; Iterator++) {
switch (Iterator) {
case ScsiQuerySupportedControlTypes:
case ScsiStopAdapter:
case ScsiRestartAdapter:
case ScsiSetBootConfig:
case ScsiSetRunningConfig:
List->SupportedTypeList[Iterator] = TRUE;
break;
default:
break;
}
}
}
/*
* https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/storport/nc-storport-hw_adapter_control
*/
_Use_decl_annotations_
SCSI_ADAPTER_CONTROL_STATUS
WnbdHwAdapterControl(PVOID DeviceExtension,
SCSI_ADAPTER_CONTROL_TYPE ControlType,
PVOID Parameters)
{
UNREFERENCED_PARAMETER(DeviceExtension);
WNBD_LOG_DEBUG(
"Received control type: %s (%d)",
WnbdToStringScsiAdapterCtrlType(ControlType),
ControlType);
switch (ControlType) {
case ScsiQuerySupportedControlTypes:
WnbdScsiAdapterSupportControlTypes(
(PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters);
break;
default:
break;
}
return ScsiAdapterControlSuccess;
}
/*
* https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/storport/nc-storport-hw_find_adapter
*/
_Use_decl_annotations_
ULONG
WnbdHwFindAdapter(PVOID DeviceExtension,
PVOID HwContext,
PVOID BusInformation,
PVOID LowerDevice,
PCHAR ArgumentString,
PPORT_CONFIGURATION_INFORMATION ConfigInfo,
PBOOLEAN Again)
{
_IRQL_limited_to_(PASSIVE_LEVEL);
UNREFERENCED_PARAMETER(Again);
UNREFERENCED_PARAMETER(ArgumentString);
UNREFERENCED_PARAMETER(BusInformation);
UNREFERENCED_PARAMETER(LowerDevice);
PWNBD_EXTENSION Ext = (PWNBD_EXTENSION) DeviceExtension;
NTSTATUS Status = STATUS_SUCCESS;
/*
* https://docs.microsoft.com/en-us/previous-versions/windows/hardware/drivers/ff563901(v%3Dvs.85)
*/
// We're receiving 0 lengths for SCSIOP_READ|SCSIOP_WRITE when setting
// MaximumTransferLength to SP_UNINITIALIZED_VALUE. Keeping transfer lengths
// smaller than 32MB avoids this issue.
ConfigInfo->MaximumTransferLength = WNBD_DEFAULT_MAX_TRANSFER_LENGTH;
DWORD MaxIOReqPerAdapter =
(DWORD) WnbdDriverOptions[OptMaxIOReqPerAdapter].Value.Data.AsInt64;
if (0 < MaxIOReqPerAdapter &&
MaxIOReqPerAdapter <= WNBD_ABS_MAX_IO_REQ_PER_ADAPTER) {
WNBD_LOG_INFO("Configured maximum number of requests per adapter: %d",
MaxIOReqPerAdapter);
ConfigInfo->MaxNumberOfIO = MaxIOReqPerAdapter;
} else {
WNBD_LOG_WARN("Unsupported maximum number of requests per adapter: %d. "
"Minimum: 1. Maximum: %d. "
"Falling back to default value: %d.",
MaxIOReqPerAdapter,
WNBD_ABS_MAX_IO_REQ_PER_ADAPTER,
WNBD_DEFAULT_MAX_IO_REQ_PER_ADAPTER);
ConfigInfo->MaxNumberOfIO = WNBD_DEFAULT_MAX_IO_REQ_PER_ADAPTER;
}
DWORD MaxIOReqPerLun =
(DWORD) WnbdDriverOptions[OptMaxIOReqPerLun].Value.Data.AsInt64;
if (0 < MaxIOReqPerLun &&
MaxIOReqPerLun <= WNBD_ABS_MAX_IO_REQ_PER_LUN) {
WNBD_LOG_INFO("Configured maximum number of requests per lun: %d",
MaxIOReqPerLun);
ConfigInfo->MaxIOsPerLun = MaxIOReqPerLun;
} else {
WNBD_LOG_WARN("Unsupported maximum number of requests per lun: %d. "
"Minimum: 1. Maximum: %d. "
"Falling back to default value: %d.",
MaxIOReqPerLun,
WNBD_ABS_MAX_IO_REQ_PER_LUN,
WNBD_DEFAULT_MAX_IO_REQ_PER_LUN);
ConfigInfo->MaxIOsPerLun = WNBD_DEFAULT_MAX_IO_REQ_PER_LUN;
}
ConfigInfo->InitialLunQueueDepth = ConfigInfo->MaxIOsPerLun;
ConfigInfo->NumberOfPhysicalBreaks = SP_UNINITIALIZED_VALUE;
ConfigInfo->AlignmentMask = FILE_BYTE_ALIGNMENT;
ConfigInfo->NumberOfBuses = WNBD_MAX_BUSES_PER_ADAPTER;
ConfigInfo->ScatterGather = TRUE;
ConfigInfo->Dma64BitAddresses =
SCSI_DMA64_MINIPORT_FULL64BIT_NO_BOUNDARY_REQ_SUPPORTED;
ConfigInfo->Master = TRUE;
ConfigInfo->CachesData = TRUE;
ConfigInfo->MaximumNumberOfTargets = WNBD_MAX_TARGETS_PER_BUS;
ConfigInfo->MaximumNumberOfLogicalUnits = WNBD_MAX_LUNS_PER_TARGET;
ConfigInfo->WmiDataProvider = FALSE;
ConfigInfo->SynchronizationModel = StorSynchronizeFullDuplex;
ConfigInfo->VirtualDevice = TRUE;
Status = ExInitializeResourceLite(&Ext->DeviceCreationLock);
if (!NT_SUCCESS(Status)) {
WNBD_LOG_ERROR("Error initializing resource DeviceCreationLock. Failed with NTSTATUS: 0x%x.", Status);
Status = STATUS_INSUFFICIENT_RESOURCES;
goto Clean;
}
/*
* Initialize basic fields of the device global extension
*/
InitializeListHead(&Ext->DeviceList);
KeInitializeSpinLock(&Ext->DeviceListLock);
KeInitializeEvent(&Ext->GlobalDeviceRemovalEvent, SynchronizationEvent, FALSE);
ExInitializeRundownProtection(&Ext->RundownProtection);
GlobalExt = Ext;
/*
* Setup user-space communication device
*/
Status = IoRegisterDeviceInterface((PDEVICE_OBJECT)HwContext,
&WNBD_GUID, NULL, &Ext->DeviceInterface);
if (!NT_SUCCESS(Status)) {
WNBD_LOG_ERROR("Error calling IoRegisterDeviceInterface. Failed with NTSTATUS: 0x%x.", Status);
goto CleanLock;
}
WnbdInitScsiIds();
if (!NT_SUCCESS(Status)) {
goto Exit;
}
WNBD_LOG_DEBUG("Exit SP_RETURN_FOUND");
return SP_RETURN_FOUND;
Exit:
RtlFreeUnicodeString(&Ext->DeviceInterface);
CleanLock:
ExDeleteResourceLite(&Ext->DeviceCreationLock);
Clean:
WNBD_LOG_DEBUG("Exit SP_RETURN_NOT_FOUND");
return SP_RETURN_NOT_FOUND;
}
/*
* https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/storport/nc-storport-hw_free_adapter_resources
*/
_Use_decl_annotations_
VOID
WnbdHwFreeAdapterResources(_In_ PVOID DeviceExtension)
{
ASSERT(DeviceExtension);
ASSERT(PASSIVE_LEVEL == KeGetCurrentIrql());
PWNBD_EXTENSION Ext = (PWNBD_EXTENSION) DeviceExtension;
WnbdCleanupAllDevices(Ext);
ExDeleteResourceLite(&Ext->DeviceCreationLock);
}
/*
* https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/storport/nc-storport-hw_initialize
*/
_Use_decl_annotations_
BOOLEAN
WnbdHwInitialize(PVOID DeviceExtension)
{
ASSERT(DeviceExtension);
PWNBD_EXTENSION Ext = (PWNBD_EXTENSION) DeviceExtension;
NTSTATUS Status = IoSetDeviceInterfaceState(&Ext->DeviceInterface, TRUE);
if (!NT_SUCCESS(Status)) {
WNBD_LOG_ERROR("Error calling IoSetDeviceInterfaceState 0x%x.", Status);
return FALSE;
}
return TRUE;
}
/*
* https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/storport/nc-storport-hw_process_service_request
*/
_Use_decl_annotations_
VOID
WnbdHwProcessServiceRequest(PVOID DeviceExtension,
PVOID Irp)
{
ASSERT(DeviceExtension);
ASSERT(Irp);
PIO_STACK_LOCATION IoLocation = IoGetCurrentIrpStackLocation((PIRP)Irp);
NTSTATUS Status = STATUS_INVALID_DEVICE_REQUEST;
if (IRP_MJ_DEVICE_CONTROL == IoLocation->MajorFunction) {
Status = WnbdParseUserIOCTL((PWNBD_EXTENSION) DeviceExtension,
(PIRP)Irp);
}
if (STATUS_PENDING != Status) {
((PIRP)Irp)->IoStatus.Status = Status;
WNBD_LOG_DEBUG("Calling StorPortCompleteServiceIrp");
StorPortCompleteServiceIrp(DeviceExtension, Irp);
} else {
WNBD_LOG_DEBUG("Pending HwProcessServiceRequest");
}
}
/*
* https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/storport/nc-storport-hw_reset_bus
*/
_Use_decl_annotations_
BOOLEAN
WnbdHwResetBus(PVOID DeviceExtension,
ULONG PathId)
{
_IRQL_limited_to_(DISPATCH_LEVEL);
StorPortCompleteRequest(DeviceExtension, (UCHAR)PathId,
SP_UNTAGGED, SP_UNTAGGED, SRB_STATUS_BUS_RESET);
return TRUE;
}
UCHAR
WnbdFirmwareInfo(PVOID Srb)
{
PSRB_IO_CONTROL SrbControl;
PFIRMWARE_REQUEST_BLOCK Request;
PSTORAGE_FIRMWARE_INFO_V2 Info;
SrbControl = (PSRB_IO_CONTROL)SrbGetDataBuffer(Srb);
Request = (PFIRMWARE_REQUEST_BLOCK)(SrbControl + 1);
if (sizeof(STORAGE_FIRMWARE_INFO_V2) > Request->DataBufferLength) {
SrbControl->ReturnCode = FIRMWARE_STATUS_INVALID_PARAMETER;
SrbSetSrbStatus(Srb, SRB_STATUS_BAD_SRB_BLOCK_LENGTH);
goto exit;
}
Info = (PSTORAGE_FIRMWARE_INFO_V2)((PUCHAR)SrbControl + Request->DataBufferOffset);
if ((STORAGE_FIRMWARE_INFO_STRUCTURE_VERSION_V2 != Info->Version ) ||
(sizeof(STORAGE_FIRMWARE_INFO_V2) > Info->Size)) {
SrbControl->ReturnCode = FIRMWARE_STATUS_INVALID_PARAMETER;
SrbSetSrbStatus(Srb, SRB_STATUS_BAD_SRB_BLOCK_LENGTH);
goto exit;
}
RtlZeroMemory((PCHAR)Info, Request->DataBufferLength);
/* Just fill data needed for WLK Firmware update test */
Info->Version = STORAGE_FIRMWARE_INFO_STRUCTURE_VERSION_V2;
Info->Size = sizeof(STORAGE_FIRMWARE_INFO_V2);
Info->UpgradeSupport = TRUE;
Info->SlotCount = 1;
Info->ActiveSlot = 0;
Info->PendingActivateSlot = STORAGE_FIRMWARE_INFO_INVALID_SLOT;
Info->FirmwareShared = FALSE;
Info->ImagePayloadAlignment = 4096;
Info->ImagePayloadMaxSize = 8096;
if ((sizeof(STORAGE_FIRMWARE_INFO_V2) + sizeof(STORAGE_FIRMWARE_SLOT_INFO_V2))
<= Request->DataBufferLength) {
Info->Slot[0].SlotNumber = 0;
Info->Slot[0].ReadOnly = FALSE;
StorPortCopyMemory(&Info->Slot[0].Revision, "01234567", 8);
SrbControl->ReturnCode = FIRMWARE_STATUS_SUCCESS;
SrbSetSrbStatus(Srb, SRB_STATUS_SUCCESS);
} else {
Request->DataBufferLength = sizeof(STORAGE_FIRMWARE_INFO_V2) + sizeof(STORAGE_FIRMWARE_SLOT_INFO_V2);
SrbControl->ReturnCode = FIRMWARE_STATUS_OUTPUT_BUFFER_TOO_SMALL;
SrbSetSrbStatus(Srb, SRB_STATUS_SUCCESS);
}
exit:
return SrbGetSrbStatus(Srb);
}
UCHAR
WnbdFirmareIOCTL(PVOID Srb)
{
ULONG BufferLength = 0;
PSRB_IO_CONTROL SrbControl;
PFIRMWARE_REQUEST_BLOCK Request;
SrbControl = (PSRB_IO_CONTROL)SrbGetDataBuffer(Srb);
BufferLength = SrbGetDataTransferLength(Srb);
if ((sizeof(SRB_IO_CONTROL) + sizeof(FIRMWARE_REQUEST_BLOCK))
>BufferLength) {
SrbSetSrbStatus(Srb, SRB_STATUS_BAD_SRB_BLOCK_LENGTH);
goto exit;
}
Request = (PFIRMWARE_REQUEST_BLOCK)(SrbControl + 1);
if (((ULONGLONG)Request->DataBufferOffset + Request->DataBufferLength)
>(ULONGLONG)(BufferLength)) {
SrbControl->ReturnCode = FIRMWARE_STATUS_INVALID_PARAMETER;
SrbSetSrbStatus(Srb, SRB_STATUS_BAD_SRB_BLOCK_LENGTH);
goto exit;
}
if (FIRMWARE_REQUEST_BLOCK_STRUCTURE_VERSION > Request->Version) {
SrbControl->ReturnCode = FIRMWARE_STATUS_INVALID_PARAMETER;
SrbSetSrbStatus(Srb, SRB_STATUS_BAD_SRB_BLOCK_LENGTH);
goto exit;
}
if (ALIGN_UP(sizeof(SRB_IO_CONTROL) + sizeof(FIRMWARE_REQUEST_BLOCK), PVOID)
> Request->DataBufferOffset) {
SrbControl->ReturnCode = FIRMWARE_STATUS_INVALID_PARAMETER;
SrbSetSrbStatus(Srb, SRB_STATUS_BAD_SRB_BLOCK_LENGTH);
goto exit;
}
switch (Request->Function) {
case FIRMWARE_FUNCTION_GET_INFO:
WnbdFirmwareInfo(Srb);
break;
case FIRMWARE_FUNCTION_DOWNLOAD:
case FIRMWARE_FUNCTION_ACTIVATE:
default:
SrbControl->ReturnCode = FIRMWARE_STATUS_SUCCESS;
SrbSetSrbStatus(Srb, SRB_STATUS_SUCCESS);
break;
}
exit:
return SrbGetSrbStatus(Srb);
}
UCHAR
WnbdProcessSrbIOCTL(PVOID Srb)
{
PSRB_IO_CONTROL SrbControl = (PSRB_IO_CONTROL)SrbGetDataBuffer(Srb);
switch (SrbControl->ControlCode) {
case IOCTL_SCSI_MINIPORT_FIRMWARE:
{
WnbdFirmareIOCTL(Srb);
}
break;
default:
SrbControl->ReturnCode = FIRMWARE_STATUS_INVALID_PARAMETER;
SrbSetSrbStatus(Srb, SRB_STATUS_BAD_SRB_BLOCK_LENGTH);
break;
}
return SrbGetSrbStatus(Srb);
}
/*
* https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/storport/nc-storport-hw_startio
*/
_Use_decl_annotations_
BOOLEAN
WnbdHwStartIo(PVOID DeviceExtension, PVOID Srb)
{
_IRQL_limited_to_(DISPATCH_LEVEL);
UCHAR SrbStatus = SRB_STATUS_INVALID_REQUEST;
BOOLEAN Complete = TRUE;
ASSERT(DeviceExtension);
PWNBD_EXTENSION Ext = (PWNBD_EXTENSION)DeviceExtension;
ULONG SrbFunction = SrbGetSrbFunction(Srb);
WNBD_LOG_DEBUG("WnbdHwStartIo Processing SRB Function = 0x%x(%s)",
SrbFunction, WnbdToStringSrbFunction(SrbFunction));
switch (SrbFunction) {
case SRB_FUNCTION_EXECUTE_SCSI:
SrbStatus = WnbdExecuteScsiFunction(Ext, Srb, &Complete);
break;
case SRB_FUNCTION_RESET_LOGICAL_UNIT:
SrbStatus = WnbdResetLogicalUnitFunction(Ext, Srb);
break;
case SRB_FUNCTION_RESET_DEVICE:
SrbStatus = WnbdResetDeviceFunction(Ext, Srb);
break;
case SRB_FUNCTION_ABORT_COMMAND:
SrbStatus = WnbdAbortFunction(Ext, Srb);
break;
case SRB_FUNCTION_PNP:
SrbStatus = WnbdPNPFunction(Srb);
break;
case SRB_FUNCTION_IO_CONTROL: {
SrbStatus = WnbdProcessSrbIOCTL(Srb);
break;
}
case SRB_FUNCTION_FLUSH:
case SRB_FUNCTION_SHUTDOWN:
/* Set to NOOP for virtual devices */
SrbStatus = SRB_STATUS_SUCCESS;
break;
case SRB_FUNCTION_WMI:
case SRB_FUNCTION_RESET_BUS:
default:
WNBD_LOG_INFO("Unknown SRB Function = 0x%x(%s)",
SrbFunction, WnbdToStringSrbFunction(SrbFunction));
SrbStatus = SRB_STATUS_INVALID_REQUEST;
break;
}
/*
* If the operation is not pending notify the Storport of completion
*/
if (Complete) {
WNBD_LOG_DEBUG("RequestComplete of %s status: 0x%x(%s)",
WnbdToStringSrbFunction(SrbFunction),
SrbStatus,
WnbdToStringSrbStatus(SrbStatus));
SrbSetSrbStatus(Srb, SrbStatus);
StorPortNotification(RequestComplete, DeviceExtension, Srb);
}
return TRUE;
}