win-pvdrivers

view xenvbd/xenvbd.c @ 812:551d17b09bc1

Force a crash if we get a buffer overrun in hibernate
author James Harper <james.harper@bendigoit.com.au>
date Sat Aug 28 13:53:10 2010 +1000 (2010-08-28)
parents 1293cb31aa0b
children 4d1539643494
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 ASSERT(!dump_mode || block_count * 512 < BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE * PAGE_SIZE);
443 xvdd->aligned_buffer_in_use = TRUE;
444 ptr = xvdd->aligned_buffer;
445 if (!decode_cdb_is_read(srb))
446 memcpy(ptr, srb->DataBuffer, block_count * 512);
447 shadow->aligned_buffer_in_use = TRUE;
448 }
449 else
450 {
451 ptr = srb->DataBuffer;
452 shadow->aligned_buffer_in_use = FALSE;
453 }
455 if (dump_mode && block_count > max_dump_mode_size)
456 {
457 max_dump_mode_size = block_count;
458 KdPrint((__DRIVER_NAME " max_dump_mode_size = %d\n", max_dump_mode_size));
459 }
461 //KdPrint((__DRIVER_NAME " sector_number = %d, block_count = %d\n", (ULONG)shadow->req.sector_number, block_count));
462 //KdPrint((__DRIVER_NAME " SrbExtension = %p\n", srb->SrbExtension));
463 //KdPrint((__DRIVER_NAME " DataBuffer = %p\n", srb->DataBuffer));
465 //KdPrint((__DRIVER_NAME " sector_number = %d\n", (ULONG)shadow->req.sector_number));
466 //KdPrint((__DRIVER_NAME " handle = %d\n", shadow->req.handle));
467 //KdPrint((__DRIVER_NAME " operation = %d\n", shadow->req.operation));
469 while (remaining > 0)
470 {
471 PHYSICAL_ADDRESS physical_address = MmGetPhysicalAddress(ptr);
473 if (dump_mode)
474 {
475 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
476 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE,
477 xvdd->dump_grant_refs[shadow->req.nr_segments], (ULONG)'XPDO');
478 }
479 else
480 {
481 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
482 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE, INVALID_GRANT_REF, (ULONG)'XVBD');
483 }
484 if (gref == INVALID_GRANT_REF)
485 {
486 ULONG i;
487 for (i = 0; i < shadow->req.nr_segments; i++)
488 {
489 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
490 shadow->req.seg[i].gref, FALSE, (ULONG)'XVBD');
491 }
492 if (shadow->aligned_buffer_in_use)
493 {
494 shadow->aligned_buffer_in_use = FALSE;
495 xvdd->aligned_buffer_in_use = FALSE;
496 }
497 /* put the srb back at the start of the queue */
498 InsertHeadList(&xvdd->srb_list, (PLIST_ENTRY)srb->SrbExtension);
499 put_shadow_on_freelist(xvdd, shadow);
500 KdPrint((__DRIVER_NAME " Out of gref's. Deferring\n"));
501 return;
502 }
503 offset = physical_address.LowPart & (PAGE_SIZE - 1);
504 length = min(PAGE_SIZE - offset, remaining);
505 ASSERT((offset & 511) == 0);
506 ASSERT((length & 511) == 0);
507 ASSERT(offset + length <= PAGE_SIZE);
508 shadow->req.seg[shadow->req.nr_segments].gref = gref;
509 shadow->req.seg[shadow->req.nr_segments].first_sect = (UCHAR)(offset >> 9);
510 shadow->req.seg[shadow->req.nr_segments].last_sect = (UCHAR)(((offset + length) >> 9) - 1);
511 remaining -= length;
512 ptr += length;
513 shadow->req.nr_segments++;
514 }
516 //KdPrint((__DRIVER_NAME " nr_segments = %d\n", shadow->req.nr_segments));
518 XenVbd_PutRequest(xvdd, &shadow->req);
520 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
521 if (notify)
522 {
523 //KdPrint((__DRIVER_NAME " Notifying\n"));
524 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
525 }
527 }
528 if (xvdd->shadow_free && !xvdd->aligned_buffer_in_use)
529 {
530 ScsiPortNotification(NextLuRequest, xvdd, 0, 0, 0);
531 }
532 //FUNCTION_EXIT();
533 }
535 static ULONG DDKAPI
536 XenVbd_HwScsiFindAdapter(PVOID DeviceExtension, PVOID HwContext, PVOID BusInformation, PCHAR ArgumentString, PPORT_CONFIGURATION_INFORMATION ConfigInfo, PBOOLEAN Again)
537 {
538 // PACCESS_RANGE AccessRange;
539 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
540 ULONG status;
541 // PXENPCI_XEN_DEVICE_DATA XenDeviceData;
542 PACCESS_RANGE access_range;
544 UNREFERENCED_PARAMETER(HwContext);
545 UNREFERENCED_PARAMETER(BusInformation);
546 UNREFERENCED_PARAMETER(ArgumentString);
548 FUNCTION_ENTER();
549 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
550 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
552 RtlZeroMemory(xvdd, sizeof(XENVBD_DEVICE_DATA));
553 *Again = FALSE;
555 KdPrint((__DRIVER_NAME " BusInterruptLevel = %d\n", ConfigInfo->BusInterruptLevel));
556 KdPrint((__DRIVER_NAME " BusInterruptVector = %03x\n", ConfigInfo->BusInterruptVector));
558 KdPrint((__DRIVER_NAME " NumberOfAccessRanges = %d\n", ConfigInfo->NumberOfAccessRanges));
559 if (ConfigInfo->NumberOfAccessRanges != 1 && ConfigInfo->NumberOfAccessRanges != 2)
560 {
561 return SP_RETURN_BAD_CONFIG;
562 }
564 access_range = &((*(ConfigInfo->AccessRanges))[0]);
565 KdPrint((__DRIVER_NAME " RangeStart = %08x, RangeLength = %08x\n",
566 access_range->RangeStart.LowPart, access_range->RangeLength));
567 xvdd->device_base = ScsiPortGetDeviceBase(
568 DeviceExtension,
569 ConfigInfo->AdapterInterfaceType,
570 ConfigInfo->SystemIoBusNumber,
571 access_range->RangeStart,
572 access_range->RangeLength,
573 !access_range->RangeInMemory);
574 if (!xvdd->device_base)
575 {
576 KdPrint((__DRIVER_NAME " Invalid config\n"));
577 FUNCTION_EXIT();
578 return SP_RETURN_BAD_CONFIG;
579 }
581 status = XenVbd_InitFromConfig(xvdd);
582 if (status != SP_RETURN_FOUND)
583 {
584 FUNCTION_EXIT();
585 return status;
586 }
588 xvdd->aligned_buffer_in_use = FALSE;
589 /* align the buffer to PAGE_SIZE */
590 xvdd->aligned_buffer = (PVOID)((ULONG_PTR)((PUCHAR)xvdd->aligned_buffer_data + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
591 KdPrint((__DRIVER_NAME " aligned_buffer_data = %p\n", xvdd->aligned_buffer_data));
592 KdPrint((__DRIVER_NAME " aligned_buffer = %p\n", xvdd->aligned_buffer));
594 if (!dump_mode)
595 {
596 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
597 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST - 1;
598 //ConfigInfo->ScatterGather = TRUE;
599 }
600 else
601 {
602 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE * PAGE_SIZE;
603 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE - 1;
604 //ConfigInfo->ScatterGather = FALSE;
605 }
606 ConfigInfo->ScatterGather = FALSE;
607 ConfigInfo->AlignmentMask = 0;
608 ConfigInfo->NumberOfBuses = 1;
609 ConfigInfo->InitiatorBusId[0] = 1;
610 ConfigInfo->MaximumNumberOfLogicalUnits = 1;
611 ConfigInfo->MaximumNumberOfTargets = 2;
612 ConfigInfo->BufferAccessScsiPortControlled = FALSE;
613 if (ConfigInfo->Dma64BitAddresses == SCSI_DMA64_SYSTEM_SUPPORTED)
614 {
615 ConfigInfo->Master = TRUE;
616 ConfigInfo->Dma64BitAddresses = SCSI_DMA64_MINIPORT_SUPPORTED;
617 ConfigInfo->Dma32BitAddresses = FALSE;
618 KdPrint((__DRIVER_NAME " Dma64BitAddresses supported\n"));
619 }
620 else
621 {
622 ConfigInfo->Master = TRUE;
623 ConfigInfo->Dma32BitAddresses = TRUE;
624 KdPrint((__DRIVER_NAME " Dma64BitAddresses not supported\n"));
625 }
627 FUNCTION_EXIT();
629 return SP_RETURN_FOUND;
630 }
632 static VOID
633 XenVbd_StartRingDetection(PXENVBD_DEVICE_DATA xvdd)
634 {
635 blkif_request_t *req;
636 int notify;
638 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT1;
639 RtlZeroMemory(xvdd->sring->ring, PAGE_SIZE - FIELD_OFFSET(blkif_sring_t, ring));
640 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
641 req->operation = 0xff;
642 xvdd->ring.req_prod_pvt++;
643 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
644 req->operation = 0xff;
645 xvdd->ring.req_prod_pvt++;
647 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
648 if (notify)
649 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
650 }
652 static BOOLEAN
653 XenVbd_HwScsiInitialize(PVOID DeviceExtension)
654 {
655 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
657 FUNCTION_ENTER();
658 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
659 KdPrint((__DRIVER_NAME " dump_mode = %d\n", dump_mode));
661 if (!xvdd->inactive)
662 {
663 if (!dump_mode)
664 {
665 XenVbd_StartRingDetection(xvdd);
666 }
667 else
668 {
669 if (xvdd->cached_use_other)
670 {
671 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
672 xvdd->use_other = TRUE;
673 }
674 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
675 }
676 InitializeListHead(&xvdd->srb_list);
677 }
678 FUNCTION_EXIT();
680 return TRUE;
681 }
683 static ULONG
684 XenVbd_FillModePage(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
685 {
686 PMODE_PARAMETER_HEADER parameter_header = NULL;
687 PMODE_PARAMETER_HEADER10 parameter_header10 = NULL;
688 PMODE_PARAMETER_BLOCK param_block;
689 PMODE_FORMAT_PAGE format_page;
690 ULONG offset = 0;
691 UCHAR buffer[1024];
692 BOOLEAN valid_page = FALSE;
693 BOOLEAN cdb_llbaa;
694 BOOLEAN cdb_dbd;
695 UCHAR cdb_page_code;
696 USHORT cdb_allocation_length;
698 UNREFERENCED_PARAMETER(xvdd);
700 RtlZeroMemory(srb->DataBuffer, srb->DataTransferLength);
701 RtlZeroMemory(buffer, ARRAY_SIZE(buffer));
702 offset = 0;
704 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
706 switch (srb->Cdb[0])
707 {
708 case SCSIOP_MODE_SENSE:
709 cdb_llbaa = FALSE;
710 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
711 cdb_page_code = srb->Cdb[2] & 0x3f;
712 cdb_allocation_length = srb->Cdb[4];
713 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
714 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
715 parameter_header = (PMODE_PARAMETER_HEADER)&buffer[offset];
716 parameter_header->MediumType = 0;
717 parameter_header->DeviceSpecificParameter = 0;
718 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
719 {
720 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
721 parameter_header->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
722 }
723 offset += sizeof(MODE_PARAMETER_HEADER);
724 break;
725 case SCSIOP_MODE_SENSE10:
726 cdb_llbaa = (BOOLEAN)!!(srb->Cdb[1] & 16);
727 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
728 cdb_page_code = srb->Cdb[2] & 0x3f;
729 cdb_allocation_length = (srb->Cdb[7] << 8) | srb->Cdb[8];
730 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE10 llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
731 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
732 parameter_header10 = (PMODE_PARAMETER_HEADER10)&buffer[offset];
733 parameter_header10->MediumType = 0;
734 parameter_header10->DeviceSpecificParameter = 0;
735 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
736 {
737 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
738 parameter_header10->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
739 }
740 offset += sizeof(MODE_PARAMETER_HEADER10);
741 break;
742 default:
743 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE_WTF (%02x)\n", (ULONG)srb->Cdb[0]));
744 return FALSE;
745 }
747 if (!cdb_dbd)
748 {
749 param_block = (PMODE_PARAMETER_BLOCK)&buffer[offset];
750 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK)
751 {
752 if (xvdd->total_sectors >> 32)
753 {
754 param_block->DensityCode = 0xff;
755 param_block->NumberOfBlocks[0] = 0xff;
756 param_block->NumberOfBlocks[1] = 0xff;
757 param_block->NumberOfBlocks[2] = 0xff;
758 }
759 else
760 {
761 param_block->DensityCode = (UCHAR)((xvdd->total_sectors >> 24) & 0xff);
762 param_block->NumberOfBlocks[0] = (UCHAR)((xvdd->total_sectors >> 16) & 0xff);
763 param_block->NumberOfBlocks[1] = (UCHAR)((xvdd->total_sectors >> 8) & 0xff);
764 param_block->NumberOfBlocks[2] = (UCHAR)((xvdd->total_sectors >> 0) & 0xff);
765 }
766 param_block->BlockLength[0] = (UCHAR)((xvdd->bytes_per_sector >> 16) & 0xff);
767 param_block->BlockLength[1] = (UCHAR)((xvdd->bytes_per_sector >> 8) & 0xff);
768 param_block->BlockLength[2] = (UCHAR)((xvdd->bytes_per_sector >> 0) & 0xff);
769 }
770 offset += sizeof(MODE_PARAMETER_BLOCK);
771 }
772 switch (srb->Cdb[0])
773 {
774 case SCSIOP_MODE_SENSE:
775 parameter_header->BlockDescriptorLength = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER));
776 break;
777 case SCSIOP_MODE_SENSE10:
778 parameter_header10->BlockDescriptorLength[0] = (UCHAR)((offset - sizeof(MODE_PARAMETER_HEADER10)) >> 8);
779 parameter_header10->BlockDescriptorLength[1] = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER10));
780 break;
781 }
782 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_FORMAT_DEVICE || cdb_page_code == MODE_SENSE_RETURN_ALL))
783 {
784 valid_page = TRUE;
785 format_page = (PMODE_FORMAT_PAGE)&buffer[offset];
786 format_page->PageCode = MODE_PAGE_FORMAT_DEVICE;
787 format_page->PageLength = sizeof(MODE_FORMAT_PAGE) - FIELD_OFFSET(MODE_FORMAT_PAGE, PageLength);
788 /* 256 sectors per track */
789 format_page->SectorsPerTrack[0] = 0x01;
790 format_page->SectorsPerTrack[1] = 0x00;
791 /* xxx bytes per sector */
792 format_page->BytesPerPhysicalSector[0] = (UCHAR)(xvdd->bytes_per_sector >> 8);
793 format_page->BytesPerPhysicalSector[1] = (UCHAR)(xvdd->bytes_per_sector & 0xff);
794 format_page->HardSectorFormating = TRUE;
795 format_page->SoftSectorFormating = TRUE;
796 offset += sizeof(MODE_FORMAT_PAGE);
797 }
798 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_CACHING || cdb_page_code == MODE_SENSE_RETURN_ALL))
799 {
800 PMODE_CACHING_PAGE caching_page;
801 valid_page = TRUE;
802 caching_page = (PMODE_CACHING_PAGE)&buffer[offset];
803 caching_page->PageCode = MODE_PAGE_CACHING;
804 caching_page->PageLength = sizeof(MODE_CACHING_PAGE) - FIELD_OFFSET(MODE_CACHING_PAGE, PageLength);
805 // caching_page-> // all zeros is just fine... maybe
806 offset += sizeof(MODE_CACHING_PAGE);
807 }
808 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_MEDIUM_TYPES || cdb_page_code == MODE_SENSE_RETURN_ALL))
809 {
810 PUCHAR medium_types_page;
811 valid_page = TRUE;
812 medium_types_page = &buffer[offset];
813 medium_types_page[0] = MODE_PAGE_MEDIUM_TYPES;
814 medium_types_page[1] = 0x06;
815 medium_types_page[2] = 0;
816 medium_types_page[3] = 0;
817 medium_types_page[4] = 0;
818 medium_types_page[5] = 0;
819 medium_types_page[6] = 0;
820 medium_types_page[7] = 0;
821 offset += 8;
822 }
823 switch (srb->Cdb[0])
824 {
825 case SCSIOP_MODE_SENSE:
826 parameter_header->ModeDataLength = (UCHAR)(offset - 1);
827 break;
828 case SCSIOP_MODE_SENSE10:
829 parameter_header10->ModeDataLength[0] = (UCHAR)((offset - 2) >> 8);
830 parameter_header10->ModeDataLength[1] = (UCHAR)(offset - 2);
831 break;
832 }
834 if (!valid_page && cdb_page_code != MODE_SENSE_RETURN_ALL)
835 {
836 srb->SrbStatus = SRB_STATUS_ERROR;
837 }
838 else if(offset < srb->DataTransferLength)
839 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
840 else
841 srb->SrbStatus = SRB_STATUS_SUCCESS;
842 srb->DataTransferLength = min(srb->DataTransferLength, offset);
843 srb->ScsiStatus = 0;
844 memcpy(srb->DataBuffer, buffer, srb->DataTransferLength);
846 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
848 return TRUE;
849 }
851 static VOID
852 XenVbd_MakeSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb, UCHAR sense_key, UCHAR additional_sense_code)
853 {
854 PSENSE_DATA sd = srb->SenseInfoBuffer;
856 UNREFERENCED_PARAMETER(xvdd);
858 if (!srb->SenseInfoBuffer)
859 return;
861 sd->ErrorCode = 0x70;
862 sd->Valid = 1;
863 sd->SenseKey = sense_key;
864 sd->AdditionalSenseLength = sizeof(SENSE_DATA) - FIELD_OFFSET(SENSE_DATA, AdditionalSenseLength);
865 sd->AdditionalSenseCode = additional_sense_code;
866 return;
867 }
869 static VOID
870 XenVbd_MakeAutoSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
871 {
872 if (srb->SrbStatus == SRB_STATUS_SUCCESS || srb->SrbFlags & SRB_FLAGS_DISABLE_AUTOSENSE)
873 return;
874 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
875 srb->SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
876 }
878 static BOOLEAN DDKAPI
879 XenVbd_HwScsiInterrupt(PVOID DeviceExtension)
880 {
881 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
882 PSCSI_REQUEST_BLOCK srb;
883 RING_IDX i, rp;
884 ULONG j;
885 blkif_response_t *rep;
886 int block_count;
887 int more_to_do = TRUE;
888 blkif_shadow_t *shadow;
889 ULONG suspend_resume_state_pdo;
890 BOOLEAN last_interrupt = FALSE;
892 /* in dump mode I think we get called on a timer, not by an actual IRQ */
893 if (!dump_mode && !xvdd->vectors.EvtChn_AckEvent(xvdd->vectors.context, xvdd->event_channel, &last_interrupt))
894 return FALSE; /* interrupt was not for us */
896 suspend_resume_state_pdo = xvdd->device_state->suspend_resume_state_pdo;
897 KeMemoryBarrier();
899 if (suspend_resume_state_pdo != xvdd->device_state->suspend_resume_state_fdo)
900 {
901 FUNCTION_ENTER();
902 switch (suspend_resume_state_pdo)
903 {
904 case SR_STATE_SUSPENDING:
905 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_SUSPENDING\n"));
906 break;
907 case SR_STATE_RESUMING:
908 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_RESUMING\n"));
909 XenVbd_InitFromConfig(xvdd);
910 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
911 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
912 break;
913 case SR_STATE_RUNNING:
914 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
915 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
916 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
917 ScsiPortNotification(NextRequest, DeviceExtension);
918 default:
919 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
920 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
921 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
922 break;
923 }
924 KeMemoryBarrier();
925 }
927 if (xvdd->device_state->suspend_resume_state_fdo != SR_STATE_RUNNING)
928 {
929 return last_interrupt;
930 }
932 while (more_to_do)
933 {
934 rp = xvdd->ring.sring->rsp_prod;
935 KeMemoryBarrier();
936 for (i = xvdd->ring.rsp_cons; i < rp; i++)
937 {
938 rep = XenVbd_GetResponse(xvdd, i);
939 /*
940 * This code is to automatically detect if the backend is using the same
941 * bit width or a different bit width to us. Later versions of Xen do this
942 * via a xenstore value, but not all. That 0x0fffffff (notice
943 * that the msb is not actually set, so we don't have any problems with
944 * sign extending) is to signify the last entry on the right, which is
945 * different under 32 and 64 bits, and that is why we set it up there.
947 * To do the detection, we put two initial entries on the ring, with an op
948 * of 0xff (which is invalid). The first entry is mostly okay, but the
949 * second will be grossly misaligned if the backend bit width is different,
950 * and we detect this and switch frontend structures.
951 */
952 switch (xvdd->ring_detect_state)
953 {
954 case RING_DETECT_STATE_NOT_STARTED:
955 KdPrint((__DRIVER_NAME " premature IRQ\n"));
956 break;
957 case RING_DETECT_STATE_DETECT1:
958 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));
959 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));
960 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT2;
961 break;
962 case RING_DETECT_STATE_DETECT2:
963 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));
964 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));
965 *xvdd->event_channel_ptr |= 0x80000000;
966 if (rep->operation != 0xff)
967 {
968 KdPrint((__DRIVER_NAME " switching to 'other' ring size\n"));
969 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
970 xvdd->use_other = TRUE;
971 *xvdd->event_channel_ptr |= 0x40000000;
972 }
973 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
974 ScsiPortNotification(NextRequest, DeviceExtension);
975 break;
976 case RING_DETECT_STATE_COMPLETE:
977 shadow = &xvdd->shadows[rep->id];
978 srb = shadow->srb;
979 ASSERT(srb != NULL);
980 block_count = decode_cdb_length(srb);
981 block_count *= xvdd->bytes_per_sector / 512;
982 /* 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 */
983 if (rep->status == BLKIF_RSP_OKAY || (dump_mode && dump_mode_errors++ < DUMP_MODE_ERROR_LIMIT))
984 srb->SrbStatus = SRB_STATUS_SUCCESS;
985 else
986 {
987 KdPrint((__DRIVER_NAME " Xen Operation returned error\n"));
988 if (decode_cdb_is_read(srb))
989 KdPrint((__DRIVER_NAME " Operation = Read\n"));
990 else
991 KdPrint((__DRIVER_NAME " Operation = Write\n"));
992 if (dump_mode)
993 {
994 KdPrint((__DRIVER_NAME " Sector = %08X, Count = %d\n", (ULONG)shadow->req.sector_number, block_count));
995 KdPrint((__DRIVER_NAME " DataBuffer = %p, aligned_buffer = %p\n", srb->DataBuffer, xvdd->aligned_buffer));
996 KdPrint((__DRIVER_NAME " Physical = %08x%08x\n", MmGetPhysicalAddress(srb->DataBuffer).HighPart, MmGetPhysicalAddress(srb->DataBuffer).LowPart));
997 KdPrint((__DRIVER_NAME " PFN = %08x\n", (ULONG)(MmGetPhysicalAddress(srb->DataBuffer).QuadPart >> PAGE_SHIFT)));
999 for (j = 0; j < shadow->req.nr_segments; j++)
1001 KdPrint((__DRIVER_NAME " gref = %d\n", shadow->req.seg[j].gref));
1002 KdPrint((__DRIVER_NAME " first_sect = %d\n", shadow->req.seg[j].first_sect));
1003 KdPrint((__DRIVER_NAME " last_sect = %d\n", shadow->req.seg[j].last_sect));
1006 srb->SrbStatus = SRB_STATUS_ERROR;
1007 srb->ScsiStatus = 0x02;
1008 xvdd->last_sense_key = SCSI_SENSE_MEDIUM_ERROR;
1009 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1010 XenVbd_MakeAutoSense(xvdd, srb);
1012 if (shadow->aligned_buffer_in_use)
1014 ASSERT(xvdd->aligned_buffer_in_use);
1015 xvdd->aligned_buffer_in_use = FALSE;
1016 if (decode_cdb_is_read(srb))
1017 memcpy(srb->DataBuffer, xvdd->aligned_buffer, block_count * 512);
1018 //KdPrint((__DRIVER_NAME " Completed use of buffer\n"));
1021 #if 0
1022 if (xvdd->aligned_buffer_in_use)
1024 KdPrint((__DRIVER_NAME " Completed request while aligned buffer in use\n"));
1026 #endif
1027 for (j = 0; j < shadow->req.nr_segments; j++)
1029 if (dump_mode)
1031 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1032 shadow->req.seg[j].gref, TRUE, (ULONG)'XPDO');
1034 else
1036 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1037 shadow->req.seg[j].gref, FALSE, (ULONG)'XVBD');
1040 shadow->aligned_buffer_in_use = FALSE;
1041 shadow->srb = NULL;
1042 put_shadow_on_freelist(xvdd, shadow);
1043 //KdPrint((__DRIVER_NAME " B RequestComplete srb = %p\n", srb));
1044 ScsiPortNotification(RequestComplete, xvdd, srb);
1045 break;
1049 xvdd->ring.rsp_cons = i;
1050 if (i != xvdd->ring.req_prod_pvt)
1052 RING_FINAL_CHECK_FOR_RESPONSES(&xvdd->ring, more_to_do);
1054 else
1056 xvdd->ring.sring->rsp_event = i + 1;
1057 more_to_do = FALSE;
1061 XenVbd_PutQueuedSrbsOnRing(xvdd);
1063 if (suspend_resume_state_pdo == SR_STATE_SUSPENDING)
1065 if (xvdd->inactive || xvdd->shadow_free == SHADOW_ENTRIES)
1067 /* all entries are purged from the list (or we are inactive). ready to suspend */
1068 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
1069 KeMemoryBarrier();
1070 KdPrint((__DRIVER_NAME " Set fdo state SR_STATE_SUSPENDING\n"));
1071 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xvdd->device_state->pdo_event_channel));
1072 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
1074 FUNCTION_EXIT();
1077 return last_interrupt;
1080 static BOOLEAN DDKAPI
1081 XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK srb)
1083 PUCHAR data_buffer;
1084 //ULONG data_buffer_length;
1085 PCDB cdb;
1086 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1087 ULONG data_transfer_length = srb->DataTransferLength;
1089 if (xvdd->inactive)
1091 KdPrint((__DRIVER_NAME " Inactive srb->Function = %08X\n", srb->Function));
1092 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1093 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1094 ScsiPortNotification(NextRequest, DeviceExtension);
1095 return TRUE;
1098 // If we haven't enumerated all the devices yet then just defer the request
1099 if (xvdd->ring_detect_state < RING_DETECT_STATE_COMPLETE)
1101 srb->SrbStatus = SRB_STATUS_BUSY;
1102 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1103 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Still figuring out ring)\n"));
1104 return TRUE;
1107 if (xvdd->device_state->suspend_resume_state_pdo != SR_STATE_RUNNING)
1109 KdPrint((__DRIVER_NAME " --> HwScsiStartIo (Suspending/Resuming)\n"));
1110 srb->SrbStatus = SRB_STATUS_BUSY;
1111 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1112 KdPrint((__DRIVER_NAME " <-- HwScsiStartIo (Suspending/Resuming)\n"));
1113 return TRUE;
1116 if (srb->PathId != 0 || srb->TargetId != 0)
1118 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1119 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1120 ScsiPortNotification(NextRequest, DeviceExtension);
1121 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Out of bounds)\n"));
1122 return TRUE;
1125 switch (srb->Function)
1127 case SRB_FUNCTION_EXECUTE_SCSI:
1128 cdb = (PCDB)srb->Cdb;
1130 switch(cdb->CDB6GENERIC.OperationCode)
1132 case SCSIOP_TEST_UNIT_READY:
1133 if (dump_mode)
1134 KdPrint((__DRIVER_NAME " Command = TEST_UNIT_READY\n"));
1135 srb->SrbStatus = SRB_STATUS_SUCCESS;
1136 srb->ScsiStatus = 0;
1137 break;
1138 case SCSIOP_INQUIRY:
1139 if (dump_mode)
1141 //PHYSICAL_ADDRESS physical;
1142 KdPrint((__DRIVER_NAME " Command = INQUIRY\n"));
1143 //KdPrint((__DRIVER_NAME " srb->Databuffer = %p\n", srb->DataBuffer));
1144 //physical = ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length);
1145 //KdPrint((__DRIVER_NAME " ScsiPortGetPhysicalAddress = %08x:%08x\n", physical.LowPart, physical.HighPart));
1147 // KdPrint((__DRIVER_NAME " (LUN = %d, EVPD = %d, Page Code = %02X)\n", srb->Cdb[1] >> 5, srb->Cdb[1] & 1, srb->Cdb[2]));
1148 // KdPrint((__DRIVER_NAME " (Length = %d)\n", srb->DataTransferLength));
1150 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1151 data_buffer = srb->DataBuffer;
1152 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1153 srb->SrbStatus = SRB_STATUS_SUCCESS;
1154 switch (xvdd->device_type)
1156 case XENVBD_DEVICETYPE_DISK:
1157 if ((srb->Cdb[1] & 1) == 0)
1159 if (srb->Cdb[2])
1161 srb->SrbStatus = SRB_STATUS_ERROR;
1163 else
1165 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1166 id->DeviceType = DIRECT_ACCESS_DEVICE;
1167 id->Versions = 4; /* minimum that WHQL says we must support */
1168 id->ResponseDataFormat = 2; /* not sure about this but WHQL complains otherwise */
1169 id->HiSupport = 1; /* WHQL test says we should set this */
1170 //id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1171 id->AdditionalLength = sizeof(INQUIRYDATA) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength) - 1;
1172 id->CommandQueue = 1;
1173 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1174 memcpy(id->ProductId, scsi_disk_model, 16); // product id
1175 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1176 data_transfer_length = sizeof(INQUIRYDATA);
1179 else
1181 switch (srb->Cdb[2])
1183 case VPD_SUPPORTED_PAGES: /* list of pages we support */
1184 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1185 data_buffer[1] = VPD_SUPPORTED_PAGES;
1186 data_buffer[2] = 0x00;
1187 data_buffer[3] = 2;
1188 data_buffer[4] = 0x00;
1189 data_buffer[5] = 0x80;
1190 data_transfer_length = 6;
1191 break;
1192 case VPD_SERIAL_NUMBER: /* serial number */
1193 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1194 data_buffer[1] = VPD_SERIAL_NUMBER;
1195 data_buffer[2] = 0x00;
1196 data_buffer[3] = 8;
1197 memset(&data_buffer[4], ' ', 8);
1198 data_transfer_length = 12;
1199 break;
1200 case VPD_DEVICE_IDENTIFIERS: /* identification - we don't support any so just return zero */
1201 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1202 data_buffer[1] = VPD_DEVICE_IDENTIFIERS;
1203 data_buffer[2] = 0x00;
1204 data_buffer[3] = 4 + (UCHAR)strlen(xvdd->vectors.path); /* length */
1205 data_buffer[4] = 2; /* ASCII */
1206 data_buffer[5] = 1; /* VendorId */
1207 data_buffer[6] = 0;
1208 data_buffer[7] = (UCHAR)strlen(xvdd->vectors.path);
1209 memcpy(&data_buffer[8], xvdd->vectors.path, strlen(xvdd->vectors.path));
1210 data_transfer_length = (ULONG)(8 + strlen(xvdd->vectors.path));
1211 break;
1212 default:
1213 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1214 srb->SrbStatus = SRB_STATUS_ERROR;
1215 break;
1218 break;
1219 case XENVBD_DEVICETYPE_CDROM:
1220 if ((srb->Cdb[1] & 1) == 0)
1222 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1223 id->DeviceType = READ_ONLY_DIRECT_ACCESS_DEVICE;
1224 id->RemovableMedia = 1;
1225 id->Versions = 3;
1226 id->ResponseDataFormat = 0;
1227 id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1228 id->CommandQueue = 1;
1229 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1230 memcpy(id->ProductId, scsi_cdrom_model, 16); // product id
1231 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1233 else
1235 switch (srb->Cdb[2])
1237 case 0x00:
1238 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1239 data_buffer[1] = 0x00;
1240 data_buffer[2] = 0x00;
1241 data_buffer[3] = 2;
1242 data_buffer[4] = 0x00;
1243 data_buffer[5] = 0x80;
1244 break;
1245 case 0x80:
1246 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1247 data_buffer[1] = 0x80;
1248 data_buffer[2] = 0x00;
1249 data_buffer[3] = 8;
1250 data_buffer[4] = 0x31;
1251 data_buffer[5] = 0x32;
1252 data_buffer[6] = 0x33;
1253 data_buffer[7] = 0x34;
1254 data_buffer[8] = 0x35;
1255 data_buffer[9] = 0x36;
1256 data_buffer[10] = 0x37;
1257 data_buffer[11] = 0x38;
1258 break;
1259 default:
1260 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1261 srb->SrbStatus = SRB_STATUS_ERROR;
1262 break;
1265 break;
1266 default:
1267 //KdPrint((__DRIVER_NAME " Unknown DeviceType %02x requested\n", xvdd->device_type));
1268 srb->SrbStatus = SRB_STATUS_ERROR;
1269 break;
1271 break;
1272 case SCSIOP_READ_CAPACITY:
1273 if (dump_mode)
1274 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1275 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1276 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1277 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1278 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1279 data_buffer = srb->DataBuffer;
1280 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1281 if ((xvdd->total_sectors - 1) >> 32)
1283 data_buffer[0] = 0xff;
1284 data_buffer[1] = 0xff;
1285 data_buffer[2] = 0xff;
1286 data_buffer[3] = 0xff;
1288 else
1290 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1291 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1292 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1293 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1295 data_buffer[4] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1296 data_buffer[5] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1297 data_buffer[6] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1298 data_buffer[7] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1299 srb->ScsiStatus = 0;
1300 srb->SrbStatus = SRB_STATUS_SUCCESS;
1301 break;
1302 case SCSIOP_READ_CAPACITY16:
1303 if (dump_mode)
1304 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1305 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1306 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1307 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1308 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1309 data_buffer = srb->DataBuffer;
1310 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1311 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 56) & 0xff;
1312 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 48) & 0xff;
1313 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 40) & 0xff;
1314 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 32) & 0xff;
1315 data_buffer[4] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1316 data_buffer[5] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1317 data_buffer[6] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1318 data_buffer[7] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1319 data_buffer[8] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1320 data_buffer[9] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1321 data_buffer[10] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1322 data_buffer[11] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1323 srb->ScsiStatus = 0;
1324 srb->SrbStatus = SRB_STATUS_SUCCESS;
1325 break;
1326 case SCSIOP_MODE_SENSE:
1327 case SCSIOP_MODE_SENSE10:
1328 if (dump_mode)
1329 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));
1330 XenVbd_FillModePage(xvdd, srb);
1331 break;
1332 case SCSIOP_READ:
1333 case SCSIOP_READ16:
1334 case SCSIOP_WRITE:
1335 case SCSIOP_WRITE16:
1336 XenVbd_PutSrbOnList(xvdd, srb);
1337 XenVbd_PutQueuedSrbsOnRing(xvdd);
1338 break;
1339 case SCSIOP_VERIFY:
1340 case SCSIOP_VERIFY16:
1341 // Should we do more here?
1342 if (dump_mode)
1343 KdPrint((__DRIVER_NAME " Command = VERIFY\n"));
1344 srb->SrbStatus = SRB_STATUS_SUCCESS;
1345 break;
1346 case SCSIOP_REPORT_LUNS:
1347 if (dump_mode)
1348 KdPrint((__DRIVER_NAME " Command = REPORT_LUNS\n"));
1349 srb->SrbStatus = SRB_STATUS_SUCCESS;;
1350 break;
1351 case SCSIOP_REQUEST_SENSE:
1352 if (dump_mode)
1353 KdPrint((__DRIVER_NAME " Command = REQUEST_SENSE\n"));
1354 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
1355 srb->SrbStatus = SRB_STATUS_SUCCESS;
1356 break;
1357 case SCSIOP_READ_TOC:
1358 if (dump_mode)
1359 KdPrint((__DRIVER_NAME " Command = READ_TOC\n"));
1360 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1361 data_buffer = srb->DataBuffer;
1362 // DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
1363 /*
1364 #define READ_TOC_FORMAT_TOC 0x00
1365 #define READ_TOC_FORMAT_SESSION 0x01
1366 #define READ_TOC_FORMAT_FULL_TOC 0x02
1367 #define READ_TOC_FORMAT_PMA 0x03
1368 #define READ_TOC_FORMAT_ATIP 0x04
1369 */
1370 // KdPrint((__DRIVER_NAME " Msf = %d\n", cdb->READ_TOC.Msf));
1371 // KdPrint((__DRIVER_NAME " LogicalUnitNumber = %d\n", cdb->READ_TOC.LogicalUnitNumber));
1372 // KdPrint((__DRIVER_NAME " Format2 = %d\n", cdb->READ_TOC.Format2));
1373 // KdPrint((__DRIVER_NAME " StartingTrack = %d\n", cdb->READ_TOC.StartingTrack));
1374 // KdPrint((__DRIVER_NAME " AllocationLength = %d\n", (cdb->READ_TOC.AllocationLength[0] << 8) | cdb->READ_TOC.AllocationLength[1]));
1375 // KdPrint((__DRIVER_NAME " Control = %d\n", cdb->READ_TOC.Control));
1376 // KdPrint((__DRIVER_NAME " Format = %d\n", cdb->READ_TOC.Format));
1377 switch (cdb->READ_TOC.Format2)
1379 case READ_TOC_FORMAT_TOC:
1380 data_buffer[0] = 0; // length MSB
1381 data_buffer[1] = 10; // length LSB
1382 data_buffer[2] = 1; // First Track
1383 data_buffer[3] = 1; // Last Track
1384 data_buffer[4] = 0; // Reserved
1385 data_buffer[5] = 0x14; // current position data + uninterrupted data
1386 data_buffer[6] = 1; // last complete track
1387 data_buffer[7] = 0; // reserved
1388 data_buffer[8] = 0; // MSB Block
1389 data_buffer[9] = 0;
1390 data_buffer[10] = 0;
1391 data_buffer[11] = 0; // LSB Block
1392 srb->SrbStatus = SRB_STATUS_SUCCESS;
1393 break;
1394 case READ_TOC_FORMAT_SESSION:
1395 case READ_TOC_FORMAT_FULL_TOC:
1396 case READ_TOC_FORMAT_PMA:
1397 case READ_TOC_FORMAT_ATIP:
1398 srb->SrbStatus = SRB_STATUS_ERROR;
1399 break;
1401 break;
1402 case SCSIOP_START_STOP_UNIT:
1403 KdPrint((__DRIVER_NAME " Command = SCSIOP_START_STOP_UNIT\n"));
1404 srb->SrbStatus = SRB_STATUS_SUCCESS;
1405 break;
1406 case SCSIOP_RESERVE_UNIT:
1407 KdPrint((__DRIVER_NAME " Command = SCSIOP_RESERVE_UNIT\n"));
1408 srb->SrbStatus = SRB_STATUS_SUCCESS;
1409 break;
1410 case SCSIOP_RELEASE_UNIT:
1411 KdPrint((__DRIVER_NAME " Command = SCSIOP_RELEASE_UNIT\n"));
1412 srb->SrbStatus = SRB_STATUS_SUCCESS;
1413 break;
1414 default:
1415 KdPrint((__DRIVER_NAME " Unhandled EXECUTE_SCSI Command = %02X\n", srb->Cdb[0]));
1416 srb->SrbStatus = SRB_STATUS_ERROR;
1417 break;
1419 if (srb->SrbStatus == SRB_STATUS_ERROR)
1421 KdPrint((__DRIVER_NAME " EXECUTE_SCSI Command = %02X returned error %02x\n", srb->Cdb[0], xvdd->last_sense_key));
1422 if (xvdd->last_sense_key == SCSI_SENSE_NO_SENSE)
1424 xvdd->last_sense_key = SCSI_SENSE_ILLEGAL_REQUEST;
1425 xvdd->last_additional_sense_code = SCSI_ADSENSE_INVALID_CDB;
1427 srb->ScsiStatus = 0x02;
1428 XenVbd_MakeAutoSense(xvdd, srb);
1429 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1430 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1432 ScsiPortNotification(NextRequest, DeviceExtension);
1435 else if (srb->SrbStatus != SRB_STATUS_PENDING)
1437 if (srb->SrbStatus == SRB_STATUS_SUCCESS && data_transfer_length < srb->DataTransferLength)
1439 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
1440 srb->DataTransferLength = data_transfer_length;
1442 xvdd->last_sense_key = SCSI_SENSE_NO_SENSE;
1443 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1444 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1445 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1447 ScsiPortNotification(NextRequest, DeviceExtension);
1450 break;
1451 case SRB_FUNCTION_IO_CONTROL:
1452 KdPrint((__DRIVER_NAME " SRB_FUNCTION_IO_CONTROL\n"));
1453 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1454 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1455 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1457 ScsiPortNotification(NextRequest, DeviceExtension);
1459 break;
1460 case SRB_FUNCTION_FLUSH:
1461 KdPrint((__DRIVER_NAME " SRB_FUNCTION_FLUSH %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1462 srb->SrbStatus = SRB_STATUS_SUCCESS;
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_SHUTDOWN:
1470 KdPrint((__DRIVER_NAME " SRB_FUNCTION_SHUTDOWN %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 default:
1479 KdPrint((__DRIVER_NAME " Unhandled srb->Function = %08X\n", srb->Function));
1480 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1481 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1482 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1484 ScsiPortNotification(NextRequest, DeviceExtension);
1486 break;
1489 //FUNCTION_EXIT();
1490 return TRUE;
1493 static BOOLEAN
1494 XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId)
1496 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1498 UNREFERENCED_PARAMETER(DeviceExtension);
1499 UNREFERENCED_PARAMETER(PathId);
1501 FUNCTION_ENTER();
1503 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1504 if (xvdd->ring_detect_state == RING_DETECT_STATE_COMPLETE && xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1506 ScsiPortNotification(NextRequest, DeviceExtension);
1509 FUNCTION_EXIT();
1512 return TRUE;
1515 static BOOLEAN
1516 XenVbd_HwScsiAdapterState(PVOID DeviceExtension, PVOID Context, BOOLEAN SaveState)
1518 UNREFERENCED_PARAMETER(DeviceExtension);
1519 UNREFERENCED_PARAMETER(Context);
1520 UNREFERENCED_PARAMETER(SaveState);
1522 FUNCTION_ENTER();
1523 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1525 FUNCTION_EXIT();
1527 return TRUE;
1530 static SCSI_ADAPTER_CONTROL_STATUS DDKAPI
1531 XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters)
1533 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1534 SCSI_ADAPTER_CONTROL_STATUS Status = ScsiAdapterControlSuccess;
1535 PSCSI_SUPPORTED_CONTROL_TYPE_LIST SupportedControlTypeList;
1536 //KIRQL OldIrql;
1538 FUNCTION_ENTER();
1539 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1540 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
1542 switch (ControlType)
1544 case ScsiQuerySupportedControlTypes:
1545 SupportedControlTypeList = (PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters;
1546 KdPrint((__DRIVER_NAME " ScsiQuerySupportedControlTypes (Max = %d)\n", SupportedControlTypeList->MaxControlType));
1547 SupportedControlTypeList->SupportedTypeList[ScsiQuerySupportedControlTypes] = TRUE;
1548 SupportedControlTypeList->SupportedTypeList[ScsiStopAdapter] = TRUE;
1549 SupportedControlTypeList->SupportedTypeList[ScsiRestartAdapter] = TRUE;
1550 break;
1551 case ScsiStopAdapter:
1552 KdPrint((__DRIVER_NAME " ScsiStopAdapter\n"));
1553 /* I don't think we actually have to do anything here... xenpci cleans up all the xenbus stuff for us */
1554 break;
1555 case ScsiRestartAdapter:
1556 KdPrint((__DRIVER_NAME " ScsiRestartAdapter\n"));
1557 if (!xvdd->inactive)
1559 if (XenVbd_InitFromConfig(xvdd) != SP_RETURN_FOUND)
1560 KeBugCheckEx(DATA_COHERENCY_EXCEPTION, 0, (ULONG_PTR) xvdd, 0, 0);
1561 XenVbd_StartRingDetection(xvdd);
1563 break;
1564 case ScsiSetBootConfig:
1565 KdPrint((__DRIVER_NAME " ScsiSetBootConfig\n"));
1566 break;
1567 case ScsiSetRunningConfig:
1568 KdPrint((__DRIVER_NAME " ScsiSetRunningConfig\n"));
1569 break;
1570 default:
1571 KdPrint((__DRIVER_NAME " UNKNOWN\n"));
1572 break;
1575 FUNCTION_EXIT();
1577 return Status;
1580 NTSTATUS
1581 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
1583 ULONG status;
1584 HW_INITIALIZATION_DATA HwInitializationData;
1585 PVOID driver_extension;
1586 PUCHAR ptr;
1587 OBJECT_ATTRIBUTES oa;
1588 HANDLE service_handle;
1589 UNICODE_STRING param_name;
1590 HANDLE param_handle;
1591 UNICODE_STRING value_name;
1592 CHAR buf[256];
1593 ULONG buf_len;
1594 PKEY_VALUE_PARTIAL_INFORMATION kpv;
1596 FUNCTION_ENTER();
1597 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1598 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
1600 /* RegistryPath == NULL when we are invoked as a crash dump driver */
1601 if (!RegistryPath)
1603 dump_mode = TRUE;
1606 if (!dump_mode)
1608 IoAllocateDriverObjectExtension(DriverObject, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC), PAGE_SIZE, &driver_extension);
1609 ptr = driver_extension;
1610 //ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
1611 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "ring-ref", NULL, NULL);
1612 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_IRQ, "event-channel", NULL, NULL);
1613 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_FRONT, "device-type", NULL, NULL);
1614 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mode", NULL, NULL);
1615 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sectors", NULL, NULL);
1616 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sector-size", NULL, NULL);
1617 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_GRANT_ENTRIES, NULL, ULongToPtr(BLKIF_MAX_SEGMENTS_PER_REQUEST), NULL); /* for use in crash dump */
1618 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT, NULL, NULL, NULL);
1619 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1620 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1621 __ADD_XEN_INIT_UCHAR(&ptr, 20);
1622 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1623 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT, NULL, NULL, NULL);
1624 //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1625 //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1626 //__ADD_XEN_INIT_UCHAR(&ptr, 20);
1627 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1628 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN, NULL, NULL, NULL);
1629 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
1630 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
1631 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1632 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
1633 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
1634 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1635 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitialising);
1636 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait);
1637 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1638 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1640 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1642 InitializeObjectAttributes(&oa, RegistryPath, OBJ_CASE_INSENSITIVE, NULL, NULL);
1643 status = ZwOpenKey(&service_handle, KEY_READ, &oa);
1644 if(!NT_SUCCESS(status))
1646 KdPrint((__DRIVER_NAME " ZwOpenKey(Service) returned %08x\n", status));
1648 else
1650 RtlInitUnicodeString(&param_name, L"Parameters");
1651 InitializeObjectAttributes(&oa, &param_name, OBJ_CASE_INSENSITIVE, service_handle, NULL);
1652 status = ZwOpenKey(&param_handle, KEY_READ, &oa);
1653 if(!NT_SUCCESS(status))
1655 KdPrint((__DRIVER_NAME " ZwOpenKey(Parameters) returned %08x\n", status));
1657 else
1659 kpv = (PKEY_VALUE_PARTIAL_INFORMATION)buf;
1660 RtlFillMemory(scsi_device_manufacturer, 8, ' ');
1661 RtlFillMemory(scsi_disk_model, 16, ' ');
1662 RtlFillMemory(scsi_cdrom_model, 16, ' ');
1664 RtlInitUnicodeString(&value_name, L"Manufacturer");
1665 buf_len = 256;
1666 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1667 if(NT_SUCCESS(status))
1668 wcstombs(scsi_device_manufacturer, (PWCHAR)kpv->Data, min(kpv->DataLength, 8));
1669 else
1670 RtlStringCbCopyA(scsi_device_manufacturer, 8, "XEN ");
1672 RtlInitUnicodeString(&value_name, L"Disk_Model");
1673 buf_len = 256;
1674 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1675 if(NT_SUCCESS(status))
1676 wcstombs(scsi_disk_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1677 else
1678 RtlStringCbCopyA(scsi_disk_model, 16, "PV DISK ");
1680 RtlInitUnicodeString(&value_name, L"CDROM_Model");
1681 buf_len = 256;
1682 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1683 if(NT_SUCCESS(status))
1684 wcstombs(scsi_cdrom_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1685 else
1686 RtlStringCbCopyA(scsi_cdrom_model, 16, "PV CDROM ");
1688 ZwClose(service_handle);
1692 RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
1694 HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
1695 HwInitializationData.AdapterInterfaceType = PNPBus;
1696 if (!dump_mode)
1697 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_DEVICE_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE;
1698 else
1699 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_DEVICE_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE_DUMP_MODE;
1700 HwInitializationData.SpecificLuExtensionSize = 0;
1701 HwInitializationData.SrbExtensionSize = sizeof(srb_list_entry_t);
1702 HwInitializationData.NumberOfAccessRanges = 1;
1703 HwInitializationData.MapBuffers = TRUE;
1704 HwInitializationData.NeedPhysicalAddresses = FALSE;
1705 HwInitializationData.TaggedQueuing = TRUE;
1706 HwInitializationData.AutoRequestSense = TRUE;
1707 HwInitializationData.MultipleRequestPerLu = TRUE;
1708 HwInitializationData.ReceiveEvent = FALSE;
1709 HwInitializationData.VendorIdLength = 0;
1710 HwInitializationData.VendorId = NULL;
1711 HwInitializationData.DeviceIdLength = 0;
1712 HwInitializationData.DeviceId = NULL;
1714 HwInitializationData.HwInitialize = XenVbd_HwScsiInitialize;
1715 HwInitializationData.HwStartIo = XenVbd_HwScsiStartIo;
1716 HwInitializationData.HwInterrupt = XenVbd_HwScsiInterrupt;
1717 HwInitializationData.HwFindAdapter = XenVbd_HwScsiFindAdapter;
1718 HwInitializationData.HwResetBus = XenVbd_HwScsiResetBus;
1719 HwInitializationData.HwDmaStarted = NULL;
1720 HwInitializationData.HwAdapterState = NULL; //XenVbd_HwScsiAdapterState;
1721 HwInitializationData.HwAdapterControl = XenVbd_HwScsiAdapterControl;
1723 status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
1725 if(!NT_SUCCESS(status))
1727 KdPrint((__DRIVER_NAME " ScsiPortInitialize failed with status 0x%08x\n", status));
1730 FUNCTION_EXIT();
1732 return status;