win-pvdrivers

view xenvbd/xenvbd.c @ 799:213306951ec5

Stall the xenvbd queue if we detect a write that overlaps a write already in the pipe. This prevents warnings with drbd and has no measurable loss of performance during normal use.
author James Harper <james.harper@bendigoit.com.au>
date Sat May 22 20:39:27 2010 +1000 (2010-05-22)
parents 467005e7f509
children bbc6c94b9621
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 {
552 int i;
553 for (i = 0; i < sizeof(XENVBD_DEVICE_DATA); i++)
554 {
555 if (((PUCHAR)xvdd)[i])
556 {
557 KdPrint((__DRIVER_NAME " DeviceExtension is not zeroed!!!\n"));
558 break;
559 }
560 }
561 }
562 RtlZeroMemory(xvdd, sizeof(XENVBD_DEVICE_DATA));
563 *Again = FALSE;
565 KdPrint((__DRIVER_NAME " BusInterruptLevel = %d\n", ConfigInfo->BusInterruptLevel));
566 KdPrint((__DRIVER_NAME " BusInterruptVector = %03x\n", ConfigInfo->BusInterruptVector));
568 KdPrint((__DRIVER_NAME " NumberOfAccessRanges = %d\n", ConfigInfo->NumberOfAccessRanges));
569 if (ConfigInfo->NumberOfAccessRanges != 1 && ConfigInfo->NumberOfAccessRanges != 2)
570 {
571 return SP_RETURN_BAD_CONFIG;
572 }
574 access_range = &((*(ConfigInfo->AccessRanges))[0]);
575 KdPrint((__DRIVER_NAME " RangeStart = %08x, RangeLength = %08x\n",
576 access_range->RangeStart.LowPart, access_range->RangeLength));
577 xvdd->device_base = ScsiPortGetDeviceBase(
578 DeviceExtension,
579 ConfigInfo->AdapterInterfaceType,
580 ConfigInfo->SystemIoBusNumber,
581 access_range->RangeStart,
582 access_range->RangeLength,
583 !access_range->RangeInMemory);
584 if (!xvdd->device_base)
585 {
586 KdPrint((__DRIVER_NAME " Invalid config\n"));
587 FUNCTION_EXIT();
588 return SP_RETURN_BAD_CONFIG;
589 }
591 status = XenVbd_InitFromConfig(xvdd);
592 if (status != SP_RETURN_FOUND)
593 {
594 FUNCTION_EXIT();
595 return status;
596 }
598 xvdd->aligned_buffer_in_use = FALSE;
599 /* align the buffer to PAGE_SIZE */
600 xvdd->aligned_buffer = (PVOID)((ULONG_PTR)((PUCHAR)xvdd->aligned_buffer_data + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
601 KdPrint((__DRIVER_NAME " aligned_buffer_data = %p\n", xvdd->aligned_buffer_data));
602 KdPrint((__DRIVER_NAME " aligned_buffer = %p\n", xvdd->aligned_buffer));
604 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
605 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST - 1;
606 ConfigInfo->ScatterGather = TRUE;
608 #if 0
609 if (!dump_mode)
610 {
611 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
612 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST - 1;
613 ConfigInfo->ScatterGather = TRUE;
614 }
615 else
616 {
617 ConfigInfo->MaximumTransferLength = 4096;
618 ConfigInfo->NumberOfPhysicalBreaks = 0;
619 ConfigInfo->ScatterGather = FALSE;
620 }
621 #endif
622 ConfigInfo->AlignmentMask = 0;
623 ConfigInfo->NumberOfBuses = 1;
624 ConfigInfo->InitiatorBusId[0] = 1;
625 ConfigInfo->MaximumNumberOfLogicalUnits = 1;
626 ConfigInfo->MaximumNumberOfTargets = 2;
627 ConfigInfo->BufferAccessScsiPortControlled = TRUE;
628 if (ConfigInfo->Dma64BitAddresses == SCSI_DMA64_SYSTEM_SUPPORTED)
629 {
630 ConfigInfo->Master = TRUE;
631 ConfigInfo->Dma64BitAddresses = SCSI_DMA64_MINIPORT_SUPPORTED;
632 ConfigInfo->Dma32BitAddresses = FALSE;
633 KdPrint((__DRIVER_NAME " Dma64BitAddresses supported\n"));
634 }
635 else
636 {
637 ConfigInfo->Master = TRUE;
638 ConfigInfo->Dma32BitAddresses = TRUE;
639 KdPrint((__DRIVER_NAME " Dma64BitAddresses not supported\n"));
640 }
642 FUNCTION_EXIT();
644 return SP_RETURN_FOUND;
645 }
647 static VOID
648 XenVbd_StartRingDetection(PXENVBD_DEVICE_DATA xvdd)
649 {
650 blkif_request_t *req;
651 int notify;
653 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT1;
654 RtlZeroMemory(xvdd->sring->ring, PAGE_SIZE - FIELD_OFFSET(blkif_sring_t, ring));
655 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
656 req->operation = 0xff;
657 xvdd->ring.req_prod_pvt++;
658 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
659 req->operation = 0xff;
660 xvdd->ring.req_prod_pvt++;
662 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
663 if (notify)
664 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
665 }
667 static BOOLEAN
668 XenVbd_HwScsiInitialize(PVOID DeviceExtension)
669 {
670 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
672 FUNCTION_ENTER();
673 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
674 KdPrint((__DRIVER_NAME " dump_mode = %d\n", dump_mode));
676 if (!xvdd->inactive)
677 {
678 if (!dump_mode)
679 {
680 XenVbd_StartRingDetection(xvdd);
681 }
682 else
683 {
684 if (xvdd->cached_use_other)
685 {
686 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
687 xvdd->use_other = TRUE;
688 }
689 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
690 }
691 InitializeListHead(&xvdd->srb_list);
692 }
693 FUNCTION_EXIT();
695 return TRUE;
696 }
698 static ULONG
699 XenVbd_FillModePage(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
700 {
701 PMODE_PARAMETER_HEADER parameter_header = NULL;
702 PMODE_PARAMETER_HEADER10 parameter_header10 = NULL;
703 PMODE_PARAMETER_BLOCK param_block;
704 PMODE_FORMAT_PAGE format_page;
705 ULONG offset = 0;
706 UCHAR buffer[1024];
707 BOOLEAN valid_page = FALSE;
708 BOOLEAN cdb_llbaa;
709 BOOLEAN cdb_dbd;
710 UCHAR cdb_page_code;
711 USHORT cdb_allocation_length;
713 UNREFERENCED_PARAMETER(xvdd);
715 RtlZeroMemory(srb->DataBuffer, srb->DataTransferLength);
716 RtlZeroMemory(buffer, ARRAY_SIZE(buffer));
717 offset = 0;
719 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
721 switch (srb->Cdb[0])
722 {
723 case SCSIOP_MODE_SENSE:
724 cdb_llbaa = FALSE;
725 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
726 cdb_page_code = srb->Cdb[2] & 0x3f;
727 cdb_allocation_length = srb->Cdb[4];
728 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
729 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
730 parameter_header = (PMODE_PARAMETER_HEADER)&buffer[offset];
731 parameter_header->MediumType = 0;
732 parameter_header->DeviceSpecificParameter = 0;
733 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
734 {
735 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
736 parameter_header->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
737 }
738 offset += sizeof(MODE_PARAMETER_HEADER);
739 break;
740 case SCSIOP_MODE_SENSE10:
741 cdb_llbaa = (BOOLEAN)!!(srb->Cdb[1] & 16);
742 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
743 cdb_page_code = srb->Cdb[2] & 0x3f;
744 cdb_allocation_length = (srb->Cdb[7] << 8) | srb->Cdb[8];
745 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE10 llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
746 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
747 parameter_header10 = (PMODE_PARAMETER_HEADER10)&buffer[offset];
748 parameter_header10->MediumType = 0;
749 parameter_header10->DeviceSpecificParameter = 0;
750 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
751 {
752 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
753 parameter_header10->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
754 }
755 offset += sizeof(MODE_PARAMETER_HEADER10);
756 break;
757 default:
758 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE_WTF (%02x)\n", (ULONG)srb->Cdb[0]));
759 return FALSE;
760 }
762 if (!cdb_dbd)
763 {
764 param_block = (PMODE_PARAMETER_BLOCK)&buffer[offset];
765 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK)
766 {
767 if (xvdd->total_sectors >> 32)
768 {
769 param_block->DensityCode = 0xff;
770 param_block->NumberOfBlocks[0] = 0xff;
771 param_block->NumberOfBlocks[1] = 0xff;
772 param_block->NumberOfBlocks[2] = 0xff;
773 }
774 else
775 {
776 param_block->DensityCode = (UCHAR)((xvdd->total_sectors >> 24) & 0xff);
777 param_block->NumberOfBlocks[0] = (UCHAR)((xvdd->total_sectors >> 16) & 0xff);
778 param_block->NumberOfBlocks[1] = (UCHAR)((xvdd->total_sectors >> 8) & 0xff);
779 param_block->NumberOfBlocks[2] = (UCHAR)((xvdd->total_sectors >> 0) & 0xff);
780 }
781 param_block->BlockLength[0] = (UCHAR)((xvdd->bytes_per_sector >> 16) & 0xff);
782 param_block->BlockLength[1] = (UCHAR)((xvdd->bytes_per_sector >> 8) & 0xff);
783 param_block->BlockLength[2] = (UCHAR)((xvdd->bytes_per_sector >> 0) & 0xff);
784 }
785 offset += sizeof(MODE_PARAMETER_BLOCK);
786 }
787 switch (srb->Cdb[0])
788 {
789 case SCSIOP_MODE_SENSE:
790 parameter_header->BlockDescriptorLength = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER));
791 break;
792 case SCSIOP_MODE_SENSE10:
793 parameter_header10->BlockDescriptorLength[0] = (UCHAR)((offset - sizeof(MODE_PARAMETER_HEADER10)) >> 8);
794 parameter_header10->BlockDescriptorLength[1] = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER10));
795 break;
796 }
797 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_FORMAT_DEVICE || cdb_page_code == MODE_SENSE_RETURN_ALL))
798 {
799 valid_page = TRUE;
800 format_page = (PMODE_FORMAT_PAGE)&buffer[offset];
801 format_page->PageCode = MODE_PAGE_FORMAT_DEVICE;
802 format_page->PageLength = sizeof(MODE_FORMAT_PAGE) - FIELD_OFFSET(MODE_FORMAT_PAGE, PageLength);
803 /* 256 sectors per track */
804 format_page->SectorsPerTrack[0] = 0x01;
805 format_page->SectorsPerTrack[1] = 0x00;
806 /* xxx bytes per sector */
807 format_page->BytesPerPhysicalSector[0] = (UCHAR)(xvdd->bytes_per_sector >> 8);
808 format_page->BytesPerPhysicalSector[1] = (UCHAR)(xvdd->bytes_per_sector & 0xff);
809 format_page->HardSectorFormating = TRUE;
810 format_page->SoftSectorFormating = TRUE;
811 offset += sizeof(MODE_FORMAT_PAGE);
812 }
813 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_CACHING || cdb_page_code == MODE_SENSE_RETURN_ALL))
814 {
815 PMODE_CACHING_PAGE caching_page;
816 valid_page = TRUE;
817 caching_page = (PMODE_CACHING_PAGE)&buffer[offset];
818 caching_page->PageCode = MODE_PAGE_CACHING;
819 caching_page->PageLength = sizeof(MODE_CACHING_PAGE) - FIELD_OFFSET(MODE_CACHING_PAGE, PageLength);
820 // caching_page-> // all zeros is just fine... maybe
821 offset += sizeof(MODE_CACHING_PAGE);
822 }
823 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_MEDIUM_TYPES || cdb_page_code == MODE_SENSE_RETURN_ALL))
824 {
825 PUCHAR medium_types_page;
826 valid_page = TRUE;
827 medium_types_page = &buffer[offset];
828 medium_types_page[0] = MODE_PAGE_MEDIUM_TYPES;
829 medium_types_page[1] = 0x06;
830 medium_types_page[2] = 0;
831 medium_types_page[3] = 0;
832 medium_types_page[4] = 0;
833 medium_types_page[5] = 0;
834 medium_types_page[6] = 0;
835 medium_types_page[7] = 0;
836 offset += 8;
837 }
838 switch (srb->Cdb[0])
839 {
840 case SCSIOP_MODE_SENSE:
841 parameter_header->ModeDataLength = (UCHAR)(offset - 1);
842 break;
843 case SCSIOP_MODE_SENSE10:
844 parameter_header10->ModeDataLength[0] = (UCHAR)((offset - 2) >> 8);
845 parameter_header10->ModeDataLength[1] = (UCHAR)(offset - 2);
846 break;
847 }
849 if (!valid_page && cdb_page_code != MODE_SENSE_RETURN_ALL)
850 {
851 srb->SrbStatus = SRB_STATUS_ERROR;
852 }
853 else if(offset < srb->DataTransferLength)
854 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
855 else
856 srb->SrbStatus = SRB_STATUS_SUCCESS;
857 srb->DataTransferLength = min(srb->DataTransferLength, offset);
858 srb->ScsiStatus = 0;
859 memcpy(srb->DataBuffer, buffer, srb->DataTransferLength);
861 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
863 return TRUE;
864 }
866 static VOID
867 XenVbd_MakeSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb, UCHAR sense_key, UCHAR additional_sense_code)
868 {
869 PSENSE_DATA sd = srb->SenseInfoBuffer;
871 UNREFERENCED_PARAMETER(xvdd);
873 if (!srb->SenseInfoBuffer)
874 return;
876 sd->ErrorCode = 0x70;
877 sd->Valid = 1;
878 sd->SenseKey = sense_key;
879 sd->AdditionalSenseLength = sizeof(SENSE_DATA) - FIELD_OFFSET(SENSE_DATA, AdditionalSenseLength);
880 sd->AdditionalSenseCode = additional_sense_code;
881 return;
882 }
884 static VOID
885 XenVbd_MakeAutoSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
886 {
887 if (srb->SrbStatus == SRB_STATUS_SUCCESS || srb->SrbFlags & SRB_FLAGS_DISABLE_AUTOSENSE)
888 return;
889 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
890 srb->SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
891 }
893 static BOOLEAN DDKAPI
894 XenVbd_HwScsiInterrupt(PVOID DeviceExtension)
895 {
896 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
897 PSCSI_REQUEST_BLOCK srb;
898 RING_IDX i, rp;
899 ULONG j;
900 blkif_response_t *rep;
901 int block_count;
902 int more_to_do = TRUE;
903 blkif_shadow_t *shadow;
904 ULONG suspend_resume_state_pdo;
905 BOOLEAN last_interrupt = FALSE;
907 /* in dump mode I think we get called on a timer, not by an actual IRQ */
908 if (!dump_mode && !xvdd->vectors.EvtChn_AckEvent(xvdd->vectors.context, xvdd->event_channel, &last_interrupt))
909 return FALSE; /* interrupt was not for us */
911 suspend_resume_state_pdo = xvdd->device_state->suspend_resume_state_pdo;
912 KeMemoryBarrier();
914 if (suspend_resume_state_pdo != xvdd->device_state->suspend_resume_state_fdo)
915 {
916 FUNCTION_ENTER();
917 switch (suspend_resume_state_pdo)
918 {
919 case SR_STATE_SUSPENDING:
920 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_SUSPENDING\n"));
921 break;
922 case SR_STATE_RESUMING:
923 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_RESUMING\n"));
924 XenVbd_InitFromConfig(xvdd);
925 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
926 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
927 break;
928 case SR_STATE_RUNNING:
929 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
930 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
931 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
932 ScsiPortNotification(NextRequest, DeviceExtension);
933 default:
934 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
935 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
936 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
937 break;
938 }
939 KeMemoryBarrier();
940 }
942 if (xvdd->device_state->suspend_resume_state_fdo != SR_STATE_RUNNING)
943 {
944 return last_interrupt;
945 }
947 while (more_to_do)
948 {
949 rp = xvdd->ring.sring->rsp_prod;
950 KeMemoryBarrier();
951 for (i = xvdd->ring.rsp_cons; i < rp; i++)
952 {
953 rep = XenVbd_GetResponse(xvdd, i);
954 /*
955 * This code is to automatically detect if the backend is using the same
956 * bit width or a different bit width to us. Later versions of Xen do this
957 * via a xenstore value, but not all. That 0x0fffffff (notice
958 * that the msb is not actually set, so we don't have any problems with
959 * sign extending) is to signify the last entry on the right, which is
960 * different under 32 and 64 bits, and that is why we set it up there.
962 * To do the detection, we put two initial entries on the ring, with an op
963 * of 0xff (which is invalid). The first entry is mostly okay, but the
964 * second will be grossly misaligned if the backend bit width is different,
965 * and we detect this and switch frontend structures.
966 */
967 switch (xvdd->ring_detect_state)
968 {
969 case RING_DETECT_STATE_NOT_STARTED:
970 KdPrint((__DRIVER_NAME " premature IRQ\n"));
971 break;
972 case RING_DETECT_STATE_DETECT1:
973 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));
974 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));
975 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT2;
976 break;
977 case RING_DETECT_STATE_DETECT2:
978 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));
979 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));
980 *xvdd->event_channel_ptr |= 0x80000000;
981 if (rep->operation != 0xff)
982 {
983 KdPrint((__DRIVER_NAME " switching to 'other' ring size\n"));
984 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
985 xvdd->use_other = TRUE;
986 *xvdd->event_channel_ptr |= 0x40000000;
987 }
988 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
989 ScsiPortNotification(NextRequest, DeviceExtension);
990 break;
991 case RING_DETECT_STATE_COMPLETE:
992 shadow = &xvdd->shadows[rep->id];
993 srb = shadow->srb;
994 ASSERT(srb != NULL);
995 block_count = decode_cdb_length(srb);
996 block_count *= xvdd->bytes_per_sector / 512;
997 /* 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 */
998 if (rep->status == BLKIF_RSP_OKAY || (dump_mode && dump_mode_errors++ < DUMP_MODE_ERROR_LIMIT))
999 srb->SrbStatus = SRB_STATUS_SUCCESS;
1000 else
1002 KdPrint((__DRIVER_NAME " Xen Operation returned error\n"));
1003 if (decode_cdb_is_read(srb))
1004 KdPrint((__DRIVER_NAME " Operation = Read\n"));
1005 else
1006 KdPrint((__DRIVER_NAME " Operation = Write\n"));
1007 if (dump_mode)
1009 KdPrint((__DRIVER_NAME " Sector = %08X, Count = %d\n", (ULONG)shadow->req.sector_number, block_count));
1010 KdPrint((__DRIVER_NAME " DataBuffer = %p, aligned_buffer = %p\n", srb->DataBuffer, xvdd->aligned_buffer));
1011 KdPrint((__DRIVER_NAME " Physical = %08x%08x\n", MmGetPhysicalAddress(srb->DataBuffer).HighPart, MmGetPhysicalAddress(srb->DataBuffer).LowPart));
1012 KdPrint((__DRIVER_NAME " PFN = %08x\n", (ULONG)(MmGetPhysicalAddress(srb->DataBuffer).QuadPart >> PAGE_SHIFT)));
1014 for (j = 0; j < shadow->req.nr_segments; j++)
1016 KdPrint((__DRIVER_NAME " gref = %d\n", shadow->req.seg[j].gref));
1017 KdPrint((__DRIVER_NAME " first_sect = %d\n", shadow->req.seg[j].first_sect));
1018 KdPrint((__DRIVER_NAME " last_sect = %d\n", shadow->req.seg[j].last_sect));
1021 srb->SrbStatus = SRB_STATUS_ERROR;
1022 srb->ScsiStatus = 0x02;
1023 xvdd->last_sense_key = SCSI_SENSE_MEDIUM_ERROR;
1024 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1025 XenVbd_MakeAutoSense(xvdd, srb);
1027 if (shadow->aligned_buffer_in_use)
1029 ASSERT(xvdd->aligned_buffer_in_use);
1030 xvdd->aligned_buffer_in_use = FALSE;
1031 if (decode_cdb_is_read(srb))
1032 memcpy(srb->DataBuffer, xvdd->aligned_buffer, block_count * 512);
1033 //KdPrint((__DRIVER_NAME " Completed use of buffer\n"));
1036 #if 0
1037 if (xvdd->aligned_buffer_in_use)
1039 KdPrint((__DRIVER_NAME " Completed request while aligned buffer in use\n"));
1041 #endif
1042 for (j = 0; j < shadow->req.nr_segments; j++)
1044 if (dump_mode)
1046 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1047 shadow->req.seg[j].gref, TRUE, (ULONG)'XPDO');
1049 else
1051 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1052 shadow->req.seg[j].gref, FALSE, (ULONG)'XVBD');
1055 shadow->aligned_buffer_in_use = FALSE;
1056 shadow->srb = NULL;
1057 put_shadow_on_freelist(xvdd, shadow);
1058 //KdPrint((__DRIVER_NAME " B RequestComplete srb = %p\n", srb));
1059 ScsiPortNotification(RequestComplete, xvdd, srb);
1060 break;
1064 xvdd->ring.rsp_cons = i;
1065 if (i != xvdd->ring.req_prod_pvt)
1067 RING_FINAL_CHECK_FOR_RESPONSES(&xvdd->ring, more_to_do);
1069 else
1071 xvdd->ring.sring->rsp_event = i + 1;
1072 more_to_do = FALSE;
1076 XenVbd_PutQueuedSrbsOnRing(xvdd);
1078 if (suspend_resume_state_pdo == SR_STATE_SUSPENDING)
1080 if (xvdd->inactive || xvdd->shadow_free == SHADOW_ENTRIES)
1082 /* all entries are purged from the list (or we are inactive). ready to suspend */
1083 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
1084 KeMemoryBarrier();
1085 KdPrint((__DRIVER_NAME " Set fdo state SR_STATE_SUSPENDING\n"));
1086 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xvdd->device_state->pdo_event_channel));
1087 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
1089 FUNCTION_EXIT();
1092 return last_interrupt;
1095 static BOOLEAN DDKAPI
1096 XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK srb)
1098 PUCHAR data_buffer;
1099 //ULONG data_buffer_length;
1100 PCDB cdb;
1101 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1102 ULONG data_transfer_length = srb->DataTransferLength;
1104 if (xvdd->inactive)
1106 KdPrint((__DRIVER_NAME " Inactive srb->Function = %08X\n", srb->Function));
1107 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1108 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1109 ScsiPortNotification(NextRequest, DeviceExtension);
1110 return TRUE;
1113 // If we haven't enumerated all the devices yet then just defer the request
1114 if (xvdd->ring_detect_state < RING_DETECT_STATE_COMPLETE)
1116 srb->SrbStatus = SRB_STATUS_BUSY;
1117 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1118 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Still figuring out ring)\n"));
1119 return TRUE;
1122 if (xvdd->device_state->suspend_resume_state_pdo != SR_STATE_RUNNING)
1124 KdPrint((__DRIVER_NAME " --> HwScsiStartIo (Suspending/Resuming)\n"));
1125 srb->SrbStatus = SRB_STATUS_BUSY;
1126 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1127 KdPrint((__DRIVER_NAME " <-- HwScsiStartIo (Suspending/Resuming)\n"));
1128 return TRUE;
1131 if (srb->PathId != 0 || srb->TargetId != 0)
1133 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1134 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1135 ScsiPortNotification(NextRequest, DeviceExtension);
1136 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Out of bounds)\n"));
1137 return TRUE;
1140 switch (srb->Function)
1142 case SRB_FUNCTION_EXECUTE_SCSI:
1143 cdb = (PCDB)srb->Cdb;
1145 switch(cdb->CDB6GENERIC.OperationCode)
1147 case SCSIOP_TEST_UNIT_READY:
1148 if (dump_mode)
1149 KdPrint((__DRIVER_NAME " Command = TEST_UNIT_READY\n"));
1150 srb->SrbStatus = SRB_STATUS_SUCCESS;
1151 srb->ScsiStatus = 0;
1152 break;
1153 case SCSIOP_INQUIRY:
1154 if (dump_mode)
1156 //PHYSICAL_ADDRESS physical;
1157 KdPrint((__DRIVER_NAME " Command = INQUIRY\n"));
1158 //KdPrint((__DRIVER_NAME " srb->Databuffer = %p\n", srb->DataBuffer));
1159 //physical = ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length);
1160 //KdPrint((__DRIVER_NAME " ScsiPortGetPhysicalAddress = %08x:%08x\n", physical.LowPart, physical.HighPart));
1162 // KdPrint((__DRIVER_NAME " (LUN = %d, EVPD = %d, Page Code = %02X)\n", srb->Cdb[1] >> 5, srb->Cdb[1] & 1, srb->Cdb[2]));
1163 // KdPrint((__DRIVER_NAME " (Length = %d)\n", srb->DataTransferLength));
1165 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1166 data_buffer = srb->DataBuffer;
1167 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1168 srb->SrbStatus = SRB_STATUS_SUCCESS;
1169 switch (xvdd->device_type)
1171 case XENVBD_DEVICETYPE_DISK:
1172 if ((srb->Cdb[1] & 1) == 0)
1174 if (srb->Cdb[2])
1176 srb->SrbStatus = SRB_STATUS_ERROR;
1178 else
1180 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1181 id->DeviceType = DIRECT_ACCESS_DEVICE;
1182 id->Versions = 4; /* minimum that WHQL says we must support */
1183 id->ResponseDataFormat = 2; /* not sure about this but WHQL complains otherwise */
1184 id->HiSupport = 1; /* WHQL test says we should set this */
1185 //id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1186 id->AdditionalLength = sizeof(INQUIRYDATA) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength) - 1;
1187 id->CommandQueue = 1;
1188 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1189 memcpy(id->ProductId, scsi_disk_model, 16); // product id
1190 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1191 data_transfer_length = sizeof(INQUIRYDATA);
1194 else
1196 switch (srb->Cdb[2])
1198 case VPD_SUPPORTED_PAGES: /* list of pages we support */
1199 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1200 data_buffer[1] = VPD_SUPPORTED_PAGES;
1201 data_buffer[2] = 0x00;
1202 data_buffer[3] = 2;
1203 data_buffer[4] = 0x00;
1204 data_buffer[5] = 0x80;
1205 data_transfer_length = 6;
1206 break;
1207 case VPD_SERIAL_NUMBER: /* serial number */
1208 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1209 data_buffer[1] = VPD_SERIAL_NUMBER;
1210 data_buffer[2] = 0x00;
1211 data_buffer[3] = 8;
1212 memset(&data_buffer[4], ' ', 8);
1213 data_transfer_length = 12;
1214 break;
1215 case VPD_DEVICE_IDENTIFIERS: /* identification - we don't support any so just return zero */
1216 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1217 data_buffer[1] = VPD_DEVICE_IDENTIFIERS;
1218 data_buffer[2] = 0x00;
1219 data_buffer[3] = 4 + (UCHAR)strlen(xvdd->vectors.path); /* length */
1220 data_buffer[4] = 2; /* ASCII */
1221 data_buffer[5] = 1; /* VendorId */
1222 data_buffer[6] = 0;
1223 data_buffer[7] = (UCHAR)strlen(xvdd->vectors.path);
1224 memcpy(&data_buffer[8], xvdd->vectors.path, strlen(xvdd->vectors.path));
1225 data_transfer_length = (ULONG)(8 + strlen(xvdd->vectors.path));
1226 break;
1227 default:
1228 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1229 srb->SrbStatus = SRB_STATUS_ERROR;
1230 break;
1233 break;
1234 case XENVBD_DEVICETYPE_CDROM:
1235 if ((srb->Cdb[1] & 1) == 0)
1237 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1238 id->DeviceType = READ_ONLY_DIRECT_ACCESS_DEVICE;
1239 id->RemovableMedia = 1;
1240 id->Versions = 3;
1241 id->ResponseDataFormat = 0;
1242 id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1243 id->CommandQueue = 1;
1244 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1245 memcpy(id->ProductId, scsi_cdrom_model, 16); // product id
1246 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1248 else
1250 switch (srb->Cdb[2])
1252 case 0x00:
1253 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1254 data_buffer[1] = 0x00;
1255 data_buffer[2] = 0x00;
1256 data_buffer[3] = 2;
1257 data_buffer[4] = 0x00;
1258 data_buffer[5] = 0x80;
1259 break;
1260 case 0x80:
1261 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1262 data_buffer[1] = 0x80;
1263 data_buffer[2] = 0x00;
1264 data_buffer[3] = 8;
1265 data_buffer[4] = 0x31;
1266 data_buffer[5] = 0x32;
1267 data_buffer[6] = 0x33;
1268 data_buffer[7] = 0x34;
1269 data_buffer[8] = 0x35;
1270 data_buffer[9] = 0x36;
1271 data_buffer[10] = 0x37;
1272 data_buffer[11] = 0x38;
1273 break;
1274 default:
1275 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1276 srb->SrbStatus = SRB_STATUS_ERROR;
1277 break;
1280 break;
1281 default:
1282 //KdPrint((__DRIVER_NAME " Unknown DeviceType %02x requested\n", xvdd->device_type));
1283 srb->SrbStatus = SRB_STATUS_ERROR;
1284 break;
1286 break;
1287 case SCSIOP_READ_CAPACITY:
1288 if (dump_mode)
1289 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1290 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1291 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1292 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1293 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1294 data_buffer = srb->DataBuffer;
1295 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1296 if ((xvdd->total_sectors - 1) >> 32)
1298 data_buffer[0] = 0xff;
1299 data_buffer[1] = 0xff;
1300 data_buffer[2] = 0xff;
1301 data_buffer[3] = 0xff;
1303 else
1305 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1306 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1307 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1308 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1310 data_buffer[4] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1311 data_buffer[5] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1312 data_buffer[6] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1313 data_buffer[7] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1314 srb->ScsiStatus = 0;
1315 srb->SrbStatus = SRB_STATUS_SUCCESS;
1316 break;
1317 case SCSIOP_READ_CAPACITY16:
1318 if (dump_mode)
1319 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1320 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1321 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1322 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1323 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1324 data_buffer = srb->DataBuffer;
1325 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1326 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 56) & 0xff;
1327 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 48) & 0xff;
1328 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 40) & 0xff;
1329 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 32) & 0xff;
1330 data_buffer[4] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1331 data_buffer[5] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1332 data_buffer[6] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1333 data_buffer[7] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1334 data_buffer[8] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1335 data_buffer[9] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1336 data_buffer[10] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1337 data_buffer[11] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1338 srb->ScsiStatus = 0;
1339 srb->SrbStatus = SRB_STATUS_SUCCESS;
1340 break;
1341 case SCSIOP_MODE_SENSE:
1342 case SCSIOP_MODE_SENSE10:
1343 if (dump_mode)
1344 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));
1345 XenVbd_FillModePage(xvdd, srb);
1346 break;
1347 case SCSIOP_READ:
1348 case SCSIOP_READ16:
1349 case SCSIOP_WRITE:
1350 case SCSIOP_WRITE16:
1351 XenVbd_PutSrbOnList(xvdd, srb);
1352 XenVbd_PutQueuedSrbsOnRing(xvdd);
1353 break;
1354 case SCSIOP_VERIFY:
1355 case SCSIOP_VERIFY16:
1356 // Should we do more here?
1357 if (dump_mode)
1358 KdPrint((__DRIVER_NAME " Command = VERIFY\n"));
1359 srb->SrbStatus = SRB_STATUS_SUCCESS;
1360 break;
1361 case SCSIOP_REPORT_LUNS:
1362 if (dump_mode)
1363 KdPrint((__DRIVER_NAME " Command = REPORT_LUNS\n"));
1364 srb->SrbStatus = SRB_STATUS_SUCCESS;;
1365 break;
1366 case SCSIOP_REQUEST_SENSE:
1367 if (dump_mode)
1368 KdPrint((__DRIVER_NAME " Command = REQUEST_SENSE\n"));
1369 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
1370 srb->SrbStatus = SRB_STATUS_SUCCESS;
1371 break;
1372 case SCSIOP_READ_TOC:
1373 if (dump_mode)
1374 KdPrint((__DRIVER_NAME " Command = READ_TOC\n"));
1375 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1376 data_buffer = srb->DataBuffer;
1377 // DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
1378 /*
1379 #define READ_TOC_FORMAT_TOC 0x00
1380 #define READ_TOC_FORMAT_SESSION 0x01
1381 #define READ_TOC_FORMAT_FULL_TOC 0x02
1382 #define READ_TOC_FORMAT_PMA 0x03
1383 #define READ_TOC_FORMAT_ATIP 0x04
1384 */
1385 // KdPrint((__DRIVER_NAME " Msf = %d\n", cdb->READ_TOC.Msf));
1386 // KdPrint((__DRIVER_NAME " LogicalUnitNumber = %d\n", cdb->READ_TOC.LogicalUnitNumber));
1387 // KdPrint((__DRIVER_NAME " Format2 = %d\n", cdb->READ_TOC.Format2));
1388 // KdPrint((__DRIVER_NAME " StartingTrack = %d\n", cdb->READ_TOC.StartingTrack));
1389 // KdPrint((__DRIVER_NAME " AllocationLength = %d\n", (cdb->READ_TOC.AllocationLength[0] << 8) | cdb->READ_TOC.AllocationLength[1]));
1390 // KdPrint((__DRIVER_NAME " Control = %d\n", cdb->READ_TOC.Control));
1391 // KdPrint((__DRIVER_NAME " Format = %d\n", cdb->READ_TOC.Format));
1392 switch (cdb->READ_TOC.Format2)
1394 case READ_TOC_FORMAT_TOC:
1395 data_buffer[0] = 0; // length MSB
1396 data_buffer[1] = 10; // length LSB
1397 data_buffer[2] = 1; // First Track
1398 data_buffer[3] = 1; // Last Track
1399 data_buffer[4] = 0; // Reserved
1400 data_buffer[5] = 0x14; // current position data + uninterrupted data
1401 data_buffer[6] = 1; // last complete track
1402 data_buffer[7] = 0; // reserved
1403 data_buffer[8] = 0; // MSB Block
1404 data_buffer[9] = 0;
1405 data_buffer[10] = 0;
1406 data_buffer[11] = 0; // LSB Block
1407 srb->SrbStatus = SRB_STATUS_SUCCESS;
1408 break;
1409 case READ_TOC_FORMAT_SESSION:
1410 case READ_TOC_FORMAT_FULL_TOC:
1411 case READ_TOC_FORMAT_PMA:
1412 case READ_TOC_FORMAT_ATIP:
1413 srb->SrbStatus = SRB_STATUS_ERROR;
1414 break;
1416 break;
1417 case SCSIOP_START_STOP_UNIT:
1418 KdPrint((__DRIVER_NAME " Command = SCSIOP_START_STOP_UNIT\n"));
1419 srb->SrbStatus = SRB_STATUS_SUCCESS;
1420 break;
1421 case SCSIOP_RESERVE_UNIT:
1422 KdPrint((__DRIVER_NAME " Command = SCSIOP_RESERVE_UNIT\n"));
1423 srb->SrbStatus = SRB_STATUS_SUCCESS;
1424 break;
1425 case SCSIOP_RELEASE_UNIT:
1426 KdPrint((__DRIVER_NAME " Command = SCSIOP_RELEASE_UNIT\n"));
1427 srb->SrbStatus = SRB_STATUS_SUCCESS;
1428 break;
1429 default:
1430 KdPrint((__DRIVER_NAME " Unhandled EXECUTE_SCSI Command = %02X\n", srb->Cdb[0]));
1431 srb->SrbStatus = SRB_STATUS_ERROR;
1432 break;
1434 if (srb->SrbStatus == SRB_STATUS_ERROR)
1436 KdPrint((__DRIVER_NAME " EXECUTE_SCSI Command = %02X returned error %02x\n", srb->Cdb[0], xvdd->last_sense_key));
1437 if (xvdd->last_sense_key == SCSI_SENSE_NO_SENSE)
1439 xvdd->last_sense_key = SCSI_SENSE_ILLEGAL_REQUEST;
1440 xvdd->last_additional_sense_code = SCSI_ADSENSE_INVALID_CDB;
1442 srb->ScsiStatus = 0x02;
1443 XenVbd_MakeAutoSense(xvdd, srb);
1444 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1445 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1447 ScsiPortNotification(NextRequest, DeviceExtension);
1450 else if (srb->SrbStatus != SRB_STATUS_PENDING)
1452 if (srb->SrbStatus == SRB_STATUS_SUCCESS && data_transfer_length < srb->DataTransferLength)
1454 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
1455 srb->DataTransferLength = data_transfer_length;
1457 xvdd->last_sense_key = SCSI_SENSE_NO_SENSE;
1458 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1459 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1460 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1462 ScsiPortNotification(NextRequest, DeviceExtension);
1465 break;
1466 case SRB_FUNCTION_IO_CONTROL:
1467 KdPrint((__DRIVER_NAME " SRB_FUNCTION_IO_CONTROL\n"));
1468 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1469 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1470 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1472 ScsiPortNotification(NextRequest, DeviceExtension);
1474 break;
1475 case SRB_FUNCTION_FLUSH:
1476 KdPrint((__DRIVER_NAME " SRB_FUNCTION_FLUSH %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1477 srb->SrbStatus = SRB_STATUS_SUCCESS;
1478 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1479 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1481 ScsiPortNotification(NextRequest, DeviceExtension);
1483 break;
1484 case SRB_FUNCTION_SHUTDOWN:
1485 KdPrint((__DRIVER_NAME " SRB_FUNCTION_SHUTDOWN %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1486 srb->SrbStatus = SRB_STATUS_SUCCESS;
1487 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1488 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1490 ScsiPortNotification(NextRequest, DeviceExtension);
1492 break;
1493 default:
1494 KdPrint((__DRIVER_NAME " Unhandled srb->Function = %08X\n", srb->Function));
1495 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1496 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1497 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1499 ScsiPortNotification(NextRequest, DeviceExtension);
1501 break;
1504 //FUNCTION_EXIT();
1505 return TRUE;
1508 static BOOLEAN
1509 XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId)
1511 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1513 UNREFERENCED_PARAMETER(DeviceExtension);
1514 UNREFERENCED_PARAMETER(PathId);
1516 FUNCTION_ENTER();
1518 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1519 if (xvdd->ring_detect_state == RING_DETECT_STATE_COMPLETE && xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1521 ScsiPortNotification(NextRequest, DeviceExtension);
1524 FUNCTION_EXIT();
1527 return TRUE;
1530 static BOOLEAN
1531 XenVbd_HwScsiAdapterState(PVOID DeviceExtension, PVOID Context, BOOLEAN SaveState)
1533 UNREFERENCED_PARAMETER(DeviceExtension);
1534 UNREFERENCED_PARAMETER(Context);
1535 UNREFERENCED_PARAMETER(SaveState);
1537 FUNCTION_ENTER();
1538 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1540 FUNCTION_EXIT();
1542 return TRUE;
1545 static SCSI_ADAPTER_CONTROL_STATUS DDKAPI
1546 XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters)
1548 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1549 SCSI_ADAPTER_CONTROL_STATUS Status = ScsiAdapterControlSuccess;
1550 PSCSI_SUPPORTED_CONTROL_TYPE_LIST SupportedControlTypeList;
1551 //KIRQL OldIrql;
1553 FUNCTION_ENTER();
1554 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1555 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
1557 switch (ControlType)
1559 case ScsiQuerySupportedControlTypes:
1560 SupportedControlTypeList = (PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters;
1561 KdPrint((__DRIVER_NAME " ScsiQuerySupportedControlTypes (Max = %d)\n", SupportedControlTypeList->MaxControlType));
1562 SupportedControlTypeList->SupportedTypeList[ScsiQuerySupportedControlTypes] = TRUE;
1563 SupportedControlTypeList->SupportedTypeList[ScsiStopAdapter] = TRUE;
1564 SupportedControlTypeList->SupportedTypeList[ScsiRestartAdapter] = TRUE;
1565 break;
1566 case ScsiStopAdapter:
1567 KdPrint((__DRIVER_NAME " ScsiStopAdapter\n"));
1568 /* I don't think we actually have to do anything here... xenpci cleans up all the xenbus stuff for us */
1569 break;
1570 case ScsiRestartAdapter:
1571 KdPrint((__DRIVER_NAME " ScsiRestartAdapter\n"));
1572 if (!xvdd->inactive)
1574 if (XenVbd_InitFromConfig(xvdd) != SP_RETURN_FOUND)
1575 KeBugCheckEx(DATA_COHERENCY_EXCEPTION, 0, (ULONG_PTR) xvdd, 0, 0);
1576 XenVbd_StartRingDetection(xvdd);
1578 break;
1579 case ScsiSetBootConfig:
1580 KdPrint((__DRIVER_NAME " ScsiSetBootConfig\n"));
1581 break;
1582 case ScsiSetRunningConfig:
1583 KdPrint((__DRIVER_NAME " ScsiSetRunningConfig\n"));
1584 break;
1585 default:
1586 KdPrint((__DRIVER_NAME " UNKNOWN\n"));
1587 break;
1590 FUNCTION_EXIT();
1592 return Status;
1595 NTSTATUS
1596 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
1598 ULONG status;
1599 HW_INITIALIZATION_DATA HwInitializationData;
1600 PVOID driver_extension;
1601 PUCHAR ptr;
1602 OBJECT_ATTRIBUTES oa;
1603 HANDLE service_handle;
1604 UNICODE_STRING param_name;
1605 HANDLE param_handle;
1606 UNICODE_STRING value_name;
1607 CHAR buf[256];
1608 ULONG buf_len;
1609 PKEY_VALUE_PARTIAL_INFORMATION kpv;
1611 FUNCTION_ENTER();
1612 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1613 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
1615 /* RegistryPath == NULL when we are invoked as a crash dump driver */
1616 if (!RegistryPath)
1618 dump_mode = TRUE;
1621 if (!dump_mode)
1623 IoAllocateDriverObjectExtension(DriverObject, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC), PAGE_SIZE, &driver_extension);
1624 ptr = driver_extension;
1625 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
1626 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "ring-ref", NULL, NULL);
1627 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_IRQ, "event-channel", NULL, NULL);
1628 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_FRONT, "device-type", NULL, NULL);
1629 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mode", NULL, NULL);
1630 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sectors", NULL, NULL);
1631 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sector-size", NULL, NULL);
1632 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_GRANT_ENTRIES, NULL, ULongToPtr(BLKIF_MAX_SEGMENTS_PER_REQUEST), NULL); /* for use in crash dump */
1633 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1635 InitializeObjectAttributes(&oa, RegistryPath, OBJ_CASE_INSENSITIVE, NULL, NULL);
1636 status = ZwOpenKey(&service_handle, KEY_READ, &oa);
1637 if(!NT_SUCCESS(status))
1639 KdPrint((__DRIVER_NAME " ZwOpenKey(Service) returned %08x\n", status));
1641 else
1643 RtlInitUnicodeString(&param_name, L"Parameters");
1644 InitializeObjectAttributes(&oa, &param_name, OBJ_CASE_INSENSITIVE, service_handle, NULL);
1645 status = ZwOpenKey(&param_handle, KEY_READ, &oa);
1646 if(!NT_SUCCESS(status))
1648 KdPrint((__DRIVER_NAME " ZwOpenKey(Parameters) returned %08x\n", status));
1650 else
1652 kpv = (PKEY_VALUE_PARTIAL_INFORMATION)buf;
1653 RtlFillMemory(scsi_device_manufacturer, 8, ' ');
1654 RtlFillMemory(scsi_disk_model, 16, ' ');
1655 RtlFillMemory(scsi_cdrom_model, 16, ' ');
1657 RtlInitUnicodeString(&value_name, L"Manufacturer");
1658 buf_len = 256;
1659 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1660 if(NT_SUCCESS(status))
1661 wcstombs(scsi_device_manufacturer, (PWCHAR)kpv->Data, min(kpv->DataLength, 8));
1662 else
1663 RtlStringCbCopyA(scsi_device_manufacturer, 8, "XEN ");
1665 RtlInitUnicodeString(&value_name, L"Disk_Model");
1666 buf_len = 256;
1667 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1668 if(NT_SUCCESS(status))
1669 wcstombs(scsi_disk_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1670 else
1671 RtlStringCbCopyA(scsi_disk_model, 16, "PV DISK ");
1673 RtlInitUnicodeString(&value_name, L"CDROM_Model");
1674 buf_len = 256;
1675 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1676 if(NT_SUCCESS(status))
1677 wcstombs(scsi_cdrom_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1678 else
1679 RtlStringCbCopyA(scsi_cdrom_model, 16, "PV CDROM ");
1681 ZwClose(service_handle);
1685 RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
1687 HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
1688 HwInitializationData.AdapterInterfaceType = PNPBus;
1689 HwInitializationData.DeviceExtensionSize = sizeof(XENVBD_DEVICE_DATA);
1690 HwInitializationData.SpecificLuExtensionSize = 0;
1691 HwInitializationData.SrbExtensionSize = sizeof(srb_list_entry_t);
1692 HwInitializationData.NumberOfAccessRanges = 1;
1693 HwInitializationData.MapBuffers = TRUE;
1694 HwInitializationData.NeedPhysicalAddresses = FALSE;
1695 HwInitializationData.TaggedQueuing = TRUE;
1696 HwInitializationData.AutoRequestSense = TRUE;
1697 HwInitializationData.MultipleRequestPerLu = TRUE;
1698 HwInitializationData.ReceiveEvent = FALSE;
1699 HwInitializationData.VendorIdLength = 0;
1700 HwInitializationData.VendorId = NULL;
1701 HwInitializationData.DeviceIdLength = 0;
1702 HwInitializationData.DeviceId = NULL;
1704 HwInitializationData.HwInitialize = XenVbd_HwScsiInitialize;
1705 HwInitializationData.HwStartIo = XenVbd_HwScsiStartIo;
1706 HwInitializationData.HwInterrupt = XenVbd_HwScsiInterrupt;
1707 HwInitializationData.HwFindAdapter = XenVbd_HwScsiFindAdapter;
1708 HwInitializationData.HwResetBus = XenVbd_HwScsiResetBus;
1709 HwInitializationData.HwDmaStarted = NULL;
1710 HwInitializationData.HwAdapterState = NULL; //XenVbd_HwScsiAdapterState;
1711 HwInitializationData.HwAdapterControl = XenVbd_HwScsiAdapterControl;
1713 status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
1715 if(!NT_SUCCESS(status))
1717 KdPrint((__DRIVER_NAME " ScsiPortInitialize failed with status 0x%08x\n", status));
1720 FUNCTION_EXIT();
1722 return status;