File: scsi_aix.c

package info (click to toggle)
mtx 1.3.12-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,148 kB
  • sloc: ansic: 7,121; sh: 3,199; python: 203; perl: 117; makefile: 113
file content (159 lines) | stat: -rw-r--r-- 3,128 bytes parent folder | download | duplicates (5)
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
/* Changes 2003 Steve Heck <steve.heck@am.sony.com>
   Copyright 2007-2008 by Robert Nelson <robertn@the-nelsons.org>

$Date: 2008-08-19 03:03:38 -0700 (Tue, 19 Aug 2008) $
$Revision: 193 $

  This program is free software; you may redistribute and/or modify it under
  the terms of the GNU General Public License Version 2 as published by the
  Free Software Foundation.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  for complete details.

*/


/* This is the SCSI commands for AIX using GSC Generic SCSI Interface. */

#define LONG_PRINT_REQUEST_SENSE  /* sigh! */

DEVICE_TYPE SCSI_OpenDevice(char *DeviceName)
{
	int DeviceFD = open(DeviceName, 0); 

	if (DeviceFD < 0)
		FatalError("cannot open SCSI device '%s' - %m\n", DeviceName);
	return (DEVICE_TYPE) DeviceFD;
}


void SCSI_CloseDevice(char *DeviceName, DEVICE_TYPE DeviceFD)
{
	if (close(DeviceFD) < 0)
		FatalError("cannot close SCSI device '%s' - %m\n", DeviceName);
}


#define HAS_SCSI_TIMEOUT

static int timeout = 9 * 60;

void SCSI_Set_Timeout(int to)
{
	timeout = to;
}

void SCSI_Default_Timeout(void)
{
	timeout = 9 * 60; /* the default */
}

#ifdef DEBUG
int SCSI_DumpBuffer(int DataBufferLength, unsigned char *DataBuffer)
{
	int i, j;
	j = 0;

	for (i = 0; i < DataBufferLength; i++)
	{
		if (j == 25)
		{
			fprintf(stderr, "\n");
			j = 0;
		}

		if (j == 0)
		{
			fprintf(stderr, "%04x:", i);
		}

		if (j > 0)
		{
			fprintf(stderr, " ");
		}

		fprintf(stderr, "%02x", (int)DataBuffer[i]);
		j++;
	}
	fprintf(stderr, "\n");
}
#endif



int SCSI_ExecuteCommand(DEVICE_TYPE DeviceFD,
						Direction_T Direction,
						CDB_T *CDB,
						int CDB_Length,
						void *DataBuffer,
						int DataBufferLength,
						RequestSense_T *RequestSense)
{
	int ioctl_result;
	char sbyte;
	scmd_t scmd;

#ifdef DEBUG_SCSI
	fprintf(stderr,"------CDB--------\n");
	SCSI_DumpBuffer(CDB_Length,(char *)CDB);
#endif

	/* memset(&scmd, 0, sizeof(struct scmd_t)); */
	/* memset(RequestSense, 0, sizeof(RequestSense_T)); */
	switch (Direction)
	{
	case Input:
		scmd.rw = 1;
		if (DataBufferLength > 0)
		{
			memset(DataBuffer, 0, DataBufferLength);
		}
		break;

	case Output:
		scmd.rw = 2;
		break;
	}
	/* Set timeout to 5 minutes. */
#ifdef DEBUG_TIMEOUT
	fprintf(stderr,"timeout=%d\n",timeout);
	fflush(stderr);
#endif

	scmd.timeval = timeout;

	scmd.cdb = (caddr_t) CDB;
	scmd.cdblen = CDB_Length;
	scmd.data_buf = DataBuffer;
	scmd.datalen = DataBufferLength;
	scmd.sense_buf = (caddr_t) RequestSense;
	scmd.senselen = sizeof(RequestSense_T);
	scmd.statusp = &sbyte;
	ioctl_result = ioctl(DeviceFD, GSC_CMD, (caddr_t) &scmd);

	SCSI_Default_Timeout(); /* set it back to default, sigh. */

	if (ioctl_result < 0)
	{
#ifdef DEBUG
		perror("mtx");
#endif
		return ioctl_result;
	}

	if (sbyte != 0)
	{
		return -1;
	}
#ifdef DEBUG_SCSI
	if (Direction==Input)
	{
		fprintf(stderr,"--------input data-----------\n");
		SCSI_DumpBuffer(DataBufferLength,DataBuffer);
	}
#endif
	return 0;
}