/*static char *SCCSID = "@(#)resetioh.c	6.1 90/11/17";*/
/*static char *SCCSID = "@(#)resetioh.c 13.5 90/02/16";*/

#include <os2def16.h>
#include <strat2.h>

#include "const.h"
#include "misc.h"
#include "buf.h"
#include "chain.h"
#include "dir.h"
#include "volume.h"
#include "storage.h"
#include "tables.h"
#include "frame.h"
#include "lw.h"
#include "fsdata.h"
#include "asyncio.h"
#include "assert.h"

#include "pr_misc.h"

/*
 * VOID ResetIOH( pioh, ULONG flFlags );
 *
 * Called to recycle an IO Handle after ExecIOH has been called
 *
 * Parameter:
 *     pioh	   Pointer to I/O Handle usually obtained from GetIOHand()
 *
 *     flFlags
 *	   The hi 5 bits should have the vol bits from a vlsn indicating
 *	   which volume this handle will be associatied with.
 *
 *	   The low word should be set with flag bits defined as follows:
 *	      (0x0004) GIH_INORDER     process packettes in order of appearence
 *	      (0x0008) GIH_ABORTERR    stop processing packettes on the 1st disk
 *				       error
 *	      (0x4000) GIH_PHYSADDR    indicates the addresses sent to
 *				       QDiskOp for this IOH will be physical
 *				       addresses instead of 16:16
 *	      (0x8000) GIH_AUTOFLUSH   if set, this bit causes QDiskOp to
 *				       post this command chain to the disk
 *				       driver and block, if there is not
 *				       enough room for another operation.
 *
 * ResetIOH initializes all the fields of an IO Handle so it can be
 * used with subsequent QDiskOp and ExecIOH calls.  Handles obtained by
 * GetIOHand are already initialized and therefore do not need to be
 * passed through ResetIOH before they can be used.  ResetIOH
 * is most useful for resetting an IO Handle that has been filled
 * using QDiskOp and executed using ExecIOH.  The cIOReq parameter
 * passed to GetIOHand can not be changed with ResetIOH.
 */
VOID ResetIOH( PIOH pioh, ULONG flFlags ) {

    /*
     * with pioh->exec-list do
     *	   BEGIN
     *
     *	   Request List Count = 0
     *
     *	   Lst Notify Address = NULL
     *
     *	   List Request Control = low bit of the low word of flFlags| EXCSingleReq
     *
     *	   Unit Number = volume from vol bits in flFlags
     *
     *	   List Status = 0
     *	   END;
     */
    PEXECLIST pxlh = GetIOListHead( pioh );

    ASSERT_IOH(pioh);

    pxlh->ELHdr.Count		   = 0;
    pxlh->ELHdr.Notify_Address	   = NULL;

    pxlh->ELHdr.Request_Control    = RLH_Req_From_PB |
				    (LOUSHORT( flFlags ) & ~GIH_HIFLAGS);


#ifdef DEBUG
    /* Make sure vol bits are on    */
    if( (flFlags & ~SECMASK) == 0 ) {
	INTERR( __FILE__": no vol bits!" );
    }
#endif

    pxlh->ELHdr.Block_Dev_Unit =
		   (pioh->pvol = VolPtr[ IVOLFROMLSN( flFlags ) ])->idLogDisk;

    pxlh->ELHdr.Lst_Status	   = 0;


    /*
     * Set the hibits of the low word of flFlags in the flags field of
     * the IOH
     */
    pioh->usFlags = (LOUSHORT( flFlags ) & GIH_HIFLAGS);

    /* set the pointer to the current IOReq in the IOH to 0 */
    pioh->preqCurr = NULL;


#ifdef DEBUG
    if( pioh->plhChain != NULL )
	INTERR( __FILE__": PIOH might have locked pages" );
#endif

    /* Init the lock handle chain, and cbLeft */
    pioh->semDone = NULL;
    pioh->piohNext = NULL;
    pioh->bDummy = NULL;         //#85759
    pioh->pVirtData = NULL;      //#85759

    pioh->plhChain = NULL;
    pioh->cbLeft = pioh->cbTotal;

#ifdef DEBUG
    /* clear out the pioh chain, just in case */
    pioh->pioh = NULL;
#endif
}
