/*static char *SCCSID = "@(#)hybernat.h  6.1 94/07/13";*/
/*
 *      IBM Confidential
 *
 *      Copyright (c) IBM Corporation 1994
 *
 *      All Rights Reserved
 *
 * Change History
 *
 *  05/25/96  155605  cgw  clean up debug msgs to reduce memory use
 *  06/14/96  156773  TPL  Remove SWAPPER.DAT dependency
 */


/*
 * define hybernate debug msg levels for save and restore.
 * 0 = none, 1 = normal debug messages, 2+ = extra messages for development
 */
#if defined(DEBUG) | defined(PMDEBUG)
  #define HYBERDEBUG_S 1               /* debug save     - some messages     */
  #define HYBERDEBUG_R 1               /* debug restore  - some messages     */
#else
  #define HYBERDEBUG_S 0               /* retail save    - no messages       */
  #define HYBERDEBUG_R 0               /* retail restore - no messages       */
#endif

#pragma pack (1)
struct hyberComm_s {
        uchar_t         hc_dlm;                 /* divisor latch, most  */
        uchar_t         hc_dll;                 /* divisor latch, low   */
        uchar_t         hc_mcr;                 /* modem control        */
        uchar_t         hc_ien;                 /* interrupt enable     */
        uchar_t         hc_lcr;                 /* line control reg     */
        uchar_t         pad;
};
typedef struct hyberComm_s hyberComm_t;

struct dlist_s {
        unsigned long dl_sector;
        unsigned long dl_count;
};
typedef struct dlist_s dlist_t;

struct plist_s {
        unsigned short pl_frame;
        unsigned short pl_count;
};
typedef struct plist_s plist_t;

struct hyberInfoHdr_s {
        char            hih_magicID[12];
        // loader disk info
#ifndef HYBERI13
        long            hih_maxblk;             /* i/o multiple         */
        long            hih_SpT;                /* sectors per track    */
        long            hih_SpC;                /* sectors per cylinder */
        ushort_t        hih_avail;              /* avalible for use     */
#else  /* HYBERI13 */
        uchar_t  hih_drive;                     /* drive id, 80 = c:  */
        uchar_t  hih_avail;                     /* avalible for use     */
#endif /* HYBERI13 */
        // memory used by the loader
        ushort_t        hih_dataloc;            /* in pages             */
        ushort_t        hih_hldrloc;            /* in pages             */
#ifdef HYBERI13
        ushort_t        hih_iobuf;                 /* in pages          */
        ushort_t        hih_iobuflen;  /* in pages      */
#endif /* HYBERI13 */
        // information about hyberinfo
        ushort_t        hih_pfcnt;              /* # entries            */
        ulong_t         hih_dli;                /* last index of dlist  */
        ulong_t         hih_pli;                /* last index of plist  */
        // hardware state
        ushort_t        hih_GDTFrame;           /* GDT phys loc         */
        ushort_t        hih_gdt;                /* gdt limit            */
        ulong_t         hih_gdt2;               /* gdt base             */
        ushort_t        hih_cs;                 /* cs                   */
        ushort_t        hih_ds;                 /* ds                   */
        ushort_t        hih_ss;                 /* ss                   */
        ushort_t        hih_fs;                 /* fs                   */
        ushort_t        hih_gs;                 /* gs                   */
        ushort_t        hih_idt;                /* idt limit            */
        ulong_t         hih_idt2;               /* idt base             */
        ulong_t         hih_esp;                /* esp                  */
        ulong_t         hih_ebp;                /* ebp                  */
        ulong_t         hih_cr0;                /* cr0                  */
        ulong_t         hih_cr2;                /* cr2                  */
        ulong_t         hih_cr3;                /* cr3                  */
        ulong_t         hih_cr4;                /* cr4                  */
        ushort_t        hih_ldt;                /* ldt                  */
        ushort_t        hih_tr;                 /* task register        */
        ulong_t         hih_flags;              /* flags                */
        ulong_t         hih_reentry;            /* kernel restart code  */
        ulong_t         hih_pt0;                /* page dir entry 0     */
#ifdef HYBERI13
        ulong_t         hih_tkssb;  /* _TKSSBase        */
        ulong_t         hih_ptkssb; /* addr of _TKSSBase        */
#endif /* HYBERI13 */
        uchar_t         hih_rtca;               /* rtc status a         */
        uchar_t         hih_rtcb;               /* rtc status b         */
        uchar_t         hih_tmr0;               /* pit 0 status         */
        uchar_t         hih_tmr1;               /* pit 1 status         */
        uchar_t         hih_tmr2;               /* pit 2 status         */
        uchar_t         hih_pad;                /* avail                */
        uchar_t         hih_pic1mask;           /* pic 1 mask           */
        uchar_t         hih_pic2mask;           /* pic 2 mask           */
        struct hyberComm_s      hih_com1;       /* com1 state           */
        struct hyberComm_s      hih_com2;       /* com2 state           */
        struct hyberComm_s      hih_com3;       /* com3 state           */
        struct hyberComm_s      hih_com4;       /* com4 state           */
#ifdef HYBERI13
        uchar_t     hih_gdtsave[16];     /* save temp gdt entries */
#endif /* HYBERI13 */
        // misc
        ushort_t        hih_debugport;          /* debugger comm port   */
        ushort_t        hih_hyberflags;         /* misc flags           */
        // BE-A-WARE - offsets after here may differ between MSc & Cset
        struct dlist_s  *dlist;                 /* disk list offset     */
        struct plist_s  *plist;                 /* page list offset     */
};
typedef struct hyberInfoHdr_s hyberInfoHdr_t;

struct hyberInfo_s {
        ushort_t        hi_pfnum;
        ushort_t        hi_sfnum;
        ulong_t         hi_chksum;
};
typedef struct hyberInfo_s hyberInfo_t;

#ifdef HYBERI13
#define TEMPGDT      0x60    /* 2 temp gdt entries */
#endif /* HYBERI13 */
#define HYBERPHYSMEM    0xc0000000
#define HYBERFILENAME   "\\swapper2.dat"
#define SECTORSIZE      512
#ifndef PAGESIZE
#define PAGESIZE        4096
#endif
#define VME_supported   2               // Processor supports virt. mode ext.

/* Define hyberflag values */
#define HF_LOADCR4      0x0001
#define HF_APM16        0x0002
#define HF_APM32        0x0004
#define HF_STICKY       0x0008

/* Define hyberStat values */
#define HYBR_AGING              0x01
#define HYBR_AGERDONE           0x10
#define HYBR_OK                 0x20
#define HYBR_FLUSHBUF2          0x30
// in hybernate proper from here on
#define HYBR_INIT               0x40
#define HYBR_FLUSHBUF3          0x50
#define HYBR_UNDERWAY           0x60
#define HYBR_DUMPING            0x70
#define HYBR_COMPLETE           0x80
#define HYBR_COMINGUP           0x90

/* offsets in the hyberOpPkt - this pkt is the mechanism by which the driver
   passes info to the kernel hybernate function */
#define HOP_DLIST       0                       /* swap file extents */
#define HOP_DLIST_CHS   1                       /* swap file CHS struc */
#define HOP_INFOFILE    2                       /* info file extent */
#define HOP_INFO_CHS    3                       /* info file CHS struc */
#define HOP_OP          4                       /* requested operation */
#define HOP_DOSDIR      5                       /* ptr to DOS dir */
#define HOP_BIOFILE     6                       /* ptr to IBMBIO */
#define HOP_BPB         7                       /* ptr to boot BPB */
#define HOP_SIZE        8                       /* used to size pkt */

/* the hyberOpPkt[HOP_DLIST_CHS] and hyberOpPkt[HOP_INFO_CHS] contain
   a pointer to an array (offset within the DCB) with the following
   elements:      ULONG   DiskNum
                  ULONG   NumCyl
                  ULONG   NumHeads
                  ULONG   SecPerTrk
 */

/* To deal with variations in the op types we split the HOP_OP d-word in
   half. The low order word for HOP_OP itself and the high will just be a
   bit mask. */
#define HOPOP_MASK      0xffff
#define HOPOP_REBOOT    0x0001
/* HOP_OP values */
#define HOPOP_HYBERNATE 0                       /* basic - sleep the system */
#define HOPOP_TRAPDOOR  1
#define HOPOP_NotUsed   2                       /* avail */
#define HOPOP_STICKY    3                       /* HOPOP_HYBERNATE variation */

/* The following are used by the hybernat I/O world. */

struct mydata {
#ifndef HYBERI13
        long maxblk;                            /* multi max size */
        long SpT;                               /* sectors per track */
        long SpC;                               /* sectors per cylinder */
#endif /* HYBERI13 */
        struct dlist_s *pdlist;                 /* disk list */
        struct plist_s *pplist;                 /* page list */
        int dli;                                /* dlist index */
        int pli;                                /* plist index */
        ulong_t sectorCount;                    /* used to track interrupts */
#ifndef HYBERI13
        long maxio;                             /* longest io to attempt */
#endif /* HYBERI13  */
        ushort_t scratch1;                      /* rfe/debug */
        ushort_t scratch2;                      /* rfe */
};
typedef struct mydata mydata_t;

/*XLATOFF*/

#ifndef HYBERI13
#define maxblk          (mydata->maxblk)
#define maxio           (mydata->maxio)
#define SpT             (mydata->SpT)
#define SpC             (mydata->SpC)
#endif /* HYBERI13 */
#define disklist        (mydata->pdlist)
#define pagelist        (mydata->pplist)
#define dlSiz           (mydata->dli)
#define plSiz           (mydata->pli)
#define sectorCount     (mydata->sectorCount)
#define scratch         (mydata->scratch1)

enum { /* System Controls Codes */

  SYS_RESETSTAT       = 11,
  SYS_HIBERNATE       = 12,
  SYS_GETHYBERSTAT    = 13,
  SYS_FREEZE_THAW_SYS = 14,
  SYS_SYNCFS          = 15,
  SYS_SWAPOUT         = 16,
  SYS_HYBEROK         = 17,
  SYS_SWAPPATH        = 18,
  SYS_CHKSTICKY       = 19,
  SYS_SAVERESTORE     = 20

};    /* System Control Codes */

#define SYS_HY_START    SYS_RESETSTAT     /* start of hybernation SYSCTL func */
#define SYS_HY_END      SYS_SAVERESTORE   /* end of hybernation SYSCTL func   */

void hyberDispInfo();
int  hybernate();
int  hyberOK();
//156773 void hyberFreeSwapper();
int  hyberFreezeSys();
char *getSwapperPath();

/*XLATON*/
#pragma pack()
