win-pvdrivers

view xenvbd/xenvbd_scsiport.c @ 874:f5a973a8a10b

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