win-pvdrivers

view xenvbd/xenvbd_scsiport.c @ 871:babee70630ad

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