win-pvdrivers

view xenvbd/xenvbd_scsiport.c @ 882:5020c1c55f95

Windows 2000 doesn't have ScsiPortQuerySystemTime function
author James Harper <james.harper@bendigoit.com.au>
date Tue Mar 15 12:24:29 2011 +1100 (2011-03-15)
parents 7ec9138621d6
children 33b9eccb6301
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 = (BOOLEAN)!!(PtrToUlong(value) & 0x40000000);
162 KdPrint((__DRIVER_NAME " cached_use_other = %d\n", xvdd->cached_use_other));
163 }
164 }
165 break;
166 case XEN_INIT_TYPE_READ_STRING_BACK:
167 case XEN_INIT_TYPE_READ_STRING_FRONT:
168 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = %s\n", setting, value));
169 if (strcmp(setting, "sectors") == 0)
170 xvdd->total_sectors = parse_numeric_string(value);
171 else if (strcmp(setting, "sector-size") == 0)
172 xvdd->bytes_per_sector = (ULONG)parse_numeric_string(value);
173 else if (strcmp(setting, "device-type") == 0)
174 {
175 if (strcmp(value, "disk") == 0)
176 {
177 KdPrint((__DRIVER_NAME " device-type = Disk\n"));
178 xvdd->device_type = XENVBD_DEVICETYPE_DISK;
179 }
180 else if (strcmp(value, "cdrom") == 0)
181 {
182 KdPrint((__DRIVER_NAME " device-type = CDROM\n"));
183 xvdd->device_type = XENVBD_DEVICETYPE_CDROM;
184 }
185 else
186 {
187 KdPrint((__DRIVER_NAME " device-type = %s (This probably won't work!)\n", value));
188 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
189 }
190 }
191 else if (strcmp(setting, "mode") == 0)
192 {
193 if (strncmp(value, "r", 1) == 0)
194 {
195 KdPrint((__DRIVER_NAME " mode = r\n"));
196 xvdd->device_mode = XENVBD_DEVICEMODE_READ;
197 }
198 else if (strncmp(value, "w", 1) == 0)
199 {
200 KdPrint((__DRIVER_NAME " mode = w\n"));
201 xvdd->device_mode = XENVBD_DEVICEMODE_WRITE;
202 }
203 else
204 {
205 KdPrint((__DRIVER_NAME " mode = unknown\n"));
206 xvdd->device_mode = XENVBD_DEVICEMODE_UNKNOWN;
207 }
208 }
209 break;
210 case XEN_INIT_TYPE_VECTORS:
211 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_VECTORS\n"));
212 if (((PXENPCI_VECTORS)value)->length != sizeof(XENPCI_VECTORS) ||
213 ((PXENPCI_VECTORS)value)->magic != XEN_DATA_MAGIC)
214 {
215 KdPrint((__DRIVER_NAME " vectors mismatch (magic = %08x, length = %d)\n",
216 ((PXENPCI_VECTORS)value)->magic, ((PXENPCI_VECTORS)value)->length));
217 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
218 return SP_RETURN_BAD_CONFIG;
219 }
220 else
221 memcpy(&xvdd->vectors, value, sizeof(XENPCI_VECTORS));
222 break;
223 case XEN_INIT_TYPE_STATE_PTR:
224 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_DEVICE_STATE - %p\n", PtrToUlong(value)));
225 xvdd->device_state = (PXENPCI_DEVICE_STATE)value;
226 break;
227 case XEN_INIT_TYPE_QEMU_PROTOCOL_VERSION:
228 qemu_protocol_version = PtrToUlong(value);
229 break;
230 case XEN_INIT_TYPE_GRANT_ENTRIES:
231 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_GRANT_ENTRIES - entries = %d\n", PtrToUlong(value)));
232 memcpy(xvdd->dump_grant_refs, value2, PtrToUlong(value) * sizeof(grant_ref_t));
233 break;
234 case XEN_INIT_TYPE_QEMU_HIDE_FLAGS:
235 qemu_hide_flags_value = PtrToUlong(value);
236 KdPrint((__DRIVER_NAME " qemu_hide_flags_value = %d\n", qemu_hide_flags_value));
237 break;
238 case XEN_INIT_TYPE_QEMU_HIDE_FILTER:
239 qemu_hide_filter = TRUE;
240 KdPrint((__DRIVER_NAME " qemu_hide_filter = TRUE\n"));
241 break;
242 default:
243 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_%d\n", type));
244 break;
245 }
246 }
248 if (((qemu_hide_flags_value & QEMU_UNPLUG_ALL_IDE_DISKS) && xvdd->device_type != XENVBD_DEVICETYPE_CDROM) || qemu_hide_filter)
249 xvdd->inactive = FALSE;
251 if (!xvdd->inactive && (xvdd->device_type == XENVBD_DEVICETYPE_UNKNOWN
252 || xvdd->sring == NULL
253 || xvdd->event_channel == 0
254 || xvdd->total_sectors == 0
255 || xvdd->bytes_per_sector == 0))
256 {
257 KdPrint((__DRIVER_NAME " Missing settings\n"));
258 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
259 return SP_RETURN_BAD_CONFIG;
260 }
262 if (xvdd->inactive)
263 KdPrint((__DRIVER_NAME " Device is inactive\n"));
264 else
265 {
266 if (xvdd->device_type == XENVBD_DEVICETYPE_CDROM)
267 {
268 /* CD/DVD drives must have bytes_per_sector = 2048. */
269 xvdd->bytes_per_sector = 2048;
270 }
272 /* for some reason total_sectors is measured in 512 byte sectors always, so correct this to be in bytes_per_sectors */
273 xvdd->total_sectors /= xvdd->bytes_per_sector / 512;
275 xvdd->shadow_free = 0;
276 memset(xvdd->shadows, 0, sizeof(blkif_shadow_t) * SHADOW_ENTRIES);
277 for (i = 0; i < SHADOW_ENTRIES; i++)
278 {
279 xvdd->shadows[i].req.id = i;
280 /* make sure leftover real requests's are never confused with dump mode requests */
281 if (dump_mode)
282 xvdd->shadows[i].req.id |= SHADOW_ID_DUMP_FLAG;
283 put_shadow_on_freelist(xvdd, &xvdd->shadows[i]);
284 if (dump_mode)
285 {
286 /* set reset = TRUE to pick up in-flight requests when dump mode kicked in */
287 /* do it after put_shadow_on_freelist because that resets the reset flag */
288 xvdd->shadows[i].reset = TRUE;
289 }
290 }
291 }
293 return SP_RETURN_FOUND;
294 }
296 static __inline ULONG
297 decode_cdb_length(PSCSI_REQUEST_BLOCK srb)
298 {
299 switch (srb->Cdb[0])
300 {
301 case SCSIOP_READ:
302 case SCSIOP_WRITE:
303 return ((ULONG)(UCHAR)srb->Cdb[7] << 8) | (ULONG)(UCHAR)srb->Cdb[8];
304 case SCSIOP_READ16:
305 case SCSIOP_WRITE16:
306 return ((ULONG)(UCHAR)srb->Cdb[10] << 24) | ((ULONG)(UCHAR)srb->Cdb[11] << 16) | ((ULONG)(UCHAR)srb->Cdb[12] << 8) | (ULONG)(UCHAR)srb->Cdb[13];
307 default:
308 return 0;
309 }
310 }
312 static __inline ULONGLONG
313 decode_cdb_sector(PSCSI_REQUEST_BLOCK srb)
314 {
315 ULONGLONG sector;
317 switch (srb->Cdb[0])
318 {
319 case SCSIOP_READ:
320 case SCSIOP_WRITE:
321 sector = ((ULONG)(UCHAR)srb->Cdb[2] << 24) | ((ULONG)(UCHAR)srb->Cdb[3] << 16) | ((ULONG)(UCHAR)srb->Cdb[4] << 8) | (ULONG)(UCHAR)srb->Cdb[5];
322 break;
323 case SCSIOP_READ16:
324 case SCSIOP_WRITE16:
325 sector = ((ULONGLONG)(UCHAR)srb->Cdb[2] << 56) | ((ULONGLONG)(UCHAR)srb->Cdb[3] << 48)
326 | ((ULONGLONG)(UCHAR)srb->Cdb[4] << 40) | ((ULONGLONG)(UCHAR)srb->Cdb[5] << 32)
327 | ((ULONGLONG)(UCHAR)srb->Cdb[6] << 24) | ((ULONGLONG)(UCHAR)srb->Cdb[7] << 16)
328 | ((ULONGLONG)(UCHAR)srb->Cdb[8] << 8) | ((ULONGLONG)(UCHAR)srb->Cdb[9]);
329 //KdPrint((__DRIVER_NAME " sector_number = %d (high) %d (low)\n", (ULONG)(sector >> 32), (ULONG)sector));
330 break;
331 default:
332 sector = 0;
333 break;
334 }
335 return sector;
336 }
338 static __inline BOOLEAN
339 decode_cdb_is_read(PSCSI_REQUEST_BLOCK srb)
340 {
341 switch (srb->Cdb[0])
342 {
343 case SCSIOP_READ:
344 case SCSIOP_READ16:
345 return TRUE;
346 case SCSIOP_WRITE:
347 case SCSIOP_WRITE16:
348 return FALSE;
349 default:
350 return FALSE;
351 }
352 }
354 ULONG max_dump_mode_blocks = 0;
355 ULONG max_dump_mode_length = 0;
357 static VOID
358 XenVbd_PutSrbOnList(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
359 {
360 srb_list_entry_t *list_entry = srb->SrbExtension;
361 list_entry->srb = srb;
362 InsertTailList(&xvdd->srb_list, (PLIST_ENTRY)list_entry);
363 }
365 static VOID
366 XenVbd_PutQueuedSrbsOnRing(PXENVBD_DEVICE_DATA xvdd)
367 {
368 PSCSI_REQUEST_BLOCK srb;
369 srb_list_entry_t *srb_entry;
370 ULONGLONG sector_number;
371 ULONG block_count;
372 blkif_shadow_t *shadow;
373 ULONG remaining, offset, length;
374 grant_ref_t gref;
375 PUCHAR ptr;
376 int notify;
377 int i;
378 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
379 LARGE_INTEGER current_time;
380 #endif
382 //FUNCTION_ENTER();
384 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
385 ScsiPortQuerySystemTime(&current_time);
386 #endif
388 while(!xvdd->aligned_buffer_in_use && xvdd->shadow_free && (srb_entry = (srb_list_entry_t *)RemoveHeadList(&xvdd->srb_list)) != (srb_list_entry_t *)&xvdd->srb_list)
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 if (dump_mode && block_count > max_dump_mode_blocks)
476 {
477 max_dump_mode_blocks = block_count;
478 KdPrint((__DRIVER_NAME " max_dump_mode_blocks = %d\n", max_dump_mode_blocks));
479 }
480 if (dump_mode && srb->DataTransferLength > max_dump_mode_length)
481 {
482 max_dump_mode_length = srb->DataTransferLength;
483 KdPrint((__DRIVER_NAME " max_dump_mode_length = %d\n", max_dump_mode_length));
484 }
486 //KdPrint((__DRIVER_NAME " sector_number = %d, block_count = %d\n", (ULONG)shadow->req.sector_number, block_count));
487 //KdPrint((__DRIVER_NAME " SrbExtension = %p\n", srb->SrbExtension));
488 //KdPrint((__DRIVER_NAME " DataBuffer = %p\n", srb->DataBuffer));
490 //KdPrint((__DRIVER_NAME " sector_number = %d\n", (ULONG)shadow->req.sector_number));
491 //KdPrint((__DRIVER_NAME " handle = %d\n", shadow->req.handle));
492 //KdPrint((__DRIVER_NAME " operation = %d\n", shadow->req.operation));
494 while (remaining > 0)
495 {
496 PHYSICAL_ADDRESS physical_address = MmGetPhysicalAddress(ptr);
498 if (dump_mode)
499 {
500 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
501 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE,
502 xvdd->dump_grant_refs[shadow->req.nr_segments], (ULONG)'SCSI');
503 }
504 else
505 {
506 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
507 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE, INVALID_GRANT_REF, (ULONG)'SCSI');
508 }
509 if (gref == INVALID_GRANT_REF)
510 {
511 ULONG i;
512 for (i = 0; i < shadow->req.nr_segments; i++)
513 {
514 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
515 shadow->req.seg[i].gref, FALSE, (ULONG)'SCSI');
516 }
517 if (shadow->aligned_buffer_in_use)
518 {
519 shadow->aligned_buffer_in_use = FALSE;
520 xvdd->aligned_buffer_in_use = FALSE;
521 }
522 /* put the srb back at the start of the queue, then fall through so that the notify is triggered*/
523 InsertHeadList(&xvdd->srb_list, (PLIST_ENTRY)srb->SrbExtension);
524 put_shadow_on_freelist(xvdd, shadow);
525 KdPrint((__DRIVER_NAME " Out of gref's. Deferring\n"));
526 break;
527 }
528 offset = physical_address.LowPart & (PAGE_SIZE - 1);
529 length = min(PAGE_SIZE - offset, remaining);
530 ASSERT((offset & 511) == 0);
531 ASSERT((length & 511) == 0);
532 ASSERT(offset + length <= PAGE_SIZE);
533 shadow->req.seg[shadow->req.nr_segments].gref = gref;
534 shadow->req.seg[shadow->req.nr_segments].first_sect = (UCHAR)(offset >> 9);
535 shadow->req.seg[shadow->req.nr_segments].last_sect = (UCHAR)(((offset + length) >> 9) - 1);
536 remaining -= length;
537 ptr += length;
538 shadow->req.nr_segments++;
539 }
541 //KdPrint((__DRIVER_NAME " nr_segments = %d\n", shadow->req.nr_segments));
543 XenVbd_PutRequest(xvdd, &shadow->req);
544 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
545 shadow->ring_submit_time = current_time;
546 #endif
548 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
549 if (notify)
550 {
551 //KdPrint((__DRIVER_NAME " Notifying\n"));
552 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
553 }
554 }
555 if (xvdd->shadow_free && !xvdd->aligned_buffer_in_use)
556 {
557 ScsiPortNotification(NextLuRequest, xvdd, 0, 0, 0);
558 }
559 //FUNCTION_EXIT();
560 }
562 static ULONG DDKAPI
563 XenVbd_HwScsiFindAdapter(PVOID DeviceExtension, PVOID HwContext, PVOID BusInformation, PCHAR ArgumentString, PPORT_CONFIGURATION_INFORMATION ConfigInfo, PBOOLEAN Again)
564 {
565 // PACCESS_RANGE AccessRange;
566 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
567 ULONG status;
568 // PXENPCI_XEN_DEVICE_DATA XenDeviceData;
569 PACCESS_RANGE access_range;
571 UNREFERENCED_PARAMETER(HwContext);
572 UNREFERENCED_PARAMETER(BusInformation);
573 UNREFERENCED_PARAMETER(ArgumentString);
575 FUNCTION_ENTER();
576 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
577 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
579 RtlZeroMemory(xvdd, sizeof(XENVBD_DEVICE_DATA));
580 *Again = FALSE;
582 KdPrint((__DRIVER_NAME " BusInterruptLevel = %d\n", ConfigInfo->BusInterruptLevel));
583 KdPrint((__DRIVER_NAME " BusInterruptVector = %03x\n", ConfigInfo->BusInterruptVector));
585 KdPrint((__DRIVER_NAME " NumberOfAccessRanges = %d\n", ConfigInfo->NumberOfAccessRanges));
586 if (ConfigInfo->NumberOfAccessRanges != 1 && ConfigInfo->NumberOfAccessRanges != 2)
587 {
588 return SP_RETURN_BAD_CONFIG;
589 }
591 access_range = &((*(ConfigInfo->AccessRanges))[0]);
592 KdPrint((__DRIVER_NAME " RangeStart = %08x, RangeLength = %08x\n",
593 access_range->RangeStart.LowPart, access_range->RangeLength));
594 xvdd->device_base = ScsiPortGetDeviceBase(
595 DeviceExtension,
596 ConfigInfo->AdapterInterfaceType,
597 ConfigInfo->SystemIoBusNumber,
598 access_range->RangeStart,
599 access_range->RangeLength,
600 !access_range->RangeInMemory);
601 if (!xvdd->device_base)
602 {
603 KdPrint((__DRIVER_NAME " Invalid config\n"));
604 FUNCTION_EXIT();
605 return SP_RETURN_BAD_CONFIG;
606 }
608 status = XenVbd_InitFromConfig(xvdd);
609 if (status != SP_RETURN_FOUND)
610 {
611 FUNCTION_EXIT();
612 return status;
613 }
615 xvdd->aligned_buffer_in_use = FALSE;
616 /* align the buffer to PAGE_SIZE */
617 xvdd->aligned_buffer = (PVOID)((ULONG_PTR)((PUCHAR)xvdd->aligned_buffer_data + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
618 KdPrint((__DRIVER_NAME " aligned_buffer_data = %p\n", xvdd->aligned_buffer_data));
619 KdPrint((__DRIVER_NAME " aligned_buffer = %p\n", xvdd->aligned_buffer));
621 if (!dump_mode)
622 {
623 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
624 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST - 1;
625 //ConfigInfo->ScatterGather = TRUE;
626 }
627 else
628 {
629 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE * PAGE_SIZE;
630 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE - 1;
631 //ConfigInfo->ScatterGather = FALSE;
632 }
633 KdPrint((__DRIVER_NAME " ConfigInfo->MaximumTransferLength = %d\n", ConfigInfo->MaximumTransferLength));
634 KdPrint((__DRIVER_NAME " ConfigInfo->NumberOfPhysicalBreaks = %d\n", ConfigInfo->NumberOfPhysicalBreaks));
635 ConfigInfo->ScatterGather = FALSE;
636 ConfigInfo->AlignmentMask = 0;
637 ConfigInfo->NumberOfBuses = 1;
638 ConfigInfo->InitiatorBusId[0] = 1;
639 ConfigInfo->MaximumNumberOfLogicalUnits = 1;
640 ConfigInfo->MaximumNumberOfTargets = 2;
641 KdPrint((__DRIVER_NAME " ConfigInfo->CachesData was initialised to %d\n", ConfigInfo->CachesData));
642 ConfigInfo->CachesData = FALSE;
643 ConfigInfo->BufferAccessScsiPortControlled = FALSE;
644 if (ConfigInfo->Dma64BitAddresses == SCSI_DMA64_SYSTEM_SUPPORTED)
645 {
646 ConfigInfo->Master = TRUE;
647 ConfigInfo->Dma64BitAddresses = SCSI_DMA64_MINIPORT_SUPPORTED;
648 ConfigInfo->Dma32BitAddresses = FALSE;
649 KdPrint((__DRIVER_NAME " Dma64BitAddresses supported\n"));
650 }
651 else
652 {
653 ConfigInfo->Master = TRUE;
654 ConfigInfo->Dma32BitAddresses = TRUE;
655 KdPrint((__DRIVER_NAME " Dma64BitAddresses not supported\n"));
656 }
658 FUNCTION_EXIT();
660 return SP_RETURN_FOUND;
661 }
663 static VOID
664 XenVbd_StartRingDetection(PXENVBD_DEVICE_DATA xvdd)
665 {
666 blkif_request_t *req;
667 int notify;
669 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT1;
670 RtlZeroMemory(xvdd->sring->ring, PAGE_SIZE - FIELD_OFFSET(blkif_sring_t, ring));
671 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
672 req->operation = 0xff;
673 xvdd->ring.req_prod_pvt++;
674 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
675 req->operation = 0xff;
676 xvdd->ring.req_prod_pvt++;
678 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
679 if (notify)
680 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
681 }
683 static BOOLEAN
684 XenVbd_HwScsiInitialize(PVOID DeviceExtension)
685 {
686 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
688 FUNCTION_ENTER();
689 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
690 KdPrint((__DRIVER_NAME " dump_mode = %d\n", dump_mode));
692 if (!xvdd->inactive)
693 {
694 if (!dump_mode)
695 {
696 XenVbd_StartRingDetection(xvdd);
697 }
698 else
699 {
700 if (xvdd->cached_use_other)
701 {
702 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
703 xvdd->use_other = TRUE;
704 }
705 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
706 }
707 InitializeListHead(&xvdd->srb_list);
708 }
709 FUNCTION_EXIT();
711 return TRUE;
712 }
714 static ULONG
715 XenVbd_FillModePage(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
716 {
717 PMODE_PARAMETER_HEADER parameter_header = NULL;
718 PMODE_PARAMETER_HEADER10 parameter_header10 = NULL;
719 PMODE_PARAMETER_BLOCK param_block;
720 PMODE_FORMAT_PAGE format_page;
721 ULONG offset = 0;
722 UCHAR buffer[1024];
723 BOOLEAN valid_page = FALSE;
724 BOOLEAN cdb_llbaa;
725 BOOLEAN cdb_dbd;
726 UCHAR cdb_page_code;
727 USHORT cdb_allocation_length;
729 UNREFERENCED_PARAMETER(xvdd);
731 RtlZeroMemory(srb->DataBuffer, srb->DataTransferLength);
732 RtlZeroMemory(buffer, ARRAY_SIZE(buffer));
733 offset = 0;
735 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
737 switch (srb->Cdb[0])
738 {
739 case SCSIOP_MODE_SENSE:
740 cdb_llbaa = FALSE;
741 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
742 cdb_page_code = srb->Cdb[2] & 0x3f;
743 cdb_allocation_length = srb->Cdb[4];
744 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
745 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
746 parameter_header = (PMODE_PARAMETER_HEADER)&buffer[offset];
747 parameter_header->MediumType = 0;
748 parameter_header->DeviceSpecificParameter = 0;
749 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
750 {
751 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
752 parameter_header->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
753 }
754 offset += sizeof(MODE_PARAMETER_HEADER);
755 break;
756 case SCSIOP_MODE_SENSE10:
757 cdb_llbaa = (BOOLEAN)!!(srb->Cdb[1] & 16);
758 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
759 cdb_page_code = srb->Cdb[2] & 0x3f;
760 cdb_allocation_length = (srb->Cdb[7] << 8) | srb->Cdb[8];
761 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE10 llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
762 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
763 parameter_header10 = (PMODE_PARAMETER_HEADER10)&buffer[offset];
764 parameter_header10->MediumType = 0;
765 parameter_header10->DeviceSpecificParameter = 0;
766 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
767 {
768 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
769 parameter_header10->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
770 }
771 offset += sizeof(MODE_PARAMETER_HEADER10);
772 break;
773 default:
774 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE_WTF (%02x)\n", (ULONG)srb->Cdb[0]));
775 return FALSE;
776 }
778 if (!cdb_dbd)
779 {
780 param_block = (PMODE_PARAMETER_BLOCK)&buffer[offset];
781 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK)
782 {
783 if (xvdd->total_sectors >> 32)
784 {
785 param_block->DensityCode = 0xff;
786 param_block->NumberOfBlocks[0] = 0xff;
787 param_block->NumberOfBlocks[1] = 0xff;
788 param_block->NumberOfBlocks[2] = 0xff;
789 }
790 else
791 {
792 param_block->DensityCode = (UCHAR)((xvdd->total_sectors >> 24) & 0xff);
793 param_block->NumberOfBlocks[0] = (UCHAR)((xvdd->total_sectors >> 16) & 0xff);
794 param_block->NumberOfBlocks[1] = (UCHAR)((xvdd->total_sectors >> 8) & 0xff);
795 param_block->NumberOfBlocks[2] = (UCHAR)((xvdd->total_sectors >> 0) & 0xff);
796 }
797 param_block->BlockLength[0] = (UCHAR)((xvdd->bytes_per_sector >> 16) & 0xff);
798 param_block->BlockLength[1] = (UCHAR)((xvdd->bytes_per_sector >> 8) & 0xff);
799 param_block->BlockLength[2] = (UCHAR)((xvdd->bytes_per_sector >> 0) & 0xff);
800 }
801 offset += sizeof(MODE_PARAMETER_BLOCK);
802 }
803 switch (srb->Cdb[0])
804 {
805 case SCSIOP_MODE_SENSE:
806 parameter_header->BlockDescriptorLength = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER));
807 break;
808 case SCSIOP_MODE_SENSE10:
809 parameter_header10->BlockDescriptorLength[0] = (UCHAR)((offset - sizeof(MODE_PARAMETER_HEADER10)) >> 8);
810 parameter_header10->BlockDescriptorLength[1] = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER10));
811 break;
812 }
813 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_FORMAT_DEVICE || cdb_page_code == MODE_SENSE_RETURN_ALL))
814 {
815 valid_page = TRUE;
816 format_page = (PMODE_FORMAT_PAGE)&buffer[offset];
817 format_page->PageCode = MODE_PAGE_FORMAT_DEVICE;
818 format_page->PageLength = sizeof(MODE_FORMAT_PAGE) - FIELD_OFFSET(MODE_FORMAT_PAGE, PageLength);
819 /* 256 sectors per track */
820 format_page->SectorsPerTrack[0] = 0x01;
821 format_page->SectorsPerTrack[1] = 0x00;
822 /* xxx bytes per sector */
823 format_page->BytesPerPhysicalSector[0] = (UCHAR)(xvdd->bytes_per_sector >> 8);
824 format_page->BytesPerPhysicalSector[1] = (UCHAR)(xvdd->bytes_per_sector & 0xff);
825 format_page->HardSectorFormating = TRUE;
826 format_page->SoftSectorFormating = TRUE;
827 offset += sizeof(MODE_FORMAT_PAGE);
828 }
829 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_CACHING || cdb_page_code == MODE_SENSE_RETURN_ALL))
830 {
831 PMODE_CACHING_PAGE caching_page;
832 valid_page = TRUE;
833 caching_page = (PMODE_CACHING_PAGE)&buffer[offset];
834 caching_page->PageCode = MODE_PAGE_CACHING;
835 caching_page->PageLength = sizeof(MODE_CACHING_PAGE) - FIELD_OFFSET(MODE_CACHING_PAGE, PageLength);
836 // caching_page-> // all zeros is just fine... maybe
837 offset += sizeof(MODE_CACHING_PAGE);
838 }
839 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_MEDIUM_TYPES || cdb_page_code == MODE_SENSE_RETURN_ALL))
840 {
841 PUCHAR medium_types_page;
842 valid_page = TRUE;
843 medium_types_page = &buffer[offset];
844 medium_types_page[0] = MODE_PAGE_MEDIUM_TYPES;
845 medium_types_page[1] = 0x06;
846 medium_types_page[2] = 0;
847 medium_types_page[3] = 0;
848 medium_types_page[4] = 0;
849 medium_types_page[5] = 0;
850 medium_types_page[6] = 0;
851 medium_types_page[7] = 0;
852 offset += 8;
853 }
854 switch (srb->Cdb[0])
855 {
856 case SCSIOP_MODE_SENSE:
857 parameter_header->ModeDataLength = (UCHAR)(offset - 1);
858 break;
859 case SCSIOP_MODE_SENSE10:
860 parameter_header10->ModeDataLength[0] = (UCHAR)((offset - 2) >> 8);
861 parameter_header10->ModeDataLength[1] = (UCHAR)(offset - 2);
862 break;
863 }
865 if (!valid_page && cdb_page_code != MODE_SENSE_RETURN_ALL)
866 {
867 srb->SrbStatus = SRB_STATUS_ERROR;
868 }
869 else if(offset < srb->DataTransferLength)
870 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
871 else
872 srb->SrbStatus = SRB_STATUS_SUCCESS;
873 srb->DataTransferLength = min(srb->DataTransferLength, offset);
874 srb->ScsiStatus = 0;
875 memcpy(srb->DataBuffer, buffer, srb->DataTransferLength);
877 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
879 return TRUE;
880 }
882 static VOID
883 XenVbd_MakeSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb, UCHAR sense_key, UCHAR additional_sense_code)
884 {
885 PSENSE_DATA sd = srb->SenseInfoBuffer;
887 UNREFERENCED_PARAMETER(xvdd);
889 if (!srb->SenseInfoBuffer)
890 return;
892 sd->ErrorCode = 0x70;
893 sd->Valid = 1;
894 sd->SenseKey = sense_key;
895 sd->AdditionalSenseLength = sizeof(SENSE_DATA) - FIELD_OFFSET(SENSE_DATA, AdditionalSenseLength);
896 sd->AdditionalSenseCode = additional_sense_code;
897 return;
898 }
900 static VOID
901 XenVbd_MakeAutoSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
902 {
903 if (srb->SrbStatus == SRB_STATUS_SUCCESS || srb->SrbFlags & SRB_FLAGS_DISABLE_AUTOSENSE)
904 return;
905 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
906 srb->SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
907 }
909 static BOOLEAN DDKAPI
910 XenVbd_HwScsiInterrupt(PVOID DeviceExtension)
911 {
912 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
913 PSCSI_REQUEST_BLOCK srb;
914 RING_IDX i, rp;
915 ULONG j;
916 blkif_response_t *rep;
917 int block_count;
918 int more_to_do = TRUE;
919 blkif_shadow_t *shadow;
920 ULONG suspend_resume_state_pdo;
921 BOOLEAN last_interrupt = FALSE;
922 ULONG start_ring_detect_state = xvdd->ring_detect_state;
923 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
924 srb_list_entry_t *srb_entry;
925 ULONG elapsed;
926 LARGE_INTEGER current_time;
927 #endif
929 /* in dump mode I think we get called on a timer, not by an actual IRQ */
930 if (!dump_mode && !xvdd->vectors.EvtChn_AckEvent(xvdd->vectors.context, xvdd->event_channel, &last_interrupt))
931 return FALSE; /* interrupt was not for us */
933 suspend_resume_state_pdo = xvdd->device_state->suspend_resume_state_pdo;
934 KeMemoryBarrier();
936 if (suspend_resume_state_pdo != xvdd->device_state->suspend_resume_state_fdo)
937 {
938 FUNCTION_ENTER();
939 switch (suspend_resume_state_pdo)
940 {
941 case SR_STATE_SUSPENDING:
942 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_SUSPENDING\n"));
943 break;
944 case SR_STATE_RESUMING:
945 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_RESUMING\n"));
946 XenVbd_InitFromConfig(xvdd);
947 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
948 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
949 break;
950 case SR_STATE_RUNNING:
951 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
952 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
953 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
954 ScsiPortNotification(NextRequest, DeviceExtension);
955 default:
956 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
957 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
958 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
959 break;
960 }
961 KeMemoryBarrier();
962 }
964 if (xvdd->device_state->suspend_resume_state_fdo != SR_STATE_RUNNING)
965 {
966 return last_interrupt;
967 }
969 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
970 ScsiPortQuerySystemTime(&current_time);
971 #endif
973 while (more_to_do)
974 {
975 rp = xvdd->ring.sring->rsp_prod;
976 KeMemoryBarrier();
977 for (i = xvdd->ring.rsp_cons; i < rp; i++)
978 {
979 rep = XenVbd_GetResponse(xvdd, i);
980 /*
981 * This code is to automatically detect if the backend is using the same
982 * bit width or a different bit width to us. Later versions of Xen do this
983 * via a xenstore value, but not all. That 0x0fffffff (notice
984 * that the msb is not actually set, so we don't have any problems with
985 * sign extending) is to signify the last entry on the right, which is
986 * different under 32 and 64 bits, and that is why we set it up there.
988 * To do the detection, we put two initial entries on the ring, with an op
989 * of 0xff (which is invalid). The first entry is mostly okay, but the
990 * second will be grossly misaligned if the backend bit width is different,
991 * and we detect this and switch frontend structures.
992 */
993 switch (xvdd->ring_detect_state)
994 {
995 case RING_DETECT_STATE_NOT_STARTED:
996 KdPrint((__DRIVER_NAME " premature IRQ\n"));
997 break;
998 case RING_DETECT_STATE_DETECT1:
999 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));
1000 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));
1001 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT2;
1002 break;
1003 case RING_DETECT_STATE_DETECT2:
1004 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));
1005 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));
1006 *xvdd->event_channel_ptr |= 0x80000000;
1007 if (rep->operation != 0xff)
1009 KdPrint((__DRIVER_NAME " switching to 'other' ring size\n"));
1010 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
1011 xvdd->use_other = TRUE;
1012 *xvdd->event_channel_ptr |= 0x40000000;
1014 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
1015 ScsiPortNotification(NextRequest, DeviceExtension);
1016 break;
1017 case RING_DETECT_STATE_COMPLETE:
1018 shadow = &xvdd->shadows[rep->id & SHADOW_ID_ID_MASK];
1019 if (shadow->reset)
1021 KdPrint((__DRIVER_NAME " discarding reset shadow\n"));
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 != NULL);
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 (srb->SrbStatus == SRB_STATUS_SUCCESS && shadow->aligned_buffer_in_use)
1071 ASSERT(xvdd->aligned_buffer_in_use);
1072 xvdd->aligned_buffer_in_use = FALSE;
1073 if (decode_cdb_is_read(srb))
1074 memcpy(srb->DataBuffer, xvdd->aligned_buffer, block_count * 512);
1076 ScsiPortNotification(RequestComplete, xvdd, srb);
1079 for (j = 0; j < shadow->req.nr_segments; j++)
1081 if (dump_mode)
1083 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1084 shadow->req.seg[j].gref, TRUE, (ULONG)'SCSI');
1086 else
1088 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1089 shadow->req.seg[j].gref, FALSE, (ULONG)'SCSI');
1092 shadow->aligned_buffer_in_use = FALSE;
1093 shadow->reset = FALSE;
1094 shadow->srb = NULL;
1095 put_shadow_on_freelist(xvdd, shadow);
1096 break;
1100 xvdd->ring.rsp_cons = i;
1101 if (i != xvdd->ring.req_prod_pvt)
1103 RING_FINAL_CHECK_FOR_RESPONSES(&xvdd->ring, more_to_do);
1105 else
1107 xvdd->ring.sring->rsp_event = i + 1;
1108 more_to_do = FALSE;
1112 if (start_ring_detect_state == RING_DETECT_STATE_COMPLETE)
1113 XenVbd_PutQueuedSrbsOnRing(xvdd);
1115 if (suspend_resume_state_pdo == SR_STATE_SUSPENDING)
1117 if (xvdd->inactive || xvdd->shadow_free == SHADOW_ENTRIES)
1119 /* all entries are purged from the list (or we are inactive). ready to suspend */
1120 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
1121 KeMemoryBarrier();
1122 KdPrint((__DRIVER_NAME " Set fdo state SR_STATE_SUSPENDING\n"));
1123 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xvdd->device_state->pdo_event_channel));
1124 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
1126 FUNCTION_EXIT();
1129 return last_interrupt;
1132 static BOOLEAN DDKAPI
1133 XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK srb)
1135 PUCHAR data_buffer;
1136 //ULONG data_buffer_length;
1137 PCDB cdb;
1138 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1139 ULONG data_transfer_length = srb->DataTransferLength;
1141 if (xvdd->inactive)
1143 KdPrint((__DRIVER_NAME " Inactive srb->Function = %08X\n", srb->Function));
1144 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1145 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1146 ScsiPortNotification(NextRequest, DeviceExtension);
1147 return TRUE;
1150 // If we haven't enumerated all the devices yet then just defer the request
1151 if (xvdd->ring_detect_state < RING_DETECT_STATE_COMPLETE)
1153 srb->SrbStatus = SRB_STATUS_BUSY;
1154 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1155 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Still figuring out ring)\n"));
1156 return TRUE;
1159 if (xvdd->device_state->suspend_resume_state_pdo != SR_STATE_RUNNING)
1161 KdPrint((__DRIVER_NAME " --> HwScsiStartIo (Suspending/Resuming)\n"));
1162 srb->SrbStatus = SRB_STATUS_BUSY;
1163 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1164 KdPrint((__DRIVER_NAME " <-- HwScsiStartIo (Suspending/Resuming)\n"));
1165 return TRUE;
1168 if (srb->PathId != 0 || srb->TargetId != 0 || srb->Lun != 0)
1170 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1171 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1172 ScsiPortNotification(NextRequest, DeviceExtension);
1173 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Out of bounds)\n"));
1174 return TRUE;
1177 switch (srb->Function)
1179 case SRB_FUNCTION_EXECUTE_SCSI:
1180 cdb = (PCDB)srb->Cdb;
1182 switch(cdb->CDB6GENERIC.OperationCode)
1184 case SCSIOP_TEST_UNIT_READY:
1185 if (dump_mode)
1186 KdPrint((__DRIVER_NAME " Command = TEST_UNIT_READY\n"));
1187 srb->SrbStatus = SRB_STATUS_SUCCESS;
1188 srb->ScsiStatus = 0;
1189 break;
1190 case SCSIOP_INQUIRY:
1191 if (dump_mode)
1193 //PHYSICAL_ADDRESS physical;
1194 KdPrint((__DRIVER_NAME " Command = INQUIRY\n"));
1195 //KdPrint((__DRIVER_NAME " srb->Databuffer = %p\n", srb->DataBuffer));
1196 //physical = ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length);
1197 //KdPrint((__DRIVER_NAME " ScsiPortGetPhysicalAddress = %08x:%08x\n", physical.LowPart, physical.HighPart));
1199 // KdPrint((__DRIVER_NAME " (LUN = %d, EVPD = %d, Page Code = %02X)\n", srb->Cdb[1] >> 5, srb->Cdb[1] & 1, srb->Cdb[2]));
1200 // KdPrint((__DRIVER_NAME " (Length = %d)\n", srb->DataTransferLength));
1202 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1203 data_buffer = srb->DataBuffer;
1204 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1205 srb->SrbStatus = SRB_STATUS_SUCCESS;
1206 switch (xvdd->device_type)
1208 case XENVBD_DEVICETYPE_DISK:
1209 if ((srb->Cdb[1] & 1) == 0)
1211 if (srb->Cdb[2])
1213 srb->SrbStatus = SRB_STATUS_ERROR;
1215 else
1217 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1218 id->DeviceType = DIRECT_ACCESS_DEVICE;
1219 id->Versions = 4; /* minimum that WHQL says we must support */
1220 id->ResponseDataFormat = 2; /* not sure about this but WHQL complains otherwise */
1221 id->HiSupport = 1; /* WHQL test says we should set this */
1222 //id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1223 id->AdditionalLength = sizeof(INQUIRYDATA) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength) - 1;
1224 id->CommandQueue = 1;
1225 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1226 memcpy(id->ProductId, scsi_disk_model, 16); // product id
1227 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1228 data_transfer_length = sizeof(INQUIRYDATA);
1231 else
1233 switch (srb->Cdb[2])
1235 case VPD_SUPPORTED_PAGES: /* list of pages we support */
1236 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1237 data_buffer[1] = VPD_SUPPORTED_PAGES;
1238 data_buffer[2] = 0x00;
1239 data_buffer[3] = 2;
1240 data_buffer[4] = 0x00;
1241 data_buffer[5] = 0x80;
1242 data_transfer_length = 6;
1243 break;
1244 case VPD_SERIAL_NUMBER: /* serial number */
1245 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1246 data_buffer[1] = VPD_SERIAL_NUMBER;
1247 data_buffer[2] = 0x00;
1248 data_buffer[3] = 8;
1249 memset(&data_buffer[4], ' ', 8);
1250 data_transfer_length = 12;
1251 break;
1252 case VPD_DEVICE_IDENTIFIERS: /* identification - we don't support any so just return zero */
1253 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1254 data_buffer[1] = VPD_DEVICE_IDENTIFIERS;
1255 data_buffer[2] = 0x00;
1256 data_buffer[3] = 4 + (UCHAR)strlen(xvdd->vectors.path); /* length */
1257 data_buffer[4] = 2; /* ASCII */
1258 data_buffer[5] = 1; /* VendorId */
1259 data_buffer[6] = 0;
1260 data_buffer[7] = (UCHAR)strlen(xvdd->vectors.path);
1261 memcpy(&data_buffer[8], xvdd->vectors.path, strlen(xvdd->vectors.path));
1262 data_transfer_length = (ULONG)(8 + strlen(xvdd->vectors.path));
1263 break;
1264 default:
1265 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1266 srb->SrbStatus = SRB_STATUS_ERROR;
1267 break;
1270 break;
1271 case XENVBD_DEVICETYPE_CDROM:
1272 if ((srb->Cdb[1] & 1) == 0)
1274 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1275 id->DeviceType = READ_ONLY_DIRECT_ACCESS_DEVICE;
1276 id->RemovableMedia = 1;
1277 id->Versions = 3;
1278 id->ResponseDataFormat = 0;
1279 id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1280 id->CommandQueue = 1;
1281 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1282 memcpy(id->ProductId, scsi_cdrom_model, 16); // product id
1283 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1285 else
1287 switch (srb->Cdb[2])
1289 case 0x00:
1290 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1291 data_buffer[1] = 0x00;
1292 data_buffer[2] = 0x00;
1293 data_buffer[3] = 2;
1294 data_buffer[4] = 0x00;
1295 data_buffer[5] = 0x80;
1296 break;
1297 case 0x80:
1298 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1299 data_buffer[1] = 0x80;
1300 data_buffer[2] = 0x00;
1301 data_buffer[3] = 8;
1302 data_buffer[4] = 0x31;
1303 data_buffer[5] = 0x32;
1304 data_buffer[6] = 0x33;
1305 data_buffer[7] = 0x34;
1306 data_buffer[8] = 0x35;
1307 data_buffer[9] = 0x36;
1308 data_buffer[10] = 0x37;
1309 data_buffer[11] = 0x38;
1310 break;
1311 default:
1312 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1313 srb->SrbStatus = SRB_STATUS_ERROR;
1314 break;
1317 break;
1318 default:
1319 //KdPrint((__DRIVER_NAME " Unknown DeviceType %02x requested\n", xvdd->device_type));
1320 srb->SrbStatus = SRB_STATUS_ERROR;
1321 break;
1323 break;
1324 case SCSIOP_READ_CAPACITY:
1325 if (dump_mode)
1326 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1327 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1328 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1329 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1330 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1331 data_buffer = srb->DataBuffer;
1332 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1333 if ((xvdd->total_sectors - 1) >> 32)
1335 data_buffer[0] = 0xff;
1336 data_buffer[1] = 0xff;
1337 data_buffer[2] = 0xff;
1338 data_buffer[3] = 0xff;
1340 else
1342 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1343 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1344 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1345 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1347 data_buffer[4] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1348 data_buffer[5] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1349 data_buffer[6] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1350 data_buffer[7] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1351 srb->ScsiStatus = 0;
1352 srb->SrbStatus = SRB_STATUS_SUCCESS;
1353 break;
1354 case SCSIOP_READ_CAPACITY16:
1355 if (dump_mode)
1356 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1357 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1358 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1359 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1360 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1361 data_buffer = srb->DataBuffer;
1362 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1363 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 56) & 0xff;
1364 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 48) & 0xff;
1365 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 40) & 0xff;
1366 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 32) & 0xff;
1367 data_buffer[4] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1368 data_buffer[5] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1369 data_buffer[6] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1370 data_buffer[7] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1371 data_buffer[8] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1372 data_buffer[9] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1373 data_buffer[10] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1374 data_buffer[11] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1375 srb->ScsiStatus = 0;
1376 srb->SrbStatus = SRB_STATUS_SUCCESS;
1377 break;
1378 case SCSIOP_MODE_SENSE:
1379 case SCSIOP_MODE_SENSE10:
1380 if (dump_mode)
1381 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));
1382 XenVbd_FillModePage(xvdd, srb);
1383 break;
1384 case SCSIOP_READ:
1385 case SCSIOP_READ16:
1386 case SCSIOP_WRITE:
1387 case SCSIOP_WRITE16:
1388 XenVbd_PutSrbOnList(xvdd, srb);
1389 XenVbd_PutQueuedSrbsOnRing(xvdd);
1390 break;
1391 case SCSIOP_VERIFY:
1392 case SCSIOP_VERIFY16:
1393 // Should we do more here?
1394 if (dump_mode)
1395 KdPrint((__DRIVER_NAME " Command = VERIFY\n"));
1396 srb->SrbStatus = SRB_STATUS_SUCCESS;
1397 break;
1398 case SCSIOP_REPORT_LUNS:
1399 if (dump_mode)
1400 KdPrint((__DRIVER_NAME " Command = REPORT_LUNS\n"));
1401 srb->SrbStatus = SRB_STATUS_SUCCESS;;
1402 break;
1403 case SCSIOP_REQUEST_SENSE:
1404 if (dump_mode)
1405 KdPrint((__DRIVER_NAME " Command = REQUEST_SENSE\n"));
1406 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
1407 srb->SrbStatus = SRB_STATUS_SUCCESS;
1408 break;
1409 case SCSIOP_READ_TOC:
1410 if (dump_mode)
1411 KdPrint((__DRIVER_NAME " Command = READ_TOC\n"));
1412 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1413 data_buffer = srb->DataBuffer;
1414 // DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
1415 /*
1416 #define READ_TOC_FORMAT_TOC 0x00
1417 #define READ_TOC_FORMAT_SESSION 0x01
1418 #define READ_TOC_FORMAT_FULL_TOC 0x02
1419 #define READ_TOC_FORMAT_PMA 0x03
1420 #define READ_TOC_FORMAT_ATIP 0x04
1421 */
1422 // KdPrint((__DRIVER_NAME " Msf = %d\n", cdb->READ_TOC.Msf));
1423 // KdPrint((__DRIVER_NAME " LogicalUnitNumber = %d\n", cdb->READ_TOC.LogicalUnitNumber));
1424 // KdPrint((__DRIVER_NAME " Format2 = %d\n", cdb->READ_TOC.Format2));
1425 // KdPrint((__DRIVER_NAME " StartingTrack = %d\n", cdb->READ_TOC.StartingTrack));
1426 // KdPrint((__DRIVER_NAME " AllocationLength = %d\n", (cdb->READ_TOC.AllocationLength[0] << 8) | cdb->READ_TOC.AllocationLength[1]));
1427 // KdPrint((__DRIVER_NAME " Control = %d\n", cdb->READ_TOC.Control));
1428 // KdPrint((__DRIVER_NAME " Format = %d\n", cdb->READ_TOC.Format));
1429 switch (cdb->READ_TOC.Format2)
1431 case READ_TOC_FORMAT_TOC:
1432 data_buffer[0] = 0; // length MSB
1433 data_buffer[1] = 10; // length LSB
1434 data_buffer[2] = 1; // First Track
1435 data_buffer[3] = 1; // Last Track
1436 data_buffer[4] = 0; // Reserved
1437 data_buffer[5] = 0x14; // current position data + uninterrupted data
1438 data_buffer[6] = 1; // last complete track
1439 data_buffer[7] = 0; // reserved
1440 data_buffer[8] = 0; // MSB Block
1441 data_buffer[9] = 0;
1442 data_buffer[10] = 0;
1443 data_buffer[11] = 0; // LSB Block
1444 srb->SrbStatus = SRB_STATUS_SUCCESS;
1445 break;
1446 case READ_TOC_FORMAT_SESSION:
1447 case READ_TOC_FORMAT_FULL_TOC:
1448 case READ_TOC_FORMAT_PMA:
1449 case READ_TOC_FORMAT_ATIP:
1450 srb->SrbStatus = SRB_STATUS_ERROR;
1451 break;
1453 break;
1454 case SCSIOP_START_STOP_UNIT:
1455 KdPrint((__DRIVER_NAME " Command = SCSIOP_START_STOP_UNIT\n"));
1456 srb->SrbStatus = SRB_STATUS_SUCCESS;
1457 break;
1458 case SCSIOP_RESERVE_UNIT:
1459 KdPrint((__DRIVER_NAME " Command = SCSIOP_RESERVE_UNIT\n"));
1460 srb->SrbStatus = SRB_STATUS_SUCCESS;
1461 break;
1462 case SCSIOP_RELEASE_UNIT:
1463 KdPrint((__DRIVER_NAME " Command = SCSIOP_RELEASE_UNIT\n"));
1464 srb->SrbStatus = SRB_STATUS_SUCCESS;
1465 break;
1466 default:
1467 KdPrint((__DRIVER_NAME " Unhandled EXECUTE_SCSI Command = %02X\n", srb->Cdb[0]));
1468 srb->SrbStatus = SRB_STATUS_ERROR;
1469 break;
1471 if (srb->SrbStatus == SRB_STATUS_ERROR)
1473 KdPrint((__DRIVER_NAME " EXECUTE_SCSI Command = %02X returned error %02x\n", srb->Cdb[0], xvdd->last_sense_key));
1474 if (xvdd->last_sense_key == SCSI_SENSE_NO_SENSE)
1476 xvdd->last_sense_key = SCSI_SENSE_ILLEGAL_REQUEST;
1477 xvdd->last_additional_sense_code = SCSI_ADSENSE_INVALID_CDB;
1479 srb->ScsiStatus = 0x02;
1480 XenVbd_MakeAutoSense(xvdd, srb);
1481 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1482 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1484 ScsiPortNotification(NextRequest, DeviceExtension);
1487 else if (srb->SrbStatus != SRB_STATUS_PENDING)
1489 if (srb->SrbStatus == SRB_STATUS_SUCCESS && data_transfer_length < srb->DataTransferLength)
1491 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
1492 srb->DataTransferLength = data_transfer_length;
1494 xvdd->last_sense_key = SCSI_SENSE_NO_SENSE;
1495 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1496 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1497 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1499 ScsiPortNotification(NextRequest, DeviceExtension);
1502 break;
1503 case SRB_FUNCTION_IO_CONTROL:
1504 KdPrint((__DRIVER_NAME " SRB_FUNCTION_IO_CONTROL\n"));
1505 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1506 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1507 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1509 ScsiPortNotification(NextRequest, DeviceExtension);
1511 break;
1512 case SRB_FUNCTION_FLUSH:
1513 KdPrint((__DRIVER_NAME " SRB_FUNCTION_FLUSH %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1514 srb->SrbStatus = SRB_STATUS_SUCCESS;
1515 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1516 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1518 ScsiPortNotification(NextRequest, DeviceExtension);
1520 break;
1521 case SRB_FUNCTION_SHUTDOWN:
1522 KdPrint((__DRIVER_NAME " SRB_FUNCTION_SHUTDOWN %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1523 srb->SrbStatus = SRB_STATUS_SUCCESS;
1524 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1525 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1527 ScsiPortNotification(NextRequest, DeviceExtension);
1529 break;
1530 default:
1531 KdPrint((__DRIVER_NAME " Unhandled srb->Function = %08X\n", srb->Function));
1532 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1533 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1534 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1536 ScsiPortNotification(NextRequest, DeviceExtension);
1538 break;
1541 //FUNCTION_EXIT();
1542 return TRUE;
1545 static BOOLEAN
1546 XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId)
1548 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1549 srb_list_entry_t *srb_entry;
1550 PSCSI_REQUEST_BLOCK srb;
1551 int i;
1553 UNREFERENCED_PARAMETER(DeviceExtension);
1554 UNREFERENCED_PARAMETER(PathId);
1556 FUNCTION_ENTER();
1558 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1560 if (xvdd->ring_detect_state == RING_DETECT_STATE_COMPLETE && xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1562 while((srb_entry = (srb_list_entry_t *)RemoveHeadList(&xvdd->srb_list)) != (srb_list_entry_t *)&xvdd->srb_list)
1564 srb = srb_entry->srb;
1565 srb->SrbStatus = SRB_STATUS_BUS_RESET;
1566 KdPrint((__DRIVER_NAME " completing queued SRB %p with status SRB_STATUS_BUS_RESET\n", srb));
1567 ScsiPortNotification(RequestComplete, xvdd, srb);
1570 for (i = 0; i < MAX_SHADOW_ENTRIES; i++)
1572 if (xvdd->shadows[i].srb)
1574 KdPrint((__DRIVER_NAME " Completing in-flight srb %p with status SRB_STATUS_BUS_RESET\n", xvdd->shadows[i].srb));
1575 /* set reset here so that the interrupt won't do anything with the srb but will dispose of the shadow entry correctly */
1576 xvdd->shadows[i].reset = TRUE;
1577 xvdd->shadows[i].srb->SrbStatus = SRB_STATUS_BUS_RESET;
1578 ScsiPortNotification(RequestComplete, xvdd, xvdd->shadows[i].srb);
1579 xvdd->shadows[i].srb = NULL;
1583 /* send a notify to Dom0 just in case it was missed for some reason (which should _never_ happen) */
1584 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
1586 ScsiPortNotification(NextRequest, DeviceExtension);
1589 FUNCTION_EXIT();
1591 return TRUE;
1594 static BOOLEAN
1595 XenVbd_HwScsiAdapterState(PVOID DeviceExtension, PVOID Context, BOOLEAN SaveState)
1597 UNREFERENCED_PARAMETER(DeviceExtension);
1598 UNREFERENCED_PARAMETER(Context);
1599 UNREFERENCED_PARAMETER(SaveState);
1601 FUNCTION_ENTER();
1602 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1604 FUNCTION_EXIT();
1606 return TRUE;
1609 static SCSI_ADAPTER_CONTROL_STATUS DDKAPI
1610 XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters)
1612 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1613 SCSI_ADAPTER_CONTROL_STATUS Status = ScsiAdapterControlSuccess;
1614 PSCSI_SUPPORTED_CONTROL_TYPE_LIST SupportedControlTypeList;
1615 //KIRQL OldIrql;
1617 FUNCTION_ENTER();
1618 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1619 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
1621 switch (ControlType)
1623 case ScsiQuerySupportedControlTypes:
1624 SupportedControlTypeList = (PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters;
1625 KdPrint((__DRIVER_NAME " ScsiQuerySupportedControlTypes (Max = %d)\n", SupportedControlTypeList->MaxControlType));
1626 SupportedControlTypeList->SupportedTypeList[ScsiQuerySupportedControlTypes] = TRUE;
1627 SupportedControlTypeList->SupportedTypeList[ScsiStopAdapter] = TRUE;
1628 SupportedControlTypeList->SupportedTypeList[ScsiRestartAdapter] = TRUE;
1629 break;
1630 case ScsiStopAdapter:
1631 KdPrint((__DRIVER_NAME " ScsiStopAdapter\n"));
1632 /* I don't think we actually have to do anything here... xenpci cleans up all the xenbus stuff for us */
1633 break;
1634 case ScsiRestartAdapter:
1635 KdPrint((__DRIVER_NAME " ScsiRestartAdapter\n"));
1636 if (!xvdd->inactive)
1638 if (XenVbd_InitFromConfig(xvdd) != SP_RETURN_FOUND)
1639 KeBugCheckEx(DATA_COHERENCY_EXCEPTION, 0, (ULONG_PTR) xvdd, 0, 0);
1640 XenVbd_StartRingDetection(xvdd);
1642 break;
1643 case ScsiSetBootConfig:
1644 KdPrint((__DRIVER_NAME " ScsiSetBootConfig\n"));
1645 break;
1646 case ScsiSetRunningConfig:
1647 KdPrint((__DRIVER_NAME " ScsiSetRunningConfig\n"));
1648 break;
1649 default:
1650 KdPrint((__DRIVER_NAME " UNKNOWN\n"));
1651 break;
1654 FUNCTION_EXIT();
1656 return Status;
1659 NTSTATUS
1660 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
1662 ULONG status;
1663 HW_INITIALIZATION_DATA HwInitializationData;
1664 PVOID driver_extension;
1665 PUCHAR ptr;
1666 OBJECT_ATTRIBUTES oa;
1667 HANDLE service_handle;
1668 UNICODE_STRING param_name;
1669 HANDLE param_handle;
1670 UNICODE_STRING value_name;
1671 CHAR buf[256];
1672 ULONG buf_len;
1673 PKEY_VALUE_PARTIAL_INFORMATION kpv;
1675 FUNCTION_ENTER();
1676 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1677 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
1679 /* RegistryPath == NULL when we are invoked as a crash dump driver */
1680 if (!RegistryPath)
1682 dump_mode = TRUE;
1685 if (!dump_mode)
1687 IoAllocateDriverObjectExtension(DriverObject, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC), PAGE_SIZE, &driver_extension);
1688 ptr = driver_extension;
1689 //ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
1690 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "ring-ref", NULL, NULL);
1691 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_IRQ, "event-channel", NULL, NULL);
1692 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_FRONT, "device-type", NULL, NULL);
1693 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mode", NULL, NULL);
1694 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sectors", NULL, NULL);
1695 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sector-size", NULL, NULL);
1696 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 */
1697 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT, NULL, NULL, NULL);
1698 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1699 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1700 __ADD_XEN_INIT_UCHAR(&ptr, 20);
1701 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1702 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT, NULL, NULL, NULL);
1703 //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1704 //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1705 //__ADD_XEN_INIT_UCHAR(&ptr, 20);
1706 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1707 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN, NULL, NULL, NULL);
1708 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
1709 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
1710 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1711 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
1712 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
1713 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1714 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitialising);
1715 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait);
1716 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1717 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1719 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1721 InitializeObjectAttributes(&oa, RegistryPath, OBJ_CASE_INSENSITIVE, NULL, NULL);
1722 status = ZwOpenKey(&service_handle, KEY_READ, &oa);
1723 if(!NT_SUCCESS(status))
1725 KdPrint((__DRIVER_NAME " ZwOpenKey(Service) returned %08x\n", status));
1727 else
1729 RtlInitUnicodeString(&param_name, L"Parameters");
1730 InitializeObjectAttributes(&oa, &param_name, OBJ_CASE_INSENSITIVE, service_handle, NULL);
1731 status = ZwOpenKey(&param_handle, KEY_READ, &oa);
1732 if(!NT_SUCCESS(status))
1734 KdPrint((__DRIVER_NAME " ZwOpenKey(Parameters) returned %08x\n", status));
1736 else
1738 kpv = (PKEY_VALUE_PARTIAL_INFORMATION)buf;
1739 RtlFillMemory(scsi_device_manufacturer, 8, ' ');
1740 RtlFillMemory(scsi_disk_model, 16, ' ');
1741 RtlFillMemory(scsi_cdrom_model, 16, ' ');
1743 RtlInitUnicodeString(&value_name, L"Manufacturer");
1744 buf_len = 256;
1745 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1746 if(NT_SUCCESS(status))
1747 wcstombs(scsi_device_manufacturer, (PWCHAR)kpv->Data, min(kpv->DataLength, 8));
1748 else
1749 RtlStringCbCopyA(scsi_device_manufacturer, 8, "XEN ");
1751 RtlInitUnicodeString(&value_name, L"Disk_Model");
1752 buf_len = 256;
1753 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1754 if(NT_SUCCESS(status))
1755 wcstombs(scsi_disk_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1756 else
1757 RtlStringCbCopyA(scsi_disk_model, 16, "PV DISK ");
1759 RtlInitUnicodeString(&value_name, L"CDROM_Model");
1760 buf_len = 256;
1761 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1762 if(NT_SUCCESS(status))
1763 wcstombs(scsi_cdrom_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1764 else
1765 RtlStringCbCopyA(scsi_cdrom_model, 16, "PV CDROM ");
1766 ZwClose(param_handle);
1768 ZwClose(service_handle);
1772 RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
1774 HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
1775 HwInitializationData.AdapterInterfaceType = PNPBus;
1776 if (!dump_mode)
1777 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_DEVICE_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE;
1778 else
1779 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_DEVICE_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE_DUMP_MODE;
1780 HwInitializationData.SpecificLuExtensionSize = 0;
1781 HwInitializationData.SrbExtensionSize = sizeof(srb_list_entry_t);
1782 HwInitializationData.NumberOfAccessRanges = 1;
1783 HwInitializationData.MapBuffers = TRUE;
1784 HwInitializationData.NeedPhysicalAddresses = FALSE;
1785 HwInitializationData.TaggedQueuing = TRUE;
1786 HwInitializationData.AutoRequestSense = TRUE;
1787 HwInitializationData.MultipleRequestPerLu = TRUE;
1788 HwInitializationData.ReceiveEvent = FALSE;
1789 HwInitializationData.VendorIdLength = 0;
1790 HwInitializationData.VendorId = NULL;
1791 HwInitializationData.DeviceIdLength = 0;
1792 HwInitializationData.DeviceId = NULL;
1794 HwInitializationData.HwInitialize = XenVbd_HwScsiInitialize;
1795 HwInitializationData.HwStartIo = XenVbd_HwScsiStartIo;
1796 HwInitializationData.HwInterrupt = XenVbd_HwScsiInterrupt;
1797 HwInitializationData.HwFindAdapter = XenVbd_HwScsiFindAdapter;
1798 HwInitializationData.HwResetBus = XenVbd_HwScsiResetBus;
1799 HwInitializationData.HwDmaStarted = NULL;
1800 HwInitializationData.HwAdapterState = NULL; //XenVbd_HwScsiAdapterState;
1801 HwInitializationData.HwAdapterControl = XenVbd_HwScsiAdapterControl;
1803 status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
1805 if(!NT_SUCCESS(status))
1807 KdPrint((__DRIVER_NAME " ScsiPortInitialize failed with status 0x%08x\n", status));
1810 FUNCTION_EXIT();
1812 return status;