hd.c 56.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
/*
 * Copyright (C) 1996-1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * 
 * This file is part of the SimOS distribution. 
 * See LICENSE file for terms of the license. 
 *
 */

/*****************************************************************
 * simhd.c
 *
 * Simple SCSI disk device emulation. Emulates a number of
 * independent disks. Correctly models disk latencies and
 * DMA transfers, but does not model disk controller contention
 * (in fact, there is no real notion of a disk controller in
 * this model).
 *
 *   This model has evolved from a very simple hard disk driver that worked
 *   with the IRIX "sable" disk driver to someone that can act as a simple
 *   SCSI disk simulator as well as a disk driver.
 *
 *   Data handling now supported. The DMA uses in all cases
 *   an intermediate buffer for the scatter/gather
 *
 * Created by: ??
 * Revised by:
 *   Dan Teodosiu, 07/96  Cleaned up code, changed buffering scheme.
 *   Dan Teodosiu, 05/97  Multiple controllers / node.
 *
 ****************************************************************/


#include <stdio.h>
#include <stdlib.h> /* For the use of calloc() */
#include <string.h>
#include <sys/types.h>
#ifndef __alpha
#ifndef i386
#include <sys/unistd.h>
#endif
#endif
#include <sys/mman.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/signal.h>
#ifndef __alpha
#ifndef i386
#include <sys/ioccom.h>
#include <sys/filio.h>
#endif
#endif
#include <sys/time.h>
#include <sys/errno.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
/* #include <poll.h> */

#include "sim.h"
#include "syslimits.h"
#include "simtypes.h"
#include "hd.h"
#include "checkpoint.h"
#include "machine_params.h"
#include "cpu_interface.h"
#include "sim_error.h"

#include "dma.h"
#include "../disk/simos_interface.h"
#include "remote_access.h"
#include "rmtaccess.h"
#include "simutil.h"
#include "arch_specifics.h"

/*
 * SCSI commands.
 *
 * Not all of these are supported in this device. Currently, only
 * the commands used by IRIX are implemented.
 */
#define CMD_TST_UNIT_RDY	0x0
#define CMD_REQ_SENSE		0x3
#define CMD_FORMAT		0x4
#define CMD_ADD_DEFECTS		0x7
#define CMD_READ_6              0x8
#define CMD_WRITE_6             0xa
#define CMD_INQUIRY		0x12
#define CMD_MODE_SELECT		0x15
#define CMD_MODE_SENSE		0x1a
#define CMD_STARTUNIT		0x1b
#define CMD_SEND_DIAG		0x1d
#define CMD_PREVREM		0x1e
#define CMD_READCAPACITY	0x25
#define CMD_READ		0x28
#define CMD_WRITE		0x2a
#define CMD_SEEK		0x2b
#define CMD_ERASE		0x2c
#define CMD_RDEFECTS		0x37

#define CMD_NONE                0xff /* Flag meaning no SCSI command */

static char *scsiOpcode[256] = {
   "testUnitRdy", NULL, NULL, "reqSense", "format", NULL, NULL, "addDefects", 
   "read6", NULL, "write6", NULL, NULL, NULL, NULL, NULL,
   NULL, NULL, "inquiry", NULL, NULL, "modeSelect", NULL, NULL, 
   NULL, NULL, "modeSense", "startunit", NULL, "sendDiag", "prevrem", NULL,
   NULL, NULL, NULL, NULL, NULL, "readcapacity", NULL, NULL, 
   "read", NULL, "write", "seek", "erase", NULL, NULL, NULL, 
   NULL, NULL, NULL, NULL, NULL, NULL, NULL, "rdefects"
};

/* Taken from /usr/include/sys/dksc.h */

/* The data structure of the mode sense command */
struct mode_sense_data {
   u_char  sense_len;
   u_char  mediatype;
   u_char  wprot:1, reserv0:2, dpofua:1, reserv1:4;
   u_char  bd_len;
   u_char  block_descrip[8]; 
   /* Don't need this:    union   dk_pages dk_pages; */
};


/* from io/cam/scsi_all.h */
typedef struct all_inq_data {
	u_char	dtype	: 5,	/* Peripheral device type.	[0] */
		pqual	: 3;	/* Peripheral qualifier.	    */
	u_char	dmodify	: 7,	/* Device type modifier.	[1] */
		rmb	: 1;	/* Removable media.		    */
	u_char	ansi	: 3,	/* ANSI version.		[2] */
		ecma	: 3,	/* ECMA version.		    */
		iso	: 2;	/* ISO version.			    */
	u_char	rdf	: 4,	/* Response data format.	[3] */
			: 2,	/* Reserved.			    */
		trmiop	: 1,	/* Terminate I/O process	    */
		aenc	: 1;	/* Async Notification of events	    */
	u_char	addlen;		/* Additional length.		[4] */
	u_char		: 8;	/* Reserved.			[5] */
	u_char		: 8;	/* Reserved.			[6] */
	u_char	sftre	: 1,	/* Soft reset 1 = yes		[7] */
		cmdque	: 1,	/* Command queuing		    */
			: 1,	/* Reserved bit				*/
		linked	: 1,	/* Linked command support	    */
		sync	: 1,	/* Synchronous data transfers	    */
		wbus16	: 1, 	/* support of 16 bit transfers	    */
		wbus32	: 1,	/* support of 32 bit transfers	    */
		reladdr : 1;	/* Relative addressing support	    */
	u_char	vid[8];		/* Vendor ID.		     [8-15] */
	u_char	pid[16];	/* Product ID.		    [16-31] */
	u_char	revlevel[4];	/* Revision level.	    [32-35] */
} ALL_INQ_DATA;


typedef struct dir_read_cap_data {
	u_char	lbn3;		/* MSB of number of logical blocks	*/
	u_char	lbn2;		/* MID HIGH of number of logical blocks	*/
	u_char	lbn1;		/* MID LOW of number of logical blocks	*/
	u_char	lbn0;		/* LSB of number of logical blocks	*/
	u_char	block_len3;	/* MSB of block length in bytes		*/
	u_char	block_len2;	/* MID HIGH of block length in bytes	*/
	u_char	block_len1;	/* MID LOW of block length in bytes	*/
	u_char	block_len0;	/* MSB of block length in bytes		*/
}DIR_READ_CAP_DATA;


/* Defined these for magic disk and/or magic disk sector
 *
 * #define MAGIC_CTRL
 * #define MAGIC_UNIT
 * #define MAGIC_DISK_SECTOR
 *
 */

extern int inCellMode;

/****************** This section is checkpoint stuff ****************/
typedef struct Sector {
  char           data[SectorSize];
  struct Sector* next;
} Sector;

typedef struct
{
  /* Stuff from this point on are stats for the "disk" file */ 

  char filename[128];     /* Name of hd file checkpoint is based on */
  char diskname[128];     /* DISK<node>.<ctrl>.<unit> */
  off_t fileSize;         /* For consistency check of the hd file */
  time_t modifyTime;	  /* the checkpoint is based on		  */
  bool writeable;	  /* Record whether disk is read-write */
  
  bool doCheckSum;	  /* Boolean flag if checksum is done */
  int checkSum;           /* This is not used currently */
  
  int modifyMapSize;      /* This is the bitmap used to mark the modified */   
  char *modifyMap;        /* sectors in the mmapped image of the hd file  */
  Sector *modifiedSectors;/* To store changes for checkpoint restore */
  int dfd;                /* Actual diskfile */
  int sfd;                /* Shadow file for changes to COW disk */
  int *shadowOffset;      /* Offset array into shadow file */
  unsigned long nextWriteOffset; /* offset to write to in shadow file */
} SimhdSaveInfo;

typedef struct {
  int 		accessed;      /* This is int in case we have > 8 hd's */
  SimhdSaveInfo simhdStats;
} HdcptData;

/****************** End section for checkpoint stuff ****************/


typedef struct Device {

  unsigned char cmd[SIM_DISK_CMD_SIZE]; /* SCSI command */

  int   isRead;               /* TRUE if read request, FALSE if write. */
  int64   sizeInSectors;	      /* transfer size in sectors. */
  int64   sectorNum;            /* disk address (sector number) */
  int   done;                 /* 0 => no transfer in progress
			       * 1 => disk transfer pending
			       */
  
  int	bytesTransferred;     /* Return value from operation. */
  int   errnoVal;
  
  PA    pAddr[SIM_DISK_MAX_DMA_LENGTH];
  int   offset;               /* offset for first page */

  int   isReady;              /* TRUE if the disk is ready. */ 
  int   inuse;
  int   dmodel_no;            /* disk model disk # for this disk */

  /* The following is shared data used for checkpointing */
  HdcptData hdcpt;

  /* Data handling */
  struct { 
    int   diskOwner;
    int   intrCpu;
    int   execCpu;

    byte  dataBuffer[SectorSize]; /* buffer for (real) I/O */
    byte *currPtr;                /* ptr to NEXT position in buffer */
    int64   currSector;             /* keeps track of current sec on disk */
  } c;

  DMARequest dmaReq;          /* DMA request area */

} Device;


static Device**** dks;           /* disk data structures [node][ctrl][unit] */
static int        nnode;         /* number of nodes */
static int*       nctrl;         /* number of disks controllers[node] */
static int**      nunit;         /* number of disk units[node][controller] */

static void     (*int_f)(int node,int ctrl,int unit,int on); /* int fct */
static int        dmodel_next;   /* next free disk model disk number */

static int***     inFixedLatency;/* */
static int        performingSimpleIO = 0;

#define DKS(_NODE,_CTRL,_UNIT)          \
                         (*dks[_NODE][_CTRL][_UNIT])
#define DKS_filename(_NODE,_CTRL,_UNIT) \
                         (dks[_NODE][_CTRL][_UNIT]->hdcpt.simhdStats.filename)
#define DKS_diskname(_NODE,_CTRL,_UNIT) \
                         (dks[_NODE][_CTRL][_UNIT]->hdcpt.simhdStats.diskname)


extern char *DevFileDir;
extern char *MemFileDir;



static int SimhdFindSector(int node, int ctrl, int unit, int64 sectorNum,
			   int *fd, unsigned long *offset, int isRead);
static int SimhdSetupModifyMap(int node, int ctrl, int unit);

static void  DMATransfer ( int len, SimTime finishTime,
                          void (*done)(int), int encoded_ctrl_unit);
static void DiskDoneRoutine(int encoded_ctrl_unit);
static void FixedLatencyDisk(int encoded_ctrl_unit);
static void SimhdInitDisks(int restoreFromChkpt);
static void SimhdDoCmd(int node, int ctrl, int unit);
static void DoScsiCmd(int node, int ctrl, int unit);
static int  DiskCheckpointCB(CptDescriptor *cptd);
static long DetermineDeviceSize(int fd);



/*****************************************************************/
/* Trace for debugging */
/* #define DEBUG_HD */

#define DEBUG_DMA

#ifdef DEBUG_HD
static char errbuf[32];

static void Sim_Warning_NOCR(char *fmt, ...)
{
     va_list args;

     va_start(args, fmt);
     vsprintf(errbuf, fmt, args);
     write(1, errbuf, strlen(errbuf));
} 


static int dotrace = 1;
#define DTRACE(c,node,ctrl,unit) \
    if (dotrace) {Sim_Warning_NOCR("<%c%d.%d.%d>", c, node,ctrl,unit);}
#else
#define DTRACE(c,node,ctrl,unit)
#endif


/*****************************************************************/

int  rmtdiskfd = -1;


void
sim_disk_init(int nodes, int uc,
	      void (*int_fun)(int node, int ctrl, int unit, int on),
	      int restoreFromChkpt)
{
   int  n, c;   
   char name[32];

   nnode = nodes;

   /* allocate storage for disk shadow data structures */

   dks            = (Device****) ZMALLOC(nnode*sizeof(Device***), "dks");
   inFixedLatency = (int***)     ZMALLOC(nnode*sizeof(int**),"inFixedLatency");
   nctrl          = (int*)       ZMALLOC(nnode*sizeof(int), "nctrl");
   nunit          = (int**)      ZMALLOC(nnode*sizeof(int*), "nunit");

   for (n = 0; n < nnode; n++) {

     if (n >= TOTAL_CPUS) {
       /* XXX for compatibility with DISCO. Remove later XXX */
       nctrl[n] = 1;
     } else {
       nctrl[n] = NUM_DISK_CONTROLLERS(M_FROM_CPU(n), MCPU_FROM_CPU(n));
     }

     sprintf(name, "inFixedLatency[%d]", n);
     inFixedLatency[n] = (int**) ZMALLOC(nctrl[n]*sizeof(int*), name);
     ASSERT(inFixedLatency[n]);

     sprintf(name, "nunit[%d]", n);
     nunit[n] = (int*) ZMALLOC(sizeof(int), name);
     ASSERT(nunit[n]);

     for (c = 0; c < nctrl[n]; c++) {
       nunit[n][c] = uc;

       sprintf(name, "inFixedLatency[%d][%d]", n, c);
       inFixedLatency[n][c] = (int*) ZMALLOC(nunit[n][c]*sizeof(int), name);
       ASSERT(inFixedLatency[n][c]);
     }
   }

   int_f = int_fun;

   /* register diskdev files */
   Simcpt_Register("diskdev", DiskCheckpointCB, ALL_CPUS);

   /* Do any per disk initialization */
   SimhdInitDisks(restoreFromChkpt);
}


static void
disk_name(int node, int ctrl, int unit)
{
  if (!DKS_diskname(node,ctrl,unit)[0]) {
    /*
     * This disk has not been opened yet, need a name for
     * the disk file. Try to generate a name in Tcl.
     */
    char* filename = TclDiskFileName(M_FROM_CPU(node),    /* machine */
				     MCPU_FROM_CPU(node), /* node */
				     ctrl, unit);
    if (filename) {
      /* got a name from Tcl (includes path) */
      strcpy(DKS_filename(node,ctrl,unit), filename);
    } else {
      /* default */
      sprintf(DKS_filename(node,ctrl,unit), "%s/DISK%d.%d.%d",
	      DevFileDir, node, ctrl, unit);
    }
    /* diskname is an internal id (also used in the log) */
    sprintf(DKS_diskname(node,ctrl,unit), "DISK%d.%d.%d",
	    node, ctrl, unit);
  }
}


void
sim_disk_touch(int node, int ctrl, int unit)
{
  char name[32];

  ASSERT(0 <= node && node < nnode             &&
	 0 <= ctrl && ctrl < nctrl[node]       &&
	 0 <= unit && unit < nunit[node][ctrl] &&
	 dmodel_next < SIM_MAX_DISKS);

  if (!dks[node]) {
    /* node touches a disk for the first time */
    sprintf(name, "dks[%d]", node);
    dks[node] = (Device***) ZMALLOC(nctrl[node]*sizeof(Device**), name);
    ASSERT(dks[node]);
  }
  if (!dks[node][ctrl]) {
    /* controller used for the first time */
    sprintf(name, "dks[%d][%d]", node, ctrl);
    dks[node][ctrl] = (Device**) ZMALLOC(nunit[node][ctrl]*sizeof(Device*),
					 name);
    ASSERT(dks[node][ctrl]);
  }

  /* The following test will fail (eg. unit will already
   * be 'touched') if we have restored from a checkpoint.
   * The checkpoint restore code has to allocate the disk
   * data structures in order to restore them, but the shadow
   * structures in simmagic aren't yet initialized so it will
   * call through to here on the first use by the OS.
   */
  if (!dks[node][ctrl][unit]) {
     sprintf(name, "dks[%d][%d][%d]", node, ctrl, unit);
     dks[node][ctrl][unit] = (Device*) ZMALLOC(sizeof(Device), name);
     ASSERT(&DKS(node,ctrl,unit));

     /* initialize newly allocated Device */
     DKS(node,ctrl,unit).hdcpt.simhdStats.dfd = -1;
     DKS(node,ctrl,unit).isReady              = 1;
     DKS(node,ctrl,unit).hdcpt.accessed       = 0;
     DKS(node,ctrl,unit).dmodel_no            = dmodel_next++;

     disk_name(node, ctrl, unit); /* generate names */
     /*
     Sim_Warning("dks[%d][%d][%d] model entry %d.\n",
		 node, ctrl, unit, DKS(node,ctrl,unit).dmodel_no);
      */
  }

}


/*
 * Do any necessary per disk initialization
 */
static void 
SimhdInitDisks(int restoreFromChkpt)
{
  int node, u, c;
  
  if (restoreFromChkpt) {
     /* For now, only support restoring the disk devices on the
        rmtaccess server which is servicing the checkpoint restore --
        supporting 'fallback' is slightly tricky given the current
        code structure, so I'm not implementing it for now */
     if (Simcpt_IsRemote()) {
        rmtdiskfd = Simrmt_diskinit(NULL);
        if (rmtdiskfd < 0)
           CPUError("Can't contact remote disk machine\n");
        if (Simcpt_Restore("diskdev") != 0)
           CPUError("Can't contact remote disk machine\n");
     } else {            
        if (DevFileDir[0] == '$') {
           rmtdiskfd = Simrmt_diskinit(DevFileDir);
           if (rmtdiskfd < 0)
              CPUError("Can't contact remote disk machine\n");
        }
        if (Simcpt_Restore("diskdev") != 0) {
           CPUError("Can't restore disk devices\n");
        }
     }  

     for (node = 0; node < nnode; node++) {
       if (!dks[node]) continue;
       for (c = 0; c < nctrl[node]; c++) {
	 if (!dks[node][c]) continue;
	 for (u = 0; u < nunit[node][c]; u++) {
	   if (!dks[node][c][u]) continue;
	   DKS(node,c,u).cmd[0] = CMD_NONE;	/* no SCSI command pending */
	 }
       }
     }

  } else {
     if (DevFileDir[0] == '$') {
        rmtdiskfd = Simrmt_diskinit(DevFileDir);
        if (rmtdiskfd < 0) {
           CPUError("Can't contact remote disk machine\n");
        }
     }
  }
}

static int
SimhdOpenCOWDisk(int node, int ctrl, int unit, SimhdSaveInfo *hd)
{
  off_t size;
  
  if (rmtdiskfd < 0) {
    hd->dfd = open(hd->filename, O_RDONLY , 0);
    if (hd->dfd < 0) { 
       if (errno != ENOENT) {
         CPUWarning("Simhd: could not open %s COW, errno=%d\n",
                    hd->filename, errno);
       }
       return -1;
    } else {
      struct stat filestats;
      size = DetermineDeviceSize(hd->dfd);
      if (size < 1024*1024) {
	 CPUError("SimHD: DISK is smaller than 1MB (%d bytes) !!!\n",
		  size);
      }
      fstat(hd->dfd, &filestats);
      /* If we're restoring from a checkpoint, check fize size and 
	 creation time against checkpointed values */
      if ((hd->fileSize > 0) && (hd->fileSize != size)) {
	Sim_Warning("Simhd: Size of disk %s checkpoint is based on "
		    "has changed!\n", DKS_filename(node,ctrl,unit));
      }
      hd->fileSize = size;
      if ((hd->modifyTime > 0) && (hd->modifyTime != filestats.st_mtime)) {
	Sim_Warning("Simhd: DISK file checkpoint is based on "
		    "has been modified!\nThis might not work...\n");
      } else {
	hd->modifyTime = filestats.st_mtime;
      }
      Sim_Warning("%s opened using Copy On Write.\n",
		  DKS_diskname(node,ctrl,unit));
    }
  } else {
    DiskFileInfo df;
    int retval;
    /* If we have a filename for the disk (checkpoint restore), pass it 
       to the remote access server; if not, hd->filename should be null. */
    bzero((char *) &df, sizeof(df));
    ASSERT( strlen(hd->filename)+1  < sizeof(df.filename));
    strcpy(df.filename,hd->filename);

    
    retval = Simrmt_diskcmd(rmtdiskfd, NETDISK_ATTACH,
                            node, ctrl, unit,
			    0, strlen(df.filename)+1, (byte *)&df);
    if (retval < 0) {
      CPUWarning("Open of %s failed on rmtaccess server\n",
		 DKS_diskname(node,ctrl,unit));
      return retval;
    }
    strcpy(hd->filename,df.filename);
    hd->fileSize = size = df.fileSize;
    hd->modifyTime = df.modifyTime;
    Sim_Warning("%s remotely open using Copy On Write.\n",
		DKS_diskname(node,ctrl,unit));
  }



  hd->writeable = (char)0;

  hd->doCheckSum = hd->checkSum = 0;
  /* The modify map is not allocated until */
  /* we actually change the disk, this is  */
  /* to optimize checkpointing.   */
  size = (size + (SIM_DISK_SHADOW_BLKSIZE-1)) & ~(SIM_DISK_SHADOW_BLKSIZE-1);
  { 
     int pageSize = getpagesize();
     hd->modifyMapSize = size/SectorSize/8 + 1; /* 8 extra */
     hd->modifyMapSize = (hd->modifyMapSize+pageSize-1) & ~(pageSize-1);
  }
  hd->modifyMap = NULL;
  hd->sfd = 0;
  hd->shadowOffset = NULL;
  hd->nextWriteOffset = 0;
  return 0;
}

static int
SimhdOpenDisk(int node, int ctrl, int unit, SimhdSaveInfo *hd)
{
  int retval = 0;
  if (rmtdiskfd < 0) {
    hd->dfd = open(hd->filename, O_RDWR , 0);
    if (hd->dfd < 0) { 
      retval = SimhdOpenCOWDisk(node, ctrl, unit, hd);
      if (retval < 0) {
#if !defined(IRIX6_4) && !defined(SIM_X86)
	Sim_Warning("%s not opened! file=%s\n",
		    hd->diskname, hd->filename);
#endif
      }
    } else {
      CPUWarning("WARNING: %s opened read-write.\n"
		 "WARNING: remember to umount before checkpointing!!!\n",
		  DKS_diskname(node,ctrl,unit));
      DKS(node,ctrl,unit).hdcpt.accessed = 1;
      hd->writeable = (char)1;
    }
    if (hd->dfd <= 0) {
      retval = -1;
    }
  } else {
    /* Rmtaccess disks must be opened COW */
    retval = SimhdOpenCOWDisk(node, ctrl, unit, hd);
  }
  return retval;
}

static void
SimhdDoCmd(int node, int ctrl, int unit)
{
  SimhdSaveInfo *hd     = &DKS(node,ctrl,unit).hdcpt.simhdStats;
  int            retval = 0;
  int cmd;
  char *op;

  ASSERT(0 <= ctrl && ctrl < nctrl[node]       &&
	 0 <= unit && unit < nunit[node][ctrl] &&
	 !DKS(node,ctrl,unit).done);

  /* check whether this disk is opened for the first time */
  if ((rmtdiskfd >= 0) && !DKS(node,ctrl,unit).hdcpt.accessed) {

    /* Opening a disk for the first time via rmtaccess -- 
       don't provide a pathname, attempt to open COW. */
     if (!strncmp(hd->filename,"NOTDEF",strlen("NOTDEF"))) {
        retval = -1;
     } else {
        retval = SimhdOpenCOWDisk(node, ctrl, unit, hd);
        DKS(node,ctrl,unit).hdcpt.accessed = 1;
     }
  } else if ((rmtdiskfd <  0) && (hd->dfd <= 0)) {

    /* assign this disk a name (if it doesn't have one) */
    disk_name(node, ctrl, unit);

    retval = SimhdOpenDisk(node, ctrl, unit, hd);
    DKS(node,ctrl,unit).hdcpt.accessed = 1;

  }

  if (retval < 0 && DKS(node,ctrl,unit).cmd[0] != CMD_INQUIRY ) {
    DKS(node,ctrl,unit).bytesTransferred = 0;
    DKS(node,ctrl,unit).errnoVal         = errno;
    DKS(node,ctrl,unit).done             = 1;
    DTRACE('b', node, ctrl, unit);
    return;
  }

  /* interpret the SCSI command and do required setup. The actual data
   * transfer doesn't take place here, but in the SimhdIOHandler.
   */
  DoScsiCmd(node, ctrl, unit);
  
  cmd = DKS(node,ctrl,unit).cmd[0];
  if (cmd != CMD_NONE) {
     char id[10];
     op = scsiOpcode[cmd];
     ASSERT(op != NULL);

     sprintf(id, "%i.%i.%i", node, ctrl, unit);
     Tcl_SetVar2(TCLInterp, "ScsiRequest", "disk", id, 0);
     Tcl_SetVar2(TCLInterp, "ScsiRequest", "command", op, 0);
     AnnExec(AnnFind("scsi", op));
     Tcl_UnsetVar(TCLInterp, "ScsiRequest", 0);
  }

  DTRACE(DKS(node,ctrl,unit).done ? 'd' : 'e', node, ctrl, unit);
}


/*
 * Data handler: does the actual I/O to disk. Is called by the
 * DMA routines (see comments in dma.h).
 */
static void
SimhdIOHandler(DMARequest* req)
{
  int           node = DECODE_NODE(req->arg);
  int           ctrl = DECODE_CTRL(req->arg);
  int           unit = DECODE_UNIT(req->arg);
  Device*       dksp = &DKS(node,ctrl,unit);
  unsigned long offset;
  int           fd;
  int           remote;

  ASSERT(req->dmaLen > 0);

  if (req->isDMAWrite) {

    /** disk input **/
    if (dksp->c.currPtr < dksp->c.dataBuffer+SectorSize) {
      /* data has already been read into dataBuffer */
      dksp->c.currPtr += req->dmaLen;
    } else {
      /* must fetch a new sector */
       if (dksp->hdcpt.simhdStats.writeable) {
          /* this is a writable disk */
          fd = dksp->hdcpt.simhdStats.dfd;
          offset = (dksp->c.currSector)*SectorSize;
          if (lseek(fd, offset, SEEK_SET) == -1)
             CPUError("Can't seek for %s in RD\n",
		      DKS_diskname(node,ctrl,unit));
          if (read(fd, dksp->c.dataBuffer, SectorSize) != SectorSize)
             CPUWarning("Can't read from %s\n",
		      DKS_diskname(node,ctrl,unit));
       } else {
          remote = SimhdFindSector(node,ctrl,unit,
				   dksp->c.currSector,&fd,&offset,1);
          if (remote) {
             int retval = Simrmt_diskcmd(rmtdiskfd, NETDISK_READ,
                                         node, ctrl, unit,
					 dksp->c.currSector, SectorSize,
                                         dksp->c.dataBuffer);
             if (retval < 0) CPUError("Simhd: error in remote read\n");
          } else {
             int bytesRead;
             if (lseek(fd, offset, SEEK_SET) == -1)
                CPUError("Can't seek for %s in RD\n",
			 DKS_diskname(node,ctrl,unit));
             bytesRead = read(fd, dksp->c.dataBuffer, SectorSize);
             if (bytesRead != SectorSize) {
                CPUWarning("%s: Only read %d bytes at offset %lld \n",
                           DKS_diskname(node,ctrl,unit),
			   bytesRead,(uint64)offset);
                bzero(dksp->c.dataBuffer+bytesRead,SectorSize-bytesRead);
             }
          }
       }
      /* update current read state */
      dksp->c.currSector++;
      dksp->c.currPtr = dksp->c.dataBuffer + req->dmaLen;
    }
    req->data = dksp->c.currPtr - req->dmaLen;

  } else {

    /** disk output **/
    ASSERT(req->data == dksp->c.currPtr);
    dksp->c.currPtr += req->dmaLen;
    if (dksp->c.currPtr < dksp->c.dataBuffer+SectorSize) {
      /* buffer up data until one sector filled */
      ASSERT(req->remainingLen > 0); /* make sure whole sectors written */
    } else {
      /* buffer filled up, write to disk */
      ASSERT(dksp->c.currPtr == dksp->c.dataBuffer+SectorSize);
      if (dksp->hdcpt.simhdStats.writeable) {
	 int len;
         /* this is a writable disk */
         fd = dksp->hdcpt.simhdStats.dfd;
         offset = (dksp->c.currSector)*SectorSize;
         if (lseek(fd, offset, SEEK_SET) == -1)
            CPUError("Can't seek for %s in RD\n",
		     DKS_diskname(node,ctrl,unit));
         len = write(fd, dksp->c.dataBuffer, SectorSize);
	 if (len != SectorSize) { 
            CPUError("Can't write to %s. only %d written out of %d. errno=%d offset=%ld\n", 
		     DKS_diskname(node,ctrl,unit),len,SectorSize,errno,(uint64)offset);
	 }
      } else {
         SimhdFindSector(node,ctrl,unit, dksp->c.currSector, &fd, &offset, 0);
         if (lseek(fd, offset, SEEK_SET) == -1)
            CPUError("Can't seek for %s in WR\n",
		     DKS_diskname(node,ctrl,unit));
         if (write(fd, dksp->c.dataBuffer, SectorSize) != SectorSize)
            CPUWarning("Can't write to shadow file for %s\n",
                       DKS_diskname(node,ctrl,unit));
      }
      /* update current write state */
      dksp->c.currSector++;
      dksp->c.currPtr = dksp->c.dataBuffer;
    }
    req->data = dksp->c.currPtr;

  }

  ASSERT(dksp->c.currPtr <= dksp->c.dataBuffer+SectorSize);
}


/*
 * Start disk i/o. Arguments are:
 *  - (ctrl,unit) = disk
 *  - cmd         = SCSI command
 *  - pages       = array of page pointers for transfer
 *  - offset      = offset into first page
 */
void sim_disk_startio(int node, int ctrl, int unit,
		      unsigned char cmd[SIM_DISK_CMD_SIZE], /*SCSI cmd*/
		      PA            pages[SIM_DISK_MAX_DMA_LENGTH],
		      int           offset)
{ 
  int i;
  char *op;
  int cpuNum = CPUVec.CurrentCpuNum();
  enum {DISK_MODEL_FIXED_DMA,     /* Fixed latency read/write */
	DISK_MODEL_HP,            /* Realistic latency read/write */
	DISK_MODEL_SENSE          /* sense-->DMA */
  } diskModel; 

  performingSimpleIO = 0;

  ASSERT(0 <= node && node < nnode              &&
	 0 <= ctrl && ctrl < nctrl[node]        &&
	 0 <= unit && unit <= nunit[node][ctrl] &&
	 offset >= 0 && offset < NBPP && 
	 DKS(node,ctrl,unit).done == 0);

  if (!DKS(node,ctrl,unit).isReady) {
    Sim_Warning("simhd_startio: device not ready\n");
    DKS(node,ctrl,unit).errnoVal = -1;
    DKS(node,ctrl,unit).done     = 1;
    /* raise interrupt to signal operation done */
    if (int_f) int_f(node, ctrl, unit, 1);
    return;
  }

  /* XXXXXXXXXXX
   * Unfortunately, the kernel does not give a list of phsysical
   * pages to us. If the buffer was originally in kseg0, the actual
   * physical address (not paged aligned) is given to us). 
   * simscsi.c (line 674) should eventually be fixed. For now, 
   * we align the pages ourselves
   *
   * Actually, it would be even better if we had physical
   * addresses. For now we convert it here.
   * XXXXXXXXXXX
   */
  for (i=0; i < SIM_DISK_MAX_DMA_LENGTH && pages[i]; i++ ) { 
#if defined(SIM_ALPHA) || defined(SIM_X86)
     DKS(node,ctrl,unit).pAddr[i] =  ((PA)pages[i] & ~(NBPP-1));
#else
    DKS(node,ctrl,unit).pAddr[i] =
       K0_TO_PHYS(((VA)pages[i] & ~(NBPP-1)));
#endif
  }
  if( i >= SIM_DISK_MAX_DMA_LENGTH )
    CPUError("DMA size of %i pages too large.", i);
  DKS(node,ctrl,unit).pAddr[i] = 0;
  DKS(node,ctrl,unit).offset   = offset;

#ifdef DEBUG_HD
  CPUPrint("\nSim_disk: %s %08x %08x %08x %08x offs=%x\n",
	   DKS_diskname(node,ctrl,unit),
	   pages[0], pages[1], pages[2], pages[3], offset);
#endif
  
  /*
   * Decode the command and execute it, if it doesn't require a data
   * transfer. Otherwise (for I/O commands), set up parameters. Actual
   * I/O will be kicked below.
   */
  for (i = 0; i < SIM_DISK_CMD_SIZE; i++) DKS(node,ctrl,unit).cmd[i] = cmd[i];
  SimhdDoCmd(node,ctrl,unit);
  if (DKS(node,ctrl,unit).done) {
    /* raise interrupt to signal operation done */
    if (int_f) int_f(node,ctrl, unit, 1);
    return;
  }
  
  /* 
   * Set up DMA request area
   */
  DKS(node,ctrl,unit).dmaReq.isDMAWrite  = DKS(node,ctrl,unit).isRead;
  DKS(node,ctrl,unit).dmaReq.pAddrs      = &DKS(node,ctrl,unit).pAddr[0];
  DKS(node,ctrl,unit).dmaReq.offset      = DKS(node,ctrl,unit).offset;
  DKS(node,ctrl,unit).dmaReq.amountMoved = 0;

  DKS(node,ctrl,unit).dmaReq.handler     = SimhdIOHandler;
  DKS(node,ctrl,unit).dmaReq.data        = DKS(node,ctrl,unit).c.dataBuffer;
  DKS(node,ctrl,unit).dmaReq.arg         = ENCODE(node,ctrl,unit);

  /* XXXXXXX Hack: check this execCpu bussiness*/
  DKS(node,ctrl,unit).c.execCpu          = (HP_DISK_SCALING(0) > 0 ? 0 :cpuNum);
  DKS(node,ctrl,unit).c.currPtr          = DKS(node,ctrl,unit).c.dataBuffer +
                              (DKS(node,ctrl,unit).isRead ? SectorSize : 0);
  DKS(node,ctrl,unit).c.currSector       = DKS(node,ctrl,unit).sectorNum;

  /* invalidate cache / tc since this command involves I/O */
  CPUVec.DMAInval(M_FROM_CPU(node), DKS(node,ctrl,unit).pAddr);

  /* select a disk model */
  if( DKS(node,ctrl,unit).sectorNum < 0 ) { 
    /* CMD_MODE_SENSE command - who cares about timing?? */
    diskModel = DISK_MODEL_SENSE;
    DKS(node,ctrl,unit).c.currPtr = DKS(node,ctrl,unit).c.dataBuffer;
  } else
    diskModel = (strcmp(DISK_MODEL(0), "HP") != 0) ?
                DISK_MODEL_FIXED_DMA : DISK_MODEL_HP;     
  
#if defined(MAGIC_CTRL) && defined(MAGIC_UNIT)
  if (ctrl == MAGIC_DISK                                 &&
      unit == MAGIC_UNIT                                 &&
      MAGIC_DISK_SECTOR                                  &&
      DKS(node,ctrl,unit).sectorNum == MAGIC_DISK_SECTOR &&
      diskModel == DISK_MODEL_HP )
    /* Magic sector is turbo-charged */
    diskModel = DISK_MODEL_FIXED_DMA;
#endif
   
  if( DKS(node,ctrl,unit).sectorNum >= 0 ) {
    op = (DKS(node,ctrl,unit).isRead ? "RD":"WR");
  } else
    op = "SYS";
#if defined(SIM_ALPHA)
  LogEntry("DISK-req",cpuNum,
	   "disk %i.%i %s size %li sector %li off=0x%x dma=[",
	   ctrl, unit, op,
	   DKS(node,ctrl,unit).sizeInSectors, DKS(node,ctrl,unit).sectorNum,
	   DKS(node,ctrl,unit).offset);
#else
  LogEntry("DISK-req",cpuNum,
	   "disk %i.%i %s size %lli sector %lli off=0x%x dma=[",
	   ctrl, unit, op,
	   DKS(node,ctrl,unit).sizeInSectors, DKS(node,ctrl,unit).sectorNum,
	   DKS(node,ctrl,unit).offset);
#endif

  for(i=0;i<8 && DKS(node,ctrl,unit).pAddr[i]; i++) {
     CPUPrint(" 0x%llx", (uint64)DKS(node,ctrl,unit).pAddr[i]);
  }
  if (DKS(node,ctrl,unit).pAddr[i]) {
     CPUPrint(" ....]\n");
  } else {
     CPUPrint(" ]\n");
  }
  
  DTRACE('c',node,ctrl,unit);
  
  switch( diskModel ) {
    
  case DISK_MODEL_SENSE:
    DMATransfer( DKS(node,ctrl,unit).bytesTransferred,
		 (CPUVec.CycleCount ?
		  CPUVec.CycleCount(DKS(node,ctrl,unit).c.execCpu) : 0 ),
		 DiskDoneRoutine, ENCODE(node,ctrl,unit));  
    break;
    
  case DISK_MODEL_HP:
    DiskModelRequest(M_FROM_CPU(node),
		     DKS(node,ctrl,unit).dmodel_no,
		     DKS(node,ctrl,unit).sectorNum,
		     DKS(node,ctrl,unit).sizeInSectors,
		     !DKS(node,ctrl,unit).isRead,
		     DMATransfer, DiskDoneRoutine,
		     ENCODE(node,ctrl,unit));
    break;

   case DISK_MODEL_FIXED_DMA:
    FixedLatencyDisk(ENCODE(node,ctrl,unit));
    break;

  default: ASSERT(0);
  } 
  DTRACE('x',node,ctrl,unit);
}


/*
 * Root disk access from the ALPHA simos console 
 */
void sim_disk_simpleIO(long op,long unit,long count,long pAddr,long block)
{
   int i;
   int node = 0;
   int ctrl = 0;
   long x;

   performingSimpleIO = 1;
   sim_disk_touch(node, ctrl, unit);

   DKS(node,ctrl,unit).offset   = pAddr & (NBPP-1);
   DKS(node,ctrl,unit).pAddr[0] = pAddr & ~(NBPP-1);
   x = (long) DKS(node,ctrl,unit).pAddr[0] + NBPP;
   for(i=1;i<SIM_DISK_MAX_DMA_LENGTH && x<(pAddr+count);i++) { 
      DKS(node,ctrl,unit).pAddr[i] = x;
      x += PAGE_SIZE;
   }
   DKS(node,ctrl,unit).pAddr[i] = 0;
   if( i >= SIM_DISK_MAX_DMA_LENGTH )
     CPUError("DMA size of %i pages too large.", i);

#ifdef DEBUG_HD
   CPUPrint("\nSim_disk: DISK%d.%d.%d %08x %08x %08x %08x offs=%x\n",
	   node, ctrl, unit, pages[0], pages[1], pages[2], pages[3], offset);
#endif
  
  /*
   * Decode the command and execute it, if it doesn't require a data
   * transfer. Otherwise (for I/O commands), set up parameters. Actual
   * I/O will be kicked below.
   */

  SimhdDoCmd(node,ctrl,unit);
  

  if (op==0x13) {
     DKS(node,ctrl,unit).isRead = 1;
  } else {
     ASSERT(0);
  }
  DKS(node,ctrl,unit).sectorNum = block;
  DKS(node,ctrl,unit).sizeInSectors = count / SectorSize;

  
  /* 
   * Set up DMA request area
   */
  DKS(node,ctrl,unit).dmaReq.isDMAWrite  = DKS(node,ctrl,unit).isRead;
  DKS(node,ctrl,unit).dmaReq.pAddrs      = &DKS(node,ctrl,unit).pAddr[0];
  DKS(node,ctrl,unit).dmaReq.offset      = DKS(node,ctrl,unit).offset;
  DKS(node,ctrl,unit).dmaReq.amountMoved = 0;
  
  DKS(node,ctrl,unit).dmaReq.handler     = SimhdIOHandler;
  DKS(node,ctrl,unit).dmaReq.data        = DKS(node,ctrl,unit).c.dataBuffer;
  DKS(node,ctrl,unit).dmaReq.arg         = ENCODE(node,ctrl,unit);

  /* XXXXXXX Hack: check this execCpu bussiness*/
  DKS(node,ctrl,unit).c.execCpu          = 0;
  DKS(node,ctrl,unit).c.currPtr          = DKS(node,ctrl,unit).c.dataBuffer +
     (DKS(node,ctrl,unit).isRead ? SectorSize : 0);
  DKS(node,ctrl,unit).c.currSector       = DKS(node,ctrl,unit).sectorNum;
  
   
 /*
  * Gross hack. does the job
  *  Alpha SRM Console I/O implementation provides synchronous I/O.
  *  Implemented here by forcing it to take 0 cycles.
  */
  {
     int fixedLat = machines.machine[0].FixedDiskDelay;    
     int usesMemRef = CPUVec.useMemRef;
     CPUVec.useMemRef = FALSE;
     machines.machine[0].FixedDiskDelay = 0;
     FixedLatencyDisk(ENCODE(node,ctrl,unit));
     machines.machine[0].FixedDiskDelay = fixedLat;
     CPUVec.useMemRef = usesMemRef;
  }

  ASSERT( DKS(node,ctrl,unit).dmaReq.amountMoved == count);

  DKS(node,ctrl,unit).done = 0;
}  

/*
 * DiskDoneRoutine - Callback routine for when a DISK I/O has finished. 
 */
static void
DiskDoneRoutine(int encoded_ctrl_unit)
{
  int node = DECODE_NODE(encoded_ctrl_unit);
  int ctrl = DECODE_CTRL(encoded_ctrl_unit);
  int unit = DECODE_UNIT(encoded_ctrl_unit);
  int cpu;
     
  if (performingSimpleIO) {
     /* no acks, just move on */
     return;
  }

  /* Shouldn't calculate this here, but do it to print out log entry */
  if (inCellMode) {
     cpu = (node / CPUS_PER_CELL(0)) * CPUS_PER_CELL(0); /* cpu 0 in cell */
  } else {
     cpu = NUM_CPUS(M_FROM_CPU(node)) * M_FROM_CPU(node);
  }

  ASSERT(0 <= node && node < nnode             &&
	 0 <= ctrl && ctrl < nctrl[node]       &&
	 0 <= unit && unit < nunit[node][ctrl] &&
	 !DKS(node,ctrl,unit).done             &&
	 DKS(node,ctrl,unit).dmaReq.amountMoved ==
	            DKS(node,ctrl,unit).bytesTransferred);

  {
     char id[10];
     sprintf(id, "%i.%i.%i", node, ctrl, unit);
     Tcl_SetVar2(TCLInterp, "ScsiRequest", "disk", id, 0);
     Tcl_SetVar2(TCLInterp, "ScsiRequest", "command", "ack", 0);
     AnnExec(AnnFind("scsi", "ack"));
     Tcl_UnsetVar(TCLInterp, "ScsiRequest", 0);
  }

  LogEntry("DISK-ack", cpu, "disk %i.%i.%i\n", node, ctrl, unit);
  DTRACE('r', node, ctrl, unit);

  DKS(node,ctrl,unit).done = 1; /* operation done */

  /* raise interrupt to signal operation done */
  if (int_f) int_f(node, ctrl, unit, 1);
}


/*
 * DMATransfer - Model the DMA transfer from the disk. 
 */
static void 
DMATransfer( int len, SimTime finishTime,
             void (*done)(int), int encoded_ctrl_unit)
{
  int node = DECODE_NODE(encoded_ctrl_unit);
  int ctrl = DECODE_CTRL(encoded_ctrl_unit);
  int unit = DECODE_UNIT(encoded_ctrl_unit);
  int cpu;

  DMARequest *dmaReq = &DKS(node,ctrl,unit).dmaReq;
  
  if (inCellMode) {
     cpu = (node / CPUS_PER_CELL(0)) * CPUS_PER_CELL(0); /* 0 in cell */
  } else {
     cpu = NUM_CPUS(M_FROM_CPU(node)) * M_FROM_CPU(node);
  }  

  DTRACE('g', node, ctrl, unit);
  ASSERT( 0 <= ctrl && ctrl < nctrl[node] &&
	  0 <= unit && unit < nunit[node][ctrl]);
  ASSERT( dmaReq->remainingLen == 0 );
  dmaReq->remainingLen = len;
   
  /*
   * Compute start address and rate for DMA, we are given the length.
   */   

  /* XXX We can pass only one argument to the "done" callback.
   * Hence, the node/controller/unit will be encoded into a single int.
   *
   * First argument would be DKS(node,ctrl,unit).c.intrCpu if we
   * supported the interruptNode field of the DevDiskRegisters
   * struct.  The OS writes that field before every scsi transfer,
   * but the writes are ignored in simmagic.c:disk_handler.
   *
   * I'm a little scared to couple this up because we've seen
   * race conditions in the os simscsi.c in the past.  For now
   * ensure that all interrupts for a given node go to the first
   * cpu of that node.
   *
   * The first argument also ends up being the responsible node for
   * the DMAs when the firewall is checked.
   *
   * Note: We assume here that controller N attached to node N!
   * This file really shouldn't know which cpu is being interrupted;
   * that is done in simmagic (or the real magic) and computed under
   * int_f.
   */
  DMAdoTransfer( cpu, dmaReq, finishTime,
		 done, encoded_ctrl_unit,
		 DKS(node,ctrl,unit).c.execCpu );
}


/*
 * FixedLatencyDisk - Model the DMA of a fixed latency disk. 
 */
static void
FixedLatencyDisk(int encoded_ctrl_unit)
{
  int         node   = DECODE_NODE(encoded_ctrl_unit);
  int         ctrl   = DECODE_CTRL(encoded_ctrl_unit);
  int         unit   = DECODE_UNIT(encoded_ctrl_unit);
  DMARequest *dmaReq = &DKS(node,ctrl,unit).dmaReq;
  SimTime reqTime,dmaSectorTime;
  SimTime now = CPUVec.CycleCount(DKS(node,ctrl,unit).c.execCpu);
  /* note: the above was formerly intrCpu, but that caused problems
   * if the intr and exec CPU's times were skewed in Embra...
   */
  reqTime =  FIXED_DISK_DELAY(0) * 1000 * CPU_CLOCK;
  dmaSectorTime= reqTime/DKS(node,ctrl,unit).sizeInSectors;
   
  DTRACE('f',node,ctrl,unit);
  
  ASSERT(0 <= node && node < nnode       &&
	 0 <= ctrl && ctrl < nctrl[node] &&
	 0 <= unit && unit < nunit[node][ctrl]);

  if( inFixedLatency[node][ctrl][unit] ) { 
    /* this must be DMA done routine callback from 0 latency DMA */
    inFixedLatency[node][ctrl][unit]++;
    return;
  }

  while( 1 ) { 
    if (dmaReq->amountMoved == DKS(node,ctrl,unit).sizeInSectors*SectorSize) {
      /* DMA'ed the whole thing. Signal that we are done. */
      DiskDoneRoutine(ENCODE(node,ctrl,unit));
      return;
    }
    ASSERT( dmaReq->amountMoved <
	    DKS(node,ctrl,unit).sizeInSectors*SectorSize);
    
    /* Launch the next sector worth of DMA. 
     * a disk read (isRead) is a DMA write (isDMAWrite) !!! 
     * Do not negate!!!
     */
    inFixedLatency[node][ctrl][unit] = 1;
    DMATransfer( SectorSize, 
		 now + dmaSectorTime,
		 FixedLatencyDisk, ENCODE(node,ctrl,unit));
    if(inFixedLatency[node][ctrl][unit] == 1 ) { 
      inFixedLatency[node][ctrl][unit] = 0;
      break;
    }
    /* we must be in 0 latency DMA model, and the DMA scheduled above
       has finished, so loop around to do next sector */
    inFixedLatency[node][ctrl][unit] = 0;
  }
}


/*
 * I/O complete. Tells disk device that OS is done with this i/o.
 */
void sim_disk_iodone(int node, int ctrl, int unit)
{
  ASSERT(0 <= node && node < nnode             &&
	 0 <= ctrl && ctrl < nctrl[node]       &&
	 0 <= unit && unit < nunit[node][ctrl] &&
	 DKS(node,ctrl,unit).done);

  /* clear interrupt for this disk */
  if (int_f) int_f(node,ctrl,unit, 0);

  DTRACE('D',node,ctrl,unit);
  DKS(node,ctrl,unit).done = 0;
}


void sim_disk_status(int node, int ctrl, int unit,
		     int* done, int* bytes_tr, int* errno_val)
{
  ASSERT(0 <= node && node < nnode             &&
	 0 <= ctrl && ctrl < nctrl[node]       &&
	 0 <= unit && unit < nunit[node][ctrl] );

  *done      = DKS(node,ctrl,unit).done;
  *bytes_tr  = DKS(node,ctrl,unit).bytesTransferred;
  *errno_val = DKS(node,ctrl,unit).errnoVal;
}


/*
 * Do SCSI command
 *
 * Side effect:
 *  - for commands requiring a data transfer: sets up parameters
 *  - for other commands: performs them, sets results and done field.
 */
static void
DoScsiCmd(int node, int ctrl, int unit)
{
#define UNSUPPORTED(TEXT) \
  CPUWarning("Unsupported SCSI command " TEXT); goto error;

  struct mode_sense_data msd;

  switch (DKS(node,ctrl,unit).cmd[0]) {
 
  case CMD_SEEK:        UNSUPPORTED("seek");
  case CMD_RDEFECTS:    UNSUPPORTED("read defects");
  case CMD_ADD_DEFECTS: UNSUPPORTED("add defects");
  case CMD_REQ_SENSE:   UNSUPPORTED("request sense");
  case CMD_SEND_DIAG:   UNSUPPORTED("send diag");
  case CMD_MODE_SELECT: UNSUPPORTED("mode select");
  case CMD_PREVREM:     UNSUPPORTED("prev rem");
  case CMD_FORMAT:      UNSUPPORTED("format");
  case CMD_STARTUNIT:   UNSUPPORTED("start unit");
  case CMD_ERASE:       UNSUPPORTED("erase");
 
  case CMD_READ:
    DKS(node,ctrl,unit).isRead           = 1;
    DKS(node,ctrl,unit).sectorNum        = (DKS(node,ctrl,unit).cmd[2] << 24) +
                                           (DKS(node,ctrl,unit).cmd[3] << 16) +
                                           (DKS(node,ctrl,unit).cmd[4] << 8) +
                                           (DKS(node,ctrl,unit).cmd[5] << 0);
    DKS(node,ctrl,unit).sizeInSectors    = (DKS(node,ctrl,unit).cmd[7] << 8) +
                                           (DKS(node,ctrl,unit).cmd[8] << 0);
    DKS(node,ctrl,unit).errnoVal         = 0; /* will succeed */
    DKS(node,ctrl,unit).bytesTransferred = 
      DKS(node,ctrl,unit).sizeInSectors * SectorSize;
    return;

  case CMD_READ_6:
     DKS(node,ctrl,unit).isRead           = 1;
     DKS(node,ctrl,unit).sectorNum        =
        ((DKS(node,ctrl,unit).cmd[1]& 0x1f) << 16) +
        (DKS(node,ctrl,unit).cmd[2] << 8) +
        (DKS(node,ctrl,unit).cmd[3]);
     DKS(node,ctrl,unit).sizeInSectors    = (DKS(node,ctrl,unit).cmd[4]);
     if (DKS(node,ctrl,unit).sizeInSectors == 0) {
       CPUWarning("Suspicious sizeInSectors in simhd \n");
       DKS(node,ctrl,unit).sizeInSectors = 256;
     }
       
     DKS(node,ctrl,unit).errnoVal         = 0; /* will succeed */
     DKS(node,ctrl,unit).bytesTransferred =
        DKS(node,ctrl,unit).sizeInSectors * SectorSize;

     return;
     

  case CMD_WRITE:
    DKS(node,ctrl,unit).isRead           = 0;
    DKS(node,ctrl,unit).sectorNum        = (DKS(node,ctrl,unit).cmd[2] << 24) +
                                           (DKS(node,ctrl,unit).cmd[3] << 16) +
                                           (DKS(node,ctrl,unit).cmd[4] << 8) +
                                           (DKS(node,ctrl,unit).cmd[5] << 0);
    DKS(node,ctrl,unit).sizeInSectors    = (DKS(node,ctrl,unit).cmd[7] << 8) +
                                           (DKS(node,ctrl,unit).cmd[8] << 0);
    DKS(node,ctrl,unit).errnoVal         = 0; /* will succeed */
    DKS(node,ctrl,unit).bytesTransferred = 
      DKS(node,ctrl,unit).sizeInSectors * SectorSize;
    return;

   case CMD_WRITE_6:
    DKS(node,ctrl,unit).isRead           = 0;
    DKS(node,ctrl,unit).sectorNum        =
       (((DKS(node,ctrl,unit).cmd[1] << 3) >> 3) << 16) +
       (DKS(node,ctrl,unit).cmd[2] << 8) +
       (DKS(node,ctrl,unit).cmd[3]);
     DKS(node,ctrl,unit).sizeInSectors    = (DKS(node,ctrl,unit).cmd[4]);
     if (DKS(node,ctrl,unit).sizeInSectors == 0) {
	CPUWarning("Suspicious sizeInSectors in simhd \n");
	DKS(node,ctrl,unit).sizeInSectors = 256;
     }
     
    DKS(node,ctrl,unit).errnoVal         = 0; /* will succeed */
    DKS(node,ctrl,unit).bytesTransferred = 
      DKS(node,ctrl,unit).sizeInSectors * SectorSize;
    return;

  case CMD_READCAPACITY:

#if defined(SIM_ALPHA) || defined(IRIX6_4) || defined(SIM_X86)
    {
	/*
	 * Use DMA --- alpha port 
	 */
	uint64 size = (rmtdiskfd >= 0) ?
	   Simrmt_diskcmd(rmtdiskfd, NETDISK_PROBE,
			  node, ctrl, unit,
			  0,0,(byte *) 0) :
	   DetermineDeviceSize(DKS(node,ctrl,unit).hdcpt.simhdStats.dfd);

	DIR_READ_CAP_DATA cap;
	
	size = size / SectorSize;
	DKS(node,ctrl,unit).errnoVal = 0;
	DKS(node,ctrl,unit).bytesTransferred = sizeof(DIR_READ_CAP_DATA);
	DKS(node,ctrl,unit).sectorNum        = -1; /* signal this is sense */
	bzero((char *)&cap,sizeof(cap));
	cap.lbn3 = (size >>24) & 0xff;
	cap.lbn2 = (size >>16) & 0xff;
	cap.lbn1 = (size >> 8) & 0xff;
	cap.lbn0 = (size >> 0) & 0xff;
	cap.block_len3 = 0;
	cap.block_len2 = 0;
	cap.block_len1 = (SectorSize >> 8)  & 0xff;
	cap.block_len0 = (SectorSize >> 0)  & 0xff;
	bcopy((char *)&cap, DKS(node,ctrl,unit).c.dataBuffer,
	     sizeof(cap));
	CPUPrint("hd.c:: READCAPACITY unit=%d size=%d (0x%x,0x%x,0x%x,0x%x)\n",
		   unit,size,cap.lbn3, cap.lbn2, cap.lbn1,cap.lbn0);
    }
#else /* SIM_ALPHA ||IRIX6_4 */
  {	
    DKS(node,ctrl,unit).bytesTransferred = (rmtdiskfd >= 0) ?
       Simrmt_diskcmd(rmtdiskfd, NETDISK_PROBE,
		      node, ctrl, unit,
		      0,0,(byte *) 0) :
          (int)lseek(DKS(node,ctrl,unit).hdcpt.simhdStats.dfd, 0, SEEK_END);
    if (DKS(node,ctrl,unit).bytesTransferred <= 0) {
       if (rmtdiskfd < 0) perror("Lseeking disk in CMD_READCAPACITY");
       DKS(node,ctrl,unit).errnoVal = errno;
       goto error;
    }
    DKS(node,ctrl,unit).errnoVal = 0;
    DKS(node,ctrl,unit).done     = 1;
  }
#endif /* SIM_ALPHA || IRIX6_4 */
    return;
 
  case CMD_TST_UNIT_RDY:
    /*
     * XXX When does this return an error?
     * XXX What errors does it return?
     */
    DKS(node,ctrl,unit).errnoVal = 0;
    DKS(node,ctrl,unit).done     = 1;
    return;
    
  case CMD_MODE_SENSE:
    /*
     * Return that disk is not write protected and block size is 512 bytes
     * WARNING: Watch this. I omitted the last section of the
     * mode_sense_data data structure so that we could easily use
     * this structure when running on other platforms. SAH
     */
    bzero((char *)&msd, sizeof(msd));
    msd.wprot            = 0;
    msd.bd_len = 8;
    msd.block_descrip[5] = 0;
    msd.block_descrip[6] = SectorSize >> 8;
    msd.block_descrip[7] = 0;
    /* this information needs to be DMA'ed to memory. Set up correct
     * transfer size and place info in buffer.
     */
    DKS(node,ctrl,unit).isRead           = 1;
    DKS(node,ctrl,unit).bytesTransferred = sizeof(struct mode_sense_data);
    DKS(node,ctrl,unit).errnoVal         = 0;
    DKS(node,ctrl,unit).sectorNum        = -1; /* signal this is sense */
    bcopy((char *)&msd, DKS(node,ctrl,unit).c.dataBuffer,
	  sizeof(struct mode_sense_data));
    return;
    
  case CMD_INQUIRY:
    /*
     * XXX When does this return an error?
     * XXX What errors does it return?
     */
    DKS(node,ctrl,unit).errnoVal = 0;
    if (DKS(node,ctrl,unit).pAddr[0]) {
       /*
	* XXX  This MIGHT now work on a cross-endian
	* XXX simualtion
	*/

       ALL_INQ_DATA inq;

       DKS(node,ctrl,unit).isRead           = 1;
       DKS(node,ctrl,unit).bytesTransferred = sizeof(inq);
       DKS(node,ctrl,unit).sectorNum        = -1; /* signal this is sense */

       bzero((char*)&inq,sizeof(inq));
       inq.dtype = 0; /* ALL_DTYPE_DIRECT */
       if (rmtdiskfd) { 
          if (!strncmp(DKS_filename(node,ctrl,unit),"NOTDEF",strlen("NOTDEF"))) {
             inq.pqual = 3;  /* ALL_PQUAL_NO_PHYS */
          } else {
             inq.pqual = 0; /* ALL_PQUAL_CONN */
          }      
       } else { 
          if (access(DKS_filename(node,ctrl,unit),R_OK) !=0) {
#if 0
             CPUWarning("simos::hd,c: CMD_INQUIRY fails for %s\n",DKS_filename(node,ctrl,unit));
#endif
             inq.pqual = 3;  /* ALL_PQUAL_NO_PHYS */
          } else {
#if 0
             CPUWarning("simos::hd,c: CMD_INQUIRY succeeds for %s\n",DKS_filename(node,ctrl,unit));
#endif
             inq.pqual = 0; /* ALL_PQUAL_CONN */
          }
       }
       inq.dmodify = 0;
       inq.rmb = 0;
       inq.ansi = 0x2; /* ALL_SCSI2 */
       inq.ecma = 0;
       inq.iso = 0;
       inq.rdf =0;
       inq.trmiop = 0;
       strcpy((char *)inq.vid,"simosVID");
       strcpy((char *)inq.pid,"simosPID");
       
       bcopy((char *)&inq, DKS(node,ctrl,unit).c.dataBuffer,
	     sizeof(ALL_INQ_DATA));
     
       /*
	* actually need to transfer data
	*/
    } else { 
       DKS(node,ctrl,unit).done     = 1;
    }
    return;

  default:
     CPUError("hd.c: scsi command not supported (0x%x)\n",
	      DKS(node,ctrl,unit).cmd[0]);
    return;
  }

error:
  DKS(node,ctrl,unit).errnoVal         = -1;
  DKS(node,ctrl,unit).done             = 1;
  DKS(node,ctrl,unit).bytesTransferred = 0;
#undef UNSUPPORTED
}


/***************************************************************************
 *
 * Modify map utilities
 *
 ***************************************************************************/

int
SimhdSetupModifyMap(int node, int ctrl, int unit)
{
  SimhdSaveInfo *hd = &(DKS(node,ctrl,unit).hdcpt.simhdStats);
  char nameBuf[256];
  unsigned long size;
  
  /* First time we are writing to the disk */
  /* allocate bitmap of modified sectors, shadow file, etc. */
  sprintf(nameBuf, "%s/.%s_XXXXXX",
	  MemFileDir,
	  DKS_diskname(node,ctrl,unit));
#ifdef __linux__
  mkstemp(nameBuf);
#else
  mktemp(nameBuf);
#endif
  if((hd->sfd =  open(nameBuf, O_RDWR|O_CREAT|O_TRUNC , 0)) == -1) {
    Sim_Warning("Could not open shadow file %s\n", nameBuf);
    return -1;
  }
  
  unlink(nameBuf);
  
  size = (unsigned long)
     (hd->fileSize + (SIM_DISK_SHADOW_BLKSIZE-1)) & ~(SIM_DISK_SHADOW_BLKSIZE-1);
  size = size/SectorSize/(8) + 1; /* 8 extra */
  sprintf(nameBuf, "SHADOWMAP%d.%d.%d", node, ctrl, unit);
  hd->modifyMap = (char *) ZMALLOC(hd->modifyMapSize, nameBuf);
  size = (unsigned long)
     (hd->fileSize/SIM_DISK_SHADOW_BLKSIZE + 1)*sizeof(int); 
  sprintf(nameBuf, "shadowOffset%d.%d.%d", node, ctrl, unit);
  hd->shadowOffset = (int *) MALLOC(size, nameBuf);
  if((hd->modifyMap == NULL) || (hd->shadowOffset == NULL)) {
    Sim_Warning("Can't alloc. bitmap for shadow file\n");
    return -1;
  }
  
  return 0;
}


/*
 * Given a disk number and a sector number, return a file
 * descriptor and an offset
 */
int
SimhdFindSector(int node, int ctrl, int unit,
		int64 sectorNum, int *fd, unsigned long *offset, int isRead) 
{
  SimhdSaveInfo *hd = &(DKS(node,ctrl,unit).hdcpt.simhdStats);
  int remote = (rmtdiskfd >= 0);
  
  ASSERT( sectorNum/8 < hd->modifyMapSize);

  if (!isRead) {
    if (hd->modifyMap == NULL) {
      if (SimhdSetupModifyMap(node, ctrl, unit)) {
	/* this is a fatal error */
	ASSERT(0);
	exit(-1);
      }
    }
    
    /* Check if this sector has been previously modified */
    if (!(hd->modifyMap[sectorNum/8] & (1 << (sectorNum % 8)))) {
      /* 
       * check if the block is allocated on the shadow disk 
       * This code explicitly assumes that the block size is 8 times
       * the sector size. If this is changed, we need a more complicated
       * computation here.
       */
      if (!(hd->modifyMap[sectorNum/8])) {
	/* 
	 * Every 100 extend the file by a chunk
	 * Reduces the number of times metadata has to be written 
	 */
	if (!(hd->nextWriteOffset%100)){
	  int k;
	  if ((k = lseek (hd->sfd, (hd->nextWriteOffset+100) *
			                SIM_DISK_SHADOW_BLKSIZE, SEEK_SET)) 
	      != (hd->nextWriteOffset+100)*SIM_DISK_SHADOW_BLKSIZE) {
	    perror("lseek");
	    Sim_Warning("Can't seek to offset %#x in shadow file (%d) "
			"for %s return %d\n", 
			(hd->nextWriteOffset+1000)*SIM_DISK_SHADOW_BLKSIZE,
			hd->sfd, DKS_diskname(node,ctrl,unit), k);
	    exit( -1);
	  }
	}
	hd->shadowOffset[sectorNum*SectorSize/SIM_DISK_SHADOW_BLKSIZE] =
           (int)(hd->nextWriteOffset++);
#ifdef DEBUG_HD
	Sim_Warning("Block %#x allocated offset %#x\n", 
		    sectorNum*SectorSize/SIM_DISK_SHADOW_BLKSIZE,
		    hd->nextWriteOffset-1);
#endif
      }
      hd->modifyMap[sectorNum/8] |= (char) (1 << (sectorNum % 8));
    }
  } 
  
  if ((hd->modifyMap == NULL) ||
      (!(hd->modifyMap[sectorNum/8] & (1 << (sectorNum % 8))))) {
    *fd = hd->dfd;
    *offset = sectorNum*SectorSize;
  } else {
    remote = 0;
    *fd = hd->sfd;
    *offset = hd->shadowOffset[sectorNum*SectorSize/SIM_DISK_SHADOW_BLKSIZE]*
                SIM_DISK_SHADOW_BLKSIZE + (sectorNum % 8)*SectorSize;
#ifdef DEBUG_HD
    Sim_Warning("%s of %#x at offset %#x\n",
		isRead?"Read":"Write", sectorNum, *offset);
#endif
  }
  return remote;
}

/*****************************************************************
 * Given an encoded controller and unit, return the disk number.
 *****************************************************************/
int
SimhdGetDiskNum(int encoded_ctrl_unit)
{
   int node = DECODE_NODE(encoded_ctrl_unit);
   int ctrl = DECODE_CTRL(encoded_ctrl_unit);
   int unit = DECODE_UNIT(encoded_ctrl_unit);

   return DKS(node,ctrl,unit).dmodel_no;
}

/***************************************************************************
 *
 * Ckeckpoint support
 *
 ***************************************************************************/

/* XXX Note:
 * XXX
 * The former hd.c only supported one controller / node. To maintain
 * backward compatibility with the old checkpoints, we now use the
 * following encoding when writing the *.diskdev file:
 *
 *   Accessed[<node>,<disk>] ...
 *
 * becomes
 *
 *   Accessed[<node>,<ctrldisk>] ...
 *
 * where <ctrldisk> has the disk number in the ls 16 bits and the
 * controller number in the following 16 bits.
 */

#define ENCODE_CD(_CTRL,_DISK) ((((_CTRL) & 0xffff)<<16) | ((_DISK) & 0xffff))
#define DECODE_C(_CD) (((_CD)>>16) & 0xffff)
#define DECODE_D(_CD) (((_CD)>>0) & 0xffff)

int
DiskCheckpointCB(CptDescriptor *cptd)
{
  int node, c, u;
  int j;
  bool modified;
  uint diskAccessed = 0;
  char *fnptr;
  long val;

  if (cptd->mode == CPT_SAVE) {
    /*
     * Check ownership
     */
    for (node = 0; node < nnode; node++) {
      if (!dks[node]) continue;
      for (c = 0; c < nctrl[node]; c++) {
	if (!dks[node][c]) continue;       /* controller never used */
	for (u = 0; u < nunit[node][c]; u++) {
          if (!dks[node][c][u]) continue;  /* unit never used */
          if( DKS(node,c,u).done != 0)
	    CPUError("DiskCheckpointCB: disk I/O in progress, can't ckpt.\n");
          diskAccessed |= DKS(node,c,u).hdcpt.accessed;
	  /* DT: I think this check could be removed */
          if (!DKS(node,c,u).isReady) { ASSERT(0); return -1; }
	}
      }
    }
  }

  if (cptd->mode == CPT_RESTORE) {
     DevFileDir = 0;
  }
  Simcpt_CptString(cptd, "DiskPath", NO_INDEX, NO_INDEX, &DevFileDir);
   
  for (node = 0; node < nnode; node++) {
    for (c = 0; c < nctrl[node]; c++) {
      for (u = 0; u < nunit[node][c]; u++) {
	/* Note: have to be careful to ensure backward compatibility */
	int            cu = ENCODE_CD(c,u);
	SimhdSaveInfo *hd;

	if (dks[node] && dks[node][c] && dks[node][c][u]) {
	  diskAccessed = DKS(node,c,u).hdcpt.accessed;
	} else {
	  diskAccessed = 0;
	}
        
        if (diskAccessed && (DKS(node,c,u).hdcpt.simhdStats.fileSize == 0)) {
           diskAccessed = 0;
        }

	Simcpt_OptionalUint(cptd, "Accessed", node, cu, 0);
	Simcpt_CptUint(cptd,      "Accessed", node, cu, &diskAccessed);
	if (!diskAccessed) {
	  /* This disk was never accessed OR
	   * this is a compatibility case and we're restoring from
	   * an old checkpoint.
	   */  
	  continue;
	}
	
	if (!(dks[node] && dks[node][c] && dks[node][c][u])) {
	  ASSERT(cptd->mode == CPT_RESTORE);
	  sim_disk_touch(node,c,u); /* alloc mem for disk data structures */
	}
	
	DKS(node,c,u).hdcpt.accessed = 1;

	hd = &(DKS(node,c,u).hdcpt.simhdStats);
	if( hd->writeable ) {
	  Sim_Warning("Cannot take checkpoint with writable %s\n",
		      DKS_diskname(node,c,u));
	  return -1;
	}
	
	fnptr = hd->filename;
	Simcpt_CptString(cptd, "Filename", node, cu, &fnptr);
	ASSERT (strlen(hd->filename) < sizeof(hd->filename)-1);
	val = (long)hd->fileSize;
	Simcpt_CptLong(cptd,   "Filesize", node, cu, &val);
	hd->fileSize = val;
	Simcpt_CptLong(cptd,   "LastModified", node, cu, &(hd->modifyTime));
	Simcpt_CptUint(cptd,   "ChecksumDone", node, cu, &(hd->doCheckSum));
	if (hd->doCheckSum)
	  Simcpt_CptInt(cptd,  "Checksum", node, cu, &(hd->checkSum));
	else
	  hd->checkSum = 0;
	
	if (cptd->mode == CPT_RESTORE) {
	  int ret = SimhdOpenCOWDisk(node,c,u,hd);
	  if (ret != 0) return ret;
	}
	
	Simcpt_CptUint(cptd,   "Writeable", node, cu, &(hd->writeable));
	Simcpt_CptInt(cptd,    "ModifyMapSize", node,cu, &(hd->modifyMapSize));
	modified = (hd->modifyMap != NULL);
	Simcpt_CptUint(cptd,   "Modified", node, cu, &(modified));
	
	if (modified) {
	  char *modMap, *origModMap;
	  
	  if (cptd->mode == CPT_RESTORE) {
             long pageSize=getpagesize();
	    modMap = (char *) ZMALLOC(hd->modifyMapSize+pageSize, "DiskCheckpointTemp");
	    if(modMap == NULL) {
	      Sim_Warning("Can't alloc. bitmap for shadow file\n");
	      return -1;
	    }
	    origModMap = modMap;
            modMap = (char *) (((VA)modMap +(PAGE_SIZE-1))&~(PAGE_SIZE-1));
	  } else {
	    modMap = hd->modifyMap;
	  }
	  
	  Simcpt_CptBlock(cptd,"ModifyMap", node, cu,
			  modMap, hd->modifyMapSize, -1);
	  
	  for (j = 0; j < hd->modifyMapSize; j++) {
	    if (modMap[j] == 0) {
	      continue;
	    } else {
	      int k;
	      for (k = 0; k < 8; k++) {
		if ((modMap[j] >> k) & 1) {
		  int fd;
		  unsigned long offset;
		  char buf[SectorSize];
		  int sectorNum = j * 8 + k;
		  if (cptd->mode == CPT_RESTORE) {
		    Simcpt_CptBlock(cptd, "ModifiedSector",
				    /* DT this was: c*1000+u */
				    c*1000000+node*1000+u,
				    sectorNum, buf, SectorSize,-1);
		    SimhdFindSector(node, c, u, sectorNum, &fd, &offset, 0);
		    lseek (fd, offset, SEEK_SET);
		    write (fd, buf, SectorSize);
		  } else {
		    SimhdFindSector(node, c, u, sectorNum, &fd, &offset, 1);
		    lseek (fd, offset, SEEK_SET);
		    read (fd, buf, SectorSize);
		    Simcpt_CptBlock(cptd, "ModifiedSector",
				    /* DT this was: c*1000+u */
				    c*1000000+node*1000+u,
				    sectorNum, buf, SectorSize,-1);
		  }                
		}
	      }
	    }
	  }
	  if (cptd->mode == CPT_RESTORE) {
	    free(origModMap);
	  }
	}
      }
    }
  }
  return 0;
}


static long DetermineDeviceSize(int fd)
{
   long    delta =  1024 * 1024 * 1024,size = 0;
   char    buf[1];

   if ( (size = (off_t)lseek(fd, 0 ,SEEK_END)) < 0 ) {
	perror("Lseeking disk in simhd");
	ASSERT(0);
   }
  if (size) return size;

  size = 0;
   while(delta) {
      lseek(fd,(off_t)size + delta - sizeof(buf),SEEK_SET);
      if (read(fd,buf,sizeof(buf)) > 0) {
         size = lseek(fd,0,SEEK_CUR);
      } else { 
         delta /= 2;
      }
   }
   CPUWarning("SimHD: Use binsearch to determine size of disk to %lld MB \n",
              size / 1024 / 1024);
   return size;
}