win-pvdrivers

view xenvbd/xenvbd.c @ 810:1293cb31aa0b

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