win-pvdrivers

diff xenpci/xenbus_device_interface.c @ 537:2a74ac2f43bb

more big updates
dma now working under xp
author James Harper <james.harper@bendigoit.com.au>
date Wed Feb 18 22:18:23 2009 +1100 (2009-02-18)
parents 1d39de3ab8d6
children 1bae3638ab55
line diff
     1.1 --- a/xenpci/xenbus_device_interface.c	Sat Feb 14 13:35:48 2009 +1100
     1.2 +++ b/xenpci/xenbus_device_interface.c	Wed Feb 18 22:18:23 2009 +1100
     1.3 @@ -19,7 +19,6 @@ Foundation, Inc., 51 Franklin Street, Fi
     1.4  
     1.5  #include "xenpci.h"
     1.6  
     1.7 -
     1.8  typedef struct {
     1.9    LIST_ENTRY entry;
    1.10    PVOID data;
    1.11 @@ -69,7 +68,7 @@ XenPci_ProcessReadRequest(WDFQUEUE queue
    1.12    NTSTATUS status;
    1.13    WDFFILEOBJECT file_object = WdfRequestGetFileObject(request);
    1.14    PXENPCI_DEVICE_INTERFACE_DATA xpdid = GetXpdid(file_object);
    1.15 -  ULONG dst_length = length;
    1.16 +  ULONG dst_length = (ULONG)length;
    1.17    ULONG dst_offset = 0;
    1.18    ULONG copy_length;
    1.19    xenbus_read_queue_item_t *list_entry;
    1.20 @@ -94,8 +93,8 @@ XenPci_ProcessReadRequest(WDFQUEUE queue
    1.21      dst_offset += copy_length;
    1.22      if (list_entry->offset == list_entry->length)
    1.23      {
    1.24 -      // free the list entry
    1.25 -      // free the data
    1.26 +      ExFreePoolWithTag(list_entry->data, XENPCI_POOL_TAG);
    1.27 +      ExFreePoolWithTag(list_entry, XENPCI_POOL_TAG);
    1.28      }
    1.29      else
    1.30      {
    1.31 @@ -117,19 +116,23 @@ XenPci_IoWatch(char *path, PVOID context
    1.32    KIRQL old_irql;
    1.33    struct xsd_sockmsg *rep;
    1.34    xenbus_read_queue_item_t *list_entry;
    1.35 +  size_t remaining;
    1.36    WDFREQUEST request;
    1.37  
    1.38    FUNCTION_ENTER();
    1.39    
    1.40    KeAcquireSpinLock(&xpdid->lock, &old_irql);
    1.41    
    1.42 +  remaining = sizeof(struct xsd_sockmsg) + strlen(path) + 1 + strlen(watch_context->token) + 1;
    1.43    rep = ExAllocatePoolWithTag(NonPagedPool, sizeof(struct xsd_sockmsg) + strlen(path) + 1 + strlen(watch_context->token) + 1, XENPCI_POOL_TAG);
    1.44    rep->type = XS_WATCH_EVENT;
    1.45    rep->req_id = 0;
    1.46    rep->tx_id = 0;
    1.47 -  rep->len = strlen(path) + 1 + strlen(watch_context->token) + 1;
    1.48 -  strcpy((PCHAR)(rep + 1), path);
    1.49 -  strcpy((PCHAR)(rep + 1) + strlen(path) + 1, watch_context->token);
    1.50 +  rep->len = (ULONG)(strlen(path) + 1 + strlen(watch_context->token) + 1);
    1.51 +  remaining -= sizeof(struct xsd_sockmsg);
    1.52 +  RtlStringCbCopyA((PCHAR)(rep + 1), remaining, path);
    1.53 +  remaining -= strlen(path) + 1;
    1.54 +  RtlStringCbCopyA((PCHAR)(rep + 1) + strlen(path) + 1, remaining, watch_context->token);
    1.55    
    1.56    list_entry = (xenbus_read_queue_item_t *)ExAllocatePoolWithTag(NonPagedPool, sizeof(xenbus_read_queue_item_t), XENPCI_POOL_TAG);
    1.57    list_entry->data = rep;
    1.58 @@ -207,6 +210,8 @@ XenPci_EvtIoRead(WDFQUEUE queue, WDFREQU
    1.59    PXENPCI_DEVICE_INTERFACE_DATA xpdid = GetXpdid(file_object);
    1.60    KIRQL old_irql;
    1.61  
    1.62 +  UNREFERENCED_PARAMETER(queue);
    1.63 +  
    1.64    FUNCTION_ENTER();
    1.65    status = WdfRequestForwardToIoQueue(request, xpdid->io_queue);
    1.66    if (!NT_SUCCESS(status))
    1.67 @@ -266,7 +271,7 @@ XenPci_EvtIoWrite(WDFQUEUE queue, WDFREQ
    1.68    ASSERT(NT_SUCCESS(status));
    1.69    
    1.70    src_ptr = (PUCHAR)buffer;
    1.71 -  src_len = length;
    1.72 +  src_len = (ULONG)length;
    1.73    dst_ptr = xpdid->u.buffer + xpdid->len;
    1.74    while (src_len != 0)
    1.75    {
    1.76 @@ -310,10 +315,10 @@ XenPci_EvtIoWrite(WDFQUEUE queue, WDFREQ
    1.77      case XS_UNWATCH:
    1.78        KeAcquireSpinLock(&xpdid->lock, &old_irql);
    1.79        watch_context = (watch_context_t *)ExAllocatePoolWithTag(NonPagedPool, sizeof(watch_context_t), XENPCI_POOL_TAG);
    1.80 -      watch_path = xpdid->u.buffer + sizeof(struct xsd_sockmsg);
    1.81 -      watch_token = xpdid->u.buffer + sizeof(struct xsd_sockmsg) + strlen(watch_path) + 1;
    1.82 -      strcpy(watch_context->path, watch_path);
    1.83 -      strcpy(watch_context->token, watch_token);
    1.84 +      watch_path = (PCHAR)(xpdid->u.buffer + sizeof(struct xsd_sockmsg));
    1.85 +      watch_token = (PCHAR)(xpdid->u.buffer + sizeof(struct xsd_sockmsg) + strlen(watch_path) + 1);
    1.86 +      RtlStringCbCopyA(watch_context->path, ARRAY_SIZE(watch_context->path), watch_path);
    1.87 +      RtlStringCbCopyA(watch_context->token, ARRAY_SIZE(watch_context->path), watch_token);
    1.88        watch_context->file_object = file_object;
    1.89        if (xpdid->u.msg.type == XS_WATCH)
    1.90          InsertTailList(&xpdid->watch_list_head, &watch_context->entry);
    1.91 @@ -329,8 +334,8 @@ XenPci_EvtIoWrite(WDFQUEUE queue, WDFREQ
    1.92          rep->type = XS_ERROR;
    1.93          rep->req_id = xpdid->u.msg.req_id;
    1.94          rep->tx_id = xpdid->u.msg.tx_id;
    1.95 -        rep->len = strlen(msg) + 0;
    1.96 -        strcpy((PCHAR)(rep + 1), msg);
    1.97 +        rep->len = (ULONG)(strlen(msg) + 0);
    1.98 +        RtlStringCbCopyA((PCHAR)(rep + 1), strlen(msg) + 1, msg);
    1.99          if (xpdid->u.msg.type == XS_WATCH)
   1.100            RemoveEntryList(&watch_context->entry);
   1.101        }
   1.102 @@ -371,290 +376,4 @@ XenPci_EvtIoWrite(WDFQUEUE queue, WDFREQ
   1.103    WdfRequestCompleteWithInformation(request, STATUS_SUCCESS, length);
   1.104  
   1.105    FUNCTION_EXIT();
   1.106 -}
   1.107 -
   1.108 -#if 0
   1.109 -NTSTATUS
   1.110 -XenPci_Irp_Create_XenBus(PDEVICE_OBJECT device_object, PIRP irp)
   1.111 -{
   1.112 -  NTSTATUS status;
   1.113 -  PIO_STACK_LOCATION stack;
   1.114 -  PFILE_OBJECT file;
   1.115 -  device_interface_xenbus_context_t *dixc;
   1.116 -  
   1.117 -  FUNCTION_ENTER();
   1.118 -  
   1.119 -  UNREFERENCED_PARAMETER(device_object);
   1.120 -  stack = IoGetCurrentIrpStackLocation(irp);
   1.121 -  file = stack->FileObject;
   1.122 -  dixc = (device_interface_xenbus_context_t *)ExAllocatePoolWithTag(NonPagedPool, sizeof(device_interface_xenbus_context_t), XENPCI_POOL_TAG);
   1.123 -  dixc->type = DEVICE_INTERFACE_TYPE_XENBUS;
   1.124 -  KeInitializeSpinLock(&dixc->lock);
   1.125 -  InitializeListHead(&dixc->read_list_head);
   1.126 -  dixc->len = 0;
   1.127 -  file->FsContext = dixc;
   1.128 -  status = STATUS_SUCCESS;    
   1.129 -  dixc->pending_read_irp = NULL;
   1.130 -  irp->IoStatus.Status = status;
   1.131 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
   1.132 -  
   1.133 -  FUNCTION_EXIT();
   1.134 -  
   1.135 -  return status;
   1.136 -}
   1.137 -
   1.138 -NTSTATUS
   1.139 -XenPci_Irp_Close_XenBus(PDEVICE_OBJECT device_object, PIRP irp)
   1.140 -{
   1.141 -  PXENPCI_DEVICE_DATA xpdd;
   1.142 -  NTSTATUS status;
   1.143 -
   1.144 -  xpdd = (PXENPCI_DEVICE_DATA)device_object->DeviceExtension;
   1.145 -  status = STATUS_SUCCESS;    
   1.146 -  irp->IoStatus.Status = status;
   1.147 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
   1.148 -  // cleanup dixc here
   1.149 -  
   1.150 -  return status;
   1.151 -}
   1.152 -
   1.153 -static NTSTATUS
   1.154 -XenPci_Irp_Read_XenBus_Complete(device_interface_xenbus_context_t *dixc, PIRP irp)
   1.155 -{
   1.156 -  KIRQL old_irql;
   1.157 -  ULONG dst_length;
   1.158 -  ULONG dst_offset;
   1.159 -  ULONG copy_length;
   1.160 -  xenbus_read_queue_item_t *list_entry;
   1.161 -  PIO_STACK_LOCATION stack;
   1.162 -  NTSTATUS status;
   1.163 -
   1.164 -  FUNCTION_ENTER();
   1.165 -  
   1.166 -KdPrint((__DRIVER_NAME "     A - dixc = %p, irp = %p\n", dixc, irp));
   1.167 -  stack = IoGetCurrentIrpStackLocation(irp);
   1.168 -KdPrint((__DRIVER_NAME "     Aa\n"));
   1.169 -  dst_length = stack->Parameters.Read.Length;
   1.170 -KdPrint((__DRIVER_NAME "     B - dst_length = %d\n", dst_length));
   1.171 -  dst_offset = 0;
   1.172 -KdPrint((__DRIVER_NAME "     C\n"));
   1.173 -  KeAcquireSpinLock(&dixc->lock, &old_irql);
   1.174 -KdPrint((__DRIVER_NAME "     D"));
   1.175 -  while(dst_offset < dst_length && (list_entry = (xenbus_read_queue_item_t *)RemoveHeadList(&dixc->read_list_head)) != (xenbus_read_queue_item_t *)&dixc->read_list_head)
   1.176 -  {
   1.177 -KdPrint((__DRIVER_NAME "     E\n"));
   1.178 -    copy_length = min(list_entry->length - list_entry->offset, dst_length - dst_offset);
   1.179 -    KdPrint((__DRIVER_NAME "     copying %d bytes\n", copy_length));
   1.180 -    memcpy((PUCHAR)irp->AssociatedIrp.SystemBuffer + dst_offset, (PUCHAR)list_entry->data + list_entry->offset, copy_length);
   1.181 -    list_entry->offset += copy_length;
   1.182 -    dst_offset += copy_length;
   1.183 -    if (list_entry->offset == list_entry->length)
   1.184 -    {
   1.185 -      // free the list entry
   1.186 -      // free the data
   1.187 -    }
   1.188 -    else
   1.189 -    {
   1.190 -      InsertHeadList(&dixc->read_list_head, (PLIST_ENTRY)list_entry);
   1.191 -    }      
   1.192 -  }
   1.193 -  KeReleaseSpinLock(&dixc->lock, old_irql);
   1.194 -KdPrint((__DRIVER_NAME "     F\n"));
   1.195 -  
   1.196 -  if (dst_offset > 0)
   1.197 -  {
   1.198 -    KdPrint((__DRIVER_NAME "     completing request\n"));
   1.199 -    status = STATUS_SUCCESS;
   1.200 -    irp->IoStatus.Status = status;
   1.201 -    irp->IoStatus.Information = dst_offset;
   1.202 -    IoSetCancelRoutine(irp, NULL);
   1.203 -    IoCompleteRequest(irp, IO_NO_INCREMENT);
   1.204 -  }
   1.205 -  else
   1.206 -  {
   1.207 -    KdPrint((__DRIVER_NAME "     pending request\n"));
   1.208 -    status = STATUS_PENDING;
   1.209 -  }
   1.210 -
   1.211 -  FUNCTION_EXIT();
   1.212 -
   1.213 -  return status;
   1.214 -}
   1.215 -
   1.216 -static VOID
   1.217 -XenPci_Irp_Read_Cancel(PDEVICE_OBJECT device_object, PIRP irp)
   1.218 -{
   1.219 -  PIO_STACK_LOCATION stack;
   1.220 -  PFILE_OBJECT file;
   1.221 -  device_interface_xenbus_context_t *dixc;
   1.222 -  KIRQL old_irql;
   1.223 -
   1.224 -  FUNCTION_ENTER();
   1.225 -
   1.226 -  UNREFERENCED_PARAMETER(device_object);
   1.227 -
   1.228 -  stack = IoGetCurrentIrpStackLocation(irp);
   1.229 -  file = stack->FileObject;
   1.230 -  dixc = file->FsContext;
   1.231 -  IoReleaseCancelSpinLock(irp->CancelIrql);
   1.232 -  KeAcquireSpinLock(&dixc->lock, &old_irql);
   1.233 -  if (irp != dixc->pending_read_irp)
   1.234 -  {
   1.235 -    KdPrint((__DRIVER_NAME "     Not the current irp???\n"));
   1.236 -  }
   1.237 -  dixc->pending_read_irp = NULL;
   1.238 -  irp->IoStatus.Status = STATUS_CANCELLED;
   1.239 -  irp->IoStatus.Information = 0;
   1.240 -  KeReleaseSpinLock(&dixc->lock, old_irql);
   1.241 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
   1.242 -
   1.243 -  FUNCTION_EXIT();
   1.244 -}
   1.245 -
   1.246 -NTSTATUS
   1.247 -XenPci_Irp_Read_XenBus(PDEVICE_OBJECT device_object, PIRP irp)
   1.248 -{
   1.249 -  NTSTATUS status;
   1.250 -  PIO_STACK_LOCATION stack;
   1.251 -  PFILE_OBJECT file;
   1.252 -  device_interface_xenbus_context_t *dixc;
   1.253 -  KIRQL old_irql;
   1.254 -
   1.255 -  UNREFERENCED_PARAMETER(device_object);
   1.256 -
   1.257 -  stack = IoGetCurrentIrpStackLocation(irp);
   1.258 -  file = stack->FileObject;
   1.259 -  dixc = file->FsContext;
   1.260 -
   1.261 -  ASSERT(!dixc->pending_read_irp);
   1.262 -  
   1.263 -  if (stack->Parameters.Read.Length == 0)
   1.264 -  {
   1.265 -    status = STATUS_SUCCESS;    
   1.266 -    irp->IoStatus.Status = status;
   1.267 -    irp->IoStatus.Information = 0;
   1.268 -    IoCompleteRequest(irp, IO_NO_INCREMENT);
   1.269 -  }
   1.270 -  else 
   1.271 -  {
   1.272 -    status = XenPci_Irp_Read_XenBus_Complete(dixc, irp);
   1.273 -    if (status == STATUS_PENDING)
   1.274 -    {
   1.275 -      IoMarkIrpPending(irp);
   1.276 -      KeAcquireSpinLock(&dixc->lock, &old_irql);
   1.277 -      dixc->pending_read_irp = irp;
   1.278 -      KeReleaseSpinLock(&dixc->lock, old_irql);
   1.279 -      IoSetCancelRoutine(irp, XenPci_Irp_Read_Cancel);
   1.280 -    }
   1.281 -  }
   1.282 -  return status;
   1.283 -}
   1.284 -
   1.285 -NTSTATUS
   1.286 -XenPci_Irp_Write_XenBus(PDEVICE_OBJECT device_object, PIRP irp)
   1.287 -{
   1.288 -  NTSTATUS status;
   1.289 -  PIO_STACK_LOCATION stack;
   1.290 -  PFILE_OBJECT file;
   1.291 -  device_interface_xenbus_context_t *dixc;
   1.292 -  PUCHAR src_ptr;
   1.293 -  ULONG src_len;
   1.294 -  PUCHAR dst_ptr;
   1.295 -  ULONG copy_len;
   1.296 -  struct xsd_sockmsg *rep;
   1.297 -  PXENPCI_DEVICE_DATA xpdd;
   1.298 -  KIRQL old_irql;
   1.299 -  xenbus_read_queue_item_t *list_entry;
   1.300 -  PIRP read_irp;
   1.301 -  NTSTATUS read_status;
   1.302 -  
   1.303 -  FUNCTION_ENTER();
   1.304 -  
   1.305 -  xpdd = device_object->DeviceExtension;
   1.306 -  stack = IoGetCurrentIrpStackLocation(irp);
   1.307 -  file = stack->FileObject;
   1.308 -  dixc = file->FsContext;
   1.309 -  
   1.310 -  KdPrint((__DRIVER_NAME "     write length = %d\n", stack->Parameters.Write.Length));
   1.311 -  
   1.312 -  src_ptr = (PUCHAR)irp->AssociatedIrp.SystemBuffer;
   1.313 -  src_len = stack->Parameters.Write.Length;
   1.314 -  dst_ptr = dixc->u.buffer + dixc->len;
   1.315 -  while (src_len != 0)
   1.316 -  {
   1.317 -    /* get a complete msg header */
   1.318 -    if (dixc->len < sizeof(dixc->u.msg))
   1.319 -    {
   1.320 -      copy_len = min(sizeof(dixc->u.msg) - dixc->len, src_len);
   1.321 -      if (!copy_len)
   1.322 -        continue;
   1.323 -      memcpy(dst_ptr, src_ptr, copy_len);
   1.324 -      dst_ptr += copy_len;
   1.325 -      src_ptr += copy_len;
   1.326 -      src_len -= copy_len;
   1.327 -      dixc->len += copy_len;
   1.328 -    }
   1.329 -    /* exit if we can't get that */
   1.330 -    if (dixc->len < sizeof(dixc->u.msg))
   1.331 -      continue;
   1.332 -    /* get a complete msg body */
   1.333 -    if (dixc->len < sizeof(dixc->u.msg) + dixc->u.msg.len)
   1.334 -    {
   1.335 -      copy_len = min(sizeof(dixc->u.msg) + dixc->u.msg.len - dixc->len, src_len);
   1.336 -      if (!copy_len)
   1.337 -        continue;
   1.338 -      memcpy(dst_ptr, src_ptr, copy_len);
   1.339 -      dst_ptr += copy_len;
   1.340 -      src_ptr += copy_len;
   1.341 -      src_len -= copy_len;
   1.342 -      dixc->len += copy_len;
   1.343 -    }
   1.344 -    /* exit if we can't get that */
   1.345 -    if (dixc->len < sizeof(dixc->u.msg) + dixc->u.msg.len)
   1.346 -    {
   1.347 -      continue;
   1.348 -    }
   1.349 -    
   1.350 -    rep = XenBus_Raw(xpdd, &dixc->u.msg);
   1.351 -    KeAcquireSpinLock(&dixc->lock, &old_irql);
   1.352 -    list_entry = (xenbus_read_queue_item_t *)ExAllocatePoolWithTag(NonPagedPool, sizeof(xenbus_read_queue_item_t), XENPCI_POOL_TAG);
   1.353 -    list_entry->data = rep;
   1.354 -    list_entry->length = sizeof(*rep) + rep->len;
   1.355 -    list_entry->offset = 0;
   1.356 -    InsertTailList(&dixc->read_list_head, (PLIST_ENTRY)list_entry);
   1.357 -    read_irp = dixc->pending_read_irp;
   1.358 -    dixc->pending_read_irp = NULL;
   1.359 -    KeReleaseSpinLock(&dixc->lock, old_irql);
   1.360 -    if (read_irp)
   1.361 -    {
   1.362 -      read_status = XenPci_Irp_Read_XenBus_Complete(dixc, read_irp);
   1.363 -      ASSERT(read_status == STATUS_SUCCESS);
   1.364 -    }
   1.365 -  }
   1.366 -  status = STATUS_SUCCESS;    
   1.367 -  irp->IoStatus.Status = status;
   1.368 -  irp->IoStatus.Information = stack->Parameters.Write.Length;
   1.369 -
   1.370 -  KdPrint((__DRIVER_NAME "     Information = %d\n", irp->IoStatus.Information));
   1.371 -
   1.372 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
   1.373 -
   1.374 -  FUNCTION_EXIT();
   1.375 -
   1.376 -  return status;
   1.377 -}
   1.378 -
   1.379 -NTSTATUS
   1.380 -XenPci_Irp_Cleanup_XenBus(PDEVICE_OBJECT device_object, PIRP irp)
   1.381 -{
   1.382 -  PXENPCI_DEVICE_DATA xpdd;
   1.383 -  NTSTATUS status;
   1.384 -
   1.385 -  xpdd = (PXENPCI_DEVICE_DATA)device_object->DeviceExtension;
   1.386 -  status = STATUS_SUCCESS;    
   1.387 -  irp->IoStatus.Status = status;
   1.388 -  IoCompleteRequest(irp, IO_NO_INCREMENT);
   1.389 -
   1.390 -  return status;
   1.391 -}
   1.392 -#endif
   1.393 \ No newline at end of file
   1.394 +}
   1.395 \ No newline at end of file