Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

672 lines
20 KiB

  1. /*--------------------------------------------------------------------------
  2. *
  3. * Copyright (C) Cyclades Corporation, 1996-2001.
  4. * All rights reserved.
  5. *
  6. * Cyclom-Y Port Driver
  7. *
  8. * This file: cyymask.c
  9. *
  10. * Description: This module contains the code related to get/set/wait
  11. * on event mask operations in the Cyclom-Y Port driver.
  12. *
  13. * Notes: This code supports Windows 2000 and Windows XP,
  14. * x86 and IA64 processors.
  15. *
  16. * Complies with Cyclades SW Coding Standard rev 1.3.
  17. *
  18. *--------------------------------------------------------------------------
  19. */
  20. /*-------------------------------------------------------------------------
  21. *
  22. * Change History
  23. *
  24. *--------------------------------------------------------------------------
  25. *
  26. *
  27. *--------------------------------------------------------------------------
  28. */
  29. #include "precomp.h"
  30. BOOLEAN
  31. CyyGrabWaitFromIsr(
  32. IN PVOID Context
  33. );
  34. BOOLEAN
  35. CyyGiveWaitToIsr(
  36. IN PVOID Context
  37. );
  38. BOOLEAN
  39. CyyFinishOldWait(
  40. IN PVOID Context
  41. );
  42. #ifdef ALLOC_PRAGMA
  43. #pragma alloc_text(PAGESER,CyyStartMask)
  44. #pragma alloc_text(PAGESER,CyyCancelWait)
  45. #pragma alloc_text(PAGESER,CyyGrabWaitFromIsr)
  46. #pragma alloc_text(PAGESER,CyyGiveWaitToIsr)
  47. #pragma alloc_text(PAGESER,CyyFinishOldWait)
  48. #endif
  49. NTSTATUS
  50. CyyStartMask(
  51. IN PCYY_DEVICE_EXTENSION Extension
  52. )
  53. /*++
  54. Routine Description:
  55. This routine is used to process the set mask and wait
  56. mask ioctls. Calls to this routine are serialized by
  57. placing irps in the list under the protection of the
  58. cancel spin lock.
  59. Arguments:
  60. Extension - A pointer to the serial device extension.
  61. Return Value:
  62. Will return pending for everything put the first
  63. request that we actually process. Even in that
  64. case it will return pending unless it can complete
  65. it right away.
  66. --*/
  67. {
  68. //
  69. // The current stack location. This contains much of the
  70. // information we need to process this particular request.
  71. //
  72. PIO_STACK_LOCATION IrpSp;
  73. PIRP NewIrp;
  74. BOOLEAN SetFirstStatus = FALSE;
  75. NTSTATUS FirstStatus;
  76. CYY_LOCKED_PAGED_CODE();
  77. CyyDbgPrintEx(CYYDIAG3, "In CyyStartMask\n");
  78. ASSERT(Extension->CurrentMaskIrp);
  79. do {
  80. CyyDbgPrintEx(CYYDIAG4, "STARTMASK - CurrentMaskIrp: %x\n",
  81. Extension->CurrentMaskIrp);
  82. IrpSp = IoGetCurrentIrpStackLocation(Extension->CurrentMaskIrp);
  83. ASSERT((IrpSp->Parameters.DeviceIoControl.IoControlCode ==
  84. IOCTL_SERIAL_WAIT_ON_MASK) ||
  85. (IrpSp->Parameters.DeviceIoControl.IoControlCode ==
  86. IOCTL_SERIAL_SET_WAIT_MASK));
  87. if (IrpSp->Parameters.DeviceIoControl.IoControlCode ==
  88. IOCTL_SERIAL_SET_WAIT_MASK) {
  89. CyyDbgPrintEx(CYYDIAG4, "%x is a SETMASK irp\n",
  90. Extension->CurrentMaskIrp);
  91. //
  92. // Complete the old wait if there is one.
  93. //
  94. KeSynchronizeExecution(
  95. Extension->Interrupt,
  96. CyyFinishOldWait,
  97. Extension
  98. );
  99. //
  100. // Any current waits should be on its way to completion
  101. // at this point. There certainly shouldn't be any
  102. // irp mask location.
  103. //
  104. ASSERT(!Extension->IrpMaskLocation);
  105. Extension->CurrentMaskIrp->IoStatus.Status = STATUS_SUCCESS;
  106. if (!SetFirstStatus) {
  107. CyyDbgPrintEx(CYYDIAG4, "%x was the first irp processed by "
  108. "this\n"
  109. "------- invocation of startmask\n",
  110. Extension->CurrentMaskIrp);
  111. FirstStatus = STATUS_SUCCESS;
  112. SetFirstStatus = TRUE;
  113. }
  114. //
  115. // The following call will also cause the current
  116. // call to be completed.
  117. //
  118. CyyGetNextIrp(
  119. &Extension->CurrentMaskIrp,
  120. &Extension->MaskQueue,
  121. &NewIrp,
  122. TRUE,
  123. Extension
  124. );
  125. CyyDbgPrintEx(CYYDIAG4, "Perhaps another mask irp was found in "
  126. "the queue\n"
  127. "------- %x/%x <- values should be the same\n",
  128. Extension->CurrentMaskIrp,NewIrp);
  129. } else {
  130. //
  131. // First make sure that we have a non-zero mask.
  132. // If the app queues a wait on a zero mask it can't
  133. // be statisfied so it makes no sense to start it.
  134. //
  135. if ((!Extension->IsrWaitMask) || (Extension->CurrentWaitIrp)) {
  136. CyyDbgPrintEx(CYYDIAG4, "WaitIrp is invalid\n"
  137. "------- IsrWaitMask: %x\n"
  138. "------- CurrentWaitIrp: %x\n",
  139. Extension->IsrWaitMask,
  140. Extension->CurrentWaitIrp);
  141. Extension->CurrentMaskIrp->IoStatus.Status
  142. = STATUS_INVALID_PARAMETER;
  143. if (!SetFirstStatus) {
  144. CyyDbgPrintEx(CYYDIAG4, "%x was the first irp processed "
  145. "by this\n"
  146. "------- invocation of startmask\n",
  147. Extension->CurrentMaskIrp);
  148. FirstStatus = STATUS_INVALID_PARAMETER;
  149. SetFirstStatus = TRUE;
  150. }
  151. CyyGetNextIrp(&Extension->CurrentMaskIrp,
  152. &Extension->MaskQueue, &NewIrp, TRUE,
  153. Extension);
  154. CyyDbgPrintEx(CYYDIAG4, "Perhaps another mask irp was found "
  155. "in the queue\n"
  156. "------- %x/%x <- values should be the same\n",
  157. Extension->CurrentMaskIrp,NewIrp);
  158. } else {
  159. KIRQL OldIrql;
  160. //
  161. // Make the current mask irp the current wait irp and
  162. // get a new current mask irp. Note that when we get
  163. // the new current mask irp we DO NOT complete the
  164. // old current mask irp (which is now the current wait
  165. // irp.
  166. //
  167. // Then under the protection of the cancel spin lock
  168. // we check to see if the current wait irp needs to
  169. // be canceled
  170. //
  171. IoAcquireCancelSpinLock(&OldIrql);
  172. if (Extension->CurrentMaskIrp->Cancel) {
  173. CyyDbgPrintEx(CYYDIAG4, "%x irp was already marked as "
  174. "cancelled\n", Extension->CurrentMaskIrp);
  175. IoReleaseCancelSpinLock(OldIrql);
  176. Extension->CurrentMaskIrp->IoStatus.Status = STATUS_CANCELLED;
  177. if (!SetFirstStatus) {
  178. CyyDbgPrintEx(CYYDIAG4, "%x was the first irp "
  179. "processed by this\n"
  180. "------- invocation of startmask\n",
  181. Extension->CurrentMaskIrp);
  182. FirstStatus = STATUS_CANCELLED;
  183. SetFirstStatus = TRUE;
  184. }
  185. CyyGetNextIrp(&Extension->CurrentMaskIrp,
  186. &Extension->MaskQueue, &NewIrp, TRUE,
  187. Extension);
  188. CyyDbgPrintEx(CYYDIAG4, "Perhaps another mask irp was "
  189. "found in the queue\n"
  190. "------- %x/%x <- values should be the "
  191. "same\n", Extension->CurrentMaskIrp,
  192. NewIrp);
  193. } else {
  194. CyyDbgPrintEx(CYYDIAG4, "%x will become the current "
  195. "wait irp\n", Extension->CurrentMaskIrp);
  196. if (!SetFirstStatus) {
  197. CyyDbgPrintEx(CYYDIAG4, "%x was the first irp "
  198. "processed by this\n"
  199. "------- invocation of startmask\n",
  200. Extension->CurrentMaskIrp);
  201. FirstStatus = STATUS_PENDING;
  202. SetFirstStatus = TRUE;
  203. //
  204. // If we haven't already set a first status
  205. // then there is a chance that this packet
  206. // was never on the queue. We should mark
  207. // it as pending.
  208. //
  209. IoMarkIrpPending(Extension->CurrentMaskIrp);
  210. }
  211. //
  212. // There should never be a mask location when
  213. // there isn't a current wait irp. At this point
  214. // there shouldn't be a current wait irp also.
  215. //
  216. ASSERT(!Extension->IrpMaskLocation);
  217. ASSERT(!Extension->CurrentWaitIrp);
  218. Extension->CurrentWaitIrp = Extension->CurrentMaskIrp;
  219. SERIAL_INIT_REFERENCE(Extension->CurrentWaitIrp);
  220. IoSetCancelRoutine(
  221. Extension->CurrentWaitIrp,
  222. CyyCancelWait
  223. );
  224. //
  225. // Since the cancel routine has a reference to
  226. // the irp we need to update the reference
  227. // count.
  228. //
  229. SERIAL_SET_REFERENCE(
  230. Extension->CurrentWaitIrp,
  231. SERIAL_REF_CANCEL
  232. );
  233. KeSynchronizeExecution(
  234. Extension->Interrupt,
  235. CyyGiveWaitToIsr,
  236. Extension
  237. );
  238. // Code removed because it was causing blue-screen in the Modem Share
  239. // test case 77. When CurrentMaskIrp is set to NULL, we remove the
  240. // protection that avoid Starter to be called in CyyStartOrQueue.
  241. // We will let CyyGetNextIrp null out that pointer. Fanny.
  242. //
  243. // //
  244. // // Since it isn't really the mask irp anymore,
  245. // // null out that pointer.
  246. // //
  247. //
  248. // Extension->CurrentMaskIrp = NULL;
  249. Extension->CurrentMaskIrp = NULL; // back in Windows 2000. Fanny
  250. //
  251. // This will release the cancel spinlock for us
  252. //
  253. CyyGetNextIrpLocked(&Extension->CurrentMaskIrp,
  254. &Extension->MaskQueue, &NewIrp,
  255. FALSE, Extension, OldIrql);
  256. CyyDbgPrintEx(CYYDIAG4, "Perhaps another mask irp was "
  257. "found in the queue\n"
  258. "------- %x/%x <- values should be the "
  259. "same\n", Extension->CurrentMaskIrp,
  260. NewIrp);
  261. }
  262. }
  263. }
  264. } while (NewIrp);
  265. return FirstStatus;
  266. }
  267. BOOLEAN
  268. CyyGrabWaitFromIsr(
  269. IN PVOID Context
  270. )
  271. /*++
  272. Routine Description:
  273. This routine will check to see if the ISR still knows about
  274. a wait irp by checking to see if the IrpMaskLocation is non-null.
  275. If it is then it will zero the Irpmasklocation (which in effect
  276. grabs the irp away from the isr). This routine is only called
  277. buy the cancel code for the wait.
  278. NOTE: This is called by KeSynchronizeExecution.
  279. Arguments:
  280. Context - A pointer to the device extension
  281. Return Value:
  282. Always FALSE.
  283. --*/
  284. {
  285. PCYY_DEVICE_EXTENSION Extension = Context;
  286. CYY_LOCKED_PAGED_CODE();
  287. CyyDbgPrintEx(CYYDIAG3, "In CyyGrabWaitFromIsr\n");
  288. if (Extension->IrpMaskLocation) {
  289. CyyDbgPrintEx(CYYDIAG4, "The isr still owns the irp %x, mask "
  290. "location is %x\n"
  291. "------- and system buffer is %x\n",
  292. Extension->CurrentWaitIrp,Extension->IrpMaskLocation,
  293. Extension->CurrentWaitIrp->AssociatedIrp.SystemBuffer);
  294. //
  295. // The isr still "owns" the irp.
  296. //
  297. *Extension->IrpMaskLocation = 0;
  298. Extension->IrpMaskLocation = NULL;
  299. Extension->CurrentWaitIrp->IoStatus.Information = sizeof(ULONG);
  300. //
  301. // Since the isr no longer references the irp we need to
  302. // decrement the reference count.
  303. //
  304. SERIAL_CLEAR_REFERENCE(
  305. Extension->CurrentWaitIrp,
  306. SERIAL_REF_ISR
  307. );
  308. }
  309. return FALSE;
  310. }
  311. BOOLEAN
  312. CyyGiveWaitToIsr(
  313. IN PVOID Context
  314. )
  315. /*++
  316. Routine Description:
  317. This routine simply sets a variable in the device extension
  318. so that the isr knows that we have a wait irp.
  319. NOTE: This is called by KeSynchronizeExecution.
  320. NOTE: This routine assumes that it is called with the
  321. cancel spinlock held.
  322. Arguments:
  323. Context - Simply a pointer to the device extension.
  324. Return Value:
  325. Always FALSE.
  326. --*/
  327. {
  328. PCYY_DEVICE_EXTENSION Extension = Context;
  329. CYY_LOCKED_PAGED_CODE();
  330. CyyDbgPrintEx(CYYDIAG3, "In CyyGiveWaitToIsr\n");
  331. //
  332. // There certainly shouldn't be a current mask location at
  333. // this point since we have a new current wait irp.
  334. //
  335. ASSERT(!Extension->IrpMaskLocation);
  336. //
  337. // The isr may or may not actually reference this irp. It
  338. // won't if the wait can be satisfied immediately. However,
  339. // since it will then go through the normal completion sequence,
  340. // we need to have an incremented reference count anyway.
  341. //
  342. SERIAL_SET_REFERENCE(
  343. Extension->CurrentWaitIrp,
  344. SERIAL_REF_ISR
  345. );
  346. if (!Extension->HistoryMask) {
  347. CyyDbgPrintEx(CYYDIAG4, "No events occured prior to the wait call"
  348. "\n");
  349. //
  350. // Although this wait might not be for empty transmit
  351. // queue, it doesn't hurt anything to set it to false.
  352. //
  353. Extension->EmptiedTransmit = FALSE;
  354. //
  355. // Record where the "completion mask" should be set.
  356. //
  357. Extension->IrpMaskLocation =
  358. Extension->CurrentWaitIrp->AssociatedIrp.SystemBuffer;
  359. CyyDbgPrintEx( CYYDIAG4, "The isr owns the irp %x, mask location is "
  360. "%x\n"
  361. "------- and system buffer is %x\n",
  362. Extension->CurrentWaitIrp,Extension->IrpMaskLocation,
  363. Extension->CurrentWaitIrp->AssociatedIrp
  364. .SystemBuffer);
  365. } else {
  366. CyyDbgPrintEx(CYYDIAG4, "%x occurred prior to the wait - starting "
  367. "the\n"
  368. "------- completion code for %x\n",
  369. Extension->HistoryMask,Extension->CurrentWaitIrp);
  370. *((ULONG *)Extension->CurrentWaitIrp->AssociatedIrp.SystemBuffer) =
  371. Extension->HistoryMask;
  372. Extension->HistoryMask = 0;
  373. Extension->CurrentWaitIrp->IoStatus.Information = sizeof(ULONG);
  374. Extension->CurrentWaitIrp->IoStatus.Status = STATUS_SUCCESS;
  375. CyyInsertQueueDpc(&Extension->CommWaitDpc, NULL, NULL, Extension);
  376. }
  377. return FALSE;
  378. }
  379. BOOLEAN
  380. CyyFinishOldWait(
  381. IN PVOID Context
  382. )
  383. /*++
  384. Routine Description:
  385. This routine will check to see if the ISR still knows about
  386. a wait irp by checking to see if the Irpmasklocation is non-null.
  387. If it is then it will zero the Irpmasklocation (which in effect
  388. grabs the irp away from the isr). This routine is only called
  389. buy the cancel code for the wait.
  390. NOTE: This is called by KeSynchronizeExecution.
  391. Arguments:
  392. Context - A pointer to the device extension
  393. Return Value:
  394. Always FALSE.
  395. --*/
  396. {
  397. PCYY_DEVICE_EXTENSION Extension = Context;
  398. CYY_LOCKED_PAGED_CODE();
  399. CyyDbgPrintEx(CYYDIAG3, "In CyyFinishOldWait\n");
  400. if (Extension->IrpMaskLocation) {
  401. CyyDbgPrintEx(CYYDIAG4, "The isr still owns the irp %x, mask "
  402. "location is %x\n"
  403. "------- and system buffer is %x\n",
  404. Extension->CurrentWaitIrp,Extension->IrpMaskLocation,
  405. Extension->CurrentWaitIrp->AssociatedIrp.SystemBuffer);
  406. //
  407. // The isr still "owns" the irp.
  408. //
  409. *Extension->IrpMaskLocation = 0;
  410. Extension->IrpMaskLocation = NULL;
  411. Extension->CurrentWaitIrp->IoStatus.Information = sizeof(ULONG);
  412. //
  413. // We don't decrement the reference since the completion routine
  414. // will do that.
  415. //
  416. CyyInsertQueueDpc(&Extension->CommWaitDpc, NULL, NULL, Extension);
  417. }
  418. //
  419. // Don't wipe out any historical data we are still interested in.
  420. //
  421. Extension->HistoryMask &= *((ULONG *)Extension->CurrentMaskIrp->
  422. AssociatedIrp.SystemBuffer);
  423. Extension->IsrWaitMask = *((ULONG *)Extension->CurrentMaskIrp->
  424. AssociatedIrp.SystemBuffer);
  425. CyyDbgPrintEx( CYYDIAG4, "Set mask location of %x, in irp %x, with "
  426. "system buffer of %x\n",
  427. Extension->IrpMaskLocation, Extension->CurrentMaskIrp,
  428. Extension->CurrentMaskIrp->AssociatedIrp.SystemBuffer);
  429. return FALSE;
  430. }
  431. VOID
  432. CyyCancelWait(
  433. IN PDEVICE_OBJECT DeviceObject,
  434. IN PIRP Irp
  435. )
  436. /*++
  437. Routine Description:
  438. This routine is used to cancel a irp that is waiting on
  439. a comm event.
  440. Arguments:
  441. DeviceObject - Pointer to the device object for this device
  442. Irp - Pointer to the IRP for the current request
  443. Return Value:
  444. None.
  445. --*/
  446. {
  447. PCYY_DEVICE_EXTENSION Extension = DeviceObject->DeviceExtension;
  448. CYY_LOCKED_PAGED_CODE();
  449. CyyDbgPrintEx(CYYDIAG3, "In CyyCancelWait\n");
  450. CyyDbgPrintEx(CYYDIAG4, "Canceling wait for irp %x\n",
  451. Extension->CurrentWaitIrp);
  452. CyyTryToCompleteCurrent(Extension, CyyGrabWaitFromIsr,
  453. Irp->CancelIrql, STATUS_CANCELLED,
  454. &Extension->CurrentWaitIrp, NULL, NULL, NULL,
  455. NULL, NULL, SERIAL_REF_CANCEL);
  456. }
  457. VOID
  458. CyyCompleteWait(
  459. IN PKDPC Dpc,
  460. IN PVOID DeferredContext,
  461. IN PVOID SystemContext1,
  462. IN PVOID SystemContext2
  463. )
  464. {
  465. PCYY_DEVICE_EXTENSION Extension = DeferredContext;
  466. KIRQL OldIrql;
  467. CyyDbgPrintEx(DPFLTR_TRACE_LEVEL, ">CyyCompleteWait(%X)\n",
  468. Extension);
  469. CyyDbgPrintEx(CYYDIAG3, "In CyyCompleteWait\n");
  470. UNREFERENCED_PARAMETER(SystemContext1);
  471. UNREFERENCED_PARAMETER(SystemContext2);
  472. IoAcquireCancelSpinLock(&OldIrql);
  473. CyyDbgPrintEx(CYYDIAG4, "Completing wait for irp %x\n",
  474. Extension->CurrentWaitIrp);
  475. CyyTryToCompleteCurrent(Extension, NULL, OldIrql, STATUS_SUCCESS,
  476. &Extension->CurrentWaitIrp, NULL, NULL, NULL,
  477. NULL, NULL, SERIAL_REF_ISR);
  478. CyyDpcEpilogue(Extension, Dpc);
  479. CyyDbgPrintEx(DPFLTR_TRACE_LEVEL, "<CyyCompleteWait\n");
  480. }
  481.