win-pvdrivers

view xenvbd/xenvbd.c @ 816:4d1539643494

Add some debug for dump mode buffer alignment
Remove old coinstaller stuff from inf file
Fix build errors under Win2K build environment
Check for non-zero LUN
author James Harper <james.harper@bendigoit.com.au>
date Sun Oct 10 10:29:29 2010 +1100 (2010-10-10)
parents 551d17b09bc1
children d931c38fc0f2
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_blocks = 0;
345 ULONG max_dump_mode_length = 0;
347 static VOID
348 XenVbd_PutSrbOnList(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
349 {
350 srb_list_entry_t *list_entry = srb->SrbExtension;
351 list_entry->srb = srb;
352 InsertTailList(&xvdd->srb_list, (PLIST_ENTRY)list_entry);
353 }
355 static VOID
356 XenVbd_PutQueuedSrbsOnRing(PXENVBD_DEVICE_DATA xvdd)
357 {
358 PSCSI_REQUEST_BLOCK srb;
359 srb_list_entry_t *srb_entry;
360 ULONGLONG sector_number;
361 ULONG block_count;
362 blkif_shadow_t *shadow;
363 ULONG remaining, offset, length;
364 grant_ref_t gref;
365 PUCHAR ptr;
366 int notify;
367 int i;
369 //FUNCTION_ENTER();
371 if (xvdd->aligned_buffer_in_use)
372 return;
374 while(xvdd->shadow_free && (srb_entry = (srb_list_entry_t *)RemoveHeadList(&xvdd->srb_list)) != (srb_list_entry_t *)&xvdd->srb_list)
375 {
376 srb = srb_entry->srb;
377 block_count = decode_cdb_length(srb);;
378 block_count *= xvdd->bytes_per_sector / 512;
379 sector_number = decode_cdb_sector(srb);
380 sector_number *= xvdd->bytes_per_sector / 512;
382 /* look for pending writes that overlap this one */
383 /* we get warnings from drbd if we don't */
384 for (i = 0; i < MAX_SHADOW_ENTRIES; i++)
385 {
386 PSCSI_REQUEST_BLOCK srb2;
387 ULONGLONG sector_number2;
388 ULONG block_count2;
390 srb2 = xvdd->shadows[i].srb;
391 if (!srb2)
392 continue;
393 if (decode_cdb_is_read(srb2))
394 continue;
395 block_count2 = decode_cdb_length(srb2);;
396 block_count2 *= xvdd->bytes_per_sector / 512;
397 sector_number2 = decode_cdb_sector(srb2);
398 sector_number2 *= xvdd->bytes_per_sector / 512;
400 if (sector_number < sector_number2 && sector_number + block_count <= sector_number2)
401 continue;
402 if (sector_number2 < sector_number && sector_number2 + block_count2 <= sector_number)
403 continue;
405 #if 0
406 /* check if the data being written is identical to the data in the pipe */
407 {
408 PUCHAR buf, buf2;
409 ULONG byte_count;
410 int j;
412 buf = (PUCHAR)srb->DataBuffer + (max(sector_number, sector_number2) - sector_number) * xvdd->bytes_per_sector;
413 buf2 = (PUCHAR)srb2->DataBuffer + (max(sector_number, sector_number2) - sector_number2) * xvdd->bytes_per_sector;
414 byte_count = (ULONG)(min(sector_number + block_count, sector_number2 + block_count2) - max(sector_number, sector_number2)) * xvdd->bytes_per_sector;
415 for (j = 0; j < (int)byte_count; j++)
416 {
417 if (buf[j] != buf2[j])
418 break;
419 }
420 }
421 #endif
423 KdPrint((__DRIVER_NAME " Concurrent outstanding write detected (%I64d, %d) (%I64d, %d)\n",
424 sector_number, block_count, sector_number2, block_count2));
425 /* put the srb back at the start of the queue */
426 InsertHeadList(&xvdd->srb_list, (PLIST_ENTRY)srb->SrbExtension);
427 return; /* stall the queue */
428 }
430 remaining = block_count * 512;
431 shadow = get_shadow_from_freelist(xvdd);
432 ASSERT(shadow);
433 ASSERT(!shadow->aligned_buffer_in_use);
434 ASSERT(!shadow->srb);
435 shadow->req.sector_number = sector_number;
436 shadow->req.handle = 0;
437 shadow->req.operation = decode_cdb_is_read(srb)?BLKIF_OP_READ:BLKIF_OP_WRITE;
438 shadow->req.nr_segments = 0;
439 shadow->srb = srb;
441 if ((ULONG_PTR)srb->DataBuffer & 511)
442 {
443 if (dump_mode)
444 KdPrint((__DRIVER_NAME " unaligned dump mode buffer = %d bytes\n", block_count * 512));
445 ASSERT(!dump_mode || block_count * 512 < BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE * PAGE_SIZE);
446 xvdd->aligned_buffer_in_use = TRUE;
447 ptr = xvdd->aligned_buffer;
448 if (!decode_cdb_is_read(srb))
449 memcpy(ptr, srb->DataBuffer, block_count * 512);
450 shadow->aligned_buffer_in_use = TRUE;
451 }
452 else
453 {
454 ptr = srb->DataBuffer;
455 shadow->aligned_buffer_in_use = FALSE;
456 }
458 if (dump_mode && block_count > max_dump_mode_blocks)
459 {
460 max_dump_mode_blocks = block_count;
461 KdPrint((__DRIVER_NAME " max_dump_mode_blocks = %d\n", max_dump_mode_blocks));
462 }
463 if (dump_mode && srb->DataTransferLength > max_dump_mode_length)
464 {
465 max_dump_mode_length = srb->DataTransferLength;
466 KdPrint((__DRIVER_NAME " max_dump_mode_length = %d\n", max_dump_mode_length));
467 }
469 //KdPrint((__DRIVER_NAME " sector_number = %d, block_count = %d\n", (ULONG)shadow->req.sector_number, block_count));
470 //KdPrint((__DRIVER_NAME " SrbExtension = %p\n", srb->SrbExtension));
471 //KdPrint((__DRIVER_NAME " DataBuffer = %p\n", srb->DataBuffer));
473 //KdPrint((__DRIVER_NAME " sector_number = %d\n", (ULONG)shadow->req.sector_number));
474 //KdPrint((__DRIVER_NAME " handle = %d\n", shadow->req.handle));
475 //KdPrint((__DRIVER_NAME " operation = %d\n", shadow->req.operation));
477 while (remaining > 0)
478 {
479 PHYSICAL_ADDRESS physical_address = MmGetPhysicalAddress(ptr);
481 if (dump_mode)
482 {
483 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
484 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE,
485 xvdd->dump_grant_refs[shadow->req.nr_segments], (ULONG)'XPDO');
486 }
487 else
488 {
489 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
490 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE, INVALID_GRANT_REF, (ULONG)'XVBD');
491 }
492 if (gref == INVALID_GRANT_REF)
493 {
494 ULONG i;
495 for (i = 0; i < shadow->req.nr_segments; i++)
496 {
497 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
498 shadow->req.seg[i].gref, FALSE, (ULONG)'XVBD');
499 }
500 if (shadow->aligned_buffer_in_use)
501 {
502 shadow->aligned_buffer_in_use = FALSE;
503 xvdd->aligned_buffer_in_use = FALSE;
504 }
505 /* put the srb back at the start of the queue */
506 InsertHeadList(&xvdd->srb_list, (PLIST_ENTRY)srb->SrbExtension);
507 put_shadow_on_freelist(xvdd, shadow);
508 KdPrint((__DRIVER_NAME " Out of gref's. Deferring\n"));
509 return;
510 }
511 offset = physical_address.LowPart & (PAGE_SIZE - 1);
512 length = min(PAGE_SIZE - offset, remaining);
513 ASSERT((offset & 511) == 0);
514 ASSERT((length & 511) == 0);
515 ASSERT(offset + length <= PAGE_SIZE);
516 shadow->req.seg[shadow->req.nr_segments].gref = gref;
517 shadow->req.seg[shadow->req.nr_segments].first_sect = (UCHAR)(offset >> 9);
518 shadow->req.seg[shadow->req.nr_segments].last_sect = (UCHAR)(((offset + length) >> 9) - 1);
519 remaining -= length;
520 ptr += length;
521 shadow->req.nr_segments++;
522 }
524 //KdPrint((__DRIVER_NAME " nr_segments = %d\n", shadow->req.nr_segments));
526 XenVbd_PutRequest(xvdd, &shadow->req);
528 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
529 if (notify)
530 {
531 //KdPrint((__DRIVER_NAME " Notifying\n"));
532 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
533 }
534 }
535 if (xvdd->shadow_free && !xvdd->aligned_buffer_in_use)
536 {
537 ScsiPortNotification(NextLuRequest, xvdd, 0, 0, 0);
538 }
539 //FUNCTION_EXIT();
540 }
542 static ULONG DDKAPI
543 XenVbd_HwScsiFindAdapter(PVOID DeviceExtension, PVOID HwContext, PVOID BusInformation, PCHAR ArgumentString, PPORT_CONFIGURATION_INFORMATION ConfigInfo, PBOOLEAN Again)
544 {
545 // PACCESS_RANGE AccessRange;
546 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
547 ULONG status;
548 // PXENPCI_XEN_DEVICE_DATA XenDeviceData;
549 PACCESS_RANGE access_range;
551 UNREFERENCED_PARAMETER(HwContext);
552 UNREFERENCED_PARAMETER(BusInformation);
553 UNREFERENCED_PARAMETER(ArgumentString);
555 FUNCTION_ENTER();
556 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
557 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
559 RtlZeroMemory(xvdd, sizeof(XENVBD_DEVICE_DATA));
560 *Again = FALSE;
562 KdPrint((__DRIVER_NAME " BusInterruptLevel = %d\n", ConfigInfo->BusInterruptLevel));
563 KdPrint((__DRIVER_NAME " BusInterruptVector = %03x\n", ConfigInfo->BusInterruptVector));
565 KdPrint((__DRIVER_NAME " NumberOfAccessRanges = %d\n", ConfigInfo->NumberOfAccessRanges));
566 if (ConfigInfo->NumberOfAccessRanges != 1 && ConfigInfo->NumberOfAccessRanges != 2)
567 {
568 return SP_RETURN_BAD_CONFIG;
569 }
571 access_range = &((*(ConfigInfo->AccessRanges))[0]);
572 KdPrint((__DRIVER_NAME " RangeStart = %08x, RangeLength = %08x\n",
573 access_range->RangeStart.LowPart, access_range->RangeLength));
574 xvdd->device_base = ScsiPortGetDeviceBase(
575 DeviceExtension,
576 ConfigInfo->AdapterInterfaceType,
577 ConfigInfo->SystemIoBusNumber,
578 access_range->RangeStart,
579 access_range->RangeLength,
580 !access_range->RangeInMemory);
581 if (!xvdd->device_base)
582 {
583 KdPrint((__DRIVER_NAME " Invalid config\n"));
584 FUNCTION_EXIT();
585 return SP_RETURN_BAD_CONFIG;
586 }
588 status = XenVbd_InitFromConfig(xvdd);
589 if (status != SP_RETURN_FOUND)
590 {
591 FUNCTION_EXIT();
592 return status;
593 }
595 xvdd->aligned_buffer_in_use = FALSE;
596 /* align the buffer to PAGE_SIZE */
597 xvdd->aligned_buffer = (PVOID)((ULONG_PTR)((PUCHAR)xvdd->aligned_buffer_data + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
598 KdPrint((__DRIVER_NAME " aligned_buffer_data = %p\n", xvdd->aligned_buffer_data));
599 KdPrint((__DRIVER_NAME " aligned_buffer = %p\n", xvdd->aligned_buffer));
601 if (!dump_mode)
602 {
603 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
604 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST - 1;
605 //ConfigInfo->ScatterGather = TRUE;
606 }
607 else
608 {
609 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE * PAGE_SIZE;
610 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE - 1;
611 //ConfigInfo->ScatterGather = FALSE;
612 }
613 KdPrint((__DRIVER_NAME " ConfigInfo->MaximumTransferLength = %d\n", ConfigInfo->MaximumTransferLength));
614 KdPrint((__DRIVER_NAME " ConfigInfo->NumberOfPhysicalBreaks = %d\n", ConfigInfo->NumberOfPhysicalBreaks));
615 ConfigInfo->ScatterGather = FALSE;
616 ConfigInfo->AlignmentMask = 0;
617 ConfigInfo->NumberOfBuses = 1;
618 ConfigInfo->InitiatorBusId[0] = 1;
619 ConfigInfo->MaximumNumberOfLogicalUnits = 1;
620 ConfigInfo->MaximumNumberOfTargets = 2;
621 ConfigInfo->BufferAccessScsiPortControlled = FALSE;
622 if (ConfigInfo->Dma64BitAddresses == SCSI_DMA64_SYSTEM_SUPPORTED)
623 {
624 ConfigInfo->Master = TRUE;
625 ConfigInfo->Dma64BitAddresses = SCSI_DMA64_MINIPORT_SUPPORTED;
626 ConfigInfo->Dma32BitAddresses = FALSE;
627 KdPrint((__DRIVER_NAME " Dma64BitAddresses supported\n"));
628 }
629 else
630 {
631 ConfigInfo->Master = TRUE;
632 ConfigInfo->Dma32BitAddresses = TRUE;
633 KdPrint((__DRIVER_NAME " Dma64BitAddresses not supported\n"));
634 }
636 FUNCTION_EXIT();
638 return SP_RETURN_FOUND;
639 }
641 static VOID
642 XenVbd_StartRingDetection(PXENVBD_DEVICE_DATA xvdd)
643 {
644 blkif_request_t *req;
645 int notify;
647 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT1;
648 RtlZeroMemory(xvdd->sring->ring, PAGE_SIZE - FIELD_OFFSET(blkif_sring_t, ring));
649 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
650 req->operation = 0xff;
651 xvdd->ring.req_prod_pvt++;
652 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
653 req->operation = 0xff;
654 xvdd->ring.req_prod_pvt++;
656 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
657 if (notify)
658 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
659 }
661 static BOOLEAN
662 XenVbd_HwScsiInitialize(PVOID DeviceExtension)
663 {
664 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
666 FUNCTION_ENTER();
667 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
668 KdPrint((__DRIVER_NAME " dump_mode = %d\n", dump_mode));
670 if (!xvdd->inactive)
671 {
672 if (!dump_mode)
673 {
674 XenVbd_StartRingDetection(xvdd);
675 }
676 else
677 {
678 if (xvdd->cached_use_other)
679 {
680 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
681 xvdd->use_other = TRUE;
682 }
683 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
684 }
685 InitializeListHead(&xvdd->srb_list);
686 }
687 FUNCTION_EXIT();
689 return TRUE;
690 }
692 static ULONG
693 XenVbd_FillModePage(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
694 {
695 PMODE_PARAMETER_HEADER parameter_header = NULL;
696 PMODE_PARAMETER_HEADER10 parameter_header10 = NULL;
697 PMODE_PARAMETER_BLOCK param_block;
698 PMODE_FORMAT_PAGE format_page;
699 ULONG offset = 0;
700 UCHAR buffer[1024];
701 BOOLEAN valid_page = FALSE;
702 BOOLEAN cdb_llbaa;
703 BOOLEAN cdb_dbd;
704 UCHAR cdb_page_code;
705 USHORT cdb_allocation_length;
707 UNREFERENCED_PARAMETER(xvdd);
709 RtlZeroMemory(srb->DataBuffer, srb->DataTransferLength);
710 RtlZeroMemory(buffer, ARRAY_SIZE(buffer));
711 offset = 0;
713 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
715 switch (srb->Cdb[0])
716 {
717 case SCSIOP_MODE_SENSE:
718 cdb_llbaa = FALSE;
719 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
720 cdb_page_code = srb->Cdb[2] & 0x3f;
721 cdb_allocation_length = srb->Cdb[4];
722 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
723 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
724 parameter_header = (PMODE_PARAMETER_HEADER)&buffer[offset];
725 parameter_header->MediumType = 0;
726 parameter_header->DeviceSpecificParameter = 0;
727 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
728 {
729 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
730 parameter_header->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
731 }
732 offset += sizeof(MODE_PARAMETER_HEADER);
733 break;
734 case SCSIOP_MODE_SENSE10:
735 cdb_llbaa = (BOOLEAN)!!(srb->Cdb[1] & 16);
736 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
737 cdb_page_code = srb->Cdb[2] & 0x3f;
738 cdb_allocation_length = (srb->Cdb[7] << 8) | srb->Cdb[8];
739 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE10 llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
740 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
741 parameter_header10 = (PMODE_PARAMETER_HEADER10)&buffer[offset];
742 parameter_header10->MediumType = 0;
743 parameter_header10->DeviceSpecificParameter = 0;
744 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
745 {
746 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
747 parameter_header10->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
748 }
749 offset += sizeof(MODE_PARAMETER_HEADER10);
750 break;
751 default:
752 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE_WTF (%02x)\n", (ULONG)srb->Cdb[0]));
753 return FALSE;
754 }
756 if (!cdb_dbd)
757 {
758 param_block = (PMODE_PARAMETER_BLOCK)&buffer[offset];
759 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK)
760 {
761 if (xvdd->total_sectors >> 32)
762 {
763 param_block->DensityCode = 0xff;
764 param_block->NumberOfBlocks[0] = 0xff;
765 param_block->NumberOfBlocks[1] = 0xff;
766 param_block->NumberOfBlocks[2] = 0xff;
767 }
768 else
769 {
770 param_block->DensityCode = (UCHAR)((xvdd->total_sectors >> 24) & 0xff);
771 param_block->NumberOfBlocks[0] = (UCHAR)((xvdd->total_sectors >> 16) & 0xff);
772 param_block->NumberOfBlocks[1] = (UCHAR)((xvdd->total_sectors >> 8) & 0xff);
773 param_block->NumberOfBlocks[2] = (UCHAR)((xvdd->total_sectors >> 0) & 0xff);
774 }
775 param_block->BlockLength[0] = (UCHAR)((xvdd->bytes_per_sector >> 16) & 0xff);
776 param_block->BlockLength[1] = (UCHAR)((xvdd->bytes_per_sector >> 8) & 0xff);
777 param_block->BlockLength[2] = (UCHAR)((xvdd->bytes_per_sector >> 0) & 0xff);
778 }
779 offset += sizeof(MODE_PARAMETER_BLOCK);
780 }
781 switch (srb->Cdb[0])
782 {
783 case SCSIOP_MODE_SENSE:
784 parameter_header->BlockDescriptorLength = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER));
785 break;
786 case SCSIOP_MODE_SENSE10:
787 parameter_header10->BlockDescriptorLength[0] = (UCHAR)((offset - sizeof(MODE_PARAMETER_HEADER10)) >> 8);
788 parameter_header10->BlockDescriptorLength[1] = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER10));
789 break;
790 }
791 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_FORMAT_DEVICE || cdb_page_code == MODE_SENSE_RETURN_ALL))
792 {
793 valid_page = TRUE;
794 format_page = (PMODE_FORMAT_PAGE)&buffer[offset];
795 format_page->PageCode = MODE_PAGE_FORMAT_DEVICE;
796 format_page->PageLength = sizeof(MODE_FORMAT_PAGE) - FIELD_OFFSET(MODE_FORMAT_PAGE, PageLength);
797 /* 256 sectors per track */
798 format_page->SectorsPerTrack[0] = 0x01;
799 format_page->SectorsPerTrack[1] = 0x00;
800 /* xxx bytes per sector */
801 format_page->BytesPerPhysicalSector[0] = (UCHAR)(xvdd->bytes_per_sector >> 8);
802 format_page->BytesPerPhysicalSector[1] = (UCHAR)(xvdd->bytes_per_sector & 0xff);
803 format_page->HardSectorFormating = TRUE;
804 format_page->SoftSectorFormating = TRUE;
805 offset += sizeof(MODE_FORMAT_PAGE);
806 }
807 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_CACHING || cdb_page_code == MODE_SENSE_RETURN_ALL))
808 {
809 PMODE_CACHING_PAGE caching_page;
810 valid_page = TRUE;
811 caching_page = (PMODE_CACHING_PAGE)&buffer[offset];
812 caching_page->PageCode = MODE_PAGE_CACHING;
813 caching_page->PageLength = sizeof(MODE_CACHING_PAGE) - FIELD_OFFSET(MODE_CACHING_PAGE, PageLength);
814 // caching_page-> // all zeros is just fine... maybe
815 offset += sizeof(MODE_CACHING_PAGE);
816 }
817 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_MEDIUM_TYPES || cdb_page_code == MODE_SENSE_RETURN_ALL))
818 {
819 PUCHAR medium_types_page;
820 valid_page = TRUE;
821 medium_types_page = &buffer[offset];
822 medium_types_page[0] = MODE_PAGE_MEDIUM_TYPES;
823 medium_types_page[1] = 0x06;
824 medium_types_page[2] = 0;
825 medium_types_page[3] = 0;
826 medium_types_page[4] = 0;
827 medium_types_page[5] = 0;
828 medium_types_page[6] = 0;
829 medium_types_page[7] = 0;
830 offset += 8;
831 }
832 switch (srb->Cdb[0])
833 {
834 case SCSIOP_MODE_SENSE:
835 parameter_header->ModeDataLength = (UCHAR)(offset - 1);
836 break;
837 case SCSIOP_MODE_SENSE10:
838 parameter_header10->ModeDataLength[0] = (UCHAR)((offset - 2) >> 8);
839 parameter_header10->ModeDataLength[1] = (UCHAR)(offset - 2);
840 break;
841 }
843 if (!valid_page && cdb_page_code != MODE_SENSE_RETURN_ALL)
844 {
845 srb->SrbStatus = SRB_STATUS_ERROR;
846 }
847 else if(offset < srb->DataTransferLength)
848 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
849 else
850 srb->SrbStatus = SRB_STATUS_SUCCESS;
851 srb->DataTransferLength = min(srb->DataTransferLength, offset);
852 srb->ScsiStatus = 0;
853 memcpy(srb->DataBuffer, buffer, srb->DataTransferLength);
855 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
857 return TRUE;
858 }
860 static VOID
861 XenVbd_MakeSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb, UCHAR sense_key, UCHAR additional_sense_code)
862 {
863 PSENSE_DATA sd = srb->SenseInfoBuffer;
865 UNREFERENCED_PARAMETER(xvdd);
867 if (!srb->SenseInfoBuffer)
868 return;
870 sd->ErrorCode = 0x70;
871 sd->Valid = 1;
872 sd->SenseKey = sense_key;
873 sd->AdditionalSenseLength = sizeof(SENSE_DATA) - FIELD_OFFSET(SENSE_DATA, AdditionalSenseLength);
874 sd->AdditionalSenseCode = additional_sense_code;
875 return;
876 }
878 static VOID
879 XenVbd_MakeAutoSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
880 {
881 if (srb->SrbStatus == SRB_STATUS_SUCCESS || srb->SrbFlags & SRB_FLAGS_DISABLE_AUTOSENSE)
882 return;
883 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
884 srb->SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
885 }
887 static BOOLEAN DDKAPI
888 XenVbd_HwScsiInterrupt(PVOID DeviceExtension)
889 {
890 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
891 PSCSI_REQUEST_BLOCK srb;
892 RING_IDX i, rp;
893 ULONG j;
894 blkif_response_t *rep;
895 int block_count;
896 int more_to_do = TRUE;
897 blkif_shadow_t *shadow;
898 ULONG suspend_resume_state_pdo;
899 BOOLEAN last_interrupt = FALSE;
901 /* in dump mode I think we get called on a timer, not by an actual IRQ */
902 if (!dump_mode && !xvdd->vectors.EvtChn_AckEvent(xvdd->vectors.context, xvdd->event_channel, &last_interrupt))
903 return FALSE; /* interrupt was not for us */
905 suspend_resume_state_pdo = xvdd->device_state->suspend_resume_state_pdo;
906 KeMemoryBarrier();
908 if (suspend_resume_state_pdo != xvdd->device_state->suspend_resume_state_fdo)
909 {
910 FUNCTION_ENTER();
911 switch (suspend_resume_state_pdo)
912 {
913 case SR_STATE_SUSPENDING:
914 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_SUSPENDING\n"));
915 break;
916 case SR_STATE_RESUMING:
917 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_RESUMING\n"));
918 XenVbd_InitFromConfig(xvdd);
919 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
920 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
921 break;
922 case SR_STATE_RUNNING:
923 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
924 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
925 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
926 ScsiPortNotification(NextRequest, DeviceExtension);
927 default:
928 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
929 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
930 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
931 break;
932 }
933 KeMemoryBarrier();
934 }
936 if (xvdd->device_state->suspend_resume_state_fdo != SR_STATE_RUNNING)
937 {
938 return last_interrupt;
939 }
941 while (more_to_do)
942 {
943 rp = xvdd->ring.sring->rsp_prod;
944 KeMemoryBarrier();
945 for (i = xvdd->ring.rsp_cons; i < rp; i++)
946 {
947 rep = XenVbd_GetResponse(xvdd, i);
948 /*
949 * This code is to automatically detect if the backend is using the same
950 * bit width or a different bit width to us. Later versions of Xen do this
951 * via a xenstore value, but not all. That 0x0fffffff (notice
952 * that the msb is not actually set, so we don't have any problems with
953 * sign extending) is to signify the last entry on the right, which is
954 * different under 32 and 64 bits, and that is why we set it up there.
956 * To do the detection, we put two initial entries on the ring, with an op
957 * of 0xff (which is invalid). The first entry is mostly okay, but the
958 * second will be grossly misaligned if the backend bit width is different,
959 * and we detect this and switch frontend structures.
960 */
961 switch (xvdd->ring_detect_state)
962 {
963 case RING_DETECT_STATE_NOT_STARTED:
964 KdPrint((__DRIVER_NAME " premature IRQ\n"));
965 break;
966 case RING_DETECT_STATE_DETECT1:
967 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));
968 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));
969 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT2;
970 break;
971 case RING_DETECT_STATE_DETECT2:
972 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));
973 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));
974 *xvdd->event_channel_ptr |= 0x80000000;
975 if (rep->operation != 0xff)
976 {
977 KdPrint((__DRIVER_NAME " switching to 'other' ring size\n"));
978 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
979 xvdd->use_other = TRUE;
980 *xvdd->event_channel_ptr |= 0x40000000;
981 }
982 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
983 ScsiPortNotification(NextRequest, DeviceExtension);
984 break;
985 case RING_DETECT_STATE_COMPLETE:
986 shadow = &xvdd->shadows[rep->id];
987 srb = shadow->srb;
988 ASSERT(srb != NULL);
989 block_count = decode_cdb_length(srb);
990 block_count *= xvdd->bytes_per_sector / 512;
991 /* 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 */
992 if (rep->status == BLKIF_RSP_OKAY || (dump_mode && dump_mode_errors++ < DUMP_MODE_ERROR_LIMIT))
993 srb->SrbStatus = SRB_STATUS_SUCCESS;
994 else
995 {
996 KdPrint((__DRIVER_NAME " Xen Operation returned error\n"));
997 if (decode_cdb_is_read(srb))
998 KdPrint((__DRIVER_NAME " Operation = Read\n"));
999 else
1000 KdPrint((__DRIVER_NAME " Operation = Write\n"));
1001 if (dump_mode)
1003 KdPrint((__DRIVER_NAME " Sector = %08X, Count = %d\n", (ULONG)shadow->req.sector_number, block_count));
1004 KdPrint((__DRIVER_NAME " DataBuffer = %p, aligned_buffer = %p\n", srb->DataBuffer, xvdd->aligned_buffer));
1005 KdPrint((__DRIVER_NAME " Physical = %08x%08x\n", MmGetPhysicalAddress(srb->DataBuffer).HighPart, MmGetPhysicalAddress(srb->DataBuffer).LowPart));
1006 KdPrint((__DRIVER_NAME " PFN = %08x\n", (ULONG)(MmGetPhysicalAddress(srb->DataBuffer).QuadPart >> PAGE_SHIFT)));
1008 for (j = 0; j < shadow->req.nr_segments; j++)
1010 KdPrint((__DRIVER_NAME " gref = %d\n", shadow->req.seg[j].gref));
1011 KdPrint((__DRIVER_NAME " first_sect = %d\n", shadow->req.seg[j].first_sect));
1012 KdPrint((__DRIVER_NAME " last_sect = %d\n", shadow->req.seg[j].last_sect));
1015 srb->SrbStatus = SRB_STATUS_ERROR;
1016 srb->ScsiStatus = 0x02;
1017 xvdd->last_sense_key = SCSI_SENSE_MEDIUM_ERROR;
1018 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1019 XenVbd_MakeAutoSense(xvdd, srb);
1021 if (shadow->aligned_buffer_in_use)
1023 ASSERT(xvdd->aligned_buffer_in_use);
1024 xvdd->aligned_buffer_in_use = FALSE;
1025 if (decode_cdb_is_read(srb))
1026 memcpy(srb->DataBuffer, xvdd->aligned_buffer, block_count * 512);
1027 //KdPrint((__DRIVER_NAME " Completed use of buffer\n"));
1030 #if 0
1031 if (xvdd->aligned_buffer_in_use)
1033 KdPrint((__DRIVER_NAME " Completed request while aligned buffer in use\n"));
1035 #endif
1036 for (j = 0; j < shadow->req.nr_segments; j++)
1038 if (dump_mode)
1040 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1041 shadow->req.seg[j].gref, TRUE, (ULONG)'XPDO');
1043 else
1045 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1046 shadow->req.seg[j].gref, FALSE, (ULONG)'XVBD');
1049 shadow->aligned_buffer_in_use = FALSE;
1050 shadow->srb = NULL;
1051 put_shadow_on_freelist(xvdd, shadow);
1052 //KdPrint((__DRIVER_NAME " B RequestComplete srb = %p\n", srb));
1053 ScsiPortNotification(RequestComplete, xvdd, srb);
1054 break;
1058 xvdd->ring.rsp_cons = i;
1059 if (i != xvdd->ring.req_prod_pvt)
1061 RING_FINAL_CHECK_FOR_RESPONSES(&xvdd->ring, more_to_do);
1063 else
1065 xvdd->ring.sring->rsp_event = i + 1;
1066 more_to_do = FALSE;
1070 XenVbd_PutQueuedSrbsOnRing(xvdd);
1072 if (suspend_resume_state_pdo == SR_STATE_SUSPENDING)
1074 if (xvdd->inactive || xvdd->shadow_free == SHADOW_ENTRIES)
1076 /* all entries are purged from the list (or we are inactive). ready to suspend */
1077 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
1078 KeMemoryBarrier();
1079 KdPrint((__DRIVER_NAME " Set fdo state SR_STATE_SUSPENDING\n"));
1080 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xvdd->device_state->pdo_event_channel));
1081 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
1083 FUNCTION_EXIT();
1086 return last_interrupt;
1089 static BOOLEAN DDKAPI
1090 XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK srb)
1092 PUCHAR data_buffer;
1093 //ULONG data_buffer_length;
1094 PCDB cdb;
1095 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1096 ULONG data_transfer_length = srb->DataTransferLength;
1098 if (xvdd->inactive)
1100 KdPrint((__DRIVER_NAME " Inactive srb->Function = %08X\n", srb->Function));
1101 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1102 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1103 ScsiPortNotification(NextRequest, DeviceExtension);
1104 return TRUE;
1107 // If we haven't enumerated all the devices yet then just defer the request
1108 if (xvdd->ring_detect_state < RING_DETECT_STATE_COMPLETE)
1110 srb->SrbStatus = SRB_STATUS_BUSY;
1111 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1112 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Still figuring out ring)\n"));
1113 return TRUE;
1116 if (xvdd->device_state->suspend_resume_state_pdo != SR_STATE_RUNNING)
1118 KdPrint((__DRIVER_NAME " --> HwScsiStartIo (Suspending/Resuming)\n"));
1119 srb->SrbStatus = SRB_STATUS_BUSY;
1120 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1121 KdPrint((__DRIVER_NAME " <-- HwScsiStartIo (Suspending/Resuming)\n"));
1122 return TRUE;
1125 if (srb->PathId != 0 || srb->TargetId != 0 || srb->Lun != 0)
1127 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1128 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1129 ScsiPortNotification(NextRequest, DeviceExtension);
1130 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Out of bounds)\n"));
1131 return TRUE;
1134 switch (srb->Function)
1136 case SRB_FUNCTION_EXECUTE_SCSI:
1137 cdb = (PCDB)srb->Cdb;
1139 switch(cdb->CDB6GENERIC.OperationCode)
1141 case SCSIOP_TEST_UNIT_READY:
1142 if (dump_mode)
1143 KdPrint((__DRIVER_NAME " Command = TEST_UNIT_READY\n"));
1144 srb->SrbStatus = SRB_STATUS_SUCCESS;
1145 srb->ScsiStatus = 0;
1146 break;
1147 case SCSIOP_INQUIRY:
1148 if (dump_mode)
1150 //PHYSICAL_ADDRESS physical;
1151 KdPrint((__DRIVER_NAME " Command = INQUIRY\n"));
1152 //KdPrint((__DRIVER_NAME " srb->Databuffer = %p\n", srb->DataBuffer));
1153 //physical = ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length);
1154 //KdPrint((__DRIVER_NAME " ScsiPortGetPhysicalAddress = %08x:%08x\n", physical.LowPart, physical.HighPart));
1156 // KdPrint((__DRIVER_NAME " (LUN = %d, EVPD = %d, Page Code = %02X)\n", srb->Cdb[1] >> 5, srb->Cdb[1] & 1, srb->Cdb[2]));
1157 // KdPrint((__DRIVER_NAME " (Length = %d)\n", srb->DataTransferLength));
1159 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1160 data_buffer = srb->DataBuffer;
1161 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1162 srb->SrbStatus = SRB_STATUS_SUCCESS;
1163 switch (xvdd->device_type)
1165 case XENVBD_DEVICETYPE_DISK:
1166 if ((srb->Cdb[1] & 1) == 0)
1168 if (srb->Cdb[2])
1170 srb->SrbStatus = SRB_STATUS_ERROR;
1172 else
1174 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1175 id->DeviceType = DIRECT_ACCESS_DEVICE;
1176 id->Versions = 4; /* minimum that WHQL says we must support */
1177 id->ResponseDataFormat = 2; /* not sure about this but WHQL complains otherwise */
1178 id->HiSupport = 1; /* WHQL test says we should set this */
1179 //id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1180 id->AdditionalLength = sizeof(INQUIRYDATA) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength) - 1;
1181 id->CommandQueue = 1;
1182 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1183 memcpy(id->ProductId, scsi_disk_model, 16); // product id
1184 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1185 data_transfer_length = sizeof(INQUIRYDATA);
1188 else
1190 switch (srb->Cdb[2])
1192 case VPD_SUPPORTED_PAGES: /* list of pages we support */
1193 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1194 data_buffer[1] = VPD_SUPPORTED_PAGES;
1195 data_buffer[2] = 0x00;
1196 data_buffer[3] = 2;
1197 data_buffer[4] = 0x00;
1198 data_buffer[5] = 0x80;
1199 data_transfer_length = 6;
1200 break;
1201 case VPD_SERIAL_NUMBER: /* serial number */
1202 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1203 data_buffer[1] = VPD_SERIAL_NUMBER;
1204 data_buffer[2] = 0x00;
1205 data_buffer[3] = 8;
1206 memset(&data_buffer[4], ' ', 8);
1207 data_transfer_length = 12;
1208 break;
1209 case VPD_DEVICE_IDENTIFIERS: /* identification - we don't support any so just return zero */
1210 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1211 data_buffer[1] = VPD_DEVICE_IDENTIFIERS;
1212 data_buffer[2] = 0x00;
1213 data_buffer[3] = 4 + (UCHAR)strlen(xvdd->vectors.path); /* length */
1214 data_buffer[4] = 2; /* ASCII */
1215 data_buffer[5] = 1; /* VendorId */
1216 data_buffer[6] = 0;
1217 data_buffer[7] = (UCHAR)strlen(xvdd->vectors.path);
1218 memcpy(&data_buffer[8], xvdd->vectors.path, strlen(xvdd->vectors.path));
1219 data_transfer_length = (ULONG)(8 + strlen(xvdd->vectors.path));
1220 break;
1221 default:
1222 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1223 srb->SrbStatus = SRB_STATUS_ERROR;
1224 break;
1227 break;
1228 case XENVBD_DEVICETYPE_CDROM:
1229 if ((srb->Cdb[1] & 1) == 0)
1231 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1232 id->DeviceType = READ_ONLY_DIRECT_ACCESS_DEVICE;
1233 id->RemovableMedia = 1;
1234 id->Versions = 3;
1235 id->ResponseDataFormat = 0;
1236 id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1237 id->CommandQueue = 1;
1238 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1239 memcpy(id->ProductId, scsi_cdrom_model, 16); // product id
1240 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1242 else
1244 switch (srb->Cdb[2])
1246 case 0x00:
1247 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1248 data_buffer[1] = 0x00;
1249 data_buffer[2] = 0x00;
1250 data_buffer[3] = 2;
1251 data_buffer[4] = 0x00;
1252 data_buffer[5] = 0x80;
1253 break;
1254 case 0x80:
1255 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1256 data_buffer[1] = 0x80;
1257 data_buffer[2] = 0x00;
1258 data_buffer[3] = 8;
1259 data_buffer[4] = 0x31;
1260 data_buffer[5] = 0x32;
1261 data_buffer[6] = 0x33;
1262 data_buffer[7] = 0x34;
1263 data_buffer[8] = 0x35;
1264 data_buffer[9] = 0x36;
1265 data_buffer[10] = 0x37;
1266 data_buffer[11] = 0x38;
1267 break;
1268 default:
1269 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1270 srb->SrbStatus = SRB_STATUS_ERROR;
1271 break;
1274 break;
1275 default:
1276 //KdPrint((__DRIVER_NAME " Unknown DeviceType %02x requested\n", xvdd->device_type));
1277 srb->SrbStatus = SRB_STATUS_ERROR;
1278 break;
1280 break;
1281 case SCSIOP_READ_CAPACITY:
1282 if (dump_mode)
1283 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1284 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1285 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1286 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1287 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1288 data_buffer = srb->DataBuffer;
1289 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1290 if ((xvdd->total_sectors - 1) >> 32)
1292 data_buffer[0] = 0xff;
1293 data_buffer[1] = 0xff;
1294 data_buffer[2] = 0xff;
1295 data_buffer[3] = 0xff;
1297 else
1299 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1300 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1301 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1302 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1304 data_buffer[4] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1305 data_buffer[5] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1306 data_buffer[6] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1307 data_buffer[7] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1308 srb->ScsiStatus = 0;
1309 srb->SrbStatus = SRB_STATUS_SUCCESS;
1310 break;
1311 case SCSIOP_READ_CAPACITY16:
1312 if (dump_mode)
1313 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1314 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1315 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1316 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1317 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1318 data_buffer = srb->DataBuffer;
1319 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1320 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 56) & 0xff;
1321 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 48) & 0xff;
1322 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 40) & 0xff;
1323 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 32) & 0xff;
1324 data_buffer[4] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1325 data_buffer[5] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1326 data_buffer[6] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1327 data_buffer[7] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1328 data_buffer[8] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1329 data_buffer[9] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1330 data_buffer[10] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1331 data_buffer[11] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1332 srb->ScsiStatus = 0;
1333 srb->SrbStatus = SRB_STATUS_SUCCESS;
1334 break;
1335 case SCSIOP_MODE_SENSE:
1336 case SCSIOP_MODE_SENSE10:
1337 if (dump_mode)
1338 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));
1339 XenVbd_FillModePage(xvdd, srb);
1340 break;
1341 case SCSIOP_READ:
1342 case SCSIOP_READ16:
1343 case SCSIOP_WRITE:
1344 case SCSIOP_WRITE16:
1345 XenVbd_PutSrbOnList(xvdd, srb);
1346 XenVbd_PutQueuedSrbsOnRing(xvdd);
1347 break;
1348 case SCSIOP_VERIFY:
1349 case SCSIOP_VERIFY16:
1350 // Should we do more here?
1351 if (dump_mode)
1352 KdPrint((__DRIVER_NAME " Command = VERIFY\n"));
1353 srb->SrbStatus = SRB_STATUS_SUCCESS;
1354 break;
1355 case SCSIOP_REPORT_LUNS:
1356 if (dump_mode)
1357 KdPrint((__DRIVER_NAME " Command = REPORT_LUNS\n"));
1358 srb->SrbStatus = SRB_STATUS_SUCCESS;;
1359 break;
1360 case SCSIOP_REQUEST_SENSE:
1361 if (dump_mode)
1362 KdPrint((__DRIVER_NAME " Command = REQUEST_SENSE\n"));
1363 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
1364 srb->SrbStatus = SRB_STATUS_SUCCESS;
1365 break;
1366 case SCSIOP_READ_TOC:
1367 if (dump_mode)
1368 KdPrint((__DRIVER_NAME " Command = READ_TOC\n"));
1369 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1370 data_buffer = srb->DataBuffer;
1371 // DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
1372 /*
1373 #define READ_TOC_FORMAT_TOC 0x00
1374 #define READ_TOC_FORMAT_SESSION 0x01
1375 #define READ_TOC_FORMAT_FULL_TOC 0x02
1376 #define READ_TOC_FORMAT_PMA 0x03
1377 #define READ_TOC_FORMAT_ATIP 0x04
1378 */
1379 // KdPrint((__DRIVER_NAME " Msf = %d\n", cdb->READ_TOC.Msf));
1380 // KdPrint((__DRIVER_NAME " LogicalUnitNumber = %d\n", cdb->READ_TOC.LogicalUnitNumber));
1381 // KdPrint((__DRIVER_NAME " Format2 = %d\n", cdb->READ_TOC.Format2));
1382 // KdPrint((__DRIVER_NAME " StartingTrack = %d\n", cdb->READ_TOC.StartingTrack));
1383 // KdPrint((__DRIVER_NAME " AllocationLength = %d\n", (cdb->READ_TOC.AllocationLength[0] << 8) | cdb->READ_TOC.AllocationLength[1]));
1384 // KdPrint((__DRIVER_NAME " Control = %d\n", cdb->READ_TOC.Control));
1385 // KdPrint((__DRIVER_NAME " Format = %d\n", cdb->READ_TOC.Format));
1386 switch (cdb->READ_TOC.Format2)
1388 case READ_TOC_FORMAT_TOC:
1389 data_buffer[0] = 0; // length MSB
1390 data_buffer[1] = 10; // length LSB
1391 data_buffer[2] = 1; // First Track
1392 data_buffer[3] = 1; // Last Track
1393 data_buffer[4] = 0; // Reserved
1394 data_buffer[5] = 0x14; // current position data + uninterrupted data
1395 data_buffer[6] = 1; // last complete track
1396 data_buffer[7] = 0; // reserved
1397 data_buffer[8] = 0; // MSB Block
1398 data_buffer[9] = 0;
1399 data_buffer[10] = 0;
1400 data_buffer[11] = 0; // LSB Block
1401 srb->SrbStatus = SRB_STATUS_SUCCESS;
1402 break;
1403 case READ_TOC_FORMAT_SESSION:
1404 case READ_TOC_FORMAT_FULL_TOC:
1405 case READ_TOC_FORMAT_PMA:
1406 case READ_TOC_FORMAT_ATIP:
1407 srb->SrbStatus = SRB_STATUS_ERROR;
1408 break;
1410 break;
1411 case SCSIOP_START_STOP_UNIT:
1412 KdPrint((__DRIVER_NAME " Command = SCSIOP_START_STOP_UNIT\n"));
1413 srb->SrbStatus = SRB_STATUS_SUCCESS;
1414 break;
1415 case SCSIOP_RESERVE_UNIT:
1416 KdPrint((__DRIVER_NAME " Command = SCSIOP_RESERVE_UNIT\n"));
1417 srb->SrbStatus = SRB_STATUS_SUCCESS;
1418 break;
1419 case SCSIOP_RELEASE_UNIT:
1420 KdPrint((__DRIVER_NAME " Command = SCSIOP_RELEASE_UNIT\n"));
1421 srb->SrbStatus = SRB_STATUS_SUCCESS;
1422 break;
1423 default:
1424 KdPrint((__DRIVER_NAME " Unhandled EXECUTE_SCSI Command = %02X\n", srb->Cdb[0]));
1425 srb->SrbStatus = SRB_STATUS_ERROR;
1426 break;
1428 if (srb->SrbStatus == SRB_STATUS_ERROR)
1430 KdPrint((__DRIVER_NAME " EXECUTE_SCSI Command = %02X returned error %02x\n", srb->Cdb[0], xvdd->last_sense_key));
1431 if (xvdd->last_sense_key == SCSI_SENSE_NO_SENSE)
1433 xvdd->last_sense_key = SCSI_SENSE_ILLEGAL_REQUEST;
1434 xvdd->last_additional_sense_code = SCSI_ADSENSE_INVALID_CDB;
1436 srb->ScsiStatus = 0x02;
1437 XenVbd_MakeAutoSense(xvdd, srb);
1438 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1439 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1441 ScsiPortNotification(NextRequest, DeviceExtension);
1444 else if (srb->SrbStatus != SRB_STATUS_PENDING)
1446 if (srb->SrbStatus == SRB_STATUS_SUCCESS && data_transfer_length < srb->DataTransferLength)
1448 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
1449 srb->DataTransferLength = data_transfer_length;
1451 xvdd->last_sense_key = SCSI_SENSE_NO_SENSE;
1452 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1453 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1454 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1456 ScsiPortNotification(NextRequest, DeviceExtension);
1459 break;
1460 case SRB_FUNCTION_IO_CONTROL:
1461 KdPrint((__DRIVER_NAME " SRB_FUNCTION_IO_CONTROL\n"));
1462 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1463 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1464 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1466 ScsiPortNotification(NextRequest, DeviceExtension);
1468 break;
1469 case SRB_FUNCTION_FLUSH:
1470 KdPrint((__DRIVER_NAME " SRB_FUNCTION_FLUSH %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1471 srb->SrbStatus = SRB_STATUS_SUCCESS;
1472 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1473 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1475 ScsiPortNotification(NextRequest, DeviceExtension);
1477 break;
1478 case SRB_FUNCTION_SHUTDOWN:
1479 KdPrint((__DRIVER_NAME " SRB_FUNCTION_SHUTDOWN %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1480 srb->SrbStatus = SRB_STATUS_SUCCESS;
1481 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1482 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1484 ScsiPortNotification(NextRequest, DeviceExtension);
1486 break;
1487 default:
1488 KdPrint((__DRIVER_NAME " Unhandled srb->Function = %08X\n", srb->Function));
1489 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1490 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1491 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1493 ScsiPortNotification(NextRequest, DeviceExtension);
1495 break;
1498 //FUNCTION_EXIT();
1499 return TRUE;
1502 static BOOLEAN
1503 XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId)
1505 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1507 UNREFERENCED_PARAMETER(DeviceExtension);
1508 UNREFERENCED_PARAMETER(PathId);
1510 FUNCTION_ENTER();
1512 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1513 if (xvdd->ring_detect_state == RING_DETECT_STATE_COMPLETE && xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1515 ScsiPortNotification(NextRequest, DeviceExtension);
1518 FUNCTION_EXIT();
1521 return TRUE;
1524 static BOOLEAN
1525 XenVbd_HwScsiAdapterState(PVOID DeviceExtension, PVOID Context, BOOLEAN SaveState)
1527 UNREFERENCED_PARAMETER(DeviceExtension);
1528 UNREFERENCED_PARAMETER(Context);
1529 UNREFERENCED_PARAMETER(SaveState);
1531 FUNCTION_ENTER();
1532 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1534 FUNCTION_EXIT();
1536 return TRUE;
1539 static SCSI_ADAPTER_CONTROL_STATUS DDKAPI
1540 XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters)
1542 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1543 SCSI_ADAPTER_CONTROL_STATUS Status = ScsiAdapterControlSuccess;
1544 PSCSI_SUPPORTED_CONTROL_TYPE_LIST SupportedControlTypeList;
1545 //KIRQL OldIrql;
1547 FUNCTION_ENTER();
1548 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1549 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
1551 switch (ControlType)
1553 case ScsiQuerySupportedControlTypes:
1554 SupportedControlTypeList = (PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters;
1555 KdPrint((__DRIVER_NAME " ScsiQuerySupportedControlTypes (Max = %d)\n", SupportedControlTypeList->MaxControlType));
1556 SupportedControlTypeList->SupportedTypeList[ScsiQuerySupportedControlTypes] = TRUE;
1557 SupportedControlTypeList->SupportedTypeList[ScsiStopAdapter] = TRUE;
1558 SupportedControlTypeList->SupportedTypeList[ScsiRestartAdapter] = TRUE;
1559 break;
1560 case ScsiStopAdapter:
1561 KdPrint((__DRIVER_NAME " ScsiStopAdapter\n"));
1562 /* I don't think we actually have to do anything here... xenpci cleans up all the xenbus stuff for us */
1563 break;
1564 case ScsiRestartAdapter:
1565 KdPrint((__DRIVER_NAME " ScsiRestartAdapter\n"));
1566 if (!xvdd->inactive)
1568 if (XenVbd_InitFromConfig(xvdd) != SP_RETURN_FOUND)
1569 KeBugCheckEx(DATA_COHERENCY_EXCEPTION, 0, (ULONG_PTR) xvdd, 0, 0);
1570 XenVbd_StartRingDetection(xvdd);
1572 break;
1573 case ScsiSetBootConfig:
1574 KdPrint((__DRIVER_NAME " ScsiSetBootConfig\n"));
1575 break;
1576 case ScsiSetRunningConfig:
1577 KdPrint((__DRIVER_NAME " ScsiSetRunningConfig\n"));
1578 break;
1579 default:
1580 KdPrint((__DRIVER_NAME " UNKNOWN\n"));
1581 break;
1584 FUNCTION_EXIT();
1586 return Status;
1589 NTSTATUS
1590 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
1592 ULONG status;
1593 HW_INITIALIZATION_DATA HwInitializationData;
1594 PVOID driver_extension;
1595 PUCHAR ptr;
1596 OBJECT_ATTRIBUTES oa;
1597 HANDLE service_handle;
1598 UNICODE_STRING param_name;
1599 HANDLE param_handle;
1600 UNICODE_STRING value_name;
1601 CHAR buf[256];
1602 ULONG buf_len;
1603 PKEY_VALUE_PARTIAL_INFORMATION kpv;
1605 FUNCTION_ENTER();
1606 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1607 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
1609 /* RegistryPath == NULL when we are invoked as a crash dump driver */
1610 if (!RegistryPath)
1612 dump_mode = TRUE;
1615 if (!dump_mode)
1617 IoAllocateDriverObjectExtension(DriverObject, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC), PAGE_SIZE, &driver_extension);
1618 ptr = driver_extension;
1619 //ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
1620 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "ring-ref", NULL, NULL);
1621 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_IRQ, "event-channel", NULL, NULL);
1622 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_FRONT, "device-type", NULL, NULL);
1623 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mode", NULL, NULL);
1624 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sectors", NULL, NULL);
1625 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sector-size", NULL, NULL);
1626 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_GRANT_ENTRIES, NULL, ULongToPtr(BLKIF_MAX_SEGMENTS_PER_REQUEST), NULL); /* for use in crash dump */
1627 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT, NULL, NULL, NULL);
1628 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1629 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1630 __ADD_XEN_INIT_UCHAR(&ptr, 20);
1631 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1632 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT, NULL, NULL, NULL);
1633 //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1634 //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1635 //__ADD_XEN_INIT_UCHAR(&ptr, 20);
1636 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1637 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN, NULL, NULL, NULL);
1638 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
1639 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
1640 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1641 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
1642 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
1643 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1644 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitialising);
1645 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait);
1646 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1647 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1649 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1651 InitializeObjectAttributes(&oa, RegistryPath, OBJ_CASE_INSENSITIVE, NULL, NULL);
1652 status = ZwOpenKey(&service_handle, KEY_READ, &oa);
1653 if(!NT_SUCCESS(status))
1655 KdPrint((__DRIVER_NAME " ZwOpenKey(Service) returned %08x\n", status));
1657 else
1659 RtlInitUnicodeString(&param_name, L"Parameters");
1660 InitializeObjectAttributes(&oa, &param_name, OBJ_CASE_INSENSITIVE, service_handle, NULL);
1661 status = ZwOpenKey(&param_handle, KEY_READ, &oa);
1662 if(!NT_SUCCESS(status))
1664 KdPrint((__DRIVER_NAME " ZwOpenKey(Parameters) returned %08x\n", status));
1666 else
1668 kpv = (PKEY_VALUE_PARTIAL_INFORMATION)buf;
1669 RtlFillMemory(scsi_device_manufacturer, 8, ' ');
1670 RtlFillMemory(scsi_disk_model, 16, ' ');
1671 RtlFillMemory(scsi_cdrom_model, 16, ' ');
1673 RtlInitUnicodeString(&value_name, L"Manufacturer");
1674 buf_len = 256;
1675 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1676 if(NT_SUCCESS(status))
1677 wcstombs(scsi_device_manufacturer, (PWCHAR)kpv->Data, min(kpv->DataLength, 8));
1678 else
1679 RtlStringCbCopyA(scsi_device_manufacturer, 8, "XEN ");
1681 RtlInitUnicodeString(&value_name, L"Disk_Model");
1682 buf_len = 256;
1683 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1684 if(NT_SUCCESS(status))
1685 wcstombs(scsi_disk_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1686 else
1687 RtlStringCbCopyA(scsi_disk_model, 16, "PV DISK ");
1689 RtlInitUnicodeString(&value_name, L"CDROM_Model");
1690 buf_len = 256;
1691 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1692 if(NT_SUCCESS(status))
1693 wcstombs(scsi_cdrom_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1694 else
1695 RtlStringCbCopyA(scsi_cdrom_model, 16, "PV CDROM ");
1696 ZwClose(param_handle);
1698 ZwClose(service_handle);
1702 RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
1704 HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
1705 HwInitializationData.AdapterInterfaceType = PNPBus;
1706 if (!dump_mode)
1707 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_DEVICE_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE;
1708 else
1709 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_DEVICE_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE_DUMP_MODE;
1710 HwInitializationData.SpecificLuExtensionSize = 0;
1711 HwInitializationData.SrbExtensionSize = sizeof(srb_list_entry_t);
1712 HwInitializationData.NumberOfAccessRanges = 1;
1713 HwInitializationData.MapBuffers = TRUE;
1714 HwInitializationData.NeedPhysicalAddresses = FALSE;
1715 HwInitializationData.TaggedQueuing = TRUE;
1716 HwInitializationData.AutoRequestSense = TRUE;
1717 HwInitializationData.MultipleRequestPerLu = TRUE;
1718 HwInitializationData.ReceiveEvent = FALSE;
1719 HwInitializationData.VendorIdLength = 0;
1720 HwInitializationData.VendorId = NULL;
1721 HwInitializationData.DeviceIdLength = 0;
1722 HwInitializationData.DeviceId = NULL;
1724 HwInitializationData.HwInitialize = XenVbd_HwScsiInitialize;
1725 HwInitializationData.HwStartIo = XenVbd_HwScsiStartIo;
1726 HwInitializationData.HwInterrupt = XenVbd_HwScsiInterrupt;
1727 HwInitializationData.HwFindAdapter = XenVbd_HwScsiFindAdapter;
1728 HwInitializationData.HwResetBus = XenVbd_HwScsiResetBus;
1729 HwInitializationData.HwDmaStarted = NULL;
1730 HwInitializationData.HwAdapterState = NULL; //XenVbd_HwScsiAdapterState;
1731 HwInitializationData.HwAdapterControl = XenVbd_HwScsiAdapterControl;
1733 status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
1735 if(!NT_SUCCESS(status))
1737 KdPrint((__DRIVER_NAME " ScsiPortInitialize failed with status 0x%08x\n", status));
1740 FUNCTION_EXIT();
1742 return status;