win-pvdrivers

view xenvbd/xenvbd_scsiport.c @ 888:33b9eccb6301

clean up scsiport vbd driver reset code
author James Harper <james.harper@bendigoit.com.au>
date Thu Mar 17 00:04:06 2011 +1100 (2011-03-17)
parents 5020c1c55f95
children 60e760e70c21
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 }
285 }
287 return SP_RETURN_FOUND;
288 }
290 static __inline ULONG
291 decode_cdb_length(PSCSI_REQUEST_BLOCK srb)
292 {
293 switch (srb->Cdb[0])
294 {
295 case SCSIOP_READ:
296 case SCSIOP_WRITE:
297 return ((ULONG)(UCHAR)srb->Cdb[7] << 8) | (ULONG)(UCHAR)srb->Cdb[8];
298 case SCSIOP_READ16:
299 case SCSIOP_WRITE16:
300 return ((ULONG)(UCHAR)srb->Cdb[10] << 24) | ((ULONG)(UCHAR)srb->Cdb[11] << 16) | ((ULONG)(UCHAR)srb->Cdb[12] << 8) | (ULONG)(UCHAR)srb->Cdb[13];
301 default:
302 return 0;
303 }
304 }
306 static __inline ULONGLONG
307 decode_cdb_sector(PSCSI_REQUEST_BLOCK srb)
308 {
309 ULONGLONG sector;
311 switch (srb->Cdb[0])
312 {
313 case SCSIOP_READ:
314 case SCSIOP_WRITE:
315 sector = ((ULONG)(UCHAR)srb->Cdb[2] << 24) | ((ULONG)(UCHAR)srb->Cdb[3] << 16) | ((ULONG)(UCHAR)srb->Cdb[4] << 8) | (ULONG)(UCHAR)srb->Cdb[5];
316 break;
317 case SCSIOP_READ16:
318 case SCSIOP_WRITE16:
319 sector = ((ULONGLONG)(UCHAR)srb->Cdb[2] << 56) | ((ULONGLONG)(UCHAR)srb->Cdb[3] << 48)
320 | ((ULONGLONG)(UCHAR)srb->Cdb[4] << 40) | ((ULONGLONG)(UCHAR)srb->Cdb[5] << 32)
321 | ((ULONGLONG)(UCHAR)srb->Cdb[6] << 24) | ((ULONGLONG)(UCHAR)srb->Cdb[7] << 16)
322 | ((ULONGLONG)(UCHAR)srb->Cdb[8] << 8) | ((ULONGLONG)(UCHAR)srb->Cdb[9]);
323 //KdPrint((__DRIVER_NAME " sector_number = %d (high) %d (low)\n", (ULONG)(sector >> 32), (ULONG)sector));
324 break;
325 default:
326 sector = 0;
327 break;
328 }
329 return sector;
330 }
332 static __inline BOOLEAN
333 decode_cdb_is_read(PSCSI_REQUEST_BLOCK srb)
334 {
335 switch (srb->Cdb[0])
336 {
337 case SCSIOP_READ:
338 case SCSIOP_READ16:
339 return TRUE;
340 case SCSIOP_WRITE:
341 case SCSIOP_WRITE16:
342 return FALSE;
343 default:
344 return FALSE;
345 }
346 }
348 ULONG max_dump_mode_blocks = 0;
349 ULONG max_dump_mode_length = 0;
351 static VOID
352 XenVbd_PutSrbOnList(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
353 {
354 srb_list_entry_t *list_entry = srb->SrbExtension;
355 list_entry->srb = srb;
356 InsertTailList(&xvdd->srb_list, (PLIST_ENTRY)list_entry);
357 }
359 static VOID
360 XenVbd_PutQueuedSrbsOnRing(PXENVBD_DEVICE_DATA xvdd)
361 {
362 PSCSI_REQUEST_BLOCK srb;
363 srb_list_entry_t *srb_entry;
364 ULONGLONG sector_number;
365 ULONG block_count;
366 blkif_shadow_t *shadow;
367 ULONG remaining, offset, length;
368 grant_ref_t gref;
369 PUCHAR ptr;
370 int notify;
371 int i;
372 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
373 LARGE_INTEGER current_time;
374 #endif
376 //FUNCTION_ENTER();
378 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
379 ScsiPortQuerySystemTime(&current_time);
380 #endif
382 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)
383 {
384 srb = srb_entry->srb;
385 block_count = decode_cdb_length(srb);;
386 block_count *= xvdd->bytes_per_sector / 512;
387 sector_number = decode_cdb_sector(srb);
388 sector_number *= xvdd->bytes_per_sector / 512;
390 /* look for pending writes that overlap this one */
391 /* we get warnings from drbd if we don't */
392 for (i = 0; i < MAX_SHADOW_ENTRIES; i++)
393 {
394 PSCSI_REQUEST_BLOCK srb2;
395 ULONGLONG sector_number2;
396 ULONG block_count2;
398 srb2 = xvdd->shadows[i].srb;
399 if (!srb2)
400 continue;
401 if (decode_cdb_is_read(srb2))
402 continue;
403 block_count2 = decode_cdb_length(srb2);;
404 block_count2 *= xvdd->bytes_per_sector / 512;
405 sector_number2 = decode_cdb_sector(srb2);
406 sector_number2 *= xvdd->bytes_per_sector / 512;
408 if (sector_number < sector_number2 && sector_number + block_count <= sector_number2)
409 continue;
410 if (sector_number2 < sector_number && sector_number2 + block_count2 <= sector_number)
411 continue;
413 #if 0
414 /* check if the data being written is identical to the data in the pipe */
415 {
416 PUCHAR buf, buf2;
417 ULONG byte_count;
418 int j;
420 buf = (PUCHAR)srb->DataBuffer + (max(sector_number, sector_number2) - sector_number) * xvdd->bytes_per_sector;
421 buf2 = (PUCHAR)srb2->DataBuffer + (max(sector_number, sector_number2) - sector_number2) * xvdd->bytes_per_sector;
422 byte_count = (ULONG)(min(sector_number + block_count, sector_number2 + block_count2) - max(sector_number, sector_number2)) * xvdd->bytes_per_sector;
423 for (j = 0; j < (int)byte_count; j++)
424 {
425 if (buf[j] != buf2[j])
426 break;
427 }
428 }
429 #endif
431 KdPrint((__DRIVER_NAME " Concurrent outstanding write detected (%I64d, %d) (%I64d, %d)\n",
432 sector_number, block_count, sector_number2, block_count2));
433 /* put the srb back at the start of the queue */
434 InsertHeadList(&xvdd->srb_list, (PLIST_ENTRY)srb->SrbExtension);
435 break;
436 }
437 if (i < MAX_SHADOW_ENTRIES)
438 break; /* stall the queue but fall through so the notify is triggered */
440 remaining = block_count * 512;
441 shadow = get_shadow_from_freelist(xvdd);
442 ASSERT(shadow);
443 ASSERT(!shadow->aligned_buffer_in_use);
444 ASSERT(!shadow->srb);
445 shadow->reset = FALSE;
446 shadow->req.sector_number = sector_number;
447 shadow->req.handle = 0;
448 shadow->req.operation = decode_cdb_is_read(srb)?BLKIF_OP_READ:BLKIF_OP_WRITE;
449 shadow->req.nr_segments = 0;
450 shadow->srb = srb;
452 if ((ULONG_PTR)srb->DataBuffer & 511)
453 {
454 if (dump_mode)
455 KdPrint((__DRIVER_NAME " unaligned dump mode buffer = %d bytes\n", block_count * 512));
456 ASSERT(!dump_mode || block_count * 512 < BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE * PAGE_SIZE);
457 xvdd->aligned_buffer_in_use = TRUE;
458 ptr = xvdd->aligned_buffer;
459 if (!decode_cdb_is_read(srb))
460 memcpy(ptr, srb->DataBuffer, block_count * 512);
461 shadow->aligned_buffer_in_use = TRUE;
462 }
463 else
464 {
465 ptr = srb->DataBuffer;
466 shadow->aligned_buffer_in_use = FALSE;
467 }
469 if (dump_mode && block_count > max_dump_mode_blocks)
470 {
471 max_dump_mode_blocks = block_count;
472 KdPrint((__DRIVER_NAME " max_dump_mode_blocks = %d\n", max_dump_mode_blocks));
473 }
474 if (dump_mode && srb->DataTransferLength > max_dump_mode_length)
475 {
476 max_dump_mode_length = srb->DataTransferLength;
477 KdPrint((__DRIVER_NAME " max_dump_mode_length = %d\n", max_dump_mode_length));
478 }
480 //KdPrint((__DRIVER_NAME " sector_number = %d, block_count = %d\n", (ULONG)shadow->req.sector_number, block_count));
481 //KdPrint((__DRIVER_NAME " SrbExtension = %p\n", srb->SrbExtension));
482 //KdPrint((__DRIVER_NAME " DataBuffer = %p\n", srb->DataBuffer));
484 //KdPrint((__DRIVER_NAME " sector_number = %d\n", (ULONG)shadow->req.sector_number));
485 //KdPrint((__DRIVER_NAME " handle = %d\n", shadow->req.handle));
486 //KdPrint((__DRIVER_NAME " operation = %d\n", shadow->req.operation));
488 while (remaining > 0)
489 {
490 PHYSICAL_ADDRESS physical_address = MmGetPhysicalAddress(ptr);
492 if (dump_mode)
493 {
494 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
495 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE,
496 xvdd->dump_grant_refs[shadow->req.nr_segments], (ULONG)'SCSI');
497 }
498 else
499 {
500 gref = xvdd->vectors.GntTbl_GrantAccess(xvdd->vectors.context, 0,
501 (ULONG)(physical_address.QuadPart >> PAGE_SHIFT), FALSE, INVALID_GRANT_REF, (ULONG)'SCSI');
502 }
503 if (gref == INVALID_GRANT_REF)
504 {
505 ULONG i;
506 for (i = 0; i < shadow->req.nr_segments; i++)
507 {
508 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
509 shadow->req.seg[i].gref, FALSE, (ULONG)'SCSI');
510 }
511 if (shadow->aligned_buffer_in_use)
512 {
513 shadow->aligned_buffer_in_use = FALSE;
514 xvdd->aligned_buffer_in_use = FALSE;
515 }
516 /* put the srb back at the start of the queue, then fall through so that the notify is triggered*/
517 InsertHeadList(&xvdd->srb_list, (PLIST_ENTRY)srb->SrbExtension);
518 put_shadow_on_freelist(xvdd, shadow);
519 KdPrint((__DRIVER_NAME " Out of gref's. Deferring\n"));
520 break;
521 }
522 offset = physical_address.LowPart & (PAGE_SIZE - 1);
523 length = min(PAGE_SIZE - offset, remaining);
524 ASSERT((offset & 511) == 0);
525 ASSERT((length & 511) == 0);
526 ASSERT(offset + length <= PAGE_SIZE);
527 shadow->req.seg[shadow->req.nr_segments].gref = gref;
528 shadow->req.seg[shadow->req.nr_segments].first_sect = (UCHAR)(offset >> 9);
529 shadow->req.seg[shadow->req.nr_segments].last_sect = (UCHAR)(((offset + length) >> 9) - 1);
530 remaining -= length;
531 ptr += length;
532 shadow->req.nr_segments++;
533 }
535 //KdPrint((__DRIVER_NAME " nr_segments = %d\n", shadow->req.nr_segments));
537 XenVbd_PutRequest(xvdd, &shadow->req);
538 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
539 shadow->ring_submit_time = current_time;
540 #endif
542 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
543 if (notify)
544 {
545 //KdPrint((__DRIVER_NAME " Notifying\n"));
546 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
547 }
548 }
549 if (xvdd->shadow_free && !xvdd->aligned_buffer_in_use)
550 {
551 ScsiPortNotification(NextLuRequest, xvdd, 0, 0, 0);
552 }
553 //FUNCTION_EXIT();
554 }
556 static ULONG DDKAPI
557 XenVbd_HwScsiFindAdapter(PVOID DeviceExtension, PVOID HwContext, PVOID BusInformation, PCHAR ArgumentString, PPORT_CONFIGURATION_INFORMATION ConfigInfo, PBOOLEAN Again)
558 {
559 // PACCESS_RANGE AccessRange;
560 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
561 ULONG status;
562 // PXENPCI_XEN_DEVICE_DATA XenDeviceData;
563 PACCESS_RANGE access_range;
565 UNREFERENCED_PARAMETER(HwContext);
566 UNREFERENCED_PARAMETER(BusInformation);
567 UNREFERENCED_PARAMETER(ArgumentString);
569 FUNCTION_ENTER();
570 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
571 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
573 RtlZeroMemory(xvdd, sizeof(XENVBD_DEVICE_DATA));
574 *Again = FALSE;
576 KdPrint((__DRIVER_NAME " BusInterruptLevel = %d\n", ConfigInfo->BusInterruptLevel));
577 KdPrint((__DRIVER_NAME " BusInterruptVector = %03x\n", ConfigInfo->BusInterruptVector));
579 KdPrint((__DRIVER_NAME " NumberOfAccessRanges = %d\n", ConfigInfo->NumberOfAccessRanges));
580 if (ConfigInfo->NumberOfAccessRanges != 1 && ConfigInfo->NumberOfAccessRanges != 2)
581 {
582 return SP_RETURN_BAD_CONFIG;
583 }
585 access_range = &((*(ConfigInfo->AccessRanges))[0]);
586 KdPrint((__DRIVER_NAME " RangeStart = %08x, RangeLength = %08x\n",
587 access_range->RangeStart.LowPart, access_range->RangeLength));
588 xvdd->device_base = ScsiPortGetDeviceBase(
589 DeviceExtension,
590 ConfigInfo->AdapterInterfaceType,
591 ConfigInfo->SystemIoBusNumber,
592 access_range->RangeStart,
593 access_range->RangeLength,
594 !access_range->RangeInMemory);
595 if (!xvdd->device_base)
596 {
597 KdPrint((__DRIVER_NAME " Invalid config\n"));
598 FUNCTION_EXIT();
599 return SP_RETURN_BAD_CONFIG;
600 }
602 status = XenVbd_InitFromConfig(xvdd);
603 if (status != SP_RETURN_FOUND)
604 {
605 FUNCTION_EXIT();
606 return status;
607 }
609 xvdd->aligned_buffer_in_use = FALSE;
610 /* align the buffer to PAGE_SIZE */
611 xvdd->aligned_buffer = (PVOID)((ULONG_PTR)((PUCHAR)xvdd->aligned_buffer_data + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
612 KdPrint((__DRIVER_NAME " aligned_buffer_data = %p\n", xvdd->aligned_buffer_data));
613 KdPrint((__DRIVER_NAME " aligned_buffer = %p\n", xvdd->aligned_buffer));
615 if (!dump_mode)
616 {
617 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
618 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST - 1;
619 //ConfigInfo->ScatterGather = TRUE;
620 }
621 else
622 {
623 ConfigInfo->MaximumTransferLength = BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE * PAGE_SIZE;
624 ConfigInfo->NumberOfPhysicalBreaks = BLKIF_MAX_SEGMENTS_PER_REQUEST_DUMP_MODE - 1;
625 //ConfigInfo->ScatterGather = FALSE;
626 }
627 KdPrint((__DRIVER_NAME " ConfigInfo->MaximumTransferLength = %d\n", ConfigInfo->MaximumTransferLength));
628 KdPrint((__DRIVER_NAME " ConfigInfo->NumberOfPhysicalBreaks = %d\n", ConfigInfo->NumberOfPhysicalBreaks));
629 ConfigInfo->ScatterGather = FALSE;
630 ConfigInfo->AlignmentMask = 0;
631 ConfigInfo->NumberOfBuses = 1;
632 ConfigInfo->InitiatorBusId[0] = 1;
633 ConfigInfo->MaximumNumberOfLogicalUnits = 1;
634 ConfigInfo->MaximumNumberOfTargets = 2;
635 KdPrint((__DRIVER_NAME " ConfigInfo->CachesData was initialised to %d\n", ConfigInfo->CachesData));
636 ConfigInfo->CachesData = FALSE;
637 ConfigInfo->BufferAccessScsiPortControlled = FALSE;
638 if (ConfigInfo->Dma64BitAddresses == SCSI_DMA64_SYSTEM_SUPPORTED)
639 {
640 ConfigInfo->Master = TRUE;
641 ConfigInfo->Dma64BitAddresses = SCSI_DMA64_MINIPORT_SUPPORTED;
642 ConfigInfo->Dma32BitAddresses = FALSE;
643 KdPrint((__DRIVER_NAME " Dma64BitAddresses supported\n"));
644 }
645 else
646 {
647 ConfigInfo->Master = TRUE;
648 ConfigInfo->Dma32BitAddresses = TRUE;
649 KdPrint((__DRIVER_NAME " Dma64BitAddresses not supported\n"));
650 }
652 FUNCTION_EXIT();
654 return SP_RETURN_FOUND;
655 }
657 static VOID
658 XenVbd_StartRingDetection(PXENVBD_DEVICE_DATA xvdd)
659 {
660 blkif_request_t *req;
661 int notify;
663 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT1;
664 RtlZeroMemory(xvdd->sring->ring, PAGE_SIZE - FIELD_OFFSET(blkif_sring_t, ring));
665 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
666 req->operation = 0xff;
667 xvdd->ring.req_prod_pvt++;
668 req = RING_GET_REQUEST(&xvdd->ring, xvdd->ring.req_prod_pvt);
669 req->operation = 0xff;
670 xvdd->ring.req_prod_pvt++;
672 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xvdd->ring, notify);
673 if (notify)
674 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
675 }
677 static BOOLEAN
678 XenVbd_HwScsiInitialize(PVOID DeviceExtension)
679 {
680 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
682 FUNCTION_ENTER();
683 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
684 KdPrint((__DRIVER_NAME " dump_mode = %d\n", dump_mode));
686 if (!xvdd->inactive)
687 {
688 if (!dump_mode)
689 {
690 XenVbd_StartRingDetection(xvdd);
691 }
692 else
693 {
694 if (xvdd->cached_use_other)
695 {
696 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
697 xvdd->use_other = TRUE;
698 }
699 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
700 }
701 InitializeListHead(&xvdd->srb_list);
702 }
703 FUNCTION_EXIT();
705 return TRUE;
706 }
708 static ULONG
709 XenVbd_FillModePage(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
710 {
711 PMODE_PARAMETER_HEADER parameter_header = NULL;
712 PMODE_PARAMETER_HEADER10 parameter_header10 = NULL;
713 PMODE_PARAMETER_BLOCK param_block;
714 PMODE_FORMAT_PAGE format_page;
715 ULONG offset = 0;
716 UCHAR buffer[1024];
717 BOOLEAN valid_page = FALSE;
718 BOOLEAN cdb_llbaa;
719 BOOLEAN cdb_dbd;
720 UCHAR cdb_page_code;
721 USHORT cdb_allocation_length;
723 UNREFERENCED_PARAMETER(xvdd);
725 RtlZeroMemory(srb->DataBuffer, srb->DataTransferLength);
726 RtlZeroMemory(buffer, ARRAY_SIZE(buffer));
727 offset = 0;
729 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
731 switch (srb->Cdb[0])
732 {
733 case SCSIOP_MODE_SENSE:
734 cdb_llbaa = FALSE;
735 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
736 cdb_page_code = srb->Cdb[2] & 0x3f;
737 cdb_allocation_length = srb->Cdb[4];
738 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
739 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
740 parameter_header = (PMODE_PARAMETER_HEADER)&buffer[offset];
741 parameter_header->MediumType = 0;
742 parameter_header->DeviceSpecificParameter = 0;
743 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
744 {
745 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
746 parameter_header->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
747 }
748 offset += sizeof(MODE_PARAMETER_HEADER);
749 break;
750 case SCSIOP_MODE_SENSE10:
751 cdb_llbaa = (BOOLEAN)!!(srb->Cdb[1] & 16);
752 cdb_dbd = (BOOLEAN)!!(srb->Cdb[1] & 8);
753 cdb_page_code = srb->Cdb[2] & 0x3f;
754 cdb_allocation_length = (srb->Cdb[7] << 8) | srb->Cdb[8];
755 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE10 llbaa = %d, dbd = %d, page_code = %d, allocation_length = %d\n",
756 cdb_llbaa, cdb_dbd, cdb_page_code, cdb_allocation_length));
757 parameter_header10 = (PMODE_PARAMETER_HEADER10)&buffer[offset];
758 parameter_header10->MediumType = 0;
759 parameter_header10->DeviceSpecificParameter = 0;
760 if (xvdd->device_mode == XENVBD_DEVICEMODE_READ)
761 {
762 KdPrint((__DRIVER_NAME " Mode sense to a read only disk.\n"));
763 parameter_header10->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
764 }
765 offset += sizeof(MODE_PARAMETER_HEADER10);
766 break;
767 default:
768 KdPrint((__DRIVER_NAME " SCSIOP_MODE_SENSE_WTF (%02x)\n", (ULONG)srb->Cdb[0]));
769 return FALSE;
770 }
772 if (!cdb_dbd)
773 {
774 param_block = (PMODE_PARAMETER_BLOCK)&buffer[offset];
775 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK)
776 {
777 if (xvdd->total_sectors >> 32)
778 {
779 param_block->DensityCode = 0xff;
780 param_block->NumberOfBlocks[0] = 0xff;
781 param_block->NumberOfBlocks[1] = 0xff;
782 param_block->NumberOfBlocks[2] = 0xff;
783 }
784 else
785 {
786 param_block->DensityCode = (UCHAR)((xvdd->total_sectors >> 24) & 0xff);
787 param_block->NumberOfBlocks[0] = (UCHAR)((xvdd->total_sectors >> 16) & 0xff);
788 param_block->NumberOfBlocks[1] = (UCHAR)((xvdd->total_sectors >> 8) & 0xff);
789 param_block->NumberOfBlocks[2] = (UCHAR)((xvdd->total_sectors >> 0) & 0xff);
790 }
791 param_block->BlockLength[0] = (UCHAR)((xvdd->bytes_per_sector >> 16) & 0xff);
792 param_block->BlockLength[1] = (UCHAR)((xvdd->bytes_per_sector >> 8) & 0xff);
793 param_block->BlockLength[2] = (UCHAR)((xvdd->bytes_per_sector >> 0) & 0xff);
794 }
795 offset += sizeof(MODE_PARAMETER_BLOCK);
796 }
797 switch (srb->Cdb[0])
798 {
799 case SCSIOP_MODE_SENSE:
800 parameter_header->BlockDescriptorLength = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER));
801 break;
802 case SCSIOP_MODE_SENSE10:
803 parameter_header10->BlockDescriptorLength[0] = (UCHAR)((offset - sizeof(MODE_PARAMETER_HEADER10)) >> 8);
804 parameter_header10->BlockDescriptorLength[1] = (UCHAR)(offset - sizeof(MODE_PARAMETER_HEADER10));
805 break;
806 }
807 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_FORMAT_DEVICE || cdb_page_code == MODE_SENSE_RETURN_ALL))
808 {
809 valid_page = TRUE;
810 format_page = (PMODE_FORMAT_PAGE)&buffer[offset];
811 format_page->PageCode = MODE_PAGE_FORMAT_DEVICE;
812 format_page->PageLength = sizeof(MODE_FORMAT_PAGE) - FIELD_OFFSET(MODE_FORMAT_PAGE, PageLength);
813 /* 256 sectors per track */
814 format_page->SectorsPerTrack[0] = 0x01;
815 format_page->SectorsPerTrack[1] = 0x00;
816 /* xxx bytes per sector */
817 format_page->BytesPerPhysicalSector[0] = (UCHAR)(xvdd->bytes_per_sector >> 8);
818 format_page->BytesPerPhysicalSector[1] = (UCHAR)(xvdd->bytes_per_sector & 0xff);
819 format_page->HardSectorFormating = TRUE;
820 format_page->SoftSectorFormating = TRUE;
821 offset += sizeof(MODE_FORMAT_PAGE);
822 }
823 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_CACHING || cdb_page_code == MODE_SENSE_RETURN_ALL))
824 {
825 PMODE_CACHING_PAGE caching_page;
826 valid_page = TRUE;
827 caching_page = (PMODE_CACHING_PAGE)&buffer[offset];
828 caching_page->PageCode = MODE_PAGE_CACHING;
829 caching_page->PageLength = sizeof(MODE_CACHING_PAGE) - FIELD_OFFSET(MODE_CACHING_PAGE, PageLength);
830 // caching_page-> // all zeros is just fine... maybe
831 offset += sizeof(MODE_CACHING_PAGE);
832 }
833 if (xvdd->device_type == XENVBD_DEVICETYPE_DISK && (cdb_page_code == MODE_PAGE_MEDIUM_TYPES || cdb_page_code == MODE_SENSE_RETURN_ALL))
834 {
835 PUCHAR medium_types_page;
836 valid_page = TRUE;
837 medium_types_page = &buffer[offset];
838 medium_types_page[0] = MODE_PAGE_MEDIUM_TYPES;
839 medium_types_page[1] = 0x06;
840 medium_types_page[2] = 0;
841 medium_types_page[3] = 0;
842 medium_types_page[4] = 0;
843 medium_types_page[5] = 0;
844 medium_types_page[6] = 0;
845 medium_types_page[7] = 0;
846 offset += 8;
847 }
848 switch (srb->Cdb[0])
849 {
850 case SCSIOP_MODE_SENSE:
851 parameter_header->ModeDataLength = (UCHAR)(offset - 1);
852 break;
853 case SCSIOP_MODE_SENSE10:
854 parameter_header10->ModeDataLength[0] = (UCHAR)((offset - 2) >> 8);
855 parameter_header10->ModeDataLength[1] = (UCHAR)(offset - 2);
856 break;
857 }
859 if (!valid_page && cdb_page_code != MODE_SENSE_RETURN_ALL)
860 {
861 srb->SrbStatus = SRB_STATUS_ERROR;
862 }
863 else if(offset < srb->DataTransferLength)
864 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
865 else
866 srb->SrbStatus = SRB_STATUS_SUCCESS;
867 srb->DataTransferLength = min(srb->DataTransferLength, offset);
868 srb->ScsiStatus = 0;
869 memcpy(srb->DataBuffer, buffer, srb->DataTransferLength);
871 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
873 return TRUE;
874 }
876 static VOID
877 XenVbd_MakeSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb, UCHAR sense_key, UCHAR additional_sense_code)
878 {
879 PSENSE_DATA sd = srb->SenseInfoBuffer;
881 UNREFERENCED_PARAMETER(xvdd);
883 if (!srb->SenseInfoBuffer)
884 return;
886 sd->ErrorCode = 0x70;
887 sd->Valid = 1;
888 sd->SenseKey = sense_key;
889 sd->AdditionalSenseLength = sizeof(SENSE_DATA) - FIELD_OFFSET(SENSE_DATA, AdditionalSenseLength);
890 sd->AdditionalSenseCode = additional_sense_code;
891 return;
892 }
894 static VOID
895 XenVbd_MakeAutoSense(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb)
896 {
897 if (srb->SrbStatus == SRB_STATUS_SUCCESS || srb->SrbFlags & SRB_FLAGS_DISABLE_AUTOSENSE)
898 return;
899 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
900 srb->SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
901 }
903 static BOOLEAN DDKAPI
904 XenVbd_HwScsiInterrupt(PVOID DeviceExtension)
905 {
906 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
907 PSCSI_REQUEST_BLOCK srb;
908 RING_IDX i, rp;
909 ULONG j;
910 blkif_response_t *rep;
911 int block_count;
912 int more_to_do = TRUE;
913 blkif_shadow_t *shadow;
914 ULONG suspend_resume_state_pdo;
915 BOOLEAN last_interrupt = FALSE;
916 ULONG start_ring_detect_state = xvdd->ring_detect_state;
917 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
918 srb_list_entry_t *srb_entry;
919 ULONG elapsed;
920 LARGE_INTEGER current_time;
921 #endif
923 /* in dump mode I think we get called on a timer, not by an actual IRQ */
924 if (!dump_mode && !xvdd->vectors.EvtChn_AckEvent(xvdd->vectors.context, xvdd->event_channel, &last_interrupt))
925 return FALSE; /* interrupt was not for us */
927 suspend_resume_state_pdo = xvdd->device_state->suspend_resume_state_pdo;
928 KeMemoryBarrier();
930 if (suspend_resume_state_pdo != xvdd->device_state->suspend_resume_state_fdo)
931 {
932 FUNCTION_ENTER();
933 switch (suspend_resume_state_pdo)
934 {
935 case SR_STATE_SUSPENDING:
936 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_SUSPENDING\n"));
937 break;
938 case SR_STATE_RESUMING:
939 KdPrint((__DRIVER_NAME " New pdo state SR_STATE_RESUMING\n"));
940 XenVbd_InitFromConfig(xvdd);
941 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
942 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
943 break;
944 case SR_STATE_RUNNING:
945 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
946 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
947 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
948 ScsiPortNotification(NextRequest, DeviceExtension);
949 default:
950 KdPrint((__DRIVER_NAME " New pdo state %d\n", suspend_resume_state_pdo));
951 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
952 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
953 break;
954 }
955 KeMemoryBarrier();
956 }
958 if (xvdd->device_state->suspend_resume_state_fdo != SR_STATE_RUNNING)
959 {
960 return last_interrupt;
961 }
963 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
964 ScsiPortQuerySystemTime(&current_time);
965 #endif
967 while (more_to_do)
968 {
969 rp = xvdd->ring.sring->rsp_prod;
970 KeMemoryBarrier();
971 for (i = xvdd->ring.rsp_cons; i < rp; i++)
972 {
973 rep = XenVbd_GetResponse(xvdd, i);
974 /*
975 * This code is to automatically detect if the backend is using the same
976 * bit width or a different bit width to us. Later versions of Xen do this
977 * via a xenstore value, but not all. That 0x0fffffff (notice
978 * that the msb is not actually set, so we don't have any problems with
979 * sign extending) is to signify the last entry on the right, which is
980 * different under 32 and 64 bits, and that is why we set it up there.
982 * To do the detection, we put two initial entries on the ring, with an op
983 * of 0xff (which is invalid). The first entry is mostly okay, but the
984 * second will be grossly misaligned if the backend bit width is different,
985 * and we detect this and switch frontend structures.
986 */
987 switch (xvdd->ring_detect_state)
988 {
989 case RING_DETECT_STATE_NOT_STARTED:
990 KdPrint((__DRIVER_NAME " premature IRQ\n"));
991 break;
992 case RING_DETECT_STATE_DETECT1:
993 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));
994 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));
995 xvdd->ring_detect_state = RING_DETECT_STATE_DETECT2;
996 break;
997 case RING_DETECT_STATE_DETECT2:
998 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));
999 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));
1000 *xvdd->event_channel_ptr |= 0x80000000;
1001 if (rep->operation != 0xff)
1003 KdPrint((__DRIVER_NAME " switching to 'other' ring size\n"));
1004 xvdd->ring.nr_ents = BLK_OTHER_RING_SIZE;
1005 xvdd->use_other = TRUE;
1006 *xvdd->event_channel_ptr |= 0x40000000;
1008 xvdd->ring_detect_state = RING_DETECT_STATE_COMPLETE;
1009 ScsiPortNotification(NextRequest, DeviceExtension);
1010 break;
1011 case RING_DETECT_STATE_COMPLETE:
1012 shadow = &xvdd->shadows[rep->id & SHADOW_ID_ID_MASK];
1013 if (shadow->reset)
1015 KdPrint((__DRIVER_NAME " discarding reset shadow\n"));
1016 for (j = 0; j < shadow->req.nr_segments; j++)
1018 if (dump_mode)
1020 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1021 shadow->req.seg[j].gref, TRUE, (ULONG)'SCSI');
1023 else
1025 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1026 shadow->req.seg[j].gref, FALSE, (ULONG)'SCSI');
1030 else if (dump_mode && !(rep->id & SHADOW_ID_DUMP_FLAG))
1032 KdPrint((__DRIVER_NAME " discarding stale (non-dump-mode) shadow\n"));
1034 else
1036 srb = shadow->srb;
1037 ASSERT(srb);
1038 block_count = decode_cdb_length(srb);
1039 block_count *= xvdd->bytes_per_sector / 512;
1040 #if DBG && NTDDI_VERSION >= NTDDI_WINXP
1041 srb_entry = srb->SrbExtension;
1042 elapsed = (ULONG)((current_time.QuadPart - shadow->ring_submit_time.QuadPart) / 10000L);
1043 if (elapsed > 5000)
1044 KdPrint((__DRIVER_NAME " WARNING: SRB completion time %dms\n", elapsed));
1045 #endif
1046 if (rep->status == BLKIF_RSP_OKAY || (dump_mode && dump_mode_errors++ < DUMP_MODE_ERROR_LIMIT))
1047 /* 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 */
1048 srb->SrbStatus = SRB_STATUS_SUCCESS;
1049 else
1051 KdPrint((__DRIVER_NAME " Xen Operation returned error\n"));
1052 if (decode_cdb_is_read(srb))
1053 KdPrint((__DRIVER_NAME " Operation = Read\n"));
1054 else
1055 KdPrint((__DRIVER_NAME " Operation = Write\n"));
1056 if (dump_mode)
1058 KdPrint((__DRIVER_NAME " Sector = %08X, Count = %d\n", (ULONG)shadow->req.sector_number, block_count));
1059 KdPrint((__DRIVER_NAME " DataBuffer = %p, aligned_buffer = %p\n", srb->DataBuffer, xvdd->aligned_buffer));
1060 KdPrint((__DRIVER_NAME " Physical = %08x%08x\n", MmGetPhysicalAddress(srb->DataBuffer).HighPart, MmGetPhysicalAddress(srb->DataBuffer).LowPart));
1061 KdPrint((__DRIVER_NAME " PFN = %08x\n", (ULONG)(MmGetPhysicalAddress(srb->DataBuffer).QuadPart >> PAGE_SHIFT)));
1063 for (j = 0; j < shadow->req.nr_segments; j++)
1065 KdPrint((__DRIVER_NAME " gref = %d\n", shadow->req.seg[j].gref));
1066 KdPrint((__DRIVER_NAME " first_sect = %d\n", shadow->req.seg[j].first_sect));
1067 KdPrint((__DRIVER_NAME " last_sect = %d\n", shadow->req.seg[j].last_sect));
1070 srb->SrbStatus = SRB_STATUS_ERROR;
1071 srb->ScsiStatus = 0x02;
1072 xvdd->last_sense_key = SCSI_SENSE_MEDIUM_ERROR;
1073 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1074 XenVbd_MakeAutoSense(xvdd, srb);
1076 if (shadow->aligned_buffer_in_use)
1078 ASSERT(xvdd->aligned_buffer_in_use);
1079 xvdd->aligned_buffer_in_use = FALSE;
1080 if (srb->SrbStatus == SRB_STATUS_SUCCESS && decode_cdb_is_read(srb))
1081 memcpy(srb->DataBuffer, xvdd->aligned_buffer, block_count * 512);
1083 for (j = 0; j < shadow->req.nr_segments; j++)
1085 if (dump_mode)
1087 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1088 shadow->req.seg[j].gref, TRUE, (ULONG)'SCSI');
1090 else
1092 xvdd->vectors.GntTbl_EndAccess(xvdd->vectors.context,
1093 shadow->req.seg[j].gref, FALSE, (ULONG)'SCSI');
1096 ScsiPortNotification(RequestComplete, xvdd, srb);
1098 shadow->aligned_buffer_in_use = FALSE;
1099 shadow->reset = FALSE;
1100 shadow->srb = NULL;
1101 put_shadow_on_freelist(xvdd, shadow);
1102 break;
1106 xvdd->ring.rsp_cons = i;
1107 if (i != xvdd->ring.req_prod_pvt)
1109 RING_FINAL_CHECK_FOR_RESPONSES(&xvdd->ring, more_to_do);
1111 else
1113 xvdd->ring.sring->rsp_event = i + 1;
1114 more_to_do = FALSE;
1118 if (start_ring_detect_state == RING_DETECT_STATE_COMPLETE)
1119 XenVbd_PutQueuedSrbsOnRing(xvdd);
1121 if (suspend_resume_state_pdo == SR_STATE_SUSPENDING)
1123 if (xvdd->inactive || xvdd->shadow_free == SHADOW_ENTRIES)
1125 /* all entries are purged from the list (or we are inactive). ready to suspend */
1126 xvdd->device_state->suspend_resume_state_fdo = suspend_resume_state_pdo;
1127 KeMemoryBarrier();
1128 KdPrint((__DRIVER_NAME " Set fdo state SR_STATE_SUSPENDING\n"));
1129 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xvdd->device_state->pdo_event_channel));
1130 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->device_state->pdo_event_channel);
1132 FUNCTION_EXIT();
1135 return last_interrupt;
1138 static BOOLEAN DDKAPI
1139 XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK srb)
1141 PUCHAR data_buffer;
1142 //ULONG data_buffer_length;
1143 PCDB cdb;
1144 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1145 ULONG data_transfer_length = srb->DataTransferLength;
1147 if (xvdd->inactive)
1149 KdPrint((__DRIVER_NAME " Inactive srb->Function = %08X\n", srb->Function));
1150 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1151 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1152 ScsiPortNotification(NextRequest, DeviceExtension);
1153 return TRUE;
1156 // If we haven't enumerated all the devices yet then just defer the request
1157 if (xvdd->ring_detect_state < RING_DETECT_STATE_COMPLETE)
1159 srb->SrbStatus = SRB_STATUS_BUSY;
1160 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1161 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Still figuring out ring)\n"));
1162 return TRUE;
1165 if (xvdd->device_state->suspend_resume_state_pdo != SR_STATE_RUNNING)
1167 KdPrint((__DRIVER_NAME " --> HwScsiStartIo (Suspending/Resuming)\n"));
1168 srb->SrbStatus = SRB_STATUS_BUSY;
1169 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1170 KdPrint((__DRIVER_NAME " <-- HwScsiStartIo (Suspending/Resuming)\n"));
1171 return TRUE;
1174 if (srb->PathId != 0 || srb->TargetId != 0 || srb->Lun != 0)
1176 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
1177 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1178 ScsiPortNotification(NextRequest, DeviceExtension);
1179 KdPrint((__DRIVER_NAME " --- HwScsiStartIo (Out of bounds)\n"));
1180 return TRUE;
1183 switch (srb->Function)
1185 case SRB_FUNCTION_EXECUTE_SCSI:
1186 cdb = (PCDB)srb->Cdb;
1188 switch(cdb->CDB6GENERIC.OperationCode)
1190 case SCSIOP_TEST_UNIT_READY:
1191 if (dump_mode)
1192 KdPrint((__DRIVER_NAME " Command = TEST_UNIT_READY\n"));
1193 srb->SrbStatus = SRB_STATUS_SUCCESS;
1194 srb->ScsiStatus = 0;
1195 break;
1196 case SCSIOP_INQUIRY:
1197 if (dump_mode)
1199 //PHYSICAL_ADDRESS physical;
1200 KdPrint((__DRIVER_NAME " Command = INQUIRY\n"));
1201 //KdPrint((__DRIVER_NAME " srb->Databuffer = %p\n", srb->DataBuffer));
1202 //physical = ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length);
1203 //KdPrint((__DRIVER_NAME " ScsiPortGetPhysicalAddress = %08x:%08x\n", physical.LowPart, physical.HighPart));
1205 // KdPrint((__DRIVER_NAME " (LUN = %d, EVPD = %d, Page Code = %02X)\n", srb->Cdb[1] >> 5, srb->Cdb[1] & 1, srb->Cdb[2]));
1206 // KdPrint((__DRIVER_NAME " (Length = %d)\n", srb->DataTransferLength));
1208 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1209 data_buffer = srb->DataBuffer;
1210 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1211 srb->SrbStatus = SRB_STATUS_SUCCESS;
1212 switch (xvdd->device_type)
1214 case XENVBD_DEVICETYPE_DISK:
1215 if ((srb->Cdb[1] & 1) == 0)
1217 if (srb->Cdb[2])
1219 srb->SrbStatus = SRB_STATUS_ERROR;
1221 else
1223 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1224 id->DeviceType = DIRECT_ACCESS_DEVICE;
1225 id->Versions = 4; /* minimum that WHQL says we must support */
1226 id->ResponseDataFormat = 2; /* not sure about this but WHQL complains otherwise */
1227 id->HiSupport = 1; /* WHQL test says we should set this */
1228 //id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1229 id->AdditionalLength = sizeof(INQUIRYDATA) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength) - 1;
1230 id->CommandQueue = 1;
1231 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1232 memcpy(id->ProductId, scsi_disk_model, 16); // product id
1233 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1234 data_transfer_length = sizeof(INQUIRYDATA);
1237 else
1239 switch (srb->Cdb[2])
1241 case VPD_SUPPORTED_PAGES: /* list of pages we support */
1242 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1243 data_buffer[1] = VPD_SUPPORTED_PAGES;
1244 data_buffer[2] = 0x00;
1245 data_buffer[3] = 2;
1246 data_buffer[4] = 0x00;
1247 data_buffer[5] = 0x80;
1248 data_transfer_length = 6;
1249 break;
1250 case VPD_SERIAL_NUMBER: /* serial number */
1251 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1252 data_buffer[1] = VPD_SERIAL_NUMBER;
1253 data_buffer[2] = 0x00;
1254 data_buffer[3] = 8;
1255 memset(&data_buffer[4], ' ', 8);
1256 data_transfer_length = 12;
1257 break;
1258 case VPD_DEVICE_IDENTIFIERS: /* identification - we don't support any so just return zero */
1259 data_buffer[0] = DIRECT_ACCESS_DEVICE;
1260 data_buffer[1] = VPD_DEVICE_IDENTIFIERS;
1261 data_buffer[2] = 0x00;
1262 data_buffer[3] = 4 + (UCHAR)strlen(xvdd->vectors.path); /* length */
1263 data_buffer[4] = 2; /* ASCII */
1264 data_buffer[5] = 1; /* VendorId */
1265 data_buffer[6] = 0;
1266 data_buffer[7] = (UCHAR)strlen(xvdd->vectors.path);
1267 memcpy(&data_buffer[8], xvdd->vectors.path, strlen(xvdd->vectors.path));
1268 data_transfer_length = (ULONG)(8 + strlen(xvdd->vectors.path));
1269 break;
1270 default:
1271 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1272 srb->SrbStatus = SRB_STATUS_ERROR;
1273 break;
1276 break;
1277 case XENVBD_DEVICETYPE_CDROM:
1278 if ((srb->Cdb[1] & 1) == 0)
1280 PINQUIRYDATA id = (PINQUIRYDATA)data_buffer;
1281 id->DeviceType = READ_ONLY_DIRECT_ACCESS_DEVICE;
1282 id->RemovableMedia = 1;
1283 id->Versions = 3;
1284 id->ResponseDataFormat = 0;
1285 id->AdditionalLength = FIELD_OFFSET(INQUIRYDATA, VendorSpecific) - FIELD_OFFSET(INQUIRYDATA, AdditionalLength);
1286 id->CommandQueue = 1;
1287 memcpy(id->VendorId, scsi_device_manufacturer, 8); // vendor id
1288 memcpy(id->ProductId, scsi_cdrom_model, 16); // product id
1289 memcpy(id->ProductRevisionLevel, "0000", 4); // product revision level
1291 else
1293 switch (srb->Cdb[2])
1295 case 0x00:
1296 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1297 data_buffer[1] = 0x00;
1298 data_buffer[2] = 0x00;
1299 data_buffer[3] = 2;
1300 data_buffer[4] = 0x00;
1301 data_buffer[5] = 0x80;
1302 break;
1303 case 0x80:
1304 data_buffer[0] = READ_ONLY_DIRECT_ACCESS_DEVICE;
1305 data_buffer[1] = 0x80;
1306 data_buffer[2] = 0x00;
1307 data_buffer[3] = 8;
1308 data_buffer[4] = 0x31;
1309 data_buffer[5] = 0x32;
1310 data_buffer[6] = 0x33;
1311 data_buffer[7] = 0x34;
1312 data_buffer[8] = 0x35;
1313 data_buffer[9] = 0x36;
1314 data_buffer[10] = 0x37;
1315 data_buffer[11] = 0x38;
1316 break;
1317 default:
1318 //KdPrint((__DRIVER_NAME " Unknown Page %02x requested\n", srb->Cdb[2]));
1319 srb->SrbStatus = SRB_STATUS_ERROR;
1320 break;
1323 break;
1324 default:
1325 //KdPrint((__DRIVER_NAME " Unknown DeviceType %02x requested\n", xvdd->device_type));
1326 srb->SrbStatus = SRB_STATUS_ERROR;
1327 break;
1329 break;
1330 case SCSIOP_READ_CAPACITY:
1331 if (dump_mode)
1332 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1333 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1334 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1335 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1336 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1337 data_buffer = srb->DataBuffer;
1338 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1339 if ((xvdd->total_sectors - 1) >> 32)
1341 data_buffer[0] = 0xff;
1342 data_buffer[1] = 0xff;
1343 data_buffer[2] = 0xff;
1344 data_buffer[3] = 0xff;
1346 else
1348 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1349 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1350 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1351 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1353 data_buffer[4] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1354 data_buffer[5] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1355 data_buffer[6] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1356 data_buffer[7] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1357 srb->ScsiStatus = 0;
1358 srb->SrbStatus = SRB_STATUS_SUCCESS;
1359 break;
1360 case SCSIOP_READ_CAPACITY16:
1361 if (dump_mode)
1362 KdPrint((__DRIVER_NAME " Command = READ_CAPACITY\n"));
1363 //KdPrint((__DRIVER_NAME " LUN = %d, RelAdr = %d\n", srb->Cdb[1] >> 4, srb->Cdb[1] & 1));
1364 //KdPrint((__DRIVER_NAME " LBA = %02x%02x%02x%02x\n", srb->Cdb[2], srb->Cdb[3], srb->Cdb[4], srb->Cdb[5]));
1365 //KdPrint((__DRIVER_NAME " PMI = %d\n", srb->Cdb[8] & 1));
1366 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1367 data_buffer = srb->DataBuffer;
1368 RtlZeroMemory(data_buffer, srb->DataTransferLength);
1369 data_buffer[0] = (unsigned char)((xvdd->total_sectors - 1) >> 56) & 0xff;
1370 data_buffer[1] = (unsigned char)((xvdd->total_sectors - 1) >> 48) & 0xff;
1371 data_buffer[2] = (unsigned char)((xvdd->total_sectors - 1) >> 40) & 0xff;
1372 data_buffer[3] = (unsigned char)((xvdd->total_sectors - 1) >> 32) & 0xff;
1373 data_buffer[4] = (unsigned char)((xvdd->total_sectors - 1) >> 24) & 0xff;
1374 data_buffer[5] = (unsigned char)((xvdd->total_sectors - 1) >> 16) & 0xff;
1375 data_buffer[6] = (unsigned char)((xvdd->total_sectors - 1) >> 8) & 0xff;
1376 data_buffer[7] = (unsigned char)((xvdd->total_sectors - 1) >> 0) & 0xff;
1377 data_buffer[8] = (unsigned char)(xvdd->bytes_per_sector >> 24) & 0xff;
1378 data_buffer[9] = (unsigned char)(xvdd->bytes_per_sector >> 16) & 0xff;
1379 data_buffer[10] = (unsigned char)(xvdd->bytes_per_sector >> 8) & 0xff;
1380 data_buffer[11] = (unsigned char)(xvdd->bytes_per_sector >> 0) & 0xff;
1381 srb->ScsiStatus = 0;
1382 srb->SrbStatus = SRB_STATUS_SUCCESS;
1383 break;
1384 case SCSIOP_MODE_SENSE:
1385 case SCSIOP_MODE_SENSE10:
1386 if (dump_mode)
1387 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));
1388 XenVbd_FillModePage(xvdd, srb);
1389 break;
1390 case SCSIOP_READ:
1391 case SCSIOP_READ16:
1392 case SCSIOP_WRITE:
1393 case SCSIOP_WRITE16:
1394 XenVbd_PutSrbOnList(xvdd, srb);
1395 XenVbd_PutQueuedSrbsOnRing(xvdd);
1396 break;
1397 case SCSIOP_VERIFY:
1398 case SCSIOP_VERIFY16:
1399 // Should we do more here?
1400 if (dump_mode)
1401 KdPrint((__DRIVER_NAME " Command = VERIFY\n"));
1402 srb->SrbStatus = SRB_STATUS_SUCCESS;
1403 break;
1404 case SCSIOP_REPORT_LUNS:
1405 if (dump_mode)
1406 KdPrint((__DRIVER_NAME " Command = REPORT_LUNS\n"));
1407 srb->SrbStatus = SRB_STATUS_SUCCESS;;
1408 break;
1409 case SCSIOP_REQUEST_SENSE:
1410 if (dump_mode)
1411 KdPrint((__DRIVER_NAME " Command = REQUEST_SENSE\n"));
1412 XenVbd_MakeSense(xvdd, srb, xvdd->last_sense_key, xvdd->last_additional_sense_code);
1413 srb->SrbStatus = SRB_STATUS_SUCCESS;
1414 break;
1415 case SCSIOP_READ_TOC:
1416 if (dump_mode)
1417 KdPrint((__DRIVER_NAME " Command = READ_TOC\n"));
1418 //data_buffer = LongLongToPtr(ScsiPortGetPhysicalAddress(xvdd, srb, srb->DataBuffer, &data_buffer_length).QuadPart);
1419 data_buffer = srb->DataBuffer;
1420 // DataBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
1421 /*
1422 #define READ_TOC_FORMAT_TOC 0x00
1423 #define READ_TOC_FORMAT_SESSION 0x01
1424 #define READ_TOC_FORMAT_FULL_TOC 0x02
1425 #define READ_TOC_FORMAT_PMA 0x03
1426 #define READ_TOC_FORMAT_ATIP 0x04
1427 */
1428 // KdPrint((__DRIVER_NAME " Msf = %d\n", cdb->READ_TOC.Msf));
1429 // KdPrint((__DRIVER_NAME " LogicalUnitNumber = %d\n", cdb->READ_TOC.LogicalUnitNumber));
1430 // KdPrint((__DRIVER_NAME " Format2 = %d\n", cdb->READ_TOC.Format2));
1431 // KdPrint((__DRIVER_NAME " StartingTrack = %d\n", cdb->READ_TOC.StartingTrack));
1432 // KdPrint((__DRIVER_NAME " AllocationLength = %d\n", (cdb->READ_TOC.AllocationLength[0] << 8) | cdb->READ_TOC.AllocationLength[1]));
1433 // KdPrint((__DRIVER_NAME " Control = %d\n", cdb->READ_TOC.Control));
1434 // KdPrint((__DRIVER_NAME " Format = %d\n", cdb->READ_TOC.Format));
1435 switch (cdb->READ_TOC.Format2)
1437 case READ_TOC_FORMAT_TOC:
1438 data_buffer[0] = 0; // length MSB
1439 data_buffer[1] = 10; // length LSB
1440 data_buffer[2] = 1; // First Track
1441 data_buffer[3] = 1; // Last Track
1442 data_buffer[4] = 0; // Reserved
1443 data_buffer[5] = 0x14; // current position data + uninterrupted data
1444 data_buffer[6] = 1; // last complete track
1445 data_buffer[7] = 0; // reserved
1446 data_buffer[8] = 0; // MSB Block
1447 data_buffer[9] = 0;
1448 data_buffer[10] = 0;
1449 data_buffer[11] = 0; // LSB Block
1450 srb->SrbStatus = SRB_STATUS_SUCCESS;
1451 break;
1452 case READ_TOC_FORMAT_SESSION:
1453 case READ_TOC_FORMAT_FULL_TOC:
1454 case READ_TOC_FORMAT_PMA:
1455 case READ_TOC_FORMAT_ATIP:
1456 srb->SrbStatus = SRB_STATUS_ERROR;
1457 break;
1459 break;
1460 case SCSIOP_START_STOP_UNIT:
1461 KdPrint((__DRIVER_NAME " Command = SCSIOP_START_STOP_UNIT\n"));
1462 srb->SrbStatus = SRB_STATUS_SUCCESS;
1463 break;
1464 case SCSIOP_RESERVE_UNIT:
1465 KdPrint((__DRIVER_NAME " Command = SCSIOP_RESERVE_UNIT\n"));
1466 srb->SrbStatus = SRB_STATUS_SUCCESS;
1467 break;
1468 case SCSIOP_RELEASE_UNIT:
1469 KdPrint((__DRIVER_NAME " Command = SCSIOP_RELEASE_UNIT\n"));
1470 srb->SrbStatus = SRB_STATUS_SUCCESS;
1471 break;
1472 default:
1473 KdPrint((__DRIVER_NAME " Unhandled EXECUTE_SCSI Command = %02X\n", srb->Cdb[0]));
1474 srb->SrbStatus = SRB_STATUS_ERROR;
1475 break;
1477 if (srb->SrbStatus == SRB_STATUS_ERROR)
1479 KdPrint((__DRIVER_NAME " EXECUTE_SCSI Command = %02X returned error %02x\n", srb->Cdb[0], xvdd->last_sense_key));
1480 if (xvdd->last_sense_key == SCSI_SENSE_NO_SENSE)
1482 xvdd->last_sense_key = SCSI_SENSE_ILLEGAL_REQUEST;
1483 xvdd->last_additional_sense_code = SCSI_ADSENSE_INVALID_CDB;
1485 srb->ScsiStatus = 0x02;
1486 XenVbd_MakeAutoSense(xvdd, srb);
1487 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1488 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1490 ScsiPortNotification(NextRequest, DeviceExtension);
1493 else if (srb->SrbStatus != SRB_STATUS_PENDING)
1495 if (srb->SrbStatus == SRB_STATUS_SUCCESS && data_transfer_length < srb->DataTransferLength)
1497 srb->SrbStatus = SRB_STATUS_DATA_OVERRUN;
1498 srb->DataTransferLength = data_transfer_length;
1500 xvdd->last_sense_key = SCSI_SENSE_NO_SENSE;
1501 xvdd->last_additional_sense_code = SCSI_ADSENSE_NO_SENSE;
1502 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1503 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1505 ScsiPortNotification(NextRequest, DeviceExtension);
1508 break;
1509 case SRB_FUNCTION_IO_CONTROL:
1510 KdPrint((__DRIVER_NAME " SRB_FUNCTION_IO_CONTROL\n"));
1511 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
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_FLUSH:
1519 KdPrint((__DRIVER_NAME " SRB_FUNCTION_FLUSH %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 case SRB_FUNCTION_SHUTDOWN:
1528 KdPrint((__DRIVER_NAME " SRB_FUNCTION_SHUTDOWN %p, xvdd->shadow_free = %d\n", srb, xvdd->shadow_free));
1529 srb->SrbStatus = SRB_STATUS_SUCCESS;
1530 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1531 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1533 ScsiPortNotification(NextRequest, DeviceExtension);
1535 break;
1536 default:
1537 KdPrint((__DRIVER_NAME " Unhandled srb->Function = %08X\n", srb->Function));
1538 srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
1539 ScsiPortNotification(RequestComplete, DeviceExtension, srb);
1540 if (xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1542 ScsiPortNotification(NextRequest, DeviceExtension);
1544 break;
1547 //FUNCTION_EXIT();
1548 return TRUE;
1551 static BOOLEAN
1552 XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId)
1554 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1555 srb_list_entry_t *srb_entry;
1556 PSCSI_REQUEST_BLOCK srb;
1557 int i;
1559 UNREFERENCED_PARAMETER(DeviceExtension);
1560 UNREFERENCED_PARAMETER(PathId);
1562 FUNCTION_ENTER();
1564 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1565 xvdd->aligned_buffer_in_use = FALSE;
1567 if (xvdd->ring_detect_state == RING_DETECT_STATE_COMPLETE && xvdd->device_state->suspend_resume_state_pdo == SR_STATE_RUNNING)
1569 while((srb_entry = (srb_list_entry_t *)RemoveHeadList(&xvdd->srb_list)) != (srb_list_entry_t *)&xvdd->srb_list)
1571 srb = srb_entry->srb;
1572 srb->SrbStatus = SRB_STATUS_BUS_RESET;
1573 KdPrint((__DRIVER_NAME " completing queued SRB %p with status SRB_STATUS_BUS_RESET\n", srb));
1574 ScsiPortNotification(RequestComplete, xvdd, srb);
1577 for (i = 0; i < MAX_SHADOW_ENTRIES; i++)
1579 if (xvdd->shadows[i].srb)
1581 KdPrint((__DRIVER_NAME " Completing in-flight srb %p with status SRB_STATUS_BUS_RESET\n", xvdd->shadows[i].srb));
1582 /* set reset here so that the interrupt won't do anything with the srb but will dispose of the shadow entry correctly */
1583 xvdd->shadows[i].reset = TRUE;
1584 xvdd->shadows[i].srb->SrbStatus = SRB_STATUS_BUS_RESET;
1585 ScsiPortNotification(RequestComplete, xvdd, xvdd->shadows[i].srb);
1586 xvdd->shadows[i].srb = NULL;
1587 xvdd->shadows[i].aligned_buffer_in_use = FALSE;
1591 /* send a notify to Dom0 just in case it was missed for some reason (which should _never_ happen) */
1592 xvdd->vectors.EvtChn_Notify(xvdd->vectors.context, xvdd->event_channel);
1594 ScsiPortNotification(NextRequest, DeviceExtension);
1597 FUNCTION_EXIT();
1599 return TRUE;
1602 static BOOLEAN
1603 XenVbd_HwScsiAdapterState(PVOID DeviceExtension, PVOID Context, BOOLEAN SaveState)
1605 UNREFERENCED_PARAMETER(DeviceExtension);
1606 UNREFERENCED_PARAMETER(Context);
1607 UNREFERENCED_PARAMETER(SaveState);
1609 FUNCTION_ENTER();
1610 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1612 FUNCTION_EXIT();
1614 return TRUE;
1617 static SCSI_ADAPTER_CONTROL_STATUS DDKAPI
1618 XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters)
1620 PXENVBD_DEVICE_DATA xvdd = DeviceExtension;
1621 SCSI_ADAPTER_CONTROL_STATUS Status = ScsiAdapterControlSuccess;
1622 PSCSI_SUPPORTED_CONTROL_TYPE_LIST SupportedControlTypeList;
1623 //KIRQL OldIrql;
1625 FUNCTION_ENTER();
1626 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1627 KdPrint((__DRIVER_NAME " xvdd = %p\n", xvdd));
1629 switch (ControlType)
1631 case ScsiQuerySupportedControlTypes:
1632 SupportedControlTypeList = (PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters;
1633 KdPrint((__DRIVER_NAME " ScsiQuerySupportedControlTypes (Max = %d)\n", SupportedControlTypeList->MaxControlType));
1634 SupportedControlTypeList->SupportedTypeList[ScsiQuerySupportedControlTypes] = TRUE;
1635 SupportedControlTypeList->SupportedTypeList[ScsiStopAdapter] = TRUE;
1636 SupportedControlTypeList->SupportedTypeList[ScsiRestartAdapter] = TRUE;
1637 break;
1638 case ScsiStopAdapter:
1639 KdPrint((__DRIVER_NAME " ScsiStopAdapter\n"));
1640 /* I don't think we actually have to do anything here... xenpci cleans up all the xenbus stuff for us */
1641 break;
1642 case ScsiRestartAdapter:
1643 KdPrint((__DRIVER_NAME " ScsiRestartAdapter\n"));
1644 if (!xvdd->inactive)
1646 if (XenVbd_InitFromConfig(xvdd) != SP_RETURN_FOUND)
1647 KeBugCheckEx(DATA_COHERENCY_EXCEPTION, 0, (ULONG_PTR) xvdd, 0, 0);
1648 XenVbd_StartRingDetection(xvdd);
1650 break;
1651 case ScsiSetBootConfig:
1652 KdPrint((__DRIVER_NAME " ScsiSetBootConfig\n"));
1653 break;
1654 case ScsiSetRunningConfig:
1655 KdPrint((__DRIVER_NAME " ScsiSetRunningConfig\n"));
1656 break;
1657 default:
1658 KdPrint((__DRIVER_NAME " UNKNOWN\n"));
1659 break;
1662 FUNCTION_EXIT();
1664 return Status;
1667 NTSTATUS
1668 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
1670 ULONG status;
1671 HW_INITIALIZATION_DATA HwInitializationData;
1672 PVOID driver_extension;
1673 PUCHAR ptr;
1674 OBJECT_ATTRIBUTES oa;
1675 HANDLE service_handle;
1676 UNICODE_STRING param_name;
1677 HANDLE param_handle;
1678 UNICODE_STRING value_name;
1679 CHAR buf[256];
1680 ULONG buf_len;
1681 PKEY_VALUE_PARTIAL_INFORMATION kpv;
1683 FUNCTION_ENTER();
1684 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
1685 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
1687 /* RegistryPath == NULL when we are invoked as a crash dump driver */
1688 if (!RegistryPath)
1690 dump_mode = TRUE;
1693 if (!dump_mode)
1695 IoAllocateDriverObjectExtension(DriverObject, UlongToPtr(XEN_INIT_DRIVER_EXTENSION_MAGIC), PAGE_SIZE, &driver_extension);
1696 ptr = driver_extension;
1697 //ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
1698 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "ring-ref", NULL, NULL);
1699 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_IRQ, "event-channel", NULL, NULL);
1700 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_FRONT, "device-type", NULL, NULL);
1701 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mode", NULL, NULL);
1702 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sectors", NULL, NULL);
1703 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sector-size", NULL, NULL);
1704 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 */
1705 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT, NULL, NULL, NULL);
1706 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1707 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1708 __ADD_XEN_INIT_UCHAR(&ptr, 20);
1709 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1710 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT, NULL, NULL, NULL);
1711 //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1712 //__ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
1713 //__ADD_XEN_INIT_UCHAR(&ptr, 20);
1714 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1715 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN, NULL, NULL, NULL);
1716 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
1717 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
1718 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1719 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
1720 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
1721 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1722 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitialising);
1723 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait);
1724 __ADD_XEN_INIT_UCHAR(&ptr, 50);
1725 __ADD_XEN_INIT_UCHAR(&ptr, 0);
1727 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
1729 InitializeObjectAttributes(&oa, RegistryPath, OBJ_CASE_INSENSITIVE, NULL, NULL);
1730 status = ZwOpenKey(&service_handle, KEY_READ, &oa);
1731 if(!NT_SUCCESS(status))
1733 KdPrint((__DRIVER_NAME " ZwOpenKey(Service) returned %08x\n", status));
1735 else
1737 RtlInitUnicodeString(&param_name, L"Parameters");
1738 InitializeObjectAttributes(&oa, &param_name, OBJ_CASE_INSENSITIVE, service_handle, NULL);
1739 status = ZwOpenKey(&param_handle, KEY_READ, &oa);
1740 if(!NT_SUCCESS(status))
1742 KdPrint((__DRIVER_NAME " ZwOpenKey(Parameters) returned %08x\n", status));
1744 else
1746 kpv = (PKEY_VALUE_PARTIAL_INFORMATION)buf;
1747 RtlFillMemory(scsi_device_manufacturer, 8, ' ');
1748 RtlFillMemory(scsi_disk_model, 16, ' ');
1749 RtlFillMemory(scsi_cdrom_model, 16, ' ');
1751 RtlInitUnicodeString(&value_name, L"Manufacturer");
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_device_manufacturer, (PWCHAR)kpv->Data, min(kpv->DataLength, 8));
1756 else
1757 RtlStringCbCopyA(scsi_device_manufacturer, 8, "XEN ");
1759 RtlInitUnicodeString(&value_name, L"Disk_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_disk_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1764 else
1765 RtlStringCbCopyA(scsi_disk_model, 16, "PV DISK ");
1767 RtlInitUnicodeString(&value_name, L"CDROM_Model");
1768 buf_len = 256;
1769 status = ZwQueryValueKey(param_handle, &value_name, KeyValuePartialInformation, buf, buf_len, &buf_len);
1770 if(NT_SUCCESS(status))
1771 wcstombs(scsi_cdrom_model, (PWCHAR)kpv->Data, min(kpv->DataLength, 16));
1772 else
1773 RtlStringCbCopyA(scsi_cdrom_model, 16, "PV CDROM ");
1774 ZwClose(param_handle);
1776 ZwClose(service_handle);
1780 RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
1782 HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
1783 HwInitializationData.AdapterInterfaceType = PNPBus;
1784 if (!dump_mode)
1785 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_DEVICE_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE;
1786 else
1787 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_DEVICE_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE_DUMP_MODE;
1788 HwInitializationData.SpecificLuExtensionSize = 0;
1789 HwInitializationData.SrbExtensionSize = sizeof(srb_list_entry_t);
1790 HwInitializationData.NumberOfAccessRanges = 1;
1791 HwInitializationData.MapBuffers = TRUE;
1792 HwInitializationData.NeedPhysicalAddresses = FALSE;
1793 HwInitializationData.TaggedQueuing = TRUE;
1794 HwInitializationData.AutoRequestSense = TRUE;
1795 HwInitializationData.MultipleRequestPerLu = TRUE;
1796 HwInitializationData.ReceiveEvent = FALSE;
1797 HwInitializationData.VendorIdLength = 0;
1798 HwInitializationData.VendorId = NULL;
1799 HwInitializationData.DeviceIdLength = 0;
1800 HwInitializationData.DeviceId = NULL;
1802 HwInitializationData.HwInitialize = XenVbd_HwScsiInitialize;
1803 HwInitializationData.HwStartIo = XenVbd_HwScsiStartIo;
1804 HwInitializationData.HwInterrupt = XenVbd_HwScsiInterrupt;
1805 HwInitializationData.HwFindAdapter = XenVbd_HwScsiFindAdapter;
1806 HwInitializationData.HwResetBus = XenVbd_HwScsiResetBus;
1807 HwInitializationData.HwDmaStarted = NULL;
1808 HwInitializationData.HwAdapterState = NULL; //XenVbd_HwScsiAdapterState;
1809 HwInitializationData.HwAdapterControl = XenVbd_HwScsiAdapterControl;
1811 status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
1813 if(!NT_SUCCESS(status))
1815 KdPrint((__DRIVER_NAME " ScsiPortInitialize failed with status 0x%08x\n", status));
1818 FUNCTION_EXIT();
1820 return status;