| 1 | #include <odin.h>
|
|---|
| 2 |
|
|---|
| 3 | #include <stdlib.h>
|
|---|
| 4 | #include <sys/types.h>
|
|---|
| 5 | #include <sys/stat.h>
|
|---|
| 6 | #include <errno.h>
|
|---|
| 7 | #include <fcntl.h>
|
|---|
| 8 | #include <memory.h>
|
|---|
| 9 | // #include <unistd.h>
|
|---|
| 10 |
|
|---|
| 11 | #include "winbase.h"
|
|---|
| 12 | #include "aspi.h"
|
|---|
| 13 | #include "wnaspi32.h"
|
|---|
| 14 | #include "options.h"
|
|---|
| 15 | #include "heap.h"
|
|---|
| 16 | #include "debugtools.h"
|
|---|
| 17 | #include "ldt.h"
|
|---|
| 18 | //#include "callback.h"
|
|---|
| 19 |
|
|---|
| 20 | DEFAULT_DEBUG_CHANNEL(aspi)
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | /* FIXME!
|
|---|
| 24 | * 1) Residual byte length reporting not handled
|
|---|
| 25 | * 2) Make this code re-entrant for multithreading
|
|---|
| 26 | * 3) Only linux supported so far
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #ifdef linux
|
|---|
| 30 |
|
|---|
| 31 | static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
|
|---|
| 32 |
|
|---|
| 33 | static int
|
|---|
| 34 | ASPI_OpenDevice(SRB_ExecSCSICmd *prb)
|
|---|
| 35 | {
|
|---|
| 36 | int fd;
|
|---|
| 37 | char idstr[20];
|
|---|
| 38 | char device_str[50];
|
|---|
| 39 | ASPI_DEVICE_INFO *curr;
|
|---|
| 40 |
|
|---|
| 41 | /* search list of devices to see if we've opened it already.
|
|---|
| 42 | * There is not an explicit open/close in ASPI land, so hopefully
|
|---|
| 43 | * keeping a device open won't be a problem.
|
|---|
| 44 | */
|
|---|
| 45 |
|
|---|
| 46 | for (curr = ASPI_open_devices; curr; curr = curr->next) {
|
|---|
| 47 | if (curr->hostId == prb->SRB_HaId &&
|
|---|
| 48 | curr->target == prb->SRB_Target &&
|
|---|
| 49 | curr->lun == prb->SRB_Lun) {
|
|---|
| 50 | return curr->fd;
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /* device wasn't cached, go ahead and open it */
|
|---|
| 55 | sprintf(idstr, "scsi c%1dt%1dd%1d", prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
|
|---|
| 56 |
|
|---|
| 57 | if (!PROFILE_GetWineIniString(idstr, "Device", "", device_str, sizeof(device_str))) {
|
|---|
| 58 | TRACE("Trying to open unlisted scsi device %s\n", idstr);
|
|---|
| 59 | return -1;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | TRACE("Opening device %s=%s\n", idstr, device_str);
|
|---|
| 63 |
|
|---|
| 64 | fd = open(device_str, O_RDWR);
|
|---|
| 65 | if (fd == -1) {
|
|---|
| 66 | int save_error = errno;
|
|---|
| 67 | #ifdef HAVE_STRERROR
|
|---|
| 68 | ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
|
|---|
| 69 | #else
|
|---|
| 70 | ERR("Error opening device %s, error %d\n", device_str, save_error);
|
|---|
| 71 | #endif
|
|---|
| 72 | return -1;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /* device is now open */
|
|---|
| 76 | curr = HeapAlloc( SystemHeap, 0, sizeof(ASPI_DEVICE_INFO) );
|
|---|
| 77 | curr->fd = fd;
|
|---|
| 78 | curr->hostId = prb->SRB_HaId;
|
|---|
| 79 | curr->target = prb->SRB_Target;
|
|---|
| 80 | curr->lun = prb->SRB_Lun;
|
|---|
| 81 |
|
|---|
| 82 | /* insert new record at beginning of open device list */
|
|---|
| 83 | curr->next = ASPI_open_devices;
|
|---|
| 84 | ASPI_open_devices = curr;
|
|---|
| 85 | return fd;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 | static void
|
|---|
| 90 | ASPI_DebugPrintCmd(SRB_ExecSCSICmd *prb)
|
|---|
| 91 | {
|
|---|
| 92 | BYTE cmd;
|
|---|
| 93 | int i;
|
|---|
| 94 | BYTE *cdb;
|
|---|
| 95 |
|
|---|
| 96 | switch (prb->CDBByte[0]) {
|
|---|
| 97 | case CMD_INQUIRY:
|
|---|
| 98 | TRACE("{\n");
|
|---|
| 99 | TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
|
|---|
| 100 | TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
|
|---|
| 101 | TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
|
|---|
| 102 | TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
|
|---|
| 103 | TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
|
|---|
| 104 | TRACE("}\n");
|
|---|
| 105 | break;
|
|---|
| 106 | case CMD_SCAN_SCAN:
|
|---|
| 107 | TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
|
|---|
| 108 | break;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | TRACE("Host Adapter: %d\n", prb->SRB_HaId);
|
|---|
| 112 | TRACE("Flags: %d\n", prb->SRB_Flags);
|
|---|
| 113 | if (TARGET_TO_HOST(prb)) {
|
|---|
| 114 | TRACE("\tData transfer: Target to host. Length checked.\n");
|
|---|
| 115 | }
|
|---|
| 116 | else if (HOST_TO_TARGET(prb)) {
|
|---|
| 117 | TRACE("\tData transfer: Host to target. Length checked.\n");
|
|---|
| 118 | }
|
|---|
| 119 | else if (NO_DATA_TRANSFERED(prb)) {
|
|---|
| 120 | TRACE("\tData transfer: none\n");
|
|---|
| 121 | }
|
|---|
| 122 | else {
|
|---|
| 123 | WARN("\tTransfer by scsi cmd. Length not checked.\n");
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
|
|---|
| 127 | TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
|
|---|
| 128 | TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
|
|---|
| 129 | TRACE("Target: %d\n", prb->SRB_Target);
|
|---|
| 130 | TRACE("Lun: %d\n", prb->SRB_Lun);
|
|---|
| 131 | TRACE("BufLen: %ld\n", prb->SRB_BufLen);
|
|---|
| 132 | TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
|
|---|
| 133 | TRACE("BufPtr: %p\n", prb->SRB_BufPointer);
|
|---|
| 134 | TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
|
|---|
| 135 | TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
|
|---|
| 136 | cdb = &prb->CDBByte[0];
|
|---|
| 137 | cmd = prb->CDBByte[0];
|
|---|
| 138 | if (TRACE_ON(aspi))
|
|---|
| 139 | {
|
|---|
| 140 | DPRINTF("CDB buffer[");
|
|---|
| 141 | for (i = 0; i < prb->SRB_CDBLen; i++) {
|
|---|
| 142 | if (i != 0) DPRINTF(",");
|
|---|
| 143 | DPRINTF("%02x", *cdb++);
|
|---|
| 144 | }
|
|---|
| 145 | DPRINTF("]\n");
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | static void
|
|---|
| 150 | ASPI_PrintSenseArea(SRB_ExecSCSICmd *prb)
|
|---|
| 151 | {
|
|---|
| 152 | int i;
|
|---|
| 153 | BYTE *cdb;
|
|---|
| 154 |
|
|---|
| 155 | if (TRACE_ON(aspi))
|
|---|
| 156 | {
|
|---|
| 157 | cdb = &prb->CDBByte[0];
|
|---|
| 158 | DPRINTF("SenseArea[");
|
|---|
| 159 | for (i = 0; i < prb->SRB_SenseLen; i++) {
|
|---|
| 160 | if (i) DPRINTF(",");
|
|---|
| 161 | DPRINTF("%02x", *cdb++);
|
|---|
| 162 | }
|
|---|
| 163 | DPRINTF("]\n");
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | static void
|
|---|
| 168 | ASPI_DebugPrintResult(SRB_ExecSCSICmd *prb)
|
|---|
| 169 | {
|
|---|
| 170 |
|
|---|
| 171 | switch (prb->CDBByte[0]) {
|
|---|
| 172 | case CMD_INQUIRY:
|
|---|
| 173 | TRACE("Vendor: '%s'\n", prb->SRB_BufPointer + INQUIRY_VENDOR);
|
|---|
| 174 | break;
|
|---|
| 175 | case CMD_TEST_UNIT_READY:
|
|---|
| 176 | ASPI_PrintSenseArea(prb);
|
|---|
| 177 | break;
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | static WORD
|
|---|
| 182 | ASPI_ExecScsiCmd(SRB_ExecSCSICmd *lpPRB)
|
|---|
| 183 | {
|
|---|
| 184 | struct sg_header *sg_hd, *sg_reply_hdr;
|
|---|
| 185 | int status;
|
|---|
| 186 | int in_len, out_len;
|
|---|
| 187 | int error_code = 0;
|
|---|
| 188 | int fd;
|
|---|
| 189 |
|
|---|
| 190 | ASPI_DebugPrintCmd(lpPRB);
|
|---|
| 191 |
|
|---|
| 192 | fd = ASPI_OpenDevice(lpPRB);
|
|---|
| 193 | if (fd == -1) {
|
|---|
| 194 | ERR("Failed: could not open device c%01dt%01dd%01d. Device permissions !?\n",
|
|---|
| 195 | lpPRB->SRB_HaId,lpPRB->SRB_Target,lpPRB->SRB_Lun);
|
|---|
| 196 | lpPRB->SRB_Status = SS_NO_DEVICE;
|
|---|
| 197 | return SS_NO_DEVICE;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | sg_hd = NULL;
|
|---|
| 201 | sg_reply_hdr = NULL;
|
|---|
| 202 |
|
|---|
| 203 | lpPRB->SRB_Status = SS_PENDING;
|
|---|
| 204 |
|
|---|
| 205 | if (!lpPRB->SRB_CDBLen) {
|
|---|
| 206 | WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
|
|---|
| 207 | lpPRB->SRB_Status = SS_ERR;
|
|---|
| 208 | return SS_ERR;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /* build up sg_header + scsi cmd */
|
|---|
| 212 | if (HOST_TO_TARGET(lpPRB)) {
|
|---|
| 213 | /* send header, command, and then data */
|
|---|
| 214 | in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
|
|---|
| 215 | sg_hd = (struct sg_header *) malloc(in_len);
|
|---|
| 216 | memset(sg_hd, 0, SCSI_OFF);
|
|---|
| 217 | memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
|
|---|
| 218 | if (lpPRB->SRB_BufLen) {
|
|---|
| 219 | memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpPRB->SRB_BufPointer, lpPRB->SRB_BufLen);
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | else {
|
|---|
| 223 | /* send header and command - no data */
|
|---|
| 224 | in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
|
|---|
| 225 | sg_hd = (struct sg_header *) malloc(in_len);
|
|---|
| 226 | memset(sg_hd, 0, SCSI_OFF);
|
|---|
| 227 | memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | if (TARGET_TO_HOST(lpPRB)) {
|
|---|
| 231 | out_len = SCSI_OFF + lpPRB->SRB_BufLen;
|
|---|
| 232 | sg_reply_hdr = (struct sg_header *) malloc(out_len);
|
|---|
| 233 | memset(sg_reply_hdr, 0, SCSI_OFF);
|
|---|
| 234 | sg_hd->reply_len = out_len;
|
|---|
| 235 | }
|
|---|
| 236 | else {
|
|---|
| 237 | out_len = SCSI_OFF;
|
|---|
| 238 | sg_reply_hdr = (struct sg_header *) malloc(out_len);
|
|---|
| 239 | memset(sg_reply_hdr, 0, SCSI_OFF);
|
|---|
| 240 | sg_hd->reply_len = out_len;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | status = write(fd, sg_hd, in_len);
|
|---|
| 244 | if (status < 0 || status != in_len) {
|
|---|
| 245 | int save_error = errno;
|
|---|
| 246 |
|
|---|
| 247 | WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
|
|---|
| 248 | if (status < 0) {
|
|---|
| 249 | if (save_error == ENOMEM) {
|
|---|
| 250 | MESSAGE("ASPI: Linux generic scsi driver\n You probably need to re-compile your kernel with a larger SG_BIG_BUFF value (sg.h)\n Suggest 130560\n");
|
|---|
| 251 | }
|
|---|
| 252 | #ifdef HAVE_STRERROR
|
|---|
| 253 | WARN("error:= '%s'\n", strerror(save_error));
|
|---|
| 254 | #else
|
|---|
| 255 | WARN("error:= %d\n", save_error);
|
|---|
| 256 | #endif
|
|---|
| 257 | }
|
|---|
| 258 | goto error_exit;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | status = read(fd, sg_reply_hdr, out_len);
|
|---|
| 262 | if (status < 0 || status != out_len) {
|
|---|
| 263 | WARN("not enough bytes read from scsi device%d\n", status);
|
|---|
| 264 | goto error_exit;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | if (sg_reply_hdr->result != 0) {
|
|---|
| 268 | error_code = sg_reply_hdr->result;
|
|---|
| 269 | WARN("reply header error (%d)\n", sg_reply_hdr->result);
|
|---|
| 270 | goto error_exit;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
|
|---|
| 274 | memcpy(lpPRB->SRB_BufPointer, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /* copy in sense buffer to amount that is available in client */
|
|---|
| 278 | if (lpPRB->SRB_SenseLen) {
|
|---|
| 279 | int sense_len = lpPRB->SRB_SenseLen;
|
|---|
| 280 | if (lpPRB->SRB_SenseLen > 16)
|
|---|
| 281 | sense_len = 16;
|
|---|
| 282 | memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 | lpPRB->SRB_Status = SS_COMP;
|
|---|
| 287 | lpPRB->SRB_HaStat = HASTAT_OK;
|
|---|
| 288 | lpPRB->SRB_TargStat = STATUS_GOOD;
|
|---|
| 289 |
|
|---|
| 290 | /* now do posting */
|
|---|
| 291 |
|
|---|
| 292 | if (lpPRB->SRB_PostProc) {
|
|---|
| 293 | if (ASPI_POSTING(lpPRB)) {
|
|---|
| 294 | TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
|
|---|
| 295 | (*lpPRB->SRB_PostProc)(lpPRB);
|
|---|
| 296 | }
|
|---|
| 297 | else
|
|---|
| 298 | if (lpPRB->SRB_Flags & SRB_EVENT_NOTIFY) {
|
|---|
| 299 | TRACE("Setting event %04x\n", (HANDLE)lpPRB->SRB_PostProc);
|
|---|
| 300 | SetEvent((HANDLE)lpPRB->SRB_PostProc); /* FIXME: correct ? */
|
|---|
| 301 | }
|
|---|
| 302 | }
|
|---|
| 303 | free(sg_reply_hdr);
|
|---|
| 304 | free(sg_hd);
|
|---|
| 305 | ASPI_DebugPrintResult(lpPRB);
|
|---|
| 306 | return SS_COMP;
|
|---|
| 307 |
|
|---|
| 308 | error_exit:
|
|---|
| 309 | if (error_code == EBUSY) {
|
|---|
| 310 | lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
|
|---|
| 311 | TRACE("Device busy\n");
|
|---|
| 312 | }
|
|---|
| 313 | else {
|
|---|
| 314 | WARN("Failed\n");
|
|---|
| 315 | lpPRB->SRB_Status = SS_ERR;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | /* I'm not sure exactly error codes work here
|
|---|
| 319 | * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
|
|---|
| 320 | */
|
|---|
| 321 | WARN("error_exit\n");
|
|---|
| 322 | free(sg_reply_hdr);
|
|---|
| 323 | free(sg_hd);
|
|---|
| 324 | return lpPRB->SRB_Status;
|
|---|
| 325 | }
|
|---|
| 326 | #endif
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 | /*******************************************************************
|
|---|
| 330 | * GetASPI32SupportInfo32 [WNASPI32.0]
|
|---|
| 331 | *
|
|---|
| 332 | * Checks if the ASPI subsystem is initialized correctly.
|
|---|
| 333 | *
|
|---|
| 334 | * RETURNS
|
|---|
| 335 | * HIWORD: 0.
|
|---|
| 336 | * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
|
|---|
| 337 | * LOBYTE of LOWORD: # of host adapters.
|
|---|
| 338 | */
|
|---|
| 339 | DWORD WINAPI GetASPI32SupportInfo()
|
|---|
| 340 | {
|
|---|
| 341 | return ((SS_COMP << 8) | 1); /* FIXME: get # of host adapters installed */
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 | /***********************************************************************
|
|---|
| 346 | * SendASPI32Command32 (WNASPI32.1)
|
|---|
| 347 | */
|
|---|
| 348 | DWORD __cdecl SendASPI32Command(LPSRB lpSRB)
|
|---|
| 349 | {
|
|---|
| 350 | #ifdef linux
|
|---|
| 351 | switch (lpSRB->common.SRB_Cmd) {
|
|---|
| 352 | case SC_HA_INQUIRY:
|
|---|
| 353 | lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
|
|---|
| 354 | lpSRB->inquiry.HA_Count = 1; /* not always */
|
|---|
| 355 | lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
|
|---|
| 356 | strcat(lpSRB->inquiry.HA_ManagerId, "ASPI for WIN32"); /* max 15 chars, don't change */
|
|---|
| 357 | strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host adapter name */
|
|---|
| 358 | memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
|
|---|
| 359 | lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
|
|---|
| 360 | FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
|
|---|
| 361 | return SS_COMP;
|
|---|
| 362 | case SC_GET_DEV_TYPE:
|
|---|
| 363 | FIXME("Not implemented SC_GET_DEV_TYPE\n");
|
|---|
| 364 | break;
|
|---|
| 365 | case SC_EXEC_SCSI_CMD:
|
|---|
| 366 | return ASPI_ExecScsiCmd(&lpSRB->cmd);
|
|---|
| 367 | break;
|
|---|
| 368 | case SC_RESET_DEV:
|
|---|
| 369 | FIXME("Not implemented SC_RESET_DEV\n");
|
|---|
| 370 | break;
|
|---|
| 371 | default:
|
|---|
| 372 | WARN("Unknown command %d\n", lpSRB->common.SRB_Cmd);
|
|---|
| 373 | }
|
|---|
| 374 | return SS_INVALID_SRB;
|
|---|
| 375 | #else
|
|---|
| 376 | return SS_INVALID_SRB;
|
|---|
| 377 | #endif
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 | /***********************************************************************
|
|---|
| 382 | * GetASPI32DLLVersion32 (WNASPI32.3)
|
|---|
| 383 | */
|
|---|
| 384 |
|
|---|
| 385 | DWORD WINAPI GetASPI32DLLVersion()
|
|---|
| 386 | {
|
|---|
| 387 | #ifdef linux
|
|---|
| 388 | return (DWORD)1;
|
|---|
| 389 | #else
|
|---|
| 390 | return (DWORD)0;
|
|---|
| 391 | #endif
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|