win-pvdrivers

view xenvbd/xenvbd_scsiport.c @ 923:5c84e3cd00b2

Fix 64 bit operation for scsiport. Fix hang on resume from hibernation
author James Harper <james.harper@bendigoit.com.au>
date Sat May 21 19:36:29 2011 +1000 (2011-05-21)
parents dae0e5c1f26c
children d16dfda0ebce
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_scsiport.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 & SHADOW_ID_ID_MASK);
79 shadow->srb = NULL;
80 shadow->reset = FALSE;
81 xvdd->shadow_free++;
82 }
84 static blkif_response_t *
85 XenVbd_GetResponse(PXENVBD_DEVICE_DATA xvdd, int i)
86 {
87 blkif_other_response_t *rep;
88 if (!xvdd->use_other)
89 return RING_GET_RESPONSE(&xvdd->ring, i);
90 rep = RING_GET_RESPONSE(&xvdd->other_ring, i);
91 xvdd->tmp_rep.id = rep->id;
92 xvdd->tmp_rep.operation = rep->operation;
93 xvdd->tmp_rep.status = rep->status;
94 return &xvdd->tmp_rep;
95 }
97 static VOID
98 XenVbd_PutRequest(PXENVBD_DEVICE_DATA xvdd, blkif_request_t *req)
99 {
100 blkif_other_request_t *other_req;
102 if (!xvdd->use_other)
103 {
104 *RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt) = *req;
105 }
106 else
107 {
108 other_req = RING_GET_REQUEST(&xvdd->other_ring, xvdd->ring.req_prod_pvt);
109 other_req->operation = req->operation;
110 other_req->nr_segments = req->nr_segments;
111 other_req->handle = req->handle;
112 other_req->id = req->id;
113 other_req->sector_number = req->sector_number;
114 memcpy(other_req->seg, req->seg, sizeof(struct blkif_request_segment) * req->nr_segments);
115 }
116 xvdd->ring.req_prod_pvt++;
117 }
119 static ULONG
120 XenVbd_InitFromConfig(PXENVBD_DEVICE_DATA xvdd)
121 {
122 ULONG i;
123 PUCHAR ptr;
124 USHORT type;
125 PCHAR setting, value, value2;
126 ULONG qemu_protocol_version = 0;
127 BOOLEAN qemu_hide_filter = FALSE;
128 ULONG qemu_hide_flags_value = 0;
130 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
131 xvdd->sring = NULL;
132 xvdd->event_channel = 0;
134 xvdd->inactive = TRUE;
135 ptr = xvdd->device_base;
136 while((type = GET_XEN_INIT_RSP(&ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
137 {
138 switch(type)
139 {
140 case XEN_INIT_TYPE_RING: /* frontend ring */
141 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %p\n", setting, value));
142 if (strcmp(setting, "ring-ref") == 0)
143 {
144 xvdd->sring = (blkif_sring_t *)value;
145 FRONT_RING_INIT(&xvdd->ring, xvdd->sring, PAGE_SIZE);
146 /* this bit is for when we have to take over an existing ring on a crash dump */
147 xvdd->ring.req_prod_pvt = xvdd->sring->req_prod;
148 xvdd->ring.rsp_cons = xvdd->ring.req_prod_pvt;
149 }
150 break;
151 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
152 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel */
153 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_EVENT_CHANNEL - %s = %d\n", setting, PtrToUlong(value) & 0x3FFFFFFF));
154 if (strcmp(setting, "event-channel") == 0)
155 {
156 /* cheat here - save the state of the ring in the topmost bits of the event-channel */
157 xvdd->event_channel_ptr = (ULONG *)(((PCHAR)ptr) - sizeof(ULONG));
158 xvdd->event_channel = PtrToUlong(value) & 0x3FFFFFFF;
159 if (PtrToUlong(value) & 0x80000000)
160 {
161 xvdd->cached_use_other_valid = TRUE;
162 xvdd->cached_use_other = (BOOLEAN)!!(PtrToUlong(value) & 0x40000000);
163 KdPrint((__DRIVER_NAME " cached_use_other = %d\n", xvdd->cached_use_other));
164 }
165 else
166 {
167 xvdd->cached_use_other_valid = FALSE;
168 }
169 }
170 break;
171 case XEN_INIT_TYPE_READ_STRING_BACK:
172 case XEN_INIT_TYPE_READ_STRING_FRONT:
173 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = %s\n", setting, value));
174 if (strcmp(setting, "sectors") == 0)
175 xvdd->total_sectors = parse_numeric_string(value);
176 else if (strcmp(setting, "sector-size") == 0)
177 xvdd->bytes_per_sector = (ULONG)parse_numeric_string(value);
178 else if (strcmp(setting, "device-type") == 0)
179 {
180 if (strcmp(value, "disk") == 0)
181 {
182 KdPrint((__DRIVER_NAME " device-type = Disk\n"));
183 xvdd->device_type = XENVBD_DEVICETYPE_DISK;
184 }
185 else if (strcmp(value, "cdrom") == 0)
186 {
187 KdPrint((__DRIVER_NAME " device-type = CDROM\n"));
188 xvdd->device_type = XENVBD_DEVICETYPE_CDROM;
189 }
190 else
191 {
192 KdPrint((__DRIVER_NAME " device-type = %s (This probably won't work!)\n", value));
193 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
194 }
195 }
196 else if (strcmp(setting, "mode") == 0)
197 {
198 if (strncmp(value, "r", 1) == 0)
199 {
200 KdPrint((__DRIVER_NAME " mode = r\n"));
201 xvdd->device_mode = XENVBD_DEVICEMODE_READ;
202 }
203 else if (strncmp(value, "w", 1) == 0)
204 {
205 KdPrint((__DRIVER_NAME " mode = w\n"));
206 xvdd->device_mode = XENVBD_DEVICEMODE_WRITE;
207 }
208 else
209 {
210 KdPrint((__DRIVER_NAME " mode = unknown\n"));
211 xvdd->device_mode = XENVBD_DEVICEMODE_UNKNOWN;
212 }
213 }
214 break;
215 case XEN_INIT_TYPE_VECTORS:
216 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_VECTORS\n"));
217 if (((PXENPCI_VECTORS)value)->length != sizeof(XENPCI_VECTORS) ||
218 ((PXENPCI_VECTORS)value)->magic != XEN_DATA_MAGIC)
219 {
220 KdPrint((__DRIVER_NAME " vectors mismatch (magic = %08x, length = %d)\n",
221 ((PXENPCI_VECTORS)value)->magic, ((PXENPCI_VECTORS)value)->length));
222 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
223 return SP_RETURN_BAD_CONFIG;
224 }
225 else
226 memcpy(&xvdd->vectors, value, sizeof(XENPCI_VECTORS));
227 break;
228 case XEN_INIT_TYPE_STATE_PTR:
229 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_DEVICE_STATE - %p\n", PtrToUlong(value)));
230 xvdd->device_state = (PXENPCI_DEVICE_STATE)value;
231 break;
232 case XEN_INIT_TYPE_QEMU_PROTOCOL_VERSION:
233 qemu_protocol_version = PtrToUlong(value);
234 break;
235 case XEN_INIT_TYPE_GRANT_ENTRIES:
236 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_GRANT_ENTRIES - entries = %d\n", PtrToUlong(value)));
237 memcpy(xvdd->dump_grant_refs, value2, PtrToUlong(value) * sizeof(grant_ref_t));
238 break;
239 case XEN_INIT_TYPE_QEMU_HIDE_FLAGS:
240 qemu_hide_flags_value = PtrToUlong(value);
241 KdPrint((__DRIVER_NAME " qemu_hide_flags_value = %d\n", qemu_hide_flags_value));
242 break;
243 case XEN_INIT_TYPE_QEMU_HIDE_FILTER:
244 qemu_hide_filter = TRUE;
245 KdPrint((__DRIVER_NAME " qemu_hide_filter = TRUE\n"));
246 break;
247 default:
248 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_%d\n", type));
249 break;
250 }
251 }
253 if (((qemu_hide_flags_value & QEMU_UNPLUG_ALL_IDE_DISKS) && xvdd->device_type != XENVBD_DEVICETYPE_CDROM) || qemu_hide_filter)
254 xvdd->inactive = FALSE;
256 if (!xvdd->inactive && (xvdd->device_type == XENVBD_DEVICETYPE_UNKNOWN
257 || xvdd->sring == NULL
258 || xvdd->event_channel == 0
259 || xvdd->total_sectors == 0
260 || xvdd->bytes_per_sector == 0))
261 {
262 KdPrint((__DRIVER_NAME " Missing settings\n"));
263 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
264 return SP_RETURN_BAD_CONFIG;
265 }
267 if (xvdd->inactive)
268 KdPrint((__DRIVER_NAME " Device is inactive\n"));
269 else
270 {
271 if (xvdd->device_type == XENVBD_DEVICETYPE_CDROM)
272 {
273 /* CD/DVD drives must have bytes_per_sector = 2048. */
274 xvdd->bytes_per_sector = 2048;
275 }
277 /* for some reason total_sectors is measured in 512 byte sectors always, so correct this to be in bytes_per_sectors */
278 xvdd->total_sectors /= xvdd->bytes_per_sector / 512;
280 xvdd->shadow_free = 0;
281 memset(xvdd->shadows, 0, sizeof(blkif_shadow_t) * SHADOW_ENTRIES);
282 for (i = 0; i < SHADOW_ENTRIES; i++)
283 {
284 xvdd->shadows[i].req.id = i;
285 /* make sure leftover real requests's are never confused with dump mode requests */
286 if (dump_mode)
287 xvdd->shadows[i].req.id |= SHADOW_ID_DUMP_FLAG;
288 put_shadow_on_freelist(xvdd, &xvdd->shadows[i]);
289 }
290 }
292 return SP_RETURN_FOUND;
293 }
295 static __inline ULONG
296 decode_cdb_length(PSCSI_REQUEST_BLOCK srb)
297 {
298 switch (srb->Cdb[0])
299 {
300 case SCSIOP_READ:
301 case SCSIOP_WRITE:
302 return ((ULONG)(UCHAR)srb->Cdb[7] << 8) | (ULONG)(UCHAR)srb->Cdb[8];
303 case SCSIOP_READ16:
304 case SCSIOP_WRITE16:
305 return ((ULONG)(UCHAR)srb->Cdb[10] << 24) | ((ULONG)(UCHAR)srb->Cdb[11] << 16) | ((ULONG)(UCHAR)srb->Cdb[12] << 8) | (ULONG)(UCHAR)srb->Cdb[13];
306 default:
307 return 0;
308 }
309 }
311 static __inline ULONGLONG
312 decode_cdb_sector(PSCSI_REQUEST_BLOCK srb)
313 {
314 ULONGLONG sector;
316 switch (srb->Cdb[0])
317 {
318 case SCSIOP_READ:
319 case SCSIOP_WRITE:
320 sector = ((ULONG)(UCHAR)srb->Cdb[2] << 24) | ((ULONG)(UCHAR)srb->Cdb[3] << 16) | ((ULONG)(UCHAR)srb->Cdb[4] << 8) | (ULONG)(UCHAR)srb->Cdb[5];
321 break;
322 case SCSIOP_READ16:
323 case SCSIOP_WRITE16:
324 sector = ((ULONGLONG)(UCHAR)srb->Cdb[2] << 56) | ((ULONGLONG)(UCHAR)srb->Cdb[3] << 48)
325 | ((ULONGLONG)(UCHAR)srb->Cdb[4] << 40) | ((ULONGLONG)(UCHAR)srb->Cdb[5] << 32)
326 | ((ULONGLONG)(UCHAR)srb->Cdb[6] << 24) | ((ULONGLONG)(UCHAR)srb->Cdb[7] << 16)
327 | ((ULONGLONG)(UCHAR)srb->Cdb[8] << 8) | ((ULONGLONG)(UCHAR)srb->Cdb[9]);
328 //KdPrint((__DRIVER_NAME " sector_number = %d (high) %d (low)\n", (ULONG)(sector >> 32), (ULONG)sector));
329 break;
330 default:
331 sector = 0;
332 break;
333 }
334 return sector;
335 }
337 static __inline BOOLEAN
338 decode_cdb_is_read(PSCSI_REQUEST_BLOCK srb)
339 {
340 switch (srb->Cdb[0])
341 {
342 case SCSIOP_READ:
343 case SCSIOP_READ16:
344 return TRUE;
345 case SCSIOP_WRITE:
346 case SCSIOP_WRITE16:
347 return FALSE;
348 default:
349 return FALSE;
350 }
351 }
353 static VOID
354 XenVbd_PutSrbOnList(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
355 {
356 srb_list_entry_t *list_entry = srb->SrbExtension;
357 list_entry->srb = srb;
358 InsertTailList(&xvdd->srb_list, (PLIST_ENTRY)list_entry);
359 }
361 static VOID
362 XenVbd_PutQueuedSrbsOnRing(PXENVBD_DEVICE_DATA xvdd)
363 {
364 PSCSI_REQUEST_BLOCK srb;
365 srb_list_entry_t *srb_entry;
366 ULONGLONG sector_number;
367 ULONG block_count;
368 blkif_shadow_t *shadow;
369 ULONG remaining, offset, length;
370 grant_ref_t gref;
371 PUCHAR ptr;
372 int notify;
373 int i;
374 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
375 LARGE_INTEGER current_time;
376 #endif
378 //FUNCTION_ENTER();
380 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
381 ScsiPortQuerySystemTime(&current_time);
382 #endif
384 while ((!dump_mode || xvdd->shadow_free == SHADOW_ENTRIES)
385 && xvdd->ring_detect_state == RING_DETECT_STATE_COMPLETE
386 && !xvdd->aligned_buffer_in_use
387 && xvdd->shadow_free
388 && (srb_entry = (srb_list_entry_t *)RemoveHeadList(&xvdd->srb_list)) != (srb_list_entry_t *)&xvdd->srb_list) /* RemoveHeadList must be last in the expression */
389 {
390 srb = srb_entry->srb;
391 block_count = decode_cdb_length(srb);;
392 block_count *= xvdd->bytes_per_sector / 512;
393 sector_number = decode_cdb_sector(srb);
394 sector_number *= xvdd->bytes_per_sector / 512;
396 /* look for pending writes that overlap this one */
397 /* we get warnings from drbd if we don't */
398 for (i = 0; i < MAX_SHADOW_ENTRIES; i++)
399 {
400 PSCSI_REQUEST_BLOCK srb2;
401 ULONGLONG sector_number2;
402 ULONG block_count2;
404 srb2 = xvdd->shadows[i].srb;
405 if (!srb2)
406 continue;
407 if (decode_cdb_is_read(srb2))
408 continue;
409 block_count2 = decode_cdb_length(srb2);;
410 block_count2 *= xvdd->bytes_per_sector / 512;
411 sector_number2 = decode_cdb_sector(srb2);
412 sector_number2 *= xvdd->bytes_per_sector / 512;
414 if (sector_number < sector_number2 && sector_number + block_count <= sector_number2)
415 continue;
416 if (sector_number2 < sector_number && sector_number2 + block_count2 <= sector_number)
417 continue;
419 #if 0
420 /* check if the data being written is identical to the data in the pipe */
421 {
422 PUCHAR buf, buf2;
423 ULONG byte_count;
424 int j;
426 buf = (PUCHAR)srb->DataBuffer + (max(sector_number, sector_number2) - sector_number) * xvdd->bytes_per_sector;
427 buf2 = (PUCHAR)srb2->DataBuffer + (max(sector_number, sector_number2) - sector_number2) * xvdd->bytes_per_sector;
428 byte_count = (ULONG)(min(sector_number + block_count, sector_number2 + block_count2) - max(sector_number, sector_number2)) * xvdd->bytes_per_sector;
429 for (j = 0; j < (int)byte_count; j++)
430 {
431 if (buf[j] != buf2[j])
432 break;
433 }
434 }
435 #endif
437 KdPrint((__DRIVER_NAME " Concurrent outstanding write detected (%I64d, %d) (%I64d, %d)\n",
438 sector_number, block_count, sector_number2, block_count2));
439 /* put the srb back at the start of the queue */
440 InsertHeadList(&xvdd->srb_list, (PLIST_ENTRY)srb->SrbExtension);
441 break;
442 }
443 if (i < MAX_SHADOW_ENTRIES)
444 break; /* stall the queue but fall through so the notify is triggered */
446 remaining = block_count * 512;
447 shadow = get_shadow_from_freelist(xvdd);
448 ASSERT(shadow);
449 ASSERT(!shadow->aligned_buffer_in_use);
450 ASSERT(!shadow->srb);
451 shadow->reset = FALSE;
452 shadow->req.sector_number = sector_number;
453 shadow->req.handle = 0;
454 shadow->req.operation = decode_cdb_is_read(srb)?BLKIF_OP_READ:BLKIF_OP_WRITE;
455 shadow->req.nr_segments = 0;
456 shadow->srb = srb;
458 if ((ULONG_PTR)srb->DataBuffer & 511)
459 {
460 if (dump_mode)
461 KdPrint((__DRIVER_NAME " unaligned dump mode buffer = %d bytes\n", block_count * 512));
462 ASSERT(!dump_mode || block_count * 512 < BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE * PAGE_SIZE);
463 xvdd->aligned_buffer_in_use = TRUE;
464 ptr = xvdd->aligned_buffer;
465 if (!decode_cdb_is_read(srb))
466 memcpy(ptr, srb->DataBuffer, block_count * 512);
467 shadow->aligned_buffer_in_use = TRUE;
468 }
469 else
470 {
471 ptr = srb->DataBuffer;
472 shadow->aligned_buffer_in_use = FALSE;
473 }
475 //KdPrint((__DRIVER_NAME " sector_number = %d, block_count = %d\n", (ULONG)shadow->req.sector_number, block_count));
476 //KdPrint((__DRIVER_NAME " SrbExtension = %p\n", srb->SrbExtension));
477 //KdPrint((__DRIVER_NAME " DataBuffer = %p\n", srb->DataBuffer));
479 //KdPrint((__DRIVER_NAME " sector_number = %d\n", (ULONG)shadow->req.sector_number));
480 //KdPrint((__DRIVER_NAME " handle = %d\n", shadow->req.handle));
481 //KdPrint((__DRIVER_NAME " operation = %d\n", shadow->req.operation));
483 while (remaining > 0)
484 {
485 PHYSICAL_ADDRESS physical_address = MmGetPhysicalAddress(ptr);
487 if (dump_mode)
488 {
489 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
490 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE,
491 xvdd->dump_grant_refs[shadow->req.nr_segments], (ULONG)'SCSI');
492 }
493 else
494 {
495 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
496 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE, INVALID_GRANT_REF, (ULONG)'SCSI');
497 }
498 if (gref == INVALID_GRANT_REF)
499 {
500 ULONG i;
501 for (i = 0; i < shadow->req.nr_segments; i++)
502 {
503 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
504 shadow->req.seg[i].gref, FALSE, (ULONG)'SCSI');
505 }
506 if (shadow->aligned_buffer_in_use)
507 {
508 shadow->aligned_buffer_in_use = FALSE;
509 xvdd->aligned_buffer_in_use = FALSE;
510 }
511 /* put the srb back at the start of the queue, then fall through so that the notify is triggered*/
512 InsertHeadList(&xvdd->srb_list, (PLIST_ENTRY)srb->SrbExtension);
513 put_shadow_on_freelist(xvdd, shadow);
514 KdPrint((__DRIVER_NAME " Out of gref's. Deferring\n"));
515 break;
516 }
517 offset = physical_address.LowPart & (PAGE_SIZE - 1);
518 length = min(PAGE_SIZE - offset, remaining);
519 ASSERT((offset & 511) == 0);
520 ASSERT((length & 511) == 0);
521 ASSERT(offset + length <= PAGE_SIZE);
522 shadow->req.seg[shadow->req.nr_segments].gref = gref;
523 shadow->req.seg[shadow->req.nr_segments].first_sect = (UCHAR)(offset >> 9);
524 shadow->req.seg[shadow->req.nr_segments].last_sect = (UCHAR)(((offset + length) >> 9) - 1);
525 remaining -= length;
526 ptr += length;
527 shadow->req.nr_segments++;
528 }
530 //KdPrint((__DRIVER_NAME " nr_segments = %d\n", shadow->req.nr_segments));
532 XenVbd_PutRequest(xvdd, &shadow->req);
533 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
534 shadow->ring_submit_time = current_time;
535 #endif
537 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
538 if (notify)
539 {
540 //KdPrint((__DRIVER_NAME " Notifying\n"));
541 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
542 }
543 }
544 if (xvdd->shadow_free && !xvdd->aligned_buffer_in_use)
545 {
546 ScsiPortNotification(NextLuRequest, xvdd, 0, 0, 0);
547 }
548 //FUNCTION_EXIT();
549 }
551 static ULONG
552 XenVbd_HwScsiFindAdapter(PVOID DeviceExtension, PVOID HwContext, PVOID BusInformation, PCHAR ArgumentString, PPORT_CONFIGURATION_INFORMATION ConfigInfo, PBOOLEAN Again)
553 {
554 // PACCESS_RANGE AccessRange;
555 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
556 ULONG status;
557 // PXENPCI_XEN_DEVICE_DATA XenDeviceData;
558 PACCESS_RANGE access_range;
560 UNREFERENCED_PARAMETER(HwContext);
561 UNREFERENCED_PARAMETER(BusInformation);
562 UNREFERENCED_PARAMETER(ArgumentString);
564 FUNCTION_ENTER();
565 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
566 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
568 RtlZeroMemory(xvdd, sizeof(XENVBD_DEVICE_DATA));
569 *Again = FALSE;
571 KdPrint((__DRIVER_NAME " BusInterruptLevel = %d\n", ConfigInfo->BusInterruptLevel));
572 KdPrint((__DRIVER_NAME " BusInterruptVector = %03x\n", ConfigInfo->BusInterruptVector));
574 KdPrint((__DRIVER_NAME " NumberOfAccessRanges = %d\n", ConfigInfo->NumberOfAccessRanges));
575 if (ConfigInfo->NumberOfAccessRanges != 1 && ConfigInfo->NumberOfAccessRanges != 2)
576 {
577 return SP_RETURN_BAD_CONFIG;
578 }
580 access_range = &((*(ConfigInfo->AccessRanges))[0]);
581 KdPrint((__DRIVER_NAME " RangeStart = %08x, RangeLength = %08x\n",
582 access_range->RangeStart.LowPart, access_range->RangeLength));
583 xvdd->device_base = ScsiPortGetDeviceBase(
584 DeviceExtension,
585 ConfigInfo->AdapterInterfaceType,
586 ConfigInfo->SystemIoBusNumber,
587 access_range->RangeStart,
588 access_range->RangeLength,
589 !access_range->RangeInMemory);
590 if (!xvdd->device_base)
591 {
592 KdPrint((__DRIVER_NAME " Invalid config\n"));
593 FUNCTION_EXIT();
594 return SP_RETURN_BAD_CONFIG;
595 }
597 status = XenVbd_InitFromConfig(xvdd);
598 if (status != SP_RETURN_FOUND)
599 {
600 FUNCTION_EXIT();
601 return status;
602 }
604 xvdd->aligned_buffer_in_use = FALSE;
605 /* align the buffer to PAGE_SIZE */
606 xvdd->aligned_buffer = (PVOID)((ULONG_PTR)((PUCHAR)xvdd->aligned_buffer_data + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
607 KdPrint((__DRIVER_NAME " aligned_buffer_data = %p\n", xvdd->aligned_buffer_data));
608 KdPrint((__DRIVER_NAME " aligned_buffer = %p\n", xvdd->aligned_buffer));
610 if (!dump_mode)
611 {
612 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
613 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST - 1;
614 //ConfigInfo->ScatterGather = TRUE;
615 }
616 else
617 {
618 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE * PAGE_SIZE;
619 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE - 1;
620 //ConfigInfo->ScatterGather = FALSE;
621 }
622 KdPrint((__DRIVER_NAME " ConfigInfo->MaximumTransferLength = %d\n", ConfigInfo->MaximumTransferLength));
623 KdPrint((__DRIVER_NAME " ConfigInfo->NumberOfPhysicalBreaks = %d\n", ConfigInfo->NumberOfPhysicalBreaks));
624 ConfigInfo->ScatterGather = FALSE;
625 ConfigInfo->AlignmentMask = 0;
626 ConfigInfo->NumberOfBuses = 1;
627 ConfigInfo->InitiatorBusId[0] = 1;
628 ConfigInfo->MaximumNumberOfLogicalUnits = 1;
629 ConfigInfo->MaximumNumberOfTargets = 2;
630 KdPrint((__DRIVER_NAME " ConfigInfo->CachesData was initialised to %d\n", ConfigInfo->CachesData));
631 ConfigInfo->CachesData = FALSE;
632 ConfigInfo->BufferAccessScsiPortControlled = FALSE;
634 if (ConfigInfo->Dma64BitAddresses == SCSI_DMA64_SYSTEM_SUPPORTED)
635 {
636 ConfigInfo->Master = TRUE;
637 ConfigInfo->Dma64BitAddresses = SCSI_DMA64_MINIPORT_SUPPORTED;
638 ConfigInfo->Dma32BitAddresses = FALSE;
639 KdPrint((__DRIVER_NAME " Dma64BitAddresses supported\n"));
640 }
641 else
642 {
643 ConfigInfo->Master = FALSE;
644 ConfigInfo->Dma32BitAddresses = TRUE;
645 KdPrint((__DRIVER_NAME " Dma64BitAddresses not supported\n"));
646 }
648 FUNCTION_EXIT();
650 return SP_RETURN_FOUND;
651 }
653 static VOID
654 XenVbd_StartRingDetection(PXENVBD_DEVICE_DATA xvdd)
655 {
656 blkif_request_t *req;
657 int notify;
659 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT1;
660 RtlZeroMemory(xvdd->sring->ring, PAGE_SIZE - FIELD_OFFSET(blkif_sring_t, ring));
661 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
662 req->operation = 0xff;
663 xvdd->ring.req_prod_pvt++;
664 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
665 req->operation = 0xff;
666 xvdd->ring.req_prod_pvt++;
668 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
669 if (notify)
670 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
671 }
673 static BOOLEAN
674 XenVbd_HwScsiInitialize(PVOID DeviceExtension)
675 {
676 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
678 FUNCTION_ENTER();
679 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
680 KdPrint((__DRIVER_NAME " dump_mode = %d\n", dump_mode));
682 if (!xvdd->inactive)
683 {
684 xvdd->ring_detect_state = RING_DETECT_STATE_NOT_STARTED;
685 if (xvdd->cached_use_other_valid)
686 {
687 if (xvdd->cached_use_other)
688 {
689 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
690 xvdd->use_other = TRUE;
691 }
692 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
693 }
694 InitializeListHead(&xvdd->srb_list);
695 }
696 FUNCTION_EXIT();
698 return TRUE;
699 }
701 static ULONG
702 XenVbd_FillModePage(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
703 {
704 PMODE_PARAMETER_HEADER parameter_header = NULL;
705 PMODE_PARAMETER_HEADER10 parameter_header10 = NULL;
706 PMODE_PARAMETER_BLOCK param_block;
707 PMODE_FORMAT_PAGE format_page;
708 ULONG offset = 0;
709 UCHAR buffer[1024];
710 BOOLEAN valid_page = FALSE;
711 BOOLEAN cdb_llbaa;
712 BOOLEAN cdb_dbd;
713 UCHAR cdb_page_code;
714 USHORT cdb_allocation_length;
716 UNREFERENCED_PARAMETER(xvdd);
718 RtlZeroMemory(srb->DataBuffer, srb->DataTransferLength);
719 RtlZeroMemory(buffer, ARRAY_SIZE(buffer));
720 offset = 0;
722 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
724 switch (srb->Cdb[0])
725 {
726 case SCSIOP_MODE_SENSE:
727 cdb_llbaa = FALSE;
728 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
729 cdb_page_code = srb->Cdb[2] & 0x3f;
730 cdb_allocation_length = srb->Cdb[4];
731 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
732 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
733 parameter_header = (PMODE_PARAMETER_HEADER)&buffer[offset];
734 parameter_header->MediumType = 0;
735 parameter_header->DeviceSpecificParameter = 0;
736 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
737 {
738 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
739 parameter_header->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
740 }
741 offset += sizeof(MODE_PARAMETER_HEADER);
742 break;
743 case SCSIOP_MODE_SENSE10:
744 cdb_llbaa = (BOOLEAN)!!(srb->Cdb[1] & 16);
745 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
746 cdb_page_code = srb->Cdb[2] & 0x3f;
747 cdb_allocation_length = (srb->Cdb[7] << 8) | srb->Cdb[8];
748 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE10 llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
749 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
750 parameter_header10 = (PMODE_PARAMETER_HEADER10)&buffer[offset];
751 parameter_header10->MediumType = 0;
752 parameter_header10->DeviceSpecificParameter = 0;
753 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
754 {
755 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
756 parameter_header10->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
757 }
758 offset += sizeof(MODE_PARAMETER_HEADER10);
759 break;
760 default:
761 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE_WTF (%02x)\n", (ULONG)srb->Cdb[0]));
762 return FALSE;
763 }
765 if (!cdb_dbd)
766 {
767 param_block = (PMODE_PARAMETER_BLOCK)&buffer[offset];
768 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK)
769 {
770 if (xvdd->total_sectors >> 32)
771 {
772 param_block->DensityCode = 0xff;
773 param_block->NumberOfBlocks[0] = 0xff;
774 param_block->NumberOfBlocks[1] = 0xff;
775 param_block->NumberOfBlocks[2] = 0xff;
776 }
777 else
778 {
779 param_block->DensityCode = (UCHAR)((xvdd->total_sectors >> 24) & 0xff);
780 param_block->NumberOfBlocks[0] = (UCHAR)((xvdd->total_sectors >> 16) & 0xff);
781 param_block->NumberOfBlocks[1] = (UCHAR)((xvdd->total_sectors >> 8) & 0xff);
782 param_block->NumberOfBlocks[2] = (UCHAR)((xvdd->total_sectors >> 0) & 0xff);
783 }
784 param_block->BlockLength[0] = (UCHAR)((xvdd->bytes_per_sector >> 16) & 0xff);
785 param_block->BlockLength[1] = (UCHAR)((xvdd->bytes_per_sector >> 8) & 0xff);
786 param_block->BlockLength[2] = (UCHAR)((xvdd->bytes_per_sector >> 0) & 0xff);
787 }
788 offset += sizeof(MODE_PARAMETER_BLOCK);
789 }
790 switch (srb->Cdb[0])
791 {
792 case SCSIOP_MODE_SENSE:
793 parameter_header->BlockDescriptorLength = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER));
794 break;
795 case SCSIOP_MODE_SENSE10:
796 parameter_header10->BlockDescriptorLength[0] = (UCHAR)((offset - sizeof(MODE_PARAMETER_HEADER10)) >> 8);
797 parameter_header10->BlockDescriptorLength[1] = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER10));
798 break;
799 }
800 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_FORMAT_DEVICE || cdb_page_code == MODE_SENSE_RETURN_ALL))
801 {
802 valid_page = TRUE;
803 format_page = (PMODE_FORMAT_PAGE)&buffer[offset];
804 format_page->PageCode = MODE_PAGE_FORMAT_DEVICE;
805 format_page->PageLength = sizeof(MODE_FORMAT_PAGE) - FIELD_OFFSET(MODE_FORMAT_PAGE, PageLength);
806 /* 256 sectors per track */
807 format_page->SectorsPerTrack[0] = 0x01;
808 format_page->SectorsPerTrack[1] = 0x00;
809 /* xxx bytes per sector */
810 format_page->BytesPerPhysicalSector[0] = (UCHAR)(xvdd->bytes_per_sector >> 8);
811 format_page->BytesPerPhysicalSector[1] = (UCHAR)(xvdd->bytes_per_sector & 0xff);
812 format_page->HardSectorFormating = TRUE;
813 format_page->SoftSectorFormating = TRUE;
814 offset += sizeof(MODE_FORMAT_PAGE);
815 }
816 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_CACHING || cdb_page_code == MODE_SENSE_RETURN_ALL))
817 {
818 PMODE_CACHING_PAGE caching_page;
819 valid_page = TRUE;
820 caching_page = (PMODE_CACHING_PAGE)&buffer[offset];
821 caching_page->PageCode = MODE_PAGE_CACHING;
822 caching_page->PageLength = sizeof(MODE_CACHING_PAGE) - FIELD_OFFSET(MODE_CACHING_PAGE, PageLength);
823 // caching_page-> // all zeros is just fine... maybe
824 offset += sizeof(MODE_CACHING_PAGE);
825 }
826 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_MEDIUM_TYPES || cdb_page_code == MODE_SENSE_RETURN_ALL))
827 {
828 PUCHAR medium_types_page;
829 valid_page = TRUE;
830 medium_types_page = &buffer[offset];
831 medium_types_page[0] = MODE_PAGE_MEDIUM_TYPES;
832 medium_types_page[1] = 0x06;
833 medium_types_page[2] = 0;
834 medium_types_page[3] = 0;
835 medium_types_page[4] = 0;
836 medium_types_page[5] = 0;
837 medium_types_page[6] = 0;
838 medium_types_page[7] = 0;
839 offset += 8;
840 }
841 switch (srb->Cdb[0])
842 {
843 case SCSIOP_MODE_SENSE:
844 parameter_header->ModeDataLength = (UCHAR)(offset - 1);
845 break;
846 case SCSIOP_MODE_SENSE10:
847 parameter_header10->ModeDataLength[0] = (UCHAR)((offset - 2) >> 8);
848 parameter_header10->ModeDataLength[1] = (UCHAR)(offset - 2);
849 break;
850 }
852 if (!valid_page && cdb_page_code != MODE_SENSE_RETURN_ALL)
853 {
854 srb->SrbStatus = SRB_STATUS_ERROR;
855 }
856 else if(offset < srb->DataTransferLength)
857 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
858 else
859 srb->SrbStatus = SRB_STATUS_SUCCESS;
860 srb->DataTransferLength = min(srb->DataTransferLength, offset);
861 srb->ScsiStatus = 0;
862 memcpy(srb->DataBuffer, buffer, srb->DataTransferLength);
864 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
866 return TRUE;
867 }
869 static VOID
870 XenVbd_MakeSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb, UCHAR sense_key, UCHAR additional_sense_code)
871 {
872 PSENSE_DATA sd = srb->SenseInfoBuffer;
874 UNREFERENCED_PARAMETER(xvdd);
876 if (!srb->SenseInfoBuffer)
877 return;
879 sd->ErrorCode = 0x70;
880 sd->Valid = 1;
881 sd->SenseKey = sense_key;
882 sd->AdditionalSenseLength = sizeof(SENSE_DATA) - FIELD_OFFSET(SENSE_DATA, AdditionalSenseLength);
883 sd->AdditionalSenseCode = additional_sense_code;
884 return;
885 }
887 static VOID
888 XenVbd_MakeAutoSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
889 {
890 if (srb->SrbStatus == SRB_STATUS_SUCCESS || srb->SrbFlags & SRB_FLAGS_DISABLE_AUTOSENSE)
891 return;
892 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
893 srb->SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
894 }
896 static BOOLEAN
897 XenVbd_HwScsiInterrupt(PVOID DeviceExtension)
898 {
899 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
900 PSCSI_REQUEST_BLOCK srb;
901 RING_IDX i, rp;
902 ULONG j;
903 blkif_response_t *rep;
904 int block_count;
905 int more_to_do = TRUE;
906 blkif_shadow_t *shadow;
907 ULONG suspend_resume_state_pdo;
908 BOOLEAN last_interrupt = FALSE;
909 ULONG start_ring_detect_state = xvdd->ring_detect_state;
910 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
911 srb_list_entry_t *srb_entry;
912 ULONG elapsed;
913 LARGE_INTEGER current_time;
914 #endif
916 /* in dump mode I think we get called on a timer, not by an actual IRQ */
917 if (!dump_mode && !xvdd->vectors.EvtChn_AckEvent(xvdd->vectors.context, xvdd->event_channel, &last_interrupt))
918 return FALSE; /* interrupt was not for us */
920 suspend_resume_state_pdo = xvdd->device_state->suspend_resume_state_pdo;
921 KeMemoryBarrier();
923 if (suspend_resume_state_pdo != xvdd->device_state->suspend_resume_state_fdo)
924 {
925 FUNCTION_ENTER();
926 switch (suspend_resume_state_pdo)
927 {
928 case SR_STATE_SUSPENDING:
929 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_SUSPENDING\n"));
930 break;
931 case SR_STATE_RESUMING:
932 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_RESUMING\n"));
933 XenVbd_InitFromConfig(xvdd);
934 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
935 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
936 break;
937 case SR_STATE_RUNNING:
938 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
939 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
940 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
941 ScsiPortNotification(NextRequest, DeviceExtension);
942 default:
943 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
944 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
945 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
946 break;
947 }
948 KeMemoryBarrier();
949 }
951 if (xvdd->device_state->suspend_resume_state_fdo != SR_STATE_RUNNING)
952 {
953 return last_interrupt;
954 }
956 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
957 ScsiPortQuerySystemTime(&current_time);
958 #endif
960 while (more_to_do)
961 {
962 rp = xvdd->ring.sring->rsp_prod;
963 KeMemoryBarrier();
964 for (i = xvdd->ring.rsp_cons; i < rp; i++)
965 {
966 rep = XenVbd_GetResponse(xvdd, i);
967 /*
968 * This code is to automatically detect if the backend is using the same
969 * bit width or a different bit width to us. Later versions of Xen do this
970 * via a xenstore value, but not all. That 0x0fffffff (notice
971 * that the msb is not actually set, so we don't have any problems with
972 * sign extending) is to signify the last entry on the right, which is
973 * different under 32 and 64 bits, and that is why we set it up there.
975 * To do the detection, we put two initial entries on the ring, with an op
976 * of 0xff (which is invalid). The first entry is mostly okay, but the
977 * second will be grossly misaligned if the backend bit width is different,
978 * and we detect this and switch frontend structures.
979 */
980 switch (xvdd->ring_detect_state)
981 {
982 case RING_DETECT_STATE_NOT_STARTED:
983 KdPrint((__DRIVER_NAME " premature IRQ\n"));
984 break;
985 case RING_DETECT_STATE_DETECT1:
986 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));
987 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));
988 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT2;
989 break;
990 case RING_DETECT_STATE_DETECT2:
991 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));
992 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));
993 *xvdd->event_channel_ptr |= 0x80000000;
994 if (rep->operation != 0xff)
995 {
996 KdPrint((__DRIVER_NAME " switching to 'other' ring size\n"));
997 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
998 xvdd->use_other = TRUE;
999 *xvdd->event_channel_ptr |= 0x40000000;
1001 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
1002 ScsiPortNotification(NextRequest, DeviceExtension);
1003 break;
1004 case RING_DETECT_STATE_COMPLETE:
1005 shadow = &xvdd->shadows[rep->id & SHADOW_ID_ID_MASK];
1006 if (shadow->reset)
1008 KdPrint((__DRIVER_NAME " discarding reset shadow\n"));
1009 for (j = 0; j < shadow->req.nr_segments; j++)
1011 if (dump_mode)
1013 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1014 shadow->req.seg[j].gref, TRUE, (ULONG)'SCSI');
1016 else
1018 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1019 shadow->req.seg[j].gref, FALSE, (ULONG)'SCSI');
1023 else if (dump_mode && !(rep->id & SHADOW_ID_DUMP_FLAG))
1025 KdPrint((__DRIVER_NAME " discarding stale (non-dump-mode) shadow\n"));
1027 else
1029 srb = shadow->srb;
1030 ASSERT(srb);
1031 block_count = decode_cdb_length(srb);
1032 block_count *= xvdd->bytes_per_sector / 512;
1033 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
1034 srb_entry = srb->SrbExtension;
1035 elapsed = (ULONG)((current_time.QuadPart - shadow->ring_submit_time.QuadPart) / 10000L);
1036 if (elapsed > 5000)
1037 KdPrint((__DRIVER_NAME " WARNING: SRB completion time %dms\n", elapsed));
1038 #endif
1039 if (rep->status == BLKIF_RSP_OKAY || (dump_mode && dump_mode_errors++ < DUMP_MODE_ERROR_LIMIT))
1040 /* 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 */
1041 srb->SrbStatus = SRB_STATUS_SUCCESS;
1042 else
1044 KdPrint((__DRIVER_NAME " Xen Operation returned error\n"));
1045 if (decode_cdb_is_read(srb))
1046 KdPrint((__DRIVER_NAME " Operation = Read\n"));
1047 else
1048 KdPrint((__DRIVER_NAME " Operation = Write\n"));
1049 if (dump_mode)
1051 KdPrint((__DRIVER_NAME " Sector = %08X, Count = %d\n", (ULONG)shadow->req.sector_number, block_count));
1052 KdPrint((__DRIVER_NAME " DataBuffer = %p, aligned_buffer = %p\n", srb->DataBuffer, xvdd->aligned_buffer));
1053 KdPrint((__DRIVER_NAME " Physical = %08x%08x\n", MmGetPhysicalAddress(srb->DataBuffer).HighPart, MmGetPhysicalAddress(srb->DataBuffer).LowPart));
1054 KdPrint((__DRIVER_NAME " PFN = %08x\n", (ULONG)(MmGetPhysicalAddress(srb->DataBuffer).QuadPart >> PAGE_SHIFT)));
1056 for (j = 0; j < shadow->req.nr_segments; j++)
1058 KdPrint((__DRIVER_NAME " gref = %d\n", shadow->req.seg[j].gref));
1059 KdPrint((__DRIVER_NAME " first_sect = %d\n", shadow->req.seg[j].first_sect));
1060 KdPrint((__DRIVER_NAME " last_sect = %d\n", shadow->req.seg[j].last_sect));
1063 srb->SrbStatus = SRB_STATUS_ERROR;
1064 srb->ScsiStatus = 0x02;
1065 xvdd->last_sense_key = SCSI_SENSE_MEDIUM_ERROR;
1066 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1067 XenVbd_MakeAutoSense(xvdd, srb);
1069 if (shadow->aligned_buffer_in_use)
1071 ASSERT(xvdd->aligned_buffer_in_use);
1072 xvdd->aligned_buffer_in_use = FALSE;
1073 if (srb->SrbStatus == SRB_STATUS_SUCCESS && decode_cdb_is_read(srb))
1074 memcpy(srb->DataBuffer, xvdd->aligned_buffer, block_count * 512);
1076 for (j = 0; j < shadow->req.nr_segments; j++)
1078 if (dump_mode)
1080 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1081 shadow->req.seg[j].gref, TRUE, (ULONG)'SCSI');
1083 else
1085 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1086 shadow->req.seg[j].gref, FALSE, (ULONG)'SCSI');
1089 ScsiPortNotification(RequestComplete, xvdd, srb);
1091 shadow->aligned_buffer_in_use = FALSE;
1092 shadow->reset = FALSE;
1093 shadow->srb = NULL;
1094 put_shadow_on_freelist(xvdd, shadow);
1095 break;
1099 xvdd->ring.rsp_cons = i;
1100 if (i != xvdd->ring.req_prod_pvt)
1102 RING_FINAL_CHECK_FOR_RESPONSES(&xvdd->ring, more_to_do);
1104 else
1106 xvdd->ring.sring->rsp_event = i + 1;
1107 more_to_do = FALSE;
1111 if (start_ring_detect_state > RING_DETECT_STATE_NOT_STARTED)
1112 XenVbd_PutQueuedSrbsOnRing(xvdd);
1114 if (suspend_resume_state_pdo == SR_STATE_SUSPENDING)
1116 if (xvdd->inactive || xvdd->shadow_free == SHADOW_ENTRIES)
1118 /* all entries are purged from the list (or we are inactive). ready to suspend */
1119 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
1120 KeMemoryBarrier();
1121 KdPrint((__DRIVER_NAME " Set fdo state SR_STATE_SUSPENDING\n"));
1122 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xvdd->device_state->pdo_event_channel));
1123 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
1125 FUNCTION_EXIT();
1128 return last_interrupt;
1131 static BOOLEAN
1132 XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK srb)
1134 PUCHAR data_buffer;
1135 //ULONG data_buffer_length;
1136 PCDB cdb;
1137 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1138 ULONG data_transfer_length = srb->DataTransferLength;
1140 if (xvdd->inactive)
1142 KdPrint((__DRIVER_NAME " Inactive srb->Function = %08X\n", srb->Function));
1143 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1144 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1145 ScsiPortNotification(NextRequest, DeviceExtension);
1146 return TRUE;
1149 // If we haven't enumerated all the devices yet then just defer the request
1150 if (xvdd->ring_detect_state < RING_DETECT_STATE_COMPLETE)
1152 if (xvdd->ring_detect_state == RING_DETECT_STATE_NOT_STARTED)
1153 XenVbd_StartRingDetection(xvdd);
1156 if (xvdd->device_state->suspend_resume_state_pdo != SR_STATE_RUNNING)
1158 KdPrint((__DRIVER_NAME " --> HwScsiStartIo (Suspending/Resuming)\n"));
1159 srb->SrbStatus = SRB_STATUS_BUSY;
1160 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1161 KdPrint((__DRIVER_NAME " <-- HwScsiStartIo (Suspending/Resuming)\n"));
1162 return TRUE;
1165 if (srb->PathId != 0 || srb->TargetId != 0 || srb->Lun != 0)
1167 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1168 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1169 ScsiPortNotification(NextRequest, DeviceExtension);
1170 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Out of bounds)\n"));
1171 return TRUE;
1174 switch (srb->Function)
1176 case SRB_FUNCTION_EXECUTE_SCSI:
1177 cdb = (PCDB)srb->Cdb;
1179 switch(cdb->CDB6GENERIC.OperationCode)
1181 case SCSIOP_TEST_UNIT_READY:
1182 if (dump_mode)
1183 KdPrint((__DRIVER_NAME " Command = TEST_UNIT_READY\n"));
1184 srb->SrbStatus = SRB_STATUS_SUCCESS;
1185 srb->ScsiStatus = 0;
1186 break;
1187 case SCSIOP_INQUIRY:
1188 if (dump_mode)
1190 //PHYSICAL_ADDRESS physical;
1191 KdPrint((__DRIVER_NAME " Command = INQUIRY\n"));
1192 //KdPrint((__DRIVER_NAME " srb->Databuffer = %p\n", srb->DataBuffer));
1193 //physical = ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length);
1194 //KdPrint((__DRIVER_NAME " ScsiPortGetPhysicalAddress = %08x:%08x\n", physical.LowPart, physical.HighPart));
1196 // KdPrint((__DRIVER_NAME " (LUN = %d, EVPD = %d, Page Code = %02X)\n", srb->Cdb[1] >> 5, srb->Cdb[1] & 1, srb->Cdb[2]));
1197 // KdPrint((__DRIVER_NAME " (Length = %d)\n", srb->DataTransferLength));
1199 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1200 data_buffer = srb->DataBuffer;
1201 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1202 srb->SrbStatus = SRB_STATUS_SUCCESS;
1203 switch (xvdd->device_type)
1205 case XENVBD_DEVICETYPE_DISK:
1206 if ((srb->Cdb[1] & 1) == 0)
1208 if (srb->Cdb[2])
1210 srb->SrbStatus = SRB_STATUS_ERROR;
1212 else
1214 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1215 id->DeviceType = DIRECT_ACCESS_DEVICE;
1216 id->Versions = 4; /* minimum that WHQL says we must support */
1217 id->ResponseDataFormat = 2; /* not sure about this but WHQL complains otherwise */
1218 id->HiSupport = 1; /* WHQL test says we should set this */
1219 //id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1220 id->AdditionalLength = sizeof(INQUIRYDATA) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength) - 1;
1221 id->CommandQueue = 1;
1222 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1223 memcpy(id->ProductId, scsi_disk_model, 16); // product id
1224 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1225 data_transfer_length = sizeof(INQUIRYDATA);
1228 else
1230 switch (srb->Cdb[2])
1232 case VPD_SUPPORTED_PAGES: /* list of pages we support */
1233 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1234 data_buffer[1] = VPD_SUPPORTED_PAGES;
1235 data_buffer[2] = 0x00;
1236 data_buffer[3] = 2;
1237 data_buffer[4] = 0x00;
1238 data_buffer[5] = 0x80;
1239 data_transfer_length = 6;
1240 break;
1241 case VPD_SERIAL_NUMBER: /* serial number */
1242 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1243 data_buffer[1] = VPD_SERIAL_NUMBER;
1244 data_buffer[2] = 0x00;
1245 data_buffer[3] = 8;
1246 memset(&data_buffer[4], ' ', 8);
1247 data_transfer_length = 12;
1248 break;
1249 case VPD_DEVICE_IDENTIFIERS: /* identification - we don't support any so just return zero */
1250 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1251 data_buffer[1] = VPD_DEVICE_IDENTIFIERS;
1252 data_buffer[2] = 0x00;
1253 data_buffer[3] = 4 + (UCHAR)strlen(xvdd->vectors.path); /* length */
1254 data_buffer[4] = 2; /* ASCII */
1255 data_buffer[5] = 1; /* VendorId */
1256 data_buffer[6] = 0;
1257 data_buffer[7] = (UCHAR)strlen(xvdd->vectors.path);
1258 memcpy(&data_buffer[8], xvdd->vectors.path, strlen(xvdd->vectors.path));
1259 data_transfer_length = (ULONG)(8 + strlen(xvdd->vectors.path));
1260 break;
1261 default:
1262 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1263 srb->SrbStatus = SRB_STATUS_ERROR;
1264 break;
1267 break;
1268 case XENVBD_DEVICETYPE_CDROM:
1269 if ((srb->Cdb[1] & 1) == 0)
1271 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1272 id->DeviceType = READ_ONLY_DIRECT_ACCESS_DEVICE;
1273 id->RemovableMedia = 1;
1274 id->Versions = 3;
1275 id->ResponseDataFormat = 0;
1276 id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1277 id->CommandQueue = 1;
1278 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1279 memcpy(id->ProductId, scsi_cdrom_model, 16); // product id
1280 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1282 else
1284 switch (srb->Cdb[2])
1286 case 0x00:
1287 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1288 data_buffer[1] = 0x00;
1289 data_buffer[2] = 0x00;
1290 data_buffer[3] = 2;
1291 data_buffer[4] = 0x00;
1292 data_buffer[5] = 0x80;
1293 break;
1294 case 0x80:
1295 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1296 data_buffer[1] = 0x80;
1297 data_buffer[2] = 0x00;
1298 data_buffer[3] = 8;
1299 data_buffer[4] = 0x31;
1300 data_buffer[5] = 0x32;
1301 data_buffer[6] = 0x33;
1302 data_buffer[7] = 0x34;
1303 data_buffer[8] = 0x35;
1304 data_buffer[9] = 0x36;
1305 data_buffer[10] = 0x37;
1306 data_buffer[11] = 0x38;
1307 break;
1308 default:
1309 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1310 srb->SrbStatus = SRB_STATUS_ERROR;
1311 break;
1314 break;
1315 default:
1316 //KdPrint((__DRIVER_NAME " Unknown DeviceType %02x requested\n", xvdd->device_type));
1317 srb->SrbStatus = SRB_STATUS_ERROR;
1318 break;
1320 break;
1321 case SCSIOP_READ_CAPACITY:
1322 if (dump_mode)
1323 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1324 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1325 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1326 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1327 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1328 data_buffer = srb->DataBuffer;
1329 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1330 if ((xvdd->total_sectors - 1) >> 32)
1332 data_buffer[0] = 0xff;
1333 data_buffer[1] = 0xff;
1334 data_buffer[2] = 0xff;
1335 data_buffer[3] = 0xff;
1337 else
1339 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1340 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1341 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1342 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1344 data_buffer[4] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1345 data_buffer[5] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1346 data_buffer[6] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1347 data_buffer[7] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1348 srb->ScsiStatus = 0;
1349 srb->SrbStatus = SRB_STATUS_SUCCESS;
1350 break;
1351 case SCSIOP_READ_CAPACITY16:
1352 if (dump_mode)
1353 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1354 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1355 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1356 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1357 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1358 data_buffer = srb->DataBuffer;
1359 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1360 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 56) & 0xff;
1361 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 48) & 0xff;
1362 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 40) & 0xff;
1363 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 32) & 0xff;
1364 data_buffer[4] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1365 data_buffer[5] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1366 data_buffer[6] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1367 data_buffer[7] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1368 data_buffer[8] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1369 data_buffer[9] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1370 data_buffer[10] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1371 data_buffer[11] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1372 srb->ScsiStatus = 0;
1373 srb->SrbStatus = SRB_STATUS_SUCCESS;
1374 break;
1375 case SCSIOP_MODE_SENSE:
1376 case SCSIOP_MODE_SENSE10:
1377 if (dump_mode)
1378 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));
1379 XenVbd_FillModePage(xvdd, srb);
1380 break;
1381 case SCSIOP_READ:
1382 case SCSIOP_READ16:
1383 case SCSIOP_WRITE:
1384 case SCSIOP_WRITE16:
1385 XenVbd_PutSrbOnList(xvdd, srb);
1386 XenVbd_PutQueuedSrbsOnRing(xvdd);
1387 break;
1388 case SCSIOP_VERIFY:
1389 case SCSIOP_VERIFY16:
1390 // Should we do more here?
1391 if (dump_mode)
1392 KdPrint((__DRIVER_NAME " Command = VERIFY\n"));
1393 srb->SrbStatus = SRB_STATUS_SUCCESS;
1394 break;
1395 case SCSIOP_REPORT_LUNS:
1396 if (dump_mode)
1397 KdPrint((__DRIVER_NAME " Command = REPORT_LUNS\n"));
1398 srb->SrbStatus = SRB_STATUS_SUCCESS;;
1399 break;
1400 case SCSIOP_REQUEST_SENSE:
1401 if (dump_mode)
1402 KdPrint((__DRIVER_NAME " Command = REQUEST_SENSE\n"));
1403 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
1404 srb->SrbStatus = SRB_STATUS_SUCCESS;
1405 break;
1406 case SCSIOP_READ_TOC:
1407 if (dump_mode)
1408 KdPrint((__DRIVER_NAME " Command = READ_TOC\n"));
1409 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1410 data_buffer = srb->DataBuffer;
1411 // DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
1412 /*
1413 #define READ_TOC_FORMAT_TOC 0x00
1414 #define READ_TOC_FORMAT_SESSION 0x01
1415 #define READ_TOC_FORMAT_FULL_TOC 0x02
1416 #define READ_TOC_FORMAT_PMA 0x03
1417 #define READ_TOC_FORMAT_ATIP 0x04
1418 */
1419 // KdPrint((__DRIVER_NAME " Msf = %d\n", cdb->READ_TOC.Msf));
1420 // KdPrint((__DRIVER_NAME " LogicalUnitNumber = %d\n", cdb->READ_TOC.LogicalUnitNumber));
1421 // KdPrint((__DRIVER_NAME " Format2 = %d\n", cdb->READ_TOC.Format2));
1422 // KdPrint((__DRIVER_NAME " StartingTrack = %d\n", cdb->READ_TOC.StartingTrack));
1423 // KdPrint((__DRIVER_NAME " AllocationLength = %d\n", (cdb->READ_TOC.AllocationLength[0] << 8) | cdb->READ_TOC.AllocationLength[1]));
1424 // KdPrint((__DRIVER_NAME " Control = %d\n", cdb->READ_TOC.Control));
1425 // KdPrint((__DRIVER_NAME " Format = %d\n", cdb->READ_TOC.Format));
1426 switch (cdb->READ_TOC.Format2)
1428 case READ_TOC_FORMAT_TOC:
1429 data_buffer[0] = 0; // length MSB
1430 data_buffer[1] = 10; // length LSB
1431 data_buffer[2] = 1; // First Track
1432 data_buffer[3] = 1; // Last Track
1433 data_buffer[4] = 0; // Reserved
1434 data_buffer[5] = 0x14; // current position data + uninterrupted data
1435 data_buffer[6] = 1; // last complete track
1436 data_buffer[7] = 0; // reserved
1437 data_buffer[8] = 0; // MSB Block
1438 data_buffer[9] = 0;
1439 data_buffer[10] = 0;
1440 data_buffer[11] = 0; // LSB Block
1441 srb->SrbStatus = SRB_STATUS_SUCCESS;
1442 break;
1443 case READ_TOC_FORMAT_SESSION:
1444 case READ_TOC_FORMAT_FULL_TOC:
1445 case READ_TOC_FORMAT_PMA:
1446 case READ_TOC_FORMAT_ATIP:
1447 srb->SrbStatus = SRB_STATUS_ERROR;
1448 break;
1450 break;
1451 case SCSIOP_START_STOP_UNIT:
1452 KdPrint((__DRIVER_NAME " Command = SCSIOP_START_STOP_UNIT\n"));
1453 srb->SrbStatus = SRB_STATUS_SUCCESS;
1454 break;
1455 case SCSIOP_RESERVE_UNIT:
1456 KdPrint((__DRIVER_NAME " Command = SCSIOP_RESERVE_UNIT\n"));
1457 srb->SrbStatus = SRB_STATUS_SUCCESS;
1458 break;
1459 case SCSIOP_RELEASE_UNIT:
1460 KdPrint((__DRIVER_NAME " Command = SCSIOP_RELEASE_UNIT\n"));
1461 srb->SrbStatus = SRB_STATUS_SUCCESS;
1462 break;
1463 default:
1464 KdPrint((__DRIVER_NAME " Unhandled EXECUTE_SCSI Command = %02X\n", srb->Cdb[0]));
1465 srb->SrbStatus = SRB_STATUS_ERROR;
1466 break;
1468 if (srb->SrbStatus == SRB_STATUS_ERROR)
1470 KdPrint((__DRIVER_NAME " EXECUTE_SCSI Command = %02X returned error %02x\n", srb->Cdb[0], xvdd->last_sense_key));
1471 if (xvdd->last_sense_key == SCSI_SENSE_NO_SENSE)
1473 xvdd->last_sense_key = SCSI_SENSE_ILLEGAL_REQUEST;
1474 xvdd->last_additional_sense_code = SCSI_ADSENSE_INVALID_CDB;
1476 srb->ScsiStatus = 0x02;
1477 XenVbd_MakeAutoSense(xvdd, srb);
1478 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1479 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1481 ScsiPortNotification(NextRequest, DeviceExtension);
1484 else if (srb->SrbStatus != SRB_STATUS_PENDING)
1486 if (srb->SrbStatus == SRB_STATUS_SUCCESS && data_transfer_length < srb->DataTransferLength)
1488 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
1489 srb->DataTransferLength = data_transfer_length;
1491 xvdd->last_sense_key = SCSI_SENSE_NO_SENSE;
1492 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1493 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1494 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1496 ScsiPortNotification(NextRequest, DeviceExtension);
1499 break;
1500 case SRB_FUNCTION_IO_CONTROL:
1501 KdPrint((__DRIVER_NAME " SRB_FUNCTION_IO_CONTROL\n"));
1502 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1503 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1504 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1506 ScsiPortNotification(NextRequest, DeviceExtension);
1508 break;
1509 case SRB_FUNCTION_FLUSH:
1510 KdPrint((__DRIVER_NAME " SRB_FUNCTION_FLUSH %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1511 srb->SrbStatus = SRB_STATUS_SUCCESS;
1512 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1513 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1515 ScsiPortNotification(NextRequest, DeviceExtension);
1517 break;
1518 case SRB_FUNCTION_SHUTDOWN:
1519 KdPrint((__DRIVER_NAME " SRB_FUNCTION_SHUTDOWN %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1520 srb->SrbStatus = SRB_STATUS_SUCCESS;
1521 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1522 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1524 ScsiPortNotification(NextRequest, DeviceExtension);
1526 break;
1527 default:
1528 KdPrint((__DRIVER_NAME " Unhandled srb->Function = %08X\n", srb->Function));
1529 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1530 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1531 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1533 ScsiPortNotification(NextRequest, DeviceExtension);
1535 break;
1538 //FUNCTION_EXIT();
1539 return TRUE;
1542 static BOOLEAN
1543 XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId)
1545 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1546 srb_list_entry_t *srb_entry;
1547 PSCSI_REQUEST_BLOCK srb;
1548 int i;
1550 UNREFERENCED_PARAMETER(DeviceExtension);
1551 UNREFERENCED_PARAMETER(PathId);
1553 FUNCTION_ENTER();
1555 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1556 xvdd->aligned_buffer_in_use = FALSE;
1558 if (xvdd->ring_detect_state == RING_DETECT_STATE_COMPLETE && xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1560 while((srb_entry = (srb_list_entry_t *)RemoveHeadList(&xvdd->srb_list)) != (srb_list_entry_t *)&xvdd->srb_list)
1562 srb = srb_entry->srb;
1563 srb->SrbStatus = SRB_STATUS_BUS_RESET;
1564 KdPrint((__DRIVER_NAME " completing queued SRB %p with status SRB_STATUS_BUS_RESET\n", srb));
1565 ScsiPortNotification(RequestComplete, xvdd, srb);
1568 for (i = 0; i < MAX_SHADOW_ENTRIES; i++)
1570 if (xvdd->shadows[i].srb)
1572 KdPrint((__DRIVER_NAME " Completing in-flight srb %p with status SRB_STATUS_BUS_RESET\n", xvdd->shadows[i].srb));
1573 /* set reset here so that the interrupt won't do anything with the srb but will dispose of the shadow entry correctly */
1574 xvdd->shadows[i].reset = TRUE;
1575 xvdd->shadows[i].srb->SrbStatus = SRB_STATUS_BUS_RESET;
1576 ScsiPortNotification(RequestComplete, xvdd, xvdd->shadows[i].srb);
1577 xvdd->shadows[i].srb = NULL;
1578 xvdd->shadows[i].aligned_buffer_in_use = FALSE;
1582 /* send a notify to Dom0 just in case it was missed for some reason (which should _never_ happen) */
1583 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
1585 ScsiPortNotification(NextRequest, DeviceExtension);
1588 FUNCTION_EXIT();
1590 return TRUE;
1593 static BOOLEAN
1594 XenVbd_HwScsiAdapterState(PVOID DeviceExtension, PVOID Context, BOOLEAN SaveState)
1596 UNREFERENCED_PARAMETER(DeviceExtension);
1597 UNREFERENCED_PARAMETER(Context);
1598 UNREFERENCED_PARAMETER(SaveState);
1600 FUNCTION_ENTER();
1601 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1603 FUNCTION_EXIT();
1605 return TRUE;
1608 static SCSI_ADAPTER_CONTROL_STATUS
1609 XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters)
1611 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1612 SCSI_ADAPTER_CONTROL_STATUS Status = ScsiAdapterControlSuccess;
1613 PSCSI_SUPPORTED_CONTROL_TYPE_LIST SupportedControlTypeList;
1614 //KIRQL OldIrql;
1616 FUNCTION_ENTER();
1617 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1618 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
1620 switch (ControlType)
1622 case ScsiQuerySupportedControlTypes:
1623 SupportedControlTypeList = (PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters;
1624 KdPrint((__DRIVER_NAME " ScsiQuerySupportedControlTypes (Max = %d)\n", SupportedControlTypeList->MaxControlType));
1625 SupportedControlTypeList->SupportedTypeList[ScsiQuerySupportedControlTypes] = TRUE;
1626 SupportedControlTypeList->SupportedTypeList[ScsiStopAdapter] = TRUE;
1627 SupportedControlTypeList->SupportedTypeList[ScsiRestartAdapter] = TRUE;
1628 break;
1629 case ScsiStopAdapter:
1630 KdPrint((__DRIVER_NAME " ScsiStopAdapter\n"));
1631 /* I don't think we actually have to do anything here... xenpci cleans up all the xenbus stuff for us */
1632 break;
1633 case ScsiRestartAdapter:
1634 KdPrint((__DRIVER_NAME " ScsiRestartAdapter\n"));
1635 if (!xvdd->inactive)
1637 if (XenVbd_InitFromConfig(xvdd) != SP_RETURN_FOUND)
1638 KeBugCheckEx(DATA_COHERENCY_EXCEPTION, 0, (ULONG_PTR) xvdd, 0, 0);
1639 xvdd->ring_detect_state = RING_DETECT_STATE_NOT_STARTED;
1641 break;
1642 case ScsiSetBootConfig:
1643 KdPrint((__DRIVER_NAME " ScsiSetBootConfig\n"));
1644 break;
1645 case ScsiSetRunningConfig:
1646 KdPrint((__DRIVER_NAME " ScsiSetRunningConfig\n"));
1647 break;
1648 default:
1649 KdPrint((__DRIVER_NAME " UNKNOWN\n"));
1650 break;
1653 FUNCTION_EXIT();
1655 return Status;
1658 NTSTATUS
1659 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
1661 ULONG status;
1662 HW_INITIALIZATION_DATA HwInitializationData;
1663 PVOID driver_extension;
1664 PUCHAR ptr;
1665 OBJECT_ATTRIBUTES oa;
1666 HANDLE service_handle;
1667 UNICODE_STRING param_name;
1668 HANDLE param_handle;
1669 UNICODE_STRING value_name;
1670 CHAR buf[256];
1671 ULONG buf_len;
1672 PKEY_VALUE_PARTIAL_INFORMATION kpv;
1674 FUNCTION_ENTER();
1675 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1676 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
1678 /* RegistryPath == NULL when we are invoked as a crash dump driver */
1679 if (!RegistryPath)
1681 dump_mode = TRUE;
1684 if (!dump_mode)
1686 IoAllocateDriverObjectExtension(DriverObject, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC), PAGE_SIZE, &driver_extension);
1687 ptr = driver_extension;
1688 //ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
1689 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "ring-ref", NULL, NULL);
1690 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_IRQ, "event-channel", NULL, NULL);
1691 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_FRONT, "device-type", NULL, NULL);
1692 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mode", NULL, NULL);
1693 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sectors", NULL, NULL);
1694 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sector-size", NULL, NULL);
1695 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_GRANT_ENTRIES, ULongToPtr((ULONG)'SCSI'), ULongToPtr(BLKIF_MAX_SEGMENTS_PER_REQUEST), NULL); /* for use in crash dump */
1696 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT, NULL, NULL, NULL);
1697 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1698 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1699 __ADD_XEN_INIT_UCHAR(&ptr, 20);
1700 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1701 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT, NULL, NULL, NULL);
1702 //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1703 //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1704 //__ADD_XEN_INIT_UCHAR(&ptr, 20);
1705 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1706 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN, NULL, NULL, NULL);
1707 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
1708 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
1709 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1710 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
1711 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
1712 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1713 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitialising);
1714 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait);
1715 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1716 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1718 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1720 InitializeObjectAttributes(&oa, RegistryPath, OBJ_CASE_INSENSITIVE, NULL, NULL);
1721 status = ZwOpenKey(&service_handle, KEY_READ, &oa);
1722 if(!NT_SUCCESS(status))
1724 KdPrint((__DRIVER_NAME " ZwOpenKey(Service) returned %08x\n", status));
1726 else
1728 RtlInitUnicodeString(&param_name, L"Parameters");
1729 InitializeObjectAttributes(&oa, &param_name, OBJ_CASE_INSENSITIVE, service_handle, NULL);
1730 status = ZwOpenKey(&param_handle, KEY_READ, &oa);
1731 if(!NT_SUCCESS(status))
1733 KdPrint((__DRIVER_NAME " ZwOpenKey(Parameters) returned %08x\n", status));
1735 else
1737 kpv = (PKEY_VALUE_PARTIAL_INFORMATION)buf;
1738 RtlFillMemory(scsi_device_manufacturer, 8, ' ');
1739 RtlFillMemory(scsi_disk_model, 16, ' ');
1740 RtlFillMemory(scsi_cdrom_model, 16, ' ');
1742 RtlInitUnicodeString(&value_name, L"Manufacturer");
1743 buf_len = 256;
1744 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1745 if(NT_SUCCESS(status))
1746 wcstombs(scsi_device_manufacturer, (PWCHAR)kpv->Data, min(kpv->DataLength, 8));
1747 else
1748 RtlStringCbCopyA(scsi_device_manufacturer, 8, "XEN ");
1750 RtlInitUnicodeString(&value_name, L"Disk_Model");
1751 buf_len = 256;
1752 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1753 if(NT_SUCCESS(status))
1754 wcstombs(scsi_disk_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1755 else
1756 RtlStringCbCopyA(scsi_disk_model, 16, "PV DISK ");
1758 RtlInitUnicodeString(&value_name, L"CDROM_Model");
1759 buf_len = 256;
1760 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1761 if(NT_SUCCESS(status))
1762 wcstombs(scsi_cdrom_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1763 else
1764 RtlStringCbCopyA(scsi_cdrom_model, 16, "PV CDROM ");
1765 ZwClose(param_handle);
1767 ZwClose(service_handle);
1771 RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
1773 HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
1774 HwInitializationData.AdapterInterfaceType = PNPBus;
1775 if (!dump_mode)
1776 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_DEVICE_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE;
1777 else
1778 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_DEVICE_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE_DUMP_MODE;
1779 HwInitializationData.SpecificLuExtensionSize = 0;
1780 HwInitializationData.SrbExtensionSize = sizeof(srb_list_entry_t);
1781 HwInitializationData.NumberOfAccessRanges = 1;
1782 HwInitializationData.MapBuffers = TRUE;
1783 HwInitializationData.NeedPhysicalAddresses = FALSE;
1784 HwInitializationData.TaggedQueuing = TRUE;
1785 HwInitializationData.AutoRequestSense = TRUE;
1786 HwInitializationData.MultipleRequestPerLu = TRUE;
1787 HwInitializationData.ReceiveEvent = FALSE;
1788 HwInitializationData.VendorIdLength = 0;
1789 HwInitializationData.VendorId = NULL;
1790 HwInitializationData.DeviceIdLength = 0;
1791 HwInitializationData.DeviceId = NULL;
1793 HwInitializationData.HwInitialize = XenVbd_HwScsiInitialize;
1794 HwInitializationData.HwStartIo = XenVbd_HwScsiStartIo;
1795 HwInitializationData.HwInterrupt = XenVbd_HwScsiInterrupt;
1796 HwInitializationData.HwFindAdapter = XenVbd_HwScsiFindAdapter;
1797 HwInitializationData.HwResetBus = XenVbd_HwScsiResetBus;
1798 HwInitializationData.HwDmaStarted = NULL;
1799 HwInitializationData.HwAdapterState = NULL; //XenVbd_HwScsiAdapterState;
1800 HwInitializationData.HwAdapterControl = XenVbd_HwScsiAdapterControl;
1802 status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
1804 if(!NT_SUCCESS(status))
1806 KdPrint((__DRIVER_NAME " ScsiPortInitialize failed with status 0x%08x\n", status));
1809 FUNCTION_EXIT();
1811 return status;