win-pvdrivers

view xenvbd/xenvbd.c @ 790:467005e7f509

Big messy changes. Add grant ref tagging to better track when things go wrong (debug build only).
Fix a race in xennet that causes crashes under heavy traffic conditions on driver shutdown.
author James Harper <james.harper@bendigoit.com.au>
date Fri Mar 12 09:38:42 2010 +1100 (2010-03-12)
parents b18131bbbaf0
children 213306951ec5
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #define INITGUID
21 #include "xenvbd.h"
22 #include <io/blkif.h>
23 #include <scsi.h>
24 #include <ntddscsi.h>
25 #include <ntdddisk.h>
26 #include <stdlib.h>
27 #include <xen_public.h>
28 #include <io/xenbus.h>
29 #include <io/protocols.h>
31 #pragma warning(disable: 4127)
33 #if defined(__x86_64__)
34 #define LongLongToPtr(x) (PVOID)(x)
35 #else
36 #define LongLongToPtr(x) UlongToPtr(x)
37 #endif
39 /* Not really necessary but keeps PREfast happy */
40 DRIVER_INITIALIZE DriverEntry;
42 static BOOLEAN dump_mode = FALSE;
43 #define DUMP_MODE_ERROR_LIMIT 64
44 static ULONG dump_mode_errors = 0;
46 CHAR scsi_device_manufacturer[8];
47 CHAR scsi_disk_model[16];
48 CHAR scsi_cdrom_model[16];
50 ULONGLONG parse_numeric_string(PCHAR string)
51 {
52 ULONGLONG val = 0;
53 while (*string != 0)
54 {
55 val = val * 10 + (*string - '0');
56 string++;
57 }
58 return val;
59 }
61 static blkif_shadow_t *
62 get_shadow_from_freelist(PXENVBD_DEVICE_DATA xvdd)
63 {
64 if (xvdd->shadow_free == 0)
65 {
66 KdPrint((__DRIVER_NAME " No more shadow entries\n"));
67 return NULL;
68 }
69 xvdd->shadow_free--;
70 if (xvdd->shadow_free < xvdd->shadow_min_free)
71 xvdd->shadow_min_free = xvdd->shadow_free;
72 return &xvdd->shadows[xvdd->shadow_free_list[xvdd->shadow_free]];
73 }
75 static VOID
76 put_shadow_on_freelist(PXENVBD_DEVICE_DATA xvdd, blkif_shadow_t *shadow)
77 {
78 xvdd->shadow_free_list[xvdd->shadow_free] = (USHORT)shadow->req.id;
79 shadow->srb = NULL;
80 xvdd->shadow_free++;
81 }
83 static blkif_response_t *
84 XenVbd_GetResponse(PXENVBD_DEVICE_DATA xvdd, int i)
85 {
86 blkif_other_response_t *rep;
87 if (!xvdd->use_other)
88 return RING_GET_RESPONSE(&xvdd->ring, i);
89 rep = RING_GET_RESPONSE(&xvdd->other_ring, i);
90 xvdd->tmp_rep.id = rep->id;
91 xvdd->tmp_rep.operation = rep->operation;
92 xvdd->tmp_rep.status = rep->status;
93 return &xvdd->tmp_rep;
94 }
96 static VOID
97 XenVbd_PutRequest(PXENVBD_DEVICE_DATA xvdd, blkif_request_t *req)
98 {
99 blkif_other_request_t *other_req;
101 if (!xvdd->use_other)
102 {
103 *RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt) = *req;
104 }
105 else
106 {
107 other_req = RING_GET_REQUEST(&xvdd->other_ring, xvdd->ring.req_prod_pvt);
108 other_req->operation = req->operation;
109 other_req->nr_segments = req->nr_segments;
110 other_req->handle = req->handle;
111 other_req->id = req->id;
112 other_req->sector_number = req->sector_number;
113 memcpy(other_req->seg, req->seg, sizeof(struct blkif_request_segment) * req->nr_segments);
114 }
115 xvdd->ring.req_prod_pvt++;
116 }
118 static ULONG
119 XenVbd_InitFromConfig(PXENVBD_DEVICE_DATA xvdd)
120 {
121 ULONG i;
122 PUCHAR ptr;
123 USHORT type;
124 PCHAR setting, value, value2;
125 ULONG qemu_protocol_version = 0;
126 BOOLEAN qemu_hide_filter = FALSE;
127 ULONG qemu_hide_flags_value = 0;
129 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
130 xvdd->sring = NULL;
131 xvdd->event_channel = 0;
133 xvdd->inactive = TRUE;
134 ptr = xvdd->device_base;
135 while((type = GET_XEN_INIT_RSP(&ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
136 {
137 switch(type)
138 {
139 case XEN_INIT_TYPE_RING: /* frontend ring */
140 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %p\n", setting, value));
141 if (strcmp(setting, "ring-ref") == 0)
142 {
143 xvdd->sring = (blkif_sring_t *)value;
144 FRONT_RING_INIT(&xvdd->ring, xvdd->sring, PAGE_SIZE);
145 /* this bit is for when we have to take over an existing ring on a crash dump */
146 xvdd->ring.req_prod_pvt = xvdd->sring->req_prod;
147 xvdd->ring.rsp_cons = xvdd->ring.req_prod_pvt;
148 }
149 break;
150 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
151 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel */
152 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_EVENT_CHANNEL - %s = %d\n", setting, PtrToUlong(value) & 0x3FFFFFFF));
153 if (strcmp(setting, "event-channel") == 0)
154 {
155 /* cheat here - save the state of the ring in the topmost bits of the event-channel */
156 xvdd->event_channel_ptr = (ULONG *)(((PCHAR)ptr) - sizeof(ULONG));
157 xvdd->event_channel = PtrToUlong(value) & 0x3FFFFFFF;
158 if (PtrToUlong(value) & 0x80000000)
159 {
160 xvdd->cached_use_other = (BOOLEAN)!!(PtrToUlong(value) & 0x40000000);
161 KdPrint((__DRIVER_NAME " cached_use_other = %d\n", xvdd->cached_use_other));
162 }
163 }
164 break;
165 case XEN_INIT_TYPE_READ_STRING_BACK:
166 case XEN_INIT_TYPE_READ_STRING_FRONT:
167 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = %s\n", setting, value));
168 if (strcmp(setting, "sectors") == 0)
169 xvdd->total_sectors = parse_numeric_string(value);
170 else if (strcmp(setting, "sector-size") == 0)
171 xvdd->bytes_per_sector = (ULONG)parse_numeric_string(value);
172 else if (strcmp(setting, "device-type") == 0)
173 {
174 if (strcmp(value, "disk") == 0)
175 {
176 KdPrint((__DRIVER_NAME " device-type = Disk\n"));
177 xvdd->device_type = XENVBD_DEVICETYPE_DISK;
178 }
179 else if (strcmp(value, "cdrom") == 0)
180 {
181 KdPrint((__DRIVER_NAME " device-type = CDROM\n"));
182 xvdd->device_type = XENVBD_DEVICETYPE_CDROM;
183 }
184 else
185 {
186 KdPrint((__DRIVER_NAME " device-type = %s (This probably won't work!)\n", value));
187 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
188 }
189 }
190 else if (strcmp(setting, "mode") == 0)
191 {
192 if (strncmp(value, "r", 1) == 0)
193 {
194 KdPrint((__DRIVER_NAME " mode = r\n"));
195 xvdd->device_mode = XENVBD_DEVICEMODE_READ;
196 }
197 else if (strncmp(value, "w", 1) == 0)
198 {
199 KdPrint((__DRIVER_NAME " mode = w\n"));
200 xvdd->device_mode = XENVBD_DEVICEMODE_WRITE;
201 }
202 else
203 {
204 KdPrint((__DRIVER_NAME " mode = unknown\n"));
205 xvdd->device_mode = XENVBD_DEVICEMODE_UNKNOWN;
206 }
207 }
208 break;
209 case XEN_INIT_TYPE_VECTORS:
210 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_VECTORS\n"));
211 if (((PXENPCI_VECTORS)value)->length != sizeof(XENPCI_VECTORS) ||
212 ((PXENPCI_VECTORS)value)->magic != XEN_DATA_MAGIC)
213 {
214 KdPrint((__DRIVER_NAME " vectors mismatch (magic = %08x, length = %d)\n",
215 ((PXENPCI_VECTORS)value)->magic, ((PXENPCI_VECTORS)value)->length));
216 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
217 return SP_RETURN_BAD_CONFIG;
218 }
219 else
220 memcpy(&xvdd->vectors, value, sizeof(XENPCI_VECTORS));
221 break;
222 case XEN_INIT_TYPE_STATE_PTR:
223 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_DEVICE_STATE - %p\n", PtrToUlong(value)));
224 xvdd->device_state = (PXENPCI_DEVICE_STATE)value;
225 break;
226 case XEN_INIT_TYPE_QEMU_PROTOCOL_VERSION:
227 qemu_protocol_version = PtrToUlong(value);
228 break;
229 case XEN_INIT_TYPE_GRANT_ENTRIES:
230 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_GRANT_ENTRIES - entries = %d\n", PtrToUlong(setting)));
231 memcpy(xvdd->dump_grant_refs, value, PtrToUlong(setting) * sizeof(grant_ref_t));
232 break;
233 case XEN_INIT_TYPE_QEMU_HIDE_FLAGS:
234 qemu_hide_flags_value = PtrToUlong(value);
235 KdPrint((__DRIVER_NAME " qemu_hide_flags_value = %d\n", qemu_hide_flags_value));
236 break;
237 case XEN_INIT_TYPE_QEMU_HIDE_FILTER:
238 qemu_hide_filter = TRUE;
239 KdPrint((__DRIVER_NAME " qemu_hide_filter = TRUE\n"));
240 break;
241 default:
242 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_%d\n", type));
243 break;
244 }
245 }
247 if (((qemu_hide_flags_value & QEMU_UNPLUG_ALL_IDE_DISKS) && xvdd->device_type != XENVBD_DEVICETYPE_CDROM) || qemu_hide_filter)
248 xvdd->inactive = FALSE;
250 if (!xvdd->inactive && (xvdd->device_type == XENVBD_DEVICETYPE_UNKNOWN
251 || xvdd->sring == NULL
252 || xvdd->event_channel == 0
253 || xvdd->total_sectors == 0
254 || xvdd->bytes_per_sector == 0))
255 {
256 KdPrint((__DRIVER_NAME " Missing settings\n"));
257 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
258 return SP_RETURN_BAD_CONFIG;
259 }
261 if (xvdd->inactive)
262 KdPrint((__DRIVER_NAME " Device is inactive\n"));
263 else
264 {
265 if (xvdd->device_type == XENVBD_DEVICETYPE_CDROM)
266 {
267 /* CD/DVD drives must have bytes_per_sector = 2048. */
268 xvdd->bytes_per_sector = 2048;
269 }
271 /* for some reason total_sectors is measured in 512 byte sectors always, so correct this to be in bytes_per_sectors */
272 xvdd->total_sectors /= xvdd->bytes_per_sector / 512;
274 xvdd->shadow_free = 0;
275 memset(xvdd->shadows, 0, sizeof(blkif_shadow_t) * SHADOW_ENTRIES);
276 for (i = 0; i < SHADOW_ENTRIES; i++)
277 {
278 xvdd->shadows[i].req.id = i;
279 put_shadow_on_freelist(xvdd, &xvdd->shadows[i]);
280 }
281 }
283 return SP_RETURN_FOUND;
284 }
286 static __inline ULONG
287 decode_cdb_length(PSCSI_REQUEST_BLOCK srb)
288 {
289 switch (srb->Cdb[0])
290 {
291 case SCSIOP_READ:
292 case SCSIOP_WRITE:
293 return ((ULONG)(UCHAR)srb->Cdb[7] << 8) | (ULONG)(UCHAR)srb->Cdb[8];
294 case SCSIOP_READ16:
295 case SCSIOP_WRITE16:
296 return ((ULONG)(UCHAR)srb->Cdb[10] << 24) | ((ULONG)(UCHAR)srb->Cdb[11] << 16) | ((ULONG)(UCHAR)srb->Cdb[12] << 8) | (ULONG)(UCHAR)srb->Cdb[13];
297 default:
298 return 0;
299 }
300 }
302 static __inline ULONGLONG
303 decode_cdb_sector(PSCSI_REQUEST_BLOCK srb)
304 {
305 ULONGLONG sector;
307 switch (srb->Cdb[0])
308 {
309 case SCSIOP_READ:
310 case SCSIOP_WRITE:
311 sector = ((ULONG)(UCHAR)srb->Cdb[2] << 24) | ((ULONG)(UCHAR)srb->Cdb[3] << 16) | ((ULONG)(UCHAR)srb->Cdb[4] << 8) | (ULONG)(UCHAR)srb->Cdb[5];
312 break;
313 case SCSIOP_READ16:
314 case SCSIOP_WRITE16:
315 sector = ((ULONGLONG)(UCHAR)srb->Cdb[2] << 56) | ((ULONGLONG)(UCHAR)srb->Cdb[3] << 48)
316 | ((ULONGLONG)(UCHAR)srb->Cdb[4] << 40) | ((ULONGLONG)(UCHAR)srb->Cdb[5] << 32)
317 | ((ULONGLONG)(UCHAR)srb->Cdb[6] << 24) | ((ULONGLONG)(UCHAR)srb->Cdb[7] << 16)
318 | ((ULONGLONG)(UCHAR)srb->Cdb[8] << 8) | ((ULONGLONG)(UCHAR)srb->Cdb[9]);
319 //KdPrint((__DRIVER_NAME " sector_number = %d (high) %d (low)\n", (ULONG)(sector >> 32), (ULONG)sector));
320 break;
321 default:
322 sector = 0;
323 break;
324 }
325 return sector;
326 }
328 static __inline BOOLEAN
329 decode_cdb_is_read(PSCSI_REQUEST_BLOCK srb)
330 {
331 switch (srb->Cdb[0])
332 {
333 case SCSIOP_READ:
334 case SCSIOP_READ16:
335 return TRUE;
336 case SCSIOP_WRITE:
337 case SCSIOP_WRITE16:
338 return FALSE;
339 default:
340 return FALSE;
341 }
342 }
344 ULONG max_dump_mode_size = 0;
346 static VOID
347 XenVbd_PutSrbOnList(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
348 {
349 srb_list_entry_t *list_entry = srb->SrbExtension;
350 list_entry->srb = srb;
351 InsertTailList(&xvdd->srb_list, (PLIST_ENTRY)list_entry);
352 }
354 static VOID
355 XenVbd_PutQueuedSrbsOnRing(PXENVBD_DEVICE_DATA xvdd)
356 {
357 PSCSI_REQUEST_BLOCK srb;
358 srb_list_entry_t *srb_entry;
359 ULONG block_count;
360 blkif_shadow_t *shadow;
361 ULONG remaining, offset, length;
362 grant_ref_t gref;
363 PUCHAR ptr;
364 int notify;
366 //FUNCTION_ENTER();
368 if (xvdd->aligned_buffer_in_use)
369 return;
371 while(xvdd->shadow_free && (srb_entry = (srb_list_entry_t *)RemoveHeadList(&xvdd->srb_list)) != (srb_list_entry_t *)&xvdd->srb_list)
372 {
373 srb = srb_entry->srb;
374 block_count = decode_cdb_length(srb);;
375 block_count *= xvdd->bytes_per_sector / 512;
376 remaining = block_count * 512;
378 shadow = get_shadow_from_freelist(xvdd);
379 ASSERT(shadow);
380 ASSERT(!shadow->aligned_buffer_in_use);
381 ASSERT(!shadow->srb);
382 shadow->req.sector_number = decode_cdb_sector(srb);
383 shadow->req.sector_number *= xvdd->bytes_per_sector / 512;
384 shadow->req.handle = 0;
385 shadow->req.operation = decode_cdb_is_read(srb)?BLKIF_OP_READ:BLKIF_OP_WRITE;
386 shadow->req.nr_segments = 0;
387 shadow->srb = srb;
389 if ((ULONG_PTR)srb->DataBuffer & 511)
390 {
391 xvdd->aligned_buffer_in_use = TRUE;
392 ptr = xvdd->aligned_buffer;
393 if (!decode_cdb_is_read(srb))
394 memcpy(ptr, srb->DataBuffer, block_count * 512);
395 shadow->aligned_buffer_in_use = TRUE;
396 }
397 else
398 {
399 ptr = srb->DataBuffer;
400 shadow->aligned_buffer_in_use = FALSE;
401 }
403 if (dump_mode && block_count > max_dump_mode_size)
404 {
405 max_dump_mode_size = block_count;
406 KdPrint((__DRIVER_NAME " max_dump_mode_size = %d\n", max_dump_mode_size));
407 }
409 //KdPrint((__DRIVER_NAME " sector_number = %d, block_count = %d\n", (ULONG)shadow->req.sector_number, block_count));
410 //KdPrint((__DRIVER_NAME " SrbExtension = %p\n", srb->SrbExtension));
411 //KdPrint((__DRIVER_NAME " DataBuffer = %p\n", srb->DataBuffer));
413 //KdPrint((__DRIVER_NAME " sector_number = %d\n", (ULONG)shadow->req.sector_number));
414 //KdPrint((__DRIVER_NAME " handle = %d\n", shadow->req.handle));
415 //KdPrint((__DRIVER_NAME " operation = %d\n", shadow->req.operation));
417 while (remaining > 0)
418 {
419 PHYSICAL_ADDRESS physical_address = MmGetPhysicalAddress(ptr);
421 if (dump_mode)
422 {
423 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
424 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE,
425 xvdd->dump_grant_refs[shadow->req.nr_segments], (ULONG)'XPDO');
426 }
427 else
428 {
429 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
430 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE, INVALID_GRANT_REF, (ULONG)'XVBD');
431 }
432 if (gref == INVALID_GRANT_REF)
433 {
434 ULONG i;
435 for (i = 0; i < shadow->req.nr_segments; i++)
436 {
437 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
438 shadow->req.seg[i].gref, FALSE, (ULONG)'XVBD');
439 }
440 if (shadow->aligned_buffer_in_use)
441 {
442 shadow->aligned_buffer_in_use = FALSE;
443 xvdd->aligned_buffer_in_use = FALSE;
444 }
445 /* put the srb back at the start of the queue */
446 InsertHeadList(&xvdd->srb_list, (PLIST_ENTRY)srb->SrbExtension);
447 put_shadow_on_freelist(xvdd, shadow);
448 KdPrint((__DRIVER_NAME " Out of gref's. Deferring\n"));
449 return;
450 }
451 offset = physical_address.LowPart & (PAGE_SIZE - 1);
452 length = min(PAGE_SIZE - offset, remaining);
453 ASSERT((offset & 511) == 0);
454 ASSERT((length & 511) == 0);
455 ASSERT(offset + length <= PAGE_SIZE);
456 shadow->req.seg[shadow->req.nr_segments].gref = gref;
457 shadow->req.seg[shadow->req.nr_segments].first_sect = (UCHAR)(offset >> 9);
458 shadow->req.seg[shadow->req.nr_segments].last_sect = (UCHAR)(((offset + length) >> 9) - 1);
459 remaining -= length;
460 ptr += length;
461 shadow->req.nr_segments++;
462 }
464 //KdPrint((__DRIVER_NAME " nr_segments = %d\n", shadow->req.nr_segments));
466 XenVbd_PutRequest(xvdd, &shadow->req);
468 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
469 if (notify)
470 {
471 //KdPrint((__DRIVER_NAME " Notifying\n"));
472 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
473 }
475 }
476 if (xvdd->shadow_free && !xvdd->aligned_buffer_in_use)
477 {
478 ScsiPortNotification(NextLuRequest, xvdd, 0, 0, 0);
479 }
480 //FUNCTION_EXIT();
481 }
483 static ULONG DDKAPI
484 XenVbd_HwScsiFindAdapter(PVOID DeviceExtension, PVOID HwContext, PVOID BusInformation, PCHAR ArgumentString, PPORT_CONFIGURATION_INFORMATION ConfigInfo, PBOOLEAN Again)
485 {
486 // PACCESS_RANGE AccessRange;
487 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
488 ULONG status;
489 // PXENPCI_XEN_DEVICE_DATA XenDeviceData;
490 PACCESS_RANGE access_range;
492 UNREFERENCED_PARAMETER(HwContext);
493 UNREFERENCED_PARAMETER(BusInformation);
494 UNREFERENCED_PARAMETER(ArgumentString);
496 FUNCTION_ENTER();
497 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
498 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
500 {
501 int i;
502 for (i = 0; i < sizeof(XENVBD_DEVICE_DATA); i++)
503 {
504 if (((PUCHAR)xvdd)[i])
505 {
506 KdPrint((__DRIVER_NAME " DeviceExtension is not zeroed!!!\n"));
507 break;
508 }
509 }
510 }
511 RtlZeroMemory(xvdd, sizeof(XENVBD_DEVICE_DATA));
512 *Again = FALSE;
514 KdPrint((__DRIVER_NAME " BusInterruptLevel = %d\n", ConfigInfo->BusInterruptLevel));
515 KdPrint((__DRIVER_NAME " BusInterruptVector = %03x\n", ConfigInfo->BusInterruptVector));
517 KdPrint((__DRIVER_NAME " NumberOfAccessRanges = %d\n", ConfigInfo->NumberOfAccessRanges));
518 if (ConfigInfo->NumberOfAccessRanges != 1 && ConfigInfo->NumberOfAccessRanges != 2)
519 {
520 return SP_RETURN_BAD_CONFIG;
521 }
523 access_range = &((*(ConfigInfo->AccessRanges))[0]);
524 KdPrint((__DRIVER_NAME " RangeStart = %08x, RangeLength = %08x\n",
525 access_range->RangeStart.LowPart, access_range->RangeLength));
526 xvdd->device_base = ScsiPortGetDeviceBase(
527 DeviceExtension,
528 ConfigInfo->AdapterInterfaceType,
529 ConfigInfo->SystemIoBusNumber,
530 access_range->RangeStart,
531 access_range->RangeLength,
532 !access_range->RangeInMemory);
533 if (!xvdd->device_base)
534 {
535 KdPrint((__DRIVER_NAME " Invalid config\n"));
536 FUNCTION_EXIT();
537 return SP_RETURN_BAD_CONFIG;
538 }
540 status = XenVbd_InitFromConfig(xvdd);
541 if (status != SP_RETURN_FOUND)
542 {
543 FUNCTION_EXIT();
544 return status;
545 }
547 xvdd->aligned_buffer_in_use = FALSE;
548 /* align the buffer to PAGE_SIZE */
549 xvdd->aligned_buffer = (PVOID)((ULONG_PTR)((PUCHAR)xvdd->aligned_buffer_data + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
550 KdPrint((__DRIVER_NAME " aligned_buffer_data = %p\n", xvdd->aligned_buffer_data));
551 KdPrint((__DRIVER_NAME " aligned_buffer = %p\n", xvdd->aligned_buffer));
553 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
554 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST - 1;
555 ConfigInfo->ScatterGather = TRUE;
557 #if 0
558 if (!dump_mode)
559 {
560 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
561 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST - 1;
562 ConfigInfo->ScatterGather = TRUE;
563 }
564 else
565 {
566 ConfigInfo->MaximumTransferLength = 4096;
567 ConfigInfo->NumberOfPhysicalBreaks = 0;
568 ConfigInfo->ScatterGather = FALSE;
569 }
570 #endif
571 ConfigInfo->AlignmentMask = 0;
572 ConfigInfo->NumberOfBuses = 1;
573 ConfigInfo->InitiatorBusId[0] = 1;
574 ConfigInfo->MaximumNumberOfLogicalUnits = 1;
575 ConfigInfo->MaximumNumberOfTargets = 2;
576 ConfigInfo->BufferAccessScsiPortControlled = TRUE;
577 if (ConfigInfo->Dma64BitAddresses == SCSI_DMA64_SYSTEM_SUPPORTED)
578 {
579 ConfigInfo->Master = TRUE;
580 ConfigInfo->Dma64BitAddresses = SCSI_DMA64_MINIPORT_SUPPORTED;
581 ConfigInfo->Dma32BitAddresses = FALSE;
582 KdPrint((__DRIVER_NAME " Dma64BitAddresses supported\n"));
583 }
584 else
585 {
586 ConfigInfo->Master = TRUE;
587 ConfigInfo->Dma32BitAddresses = TRUE;
588 KdPrint((__DRIVER_NAME " Dma64BitAddresses not supported\n"));
589 }
591 FUNCTION_EXIT();
593 return SP_RETURN_FOUND;
594 }
596 static VOID
597 XenVbd_StartRingDetection(PXENVBD_DEVICE_DATA xvdd)
598 {
599 blkif_request_t *req;
600 int notify;
602 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT1;
603 RtlZeroMemory(xvdd->sring->ring, PAGE_SIZE - FIELD_OFFSET(blkif_sring_t, ring));
604 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
605 req->operation = 0xff;
606 xvdd->ring.req_prod_pvt++;
607 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
608 req->operation = 0xff;
609 xvdd->ring.req_prod_pvt++;
611 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
612 if (notify)
613 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
614 }
616 static BOOLEAN
617 XenVbd_HwScsiInitialize(PVOID DeviceExtension)
618 {
619 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
621 FUNCTION_ENTER();
622 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
623 KdPrint((__DRIVER_NAME " dump_mode = %d\n", dump_mode));
625 if (!xvdd->inactive)
626 {
627 if (!dump_mode)
628 {
629 XenVbd_StartRingDetection(xvdd);
630 }
631 else
632 {
633 if (xvdd->cached_use_other)
634 {
635 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
636 xvdd->use_other = TRUE;
637 }
638 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
639 }
640 InitializeListHead(&xvdd->srb_list);
641 }
642 FUNCTION_EXIT();
644 return TRUE;
645 }
647 static ULONG
648 XenVbd_FillModePage(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
649 {
650 PMODE_PARAMETER_HEADER parameter_header = NULL;
651 PMODE_PARAMETER_HEADER10 parameter_header10 = NULL;
652 PMODE_PARAMETER_BLOCK param_block;
653 PMODE_FORMAT_PAGE format_page;
654 ULONG offset = 0;
655 UCHAR buffer[1024];
656 BOOLEAN valid_page = FALSE;
657 BOOLEAN cdb_llbaa;
658 BOOLEAN cdb_dbd;
659 UCHAR cdb_page_code;
660 USHORT cdb_allocation_length;
662 UNREFERENCED_PARAMETER(xvdd);
664 RtlZeroMemory(srb->DataBuffer, srb->DataTransferLength);
665 RtlZeroMemory(buffer, ARRAY_SIZE(buffer));
666 offset = 0;
668 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
670 switch (srb->Cdb[0])
671 {
672 case SCSIOP_MODE_SENSE:
673 cdb_llbaa = FALSE;
674 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
675 cdb_page_code = srb->Cdb[2] & 0x3f;
676 cdb_allocation_length = srb->Cdb[4];
677 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
678 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
679 parameter_header = (PMODE_PARAMETER_HEADER)&buffer[offset];
680 parameter_header->MediumType = 0;
681 parameter_header->DeviceSpecificParameter = 0;
682 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
683 {
684 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
685 parameter_header->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
686 }
687 offset += sizeof(MODE_PARAMETER_HEADER);
688 break;
689 case SCSIOP_MODE_SENSE10:
690 cdb_llbaa = (BOOLEAN)!!(srb->Cdb[1] & 16);
691 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
692 cdb_page_code = srb->Cdb[2] & 0x3f;
693 cdb_allocation_length = (srb->Cdb[7] << 8) | srb->Cdb[8];
694 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE10 llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
695 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
696 parameter_header10 = (PMODE_PARAMETER_HEADER10)&buffer[offset];
697 parameter_header10->MediumType = 0;
698 parameter_header10->DeviceSpecificParameter = 0;
699 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
700 {
701 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
702 parameter_header10->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
703 }
704 offset += sizeof(MODE_PARAMETER_HEADER10);
705 break;
706 default:
707 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE_WTF (%02x)\n", (ULONG)srb->Cdb[0]));
708 return FALSE;
709 }
711 if (!cdb_dbd)
712 {
713 param_block = (PMODE_PARAMETER_BLOCK)&buffer[offset];
714 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK)
715 {
716 if (xvdd->total_sectors >> 32)
717 {
718 param_block->DensityCode = 0xff;
719 param_block->NumberOfBlocks[0] = 0xff;
720 param_block->NumberOfBlocks[1] = 0xff;
721 param_block->NumberOfBlocks[2] = 0xff;
722 }
723 else
724 {
725 param_block->DensityCode = (UCHAR)((xvdd->total_sectors >> 24) & 0xff);
726 param_block->NumberOfBlocks[0] = (UCHAR)((xvdd->total_sectors >> 16) & 0xff);
727 param_block->NumberOfBlocks[1] = (UCHAR)((xvdd->total_sectors >> 8) & 0xff);
728 param_block->NumberOfBlocks[2] = (UCHAR)((xvdd->total_sectors >> 0) & 0xff);
729 }
730 param_block->BlockLength[0] = (UCHAR)((xvdd->bytes_per_sector >> 16) & 0xff);
731 param_block->BlockLength[1] = (UCHAR)((xvdd->bytes_per_sector >> 8) & 0xff);
732 param_block->BlockLength[2] = (UCHAR)((xvdd->bytes_per_sector >> 0) & 0xff);
733 }
734 offset += sizeof(MODE_PARAMETER_BLOCK);
735 }
736 switch (srb->Cdb[0])
737 {
738 case SCSIOP_MODE_SENSE:
739 parameter_header->BlockDescriptorLength = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER));
740 break;
741 case SCSIOP_MODE_SENSE10:
742 parameter_header10->BlockDescriptorLength[0] = (UCHAR)((offset - sizeof(MODE_PARAMETER_HEADER10)) >> 8);
743 parameter_header10->BlockDescriptorLength[1] = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER10));
744 break;
745 }
746 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_FORMAT_DEVICE || cdb_page_code == MODE_SENSE_RETURN_ALL))
747 {
748 valid_page = TRUE;
749 format_page = (PMODE_FORMAT_PAGE)&buffer[offset];
750 format_page->PageCode = MODE_PAGE_FORMAT_DEVICE;
751 format_page->PageLength = sizeof(MODE_FORMAT_PAGE) - FIELD_OFFSET(MODE_FORMAT_PAGE, PageLength);
752 /* 256 sectors per track */
753 format_page->SectorsPerTrack[0] = 0x01;
754 format_page->SectorsPerTrack[1] = 0x00;
755 /* xxx bytes per sector */
756 format_page->BytesPerPhysicalSector[0] = (UCHAR)(xvdd->bytes_per_sector >> 8);
757 format_page->BytesPerPhysicalSector[1] = (UCHAR)(xvdd->bytes_per_sector & 0xff);
758 format_page->HardSectorFormating = TRUE;
759 format_page->SoftSectorFormating = TRUE;
760 offset += sizeof(MODE_FORMAT_PAGE);
761 }
762 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_CACHING || cdb_page_code == MODE_SENSE_RETURN_ALL))
763 {
764 PMODE_CACHING_PAGE caching_page;
765 valid_page = TRUE;
766 caching_page = (PMODE_CACHING_PAGE)&buffer[offset];
767 caching_page->PageCode = MODE_PAGE_CACHING;
768 caching_page->PageLength = sizeof(MODE_CACHING_PAGE) - FIELD_OFFSET(MODE_CACHING_PAGE, PageLength);
769 // caching_page-> // all zeros is just fine... maybe
770 offset += sizeof(MODE_CACHING_PAGE);
771 }
772 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_MEDIUM_TYPES || cdb_page_code == MODE_SENSE_RETURN_ALL))
773 {
774 PUCHAR medium_types_page;
775 valid_page = TRUE;
776 medium_types_page = &buffer[offset];
777 medium_types_page[0] = MODE_PAGE_MEDIUM_TYPES;
778 medium_types_page[1] = 0x06;
779 medium_types_page[2] = 0;
780 medium_types_page[3] = 0;
781 medium_types_page[4] = 0;
782 medium_types_page[5] = 0;
783 medium_types_page[6] = 0;
784 medium_types_page[7] = 0;
785 offset += 8;
786 }
787 switch (srb->Cdb[0])
788 {
789 case SCSIOP_MODE_SENSE:
790 parameter_header->ModeDataLength = (UCHAR)(offset - 1);
791 break;
792 case SCSIOP_MODE_SENSE10:
793 parameter_header10->ModeDataLength[0] = (UCHAR)((offset - 2) >> 8);
794 parameter_header10->ModeDataLength[1] = (UCHAR)(offset - 2);
795 break;
796 }
798 if (!valid_page && cdb_page_code != MODE_SENSE_RETURN_ALL)
799 {
800 srb->SrbStatus = SRB_STATUS_ERROR;
801 }
802 else if(offset < srb->DataTransferLength)
803 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
804 else
805 srb->SrbStatus = SRB_STATUS_SUCCESS;
806 srb->DataTransferLength = min(srb->DataTransferLength, offset);
807 srb->ScsiStatus = 0;
808 memcpy(srb->DataBuffer, buffer, srb->DataTransferLength);
810 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
812 return TRUE;
813 }
815 static VOID
816 XenVbd_MakeSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb, UCHAR sense_key, UCHAR additional_sense_code)
817 {
818 PSENSE_DATA sd = srb->SenseInfoBuffer;
820 UNREFERENCED_PARAMETER(xvdd);
822 if (!srb->SenseInfoBuffer)
823 return;
825 sd->ErrorCode = 0x70;
826 sd->Valid = 1;
827 sd->SenseKey = sense_key;
828 sd->AdditionalSenseLength = sizeof(SENSE_DATA) - FIELD_OFFSET(SENSE_DATA, AdditionalSenseLength);
829 sd->AdditionalSenseCode = additional_sense_code;
830 return;
831 }
833 static VOID
834 XenVbd_MakeAutoSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
835 {
836 if (srb->SrbStatus == SRB_STATUS_SUCCESS || srb->SrbFlags & SRB_FLAGS_DISABLE_AUTOSENSE)
837 return;
838 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
839 srb->SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
840 }
842 static BOOLEAN DDKAPI
843 XenVbd_HwScsiInterrupt(PVOID DeviceExtension)
844 {
845 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
846 PSCSI_REQUEST_BLOCK srb;
847 RING_IDX i, rp;
848 ULONG j;
849 blkif_response_t *rep;
850 int block_count;
851 int more_to_do = TRUE;
852 blkif_shadow_t *shadow;
853 ULONG suspend_resume_state_pdo;
854 BOOLEAN last_interrupt = FALSE;
856 /* in dump mode I think we get called on a timer, not by an actual IRQ */
857 if (!dump_mode && !xvdd->vectors.EvtChn_AckEvent(xvdd->vectors.context, xvdd->event_channel, &last_interrupt))
858 return FALSE; /* interrupt was not for us */
860 suspend_resume_state_pdo = xvdd->device_state->suspend_resume_state_pdo;
861 KeMemoryBarrier();
863 if (suspend_resume_state_pdo != xvdd->device_state->suspend_resume_state_fdo)
864 {
865 FUNCTION_ENTER();
866 switch (suspend_resume_state_pdo)
867 {
868 case SR_STATE_SUSPENDING:
869 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_SUSPENDING\n"));
870 break;
871 case SR_STATE_RESUMING:
872 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_RESUMING\n"));
873 XenVbd_InitFromConfig(xvdd);
874 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
875 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
876 break;
877 case SR_STATE_RUNNING:
878 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
879 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
880 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
881 ScsiPortNotification(NextRequest, DeviceExtension);
882 default:
883 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
884 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
885 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
886 break;
887 }
888 KeMemoryBarrier();
889 }
891 if (xvdd->device_state->suspend_resume_state_fdo != SR_STATE_RUNNING)
892 {
893 return last_interrupt;
894 }
896 while (more_to_do)
897 {
898 rp = xvdd->ring.sring->rsp_prod;
899 KeMemoryBarrier();
900 for (i = xvdd->ring.rsp_cons; i < rp; i++)
901 {
902 rep = XenVbd_GetResponse(xvdd, i);
903 /*
904 * This code is to automatically detect if the backend is using the same
905 * bit width or a different bit width to us. Later versions of Xen do this
906 * via a xenstore value, but not all. That 0x0fffffff (notice
907 * that the msb is not actually set, so we don't have any problems with
908 * sign extending) is to signify the last entry on the right, which is
909 * different under 32 and 64 bits, and that is why we set it up there.
911 * To do the detection, we put two initial entries on the ring, with an op
912 * of 0xff (which is invalid). The first entry is mostly okay, but the
913 * second will be grossly misaligned if the backend bit width is different,
914 * and we detect this and switch frontend structures.
915 */
916 switch (xvdd->ring_detect_state)
917 {
918 case RING_DETECT_STATE_NOT_STARTED:
919 KdPrint((__DRIVER_NAME " premature IRQ\n"));
920 break;
921 case RING_DETECT_STATE_DETECT1:
922 KdPrint((__DRIVER_NAME " ring_detect_state = %d, index = %d, operation = %x, id = %lx, status = %d\n", xvdd->ring_detect_state, i, rep->operation, rep->id, rep->status));
923 KdPrint((__DRIVER_NAME " req_prod = %d, rsp_prod = %d, rsp_cons = %d\n", xvdd->sring->req_prod, xvdd->sring->rsp_prod, xvdd->ring.rsp_cons));
924 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT2;
925 break;
926 case RING_DETECT_STATE_DETECT2:
927 KdPrint((__DRIVER_NAME " ring_detect_state = %d, index = %d, operation = %x, id = %lx, status = %d\n", xvdd->ring_detect_state, i, rep->operation, rep->id, rep->status));
928 KdPrint((__DRIVER_NAME " req_prod = %d, rsp_prod = %d, rsp_cons = %d\n", xvdd->sring->req_prod, xvdd->sring->rsp_prod, xvdd->ring.rsp_cons));
929 *xvdd->event_channel_ptr |= 0x80000000;
930 if (rep->operation != 0xff)
931 {
932 KdPrint((__DRIVER_NAME " switching to 'other' ring size\n"));
933 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
934 xvdd->use_other = TRUE;
935 *xvdd->event_channel_ptr |= 0x40000000;
936 }
937 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
938 ScsiPortNotification(NextRequest, DeviceExtension);
939 break;
940 case RING_DETECT_STATE_COMPLETE:
941 shadow = &xvdd->shadows[rep->id];
942 srb = shadow->srb;
943 ASSERT(srb != NULL);
944 block_count = decode_cdb_length(srb);
945 block_count *= xvdd->bytes_per_sector / 512;
946 /* a few errors occur in dump mode because Xen refuses to allow us to map pages we are using for other stuff. Just ignore them */
947 if (rep->status == BLKIF_RSP_OKAY || (dump_mode && dump_mode_errors++ < DUMP_MODE_ERROR_LIMIT))
948 srb->SrbStatus = SRB_STATUS_SUCCESS;
949 else
950 {
951 KdPrint((__DRIVER_NAME " Xen Operation returned error\n"));
952 if (decode_cdb_is_read(srb))
953 KdPrint((__DRIVER_NAME " Operation = Read\n"));
954 else
955 KdPrint((__DRIVER_NAME " Operation = Write\n"));
956 if (dump_mode)
957 {
958 KdPrint((__DRIVER_NAME " Sector = %08X, Count = %d\n", (ULONG)shadow->req.sector_number, block_count));
959 KdPrint((__DRIVER_NAME " DataBuffer = %p, aligned_buffer = %p\n", srb->DataBuffer, xvdd->aligned_buffer));
960 KdPrint((__DRIVER_NAME " Physical = %08x%08x\n", MmGetPhysicalAddress(srb->DataBuffer).HighPart, MmGetPhysicalAddress(srb->DataBuffer).LowPart));
961 KdPrint((__DRIVER_NAME " PFN = %08x\n", (ULONG)(MmGetPhysicalAddress(srb->DataBuffer).QuadPart >> PAGE_SHIFT)));
963 for (j = 0; j < shadow->req.nr_segments; j++)
964 {
965 KdPrint((__DRIVER_NAME " gref = %d\n", shadow->req.seg[j].gref));
966 KdPrint((__DRIVER_NAME " first_sect = %d\n", shadow->req.seg[j].first_sect));
967 KdPrint((__DRIVER_NAME " last_sect = %d\n", shadow->req.seg[j].last_sect));
968 }
969 }
970 srb->SrbStatus = SRB_STATUS_ERROR;
971 srb->ScsiStatus = 0x02;
972 xvdd->last_sense_key = SCSI_SENSE_MEDIUM_ERROR;
973 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
974 XenVbd_MakeAutoSense(xvdd, srb);
975 }
976 if (shadow->aligned_buffer_in_use)
977 {
978 ASSERT(xvdd->aligned_buffer_in_use);
979 xvdd->aligned_buffer_in_use = FALSE;
980 if (decode_cdb_is_read(srb))
981 memcpy(srb->DataBuffer, xvdd->aligned_buffer, block_count * 512);
982 //KdPrint((__DRIVER_NAME " Completed use of buffer\n"));
983 }
985 #if 0
986 if (xvdd->aligned_buffer_in_use)
987 {
988 KdPrint((__DRIVER_NAME " Completed request while aligned buffer in use\n"));
989 }
990 #endif
991 for (j = 0; j < shadow->req.nr_segments; j++)
992 {
993 if (dump_mode)
994 {
995 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
996 shadow->req.seg[j].gref, TRUE, (ULONG)'XPDO');
997 }
998 else
999 {
1000 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1001 shadow->req.seg[j].gref, FALSE, (ULONG)'XVBD');
1004 shadow->aligned_buffer_in_use = FALSE;
1005 shadow->srb = NULL;
1006 put_shadow_on_freelist(xvdd, shadow);
1007 //KdPrint((__DRIVER_NAME " B RequestComplete srb = %p\n", srb));
1008 ScsiPortNotification(RequestComplete, xvdd, srb);
1009 break;
1013 xvdd->ring.rsp_cons = i;
1014 if (i != xvdd->ring.req_prod_pvt)
1016 RING_FINAL_CHECK_FOR_RESPONSES(&xvdd->ring, more_to_do);
1018 else
1020 xvdd->ring.sring->rsp_event = i + 1;
1021 more_to_do = FALSE;
1025 XenVbd_PutQueuedSrbsOnRing(xvdd);
1027 if (suspend_resume_state_pdo == SR_STATE_SUSPENDING)
1029 if (xvdd->inactive || xvdd->shadow_free == SHADOW_ENTRIES)
1031 /* all entries are purged from the list (or we are inactive). ready to suspend */
1032 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
1033 KeMemoryBarrier();
1034 KdPrint((__DRIVER_NAME " Set fdo state SR_STATE_SUSPENDING\n"));
1035 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xvdd->device_state->pdo_event_channel));
1036 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
1038 FUNCTION_EXIT();
1041 return last_interrupt;
1044 static BOOLEAN DDKAPI
1045 XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK srb)
1047 PUCHAR data_buffer;
1048 //ULONG data_buffer_length;
1049 PCDB cdb;
1050 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1051 ULONG data_transfer_length = srb->DataTransferLength;
1053 if (xvdd->inactive)
1055 KdPrint((__DRIVER_NAME " Inactive srb->Function = %08X\n", srb->Function));
1056 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1057 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1058 ScsiPortNotification(NextRequest, DeviceExtension);
1059 return TRUE;
1062 // If we haven't enumerated all the devices yet then just defer the request
1063 if (xvdd->ring_detect_state < RING_DETECT_STATE_COMPLETE)
1065 srb->SrbStatus = SRB_STATUS_BUSY;
1066 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1067 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Still figuring out ring)\n"));
1068 return TRUE;
1071 if (xvdd->device_state->suspend_resume_state_pdo != SR_STATE_RUNNING)
1073 KdPrint((__DRIVER_NAME " --> HwScsiStartIo (Suspending/Resuming)\n"));
1074 srb->SrbStatus = SRB_STATUS_BUSY;
1075 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1076 KdPrint((__DRIVER_NAME " <-- HwScsiStartIo (Suspending/Resuming)\n"));
1077 return TRUE;
1080 if (srb->PathId != 0 || srb->TargetId != 0)
1082 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1083 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1084 ScsiPortNotification(NextRequest, DeviceExtension);
1085 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Out of bounds)\n"));
1086 return TRUE;
1089 switch (srb->Function)
1091 case SRB_FUNCTION_EXECUTE_SCSI:
1092 cdb = (PCDB)srb->Cdb;
1094 switch(cdb->CDB6GENERIC.OperationCode)
1096 case SCSIOP_TEST_UNIT_READY:
1097 if (dump_mode)
1098 KdPrint((__DRIVER_NAME " Command = TEST_UNIT_READY\n"));
1099 srb->SrbStatus = SRB_STATUS_SUCCESS;
1100 srb->ScsiStatus = 0;
1101 break;
1102 case SCSIOP_INQUIRY:
1103 if (dump_mode)
1105 //PHYSICAL_ADDRESS physical;
1106 KdPrint((__DRIVER_NAME " Command = INQUIRY\n"));
1107 //KdPrint((__DRIVER_NAME " srb->Databuffer = %p\n", srb->DataBuffer));
1108 //physical = ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length);
1109 //KdPrint((__DRIVER_NAME " ScsiPortGetPhysicalAddress = %08x:%08x\n", physical.LowPart, physical.HighPart));
1111 // KdPrint((__DRIVER_NAME " (LUN = %d, EVPD = %d, Page Code = %02X)\n", srb->Cdb[1] >> 5, srb->Cdb[1] & 1, srb->Cdb[2]));
1112 // KdPrint((__DRIVER_NAME " (Length = %d)\n", srb->DataTransferLength));
1114 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1115 data_buffer = srb->DataBuffer;
1116 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1117 srb->SrbStatus = SRB_STATUS_SUCCESS;
1118 switch (xvdd->device_type)
1120 case XENVBD_DEVICETYPE_DISK:
1121 if ((srb->Cdb[1] & 1) == 0)
1123 if (srb->Cdb[2])
1125 srb->SrbStatus = SRB_STATUS_ERROR;
1127 else
1129 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1130 id->DeviceType = DIRECT_ACCESS_DEVICE;
1131 id->Versions = 4; /* minimum that WHQL says we must support */
1132 id->ResponseDataFormat = 2; /* not sure about this but WHQL complains otherwise */
1133 id->HiSupport = 1; /* WHQL test says we should set this */
1134 //id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1135 id->AdditionalLength = sizeof(INQUIRYDATA) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength) - 1;
1136 id->CommandQueue = 1;
1137 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1138 memcpy(id->ProductId, scsi_disk_model, 16); // product id
1139 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1140 data_transfer_length = sizeof(INQUIRYDATA);
1143 else
1145 switch (srb->Cdb[2])
1147 case VPD_SUPPORTED_PAGES: /* list of pages we support */
1148 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1149 data_buffer[1] = VPD_SUPPORTED_PAGES;
1150 data_buffer[2] = 0x00;
1151 data_buffer[3] = 2;
1152 data_buffer[4] = 0x00;
1153 data_buffer[5] = 0x80;
1154 data_transfer_length = 6;
1155 break;
1156 case VPD_SERIAL_NUMBER: /* serial number */
1157 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1158 data_buffer[1] = VPD_SERIAL_NUMBER;
1159 data_buffer[2] = 0x00;
1160 data_buffer[3] = 8;
1161 memset(&data_buffer[4], ' ', 8);
1162 data_transfer_length = 12;
1163 break;
1164 case VPD_DEVICE_IDENTIFIERS: /* identification - we don't support any so just return zero */
1165 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1166 data_buffer[1] = VPD_DEVICE_IDENTIFIERS;
1167 data_buffer[2] = 0x00;
1168 data_buffer[3] = 4 + (UCHAR)strlen(xvdd->vectors.path); /* length */
1169 data_buffer[4] = 2; /* ASCII */
1170 data_buffer[5] = 1; /* VendorId */
1171 data_buffer[6] = 0;
1172 data_buffer[7] = (UCHAR)strlen(xvdd->vectors.path);
1173 memcpy(&data_buffer[8], xvdd->vectors.path, strlen(xvdd->vectors.path));
1174 data_transfer_length = (ULONG)(8 + strlen(xvdd->vectors.path));
1175 break;
1176 default:
1177 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1178 srb->SrbStatus = SRB_STATUS_ERROR;
1179 break;
1182 break;
1183 case XENVBD_DEVICETYPE_CDROM:
1184 if ((srb->Cdb[1] & 1) == 0)
1186 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1187 id->DeviceType = READ_ONLY_DIRECT_ACCESS_DEVICE;
1188 id->RemovableMedia = 1;
1189 id->Versions = 3;
1190 id->ResponseDataFormat = 0;
1191 id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1192 id->CommandQueue = 1;
1193 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1194 memcpy(id->ProductId, scsi_cdrom_model, 16); // product id
1195 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1197 else
1199 switch (srb->Cdb[2])
1201 case 0x00:
1202 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1203 data_buffer[1] = 0x00;
1204 data_buffer[2] = 0x00;
1205 data_buffer[3] = 2;
1206 data_buffer[4] = 0x00;
1207 data_buffer[5] = 0x80;
1208 break;
1209 case 0x80:
1210 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1211 data_buffer[1] = 0x80;
1212 data_buffer[2] = 0x00;
1213 data_buffer[3] = 8;
1214 data_buffer[4] = 0x31;
1215 data_buffer[5] = 0x32;
1216 data_buffer[6] = 0x33;
1217 data_buffer[7] = 0x34;
1218 data_buffer[8] = 0x35;
1219 data_buffer[9] = 0x36;
1220 data_buffer[10] = 0x37;
1221 data_buffer[11] = 0x38;
1222 break;
1223 default:
1224 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1225 srb->SrbStatus = SRB_STATUS_ERROR;
1226 break;
1229 break;
1230 default:
1231 //KdPrint((__DRIVER_NAME " Unknown DeviceType %02x requested\n", xvdd->device_type));
1232 srb->SrbStatus = SRB_STATUS_ERROR;
1233 break;
1235 break;
1236 case SCSIOP_READ_CAPACITY:
1237 if (dump_mode)
1238 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1239 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1240 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1241 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1242 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1243 data_buffer = srb->DataBuffer;
1244 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1245 if ((xvdd->total_sectors - 1) >> 32)
1247 data_buffer[0] = 0xff;
1248 data_buffer[1] = 0xff;
1249 data_buffer[2] = 0xff;
1250 data_buffer[3] = 0xff;
1252 else
1254 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1255 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1256 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1257 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1259 data_buffer[4] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1260 data_buffer[5] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1261 data_buffer[6] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1262 data_buffer[7] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1263 srb->ScsiStatus = 0;
1264 srb->SrbStatus = SRB_STATUS_SUCCESS;
1265 break;
1266 case SCSIOP_READ_CAPACITY16:
1267 if (dump_mode)
1268 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1269 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1270 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1271 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1272 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1273 data_buffer = srb->DataBuffer;
1274 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1275 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 56) & 0xff;
1276 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 48) & 0xff;
1277 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 40) & 0xff;
1278 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 32) & 0xff;
1279 data_buffer[4] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1280 data_buffer[5] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1281 data_buffer[6] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1282 data_buffer[7] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1283 data_buffer[8] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1284 data_buffer[9] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1285 data_buffer[10] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1286 data_buffer[11] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1287 srb->ScsiStatus = 0;
1288 srb->SrbStatus = SRB_STATUS_SUCCESS;
1289 break;
1290 case SCSIOP_MODE_SENSE:
1291 case SCSIOP_MODE_SENSE10:
1292 if (dump_mode)
1293 KdPrint((__DRIVER_NAME " Command = MODE_SENSE (DBD = %d, PC = %d, Page Code = %02x)\n", srb->Cdb[1] & 0x08, srb->Cdb[2] & 0xC0, srb->Cdb[2] & 0x3F));
1294 XenVbd_FillModePage(xvdd, srb);
1295 break;
1296 case SCSIOP_READ:
1297 case SCSIOP_READ16:
1298 case SCSIOP_WRITE:
1299 case SCSIOP_WRITE16:
1300 XenVbd_PutSrbOnList(xvdd, srb);
1301 XenVbd_PutQueuedSrbsOnRing(xvdd);
1302 break;
1303 case SCSIOP_VERIFY:
1304 case SCSIOP_VERIFY16:
1305 // Should we do more here?
1306 if (dump_mode)
1307 KdPrint((__DRIVER_NAME " Command = VERIFY\n"));
1308 srb->SrbStatus = SRB_STATUS_SUCCESS;
1309 break;
1310 case SCSIOP_REPORT_LUNS:
1311 if (dump_mode)
1312 KdPrint((__DRIVER_NAME " Command = REPORT_LUNS\n"));
1313 srb->SrbStatus = SRB_STATUS_SUCCESS;;
1314 break;
1315 case SCSIOP_REQUEST_SENSE:
1316 if (dump_mode)
1317 KdPrint((__DRIVER_NAME " Command = REQUEST_SENSE\n"));
1318 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
1319 srb->SrbStatus = SRB_STATUS_SUCCESS;
1320 break;
1321 case SCSIOP_READ_TOC:
1322 if (dump_mode)
1323 KdPrint((__DRIVER_NAME " Command = READ_TOC\n"));
1324 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1325 data_buffer = srb->DataBuffer;
1326 // DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
1327 /*
1328 #define READ_TOC_FORMAT_TOC 0x00
1329 #define READ_TOC_FORMAT_SESSION 0x01
1330 #define READ_TOC_FORMAT_FULL_TOC 0x02
1331 #define READ_TOC_FORMAT_PMA 0x03
1332 #define READ_TOC_FORMAT_ATIP 0x04
1333 */
1334 // KdPrint((__DRIVER_NAME " Msf = %d\n", cdb->READ_TOC.Msf));
1335 // KdPrint((__DRIVER_NAME " LogicalUnitNumber = %d\n", cdb->READ_TOC.LogicalUnitNumber));
1336 // KdPrint((__DRIVER_NAME " Format2 = %d\n", cdb->READ_TOC.Format2));
1337 // KdPrint((__DRIVER_NAME " StartingTrack = %d\n", cdb->READ_TOC.StartingTrack));
1338 // KdPrint((__DRIVER_NAME " AllocationLength = %d\n", (cdb->READ_TOC.AllocationLength[0] << 8) | cdb->READ_TOC.AllocationLength[1]));
1339 // KdPrint((__DRIVER_NAME " Control = %d\n", cdb->READ_TOC.Control));
1340 // KdPrint((__DRIVER_NAME " Format = %d\n", cdb->READ_TOC.Format));
1341 switch (cdb->READ_TOC.Format2)
1343 case READ_TOC_FORMAT_TOC:
1344 data_buffer[0] = 0; // length MSB
1345 data_buffer[1] = 10; // length LSB
1346 data_buffer[2] = 1; // First Track
1347 data_buffer[3] = 1; // Last Track
1348 data_buffer[4] = 0; // Reserved
1349 data_buffer[5] = 0x14; // current position data + uninterrupted data
1350 data_buffer[6] = 1; // last complete track
1351 data_buffer[7] = 0; // reserved
1352 data_buffer[8] = 0; // MSB Block
1353 data_buffer[9] = 0;
1354 data_buffer[10] = 0;
1355 data_buffer[11] = 0; // LSB Block
1356 srb->SrbStatus = SRB_STATUS_SUCCESS;
1357 break;
1358 case READ_TOC_FORMAT_SESSION:
1359 case READ_TOC_FORMAT_FULL_TOC:
1360 case READ_TOC_FORMAT_PMA:
1361 case READ_TOC_FORMAT_ATIP:
1362 srb->SrbStatus = SRB_STATUS_ERROR;
1363 break;
1365 break;
1366 case SCSIOP_START_STOP_UNIT:
1367 KdPrint((__DRIVER_NAME " Command = SCSIOP_START_STOP_UNIT\n"));
1368 srb->SrbStatus = SRB_STATUS_SUCCESS;
1369 break;
1370 case SCSIOP_RESERVE_UNIT:
1371 KdPrint((__DRIVER_NAME " Command = SCSIOP_RESERVE_UNIT\n"));
1372 srb->SrbStatus = SRB_STATUS_SUCCESS;
1373 break;
1374 case SCSIOP_RELEASE_UNIT:
1375 KdPrint((__DRIVER_NAME " Command = SCSIOP_RELEASE_UNIT\n"));
1376 srb->SrbStatus = SRB_STATUS_SUCCESS;
1377 break;
1378 default:
1379 KdPrint((__DRIVER_NAME " Unhandled EXECUTE_SCSI Command = %02X\n", srb->Cdb[0]));
1380 srb->SrbStatus = SRB_STATUS_ERROR;
1381 break;
1383 if (srb->SrbStatus == SRB_STATUS_ERROR)
1385 KdPrint((__DRIVER_NAME " EXECUTE_SCSI Command = %02X returned error %02x\n", srb->Cdb[0], xvdd->last_sense_key));
1386 if (xvdd->last_sense_key == SCSI_SENSE_NO_SENSE)
1388 xvdd->last_sense_key = SCSI_SENSE_ILLEGAL_REQUEST;
1389 xvdd->last_additional_sense_code = SCSI_ADSENSE_INVALID_CDB;
1391 srb->ScsiStatus = 0x02;
1392 XenVbd_MakeAutoSense(xvdd, srb);
1393 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1394 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1396 ScsiPortNotification(NextRequest, DeviceExtension);
1399 else if (srb->SrbStatus != SRB_STATUS_PENDING)
1401 if (srb->SrbStatus == SRB_STATUS_SUCCESS && data_transfer_length < srb->DataTransferLength)
1403 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
1404 srb->DataTransferLength = data_transfer_length;
1406 xvdd->last_sense_key = SCSI_SENSE_NO_SENSE;
1407 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1408 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1409 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1411 ScsiPortNotification(NextRequest, DeviceExtension);
1414 break;
1415 case SRB_FUNCTION_IO_CONTROL:
1416 KdPrint((__DRIVER_NAME " SRB_FUNCTION_IO_CONTROL\n"));
1417 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1418 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1419 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1421 ScsiPortNotification(NextRequest, DeviceExtension);
1423 break;
1424 case SRB_FUNCTION_FLUSH:
1425 KdPrint((__DRIVER_NAME " SRB_FUNCTION_FLUSH %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1426 srb->SrbStatus = SRB_STATUS_SUCCESS;
1427 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1428 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1430 ScsiPortNotification(NextRequest, DeviceExtension);
1432 break;
1433 case SRB_FUNCTION_SHUTDOWN:
1434 KdPrint((__DRIVER_NAME " SRB_FUNCTION_SHUTDOWN %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1435 srb->SrbStatus = SRB_STATUS_SUCCESS;
1436 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1437 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1439 ScsiPortNotification(NextRequest, DeviceExtension);
1441 break;
1442 default:
1443 KdPrint((__DRIVER_NAME " Unhandled srb->Function = %08X\n", srb->Function));
1444 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1445 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1446 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1448 ScsiPortNotification(NextRequest, DeviceExtension);
1450 break;
1453 //FUNCTION_EXIT();
1454 return TRUE;
1457 static BOOLEAN
1458 XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId)
1460 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1462 UNREFERENCED_PARAMETER(DeviceExtension);
1463 UNREFERENCED_PARAMETER(PathId);
1465 FUNCTION_ENTER();
1467 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1468 if (xvdd->ring_detect_state == RING_DETECT_STATE_COMPLETE && xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1470 ScsiPortNotification(NextRequest, DeviceExtension);
1473 FUNCTION_EXIT();
1476 return TRUE;
1479 static BOOLEAN
1480 XenVbd_HwScsiAdapterState(PVOID DeviceExtension, PVOID Context, BOOLEAN SaveState)
1482 UNREFERENCED_PARAMETER(DeviceExtension);
1483 UNREFERENCED_PARAMETER(Context);
1484 UNREFERENCED_PARAMETER(SaveState);
1486 FUNCTION_ENTER();
1487 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1489 FUNCTION_EXIT();
1491 return TRUE;
1494 static SCSI_ADAPTER_CONTROL_STATUS DDKAPI
1495 XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters)
1497 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1498 SCSI_ADAPTER_CONTROL_STATUS Status = ScsiAdapterControlSuccess;
1499 PSCSI_SUPPORTED_CONTROL_TYPE_LIST SupportedControlTypeList;
1500 //KIRQL OldIrql;
1502 FUNCTION_ENTER();
1503 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1504 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
1506 switch (ControlType)
1508 case ScsiQuerySupportedControlTypes:
1509 SupportedControlTypeList = (PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters;
1510 KdPrint((__DRIVER_NAME " ScsiQuerySupportedControlTypes (Max = %d)\n", SupportedControlTypeList->MaxControlType));
1511 SupportedControlTypeList->SupportedTypeList[ScsiQuerySupportedControlTypes] = TRUE;
1512 SupportedControlTypeList->SupportedTypeList[ScsiStopAdapter] = TRUE;
1513 SupportedControlTypeList->SupportedTypeList[ScsiRestartAdapter] = TRUE;
1514 break;
1515 case ScsiStopAdapter:
1516 KdPrint((__DRIVER_NAME " ScsiStopAdapter\n"));
1517 /* I don't think we actually have to do anything here... xenpci cleans up all the xenbus stuff for us */
1518 break;
1519 case ScsiRestartAdapter:
1520 KdPrint((__DRIVER_NAME " ScsiRestartAdapter\n"));
1521 if (!xvdd->inactive)
1523 if (XenVbd_InitFromConfig(xvdd) != SP_RETURN_FOUND)
1524 KeBugCheckEx(DATA_COHERENCY_EXCEPTION, 0, (ULONG_PTR) xvdd, 0, 0);
1525 XenVbd_StartRingDetection(xvdd);
1527 break;
1528 case ScsiSetBootConfig:
1529 KdPrint((__DRIVER_NAME " ScsiSetBootConfig\n"));
1530 break;
1531 case ScsiSetRunningConfig:
1532 KdPrint((__DRIVER_NAME " ScsiSetRunningConfig\n"));
1533 break;
1534 default:
1535 KdPrint((__DRIVER_NAME " UNKNOWN\n"));
1536 break;
1539 FUNCTION_EXIT();
1541 return Status;
1544 NTSTATUS
1545 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
1547 ULONG status;
1548 HW_INITIALIZATION_DATA HwInitializationData;
1549 PVOID driver_extension;
1550 PUCHAR ptr;
1551 OBJECT_ATTRIBUTES oa;
1552 HANDLE service_handle;
1553 UNICODE_STRING param_name;
1554 HANDLE param_handle;
1555 UNICODE_STRING value_name;
1556 CHAR buf[256];
1557 ULONG buf_len;
1558 PKEY_VALUE_PARTIAL_INFORMATION kpv;
1560 FUNCTION_ENTER();
1561 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1562 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
1564 /* RegistryPath == NULL when we are invoked as a crash dump driver */
1565 if (!RegistryPath)
1567 dump_mode = TRUE;
1570 if (!dump_mode)
1572 IoAllocateDriverObjectExtension(DriverObject, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC), PAGE_SIZE, &driver_extension);
1573 ptr = driver_extension;
1574 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
1575 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "ring-ref", NULL, NULL);
1576 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_IRQ, "event-channel", NULL, NULL);
1577 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_FRONT, "device-type", NULL, NULL);
1578 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mode", NULL, NULL);
1579 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sectors", NULL, NULL);
1580 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sector-size", NULL, NULL);
1581 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_GRANT_ENTRIES, NULL, ULongToPtr(BLKIF_MAX_SEGMENTS_PER_REQUEST), NULL); /* for use in crash dump */
1582 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1584 InitializeObjectAttributes(&oa, RegistryPath, OBJ_CASE_INSENSITIVE, NULL, NULL);
1585 status = ZwOpenKey(&service_handle, KEY_READ, &oa);
1586 if(!NT_SUCCESS(status))
1588 KdPrint((__DRIVER_NAME " ZwOpenKey(Service) returned %08x\n", status));
1590 else
1592 RtlInitUnicodeString(&param_name, L"Parameters");
1593 InitializeObjectAttributes(&oa, &param_name, OBJ_CASE_INSENSITIVE, service_handle, NULL);
1594 status = ZwOpenKey(&param_handle, KEY_READ, &oa);
1595 if(!NT_SUCCESS(status))
1597 KdPrint((__DRIVER_NAME " ZwOpenKey(Parameters) returned %08x\n", status));
1599 else
1601 kpv = (PKEY_VALUE_PARTIAL_INFORMATION)buf;
1602 RtlFillMemory(scsi_device_manufacturer, 8, ' ');
1603 RtlFillMemory(scsi_disk_model, 16, ' ');
1604 RtlFillMemory(scsi_cdrom_model, 16, ' ');
1606 RtlInitUnicodeString(&value_name, L"Manufacturer");
1607 buf_len = 256;
1608 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1609 if(NT_SUCCESS(status))
1610 wcstombs(scsi_device_manufacturer, (PWCHAR)kpv->Data, min(kpv->DataLength, 8));
1611 else
1612 RtlStringCbCopyA(scsi_device_manufacturer, 8, "XEN ");
1614 RtlInitUnicodeString(&value_name, L"Disk_Model");
1615 buf_len = 256;
1616 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1617 if(NT_SUCCESS(status))
1618 wcstombs(scsi_disk_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1619 else
1620 RtlStringCbCopyA(scsi_disk_model, 16, "PV DISK ");
1622 RtlInitUnicodeString(&value_name, L"CDROM_Model");
1623 buf_len = 256;
1624 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1625 if(NT_SUCCESS(status))
1626 wcstombs(scsi_cdrom_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1627 else
1628 RtlStringCbCopyA(scsi_cdrom_model, 16, "PV CDROM ");
1630 ZwClose(service_handle);
1634 RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
1636 HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
1637 HwInitializationData.AdapterInterfaceType = PNPBus;
1638 HwInitializationData.DeviceExtensionSize = sizeof(XENVBD_DEVICE_DATA);
1639 HwInitializationData.SpecificLuExtensionSize = 0;
1640 HwInitializationData.SrbExtensionSize = sizeof(srb_list_entry_t);
1641 HwInitializationData.NumberOfAccessRanges = 1;
1642 HwInitializationData.MapBuffers = TRUE;
1643 HwInitializationData.NeedPhysicalAddresses = FALSE;
1644 HwInitializationData.TaggedQueuing = TRUE;
1645 HwInitializationData.AutoRequestSense = TRUE;
1646 HwInitializationData.MultipleRequestPerLu = TRUE;
1647 HwInitializationData.ReceiveEvent = FALSE;
1648 HwInitializationData.VendorIdLength = 0;
1649 HwInitializationData.VendorId = NULL;
1650 HwInitializationData.DeviceIdLength = 0;
1651 HwInitializationData.DeviceId = NULL;
1653 HwInitializationData.HwInitialize = XenVbd_HwScsiInitialize;
1654 HwInitializationData.HwStartIo = XenVbd_HwScsiStartIo;
1655 HwInitializationData.HwInterrupt = XenVbd_HwScsiInterrupt;
1656 HwInitializationData.HwFindAdapter = XenVbd_HwScsiFindAdapter;
1657 HwInitializationData.HwResetBus = XenVbd_HwScsiResetBus;
1658 HwInitializationData.HwDmaStarted = NULL;
1659 HwInitializationData.HwAdapterState = NULL; //XenVbd_HwScsiAdapterState;
1660 HwInitializationData.HwAdapterControl = XenVbd_HwScsiAdapterControl;
1662 status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
1664 if(!NT_SUCCESS(status))
1666 KdPrint((__DRIVER_NAME " ScsiPortInitialize failed with status 0x%08x\n", status));
1669 FUNCTION_EXIT();
1671 return status;