41ced9341d509d26906e0ba06de1c07823b34632
[debian/mtx] / scsi_aix.c
1 /* Changes 2003 Steve Heck <steve.heck@am.sony.com>
2
3 $Date: 2007-03-04 15:27:11 -0800 (Sun, 04 Mar 2007) $
4 $Revision: 164 $
5
6   This program is free software; you may redistribute and/or modify it under
7   the terms of the GNU General Public License Version 2 as published by the
8   Free Software Foundation.
9
10   This program is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
12   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13   for complete details.
14
15 */
16
17
18 /* This is the SCSI commands for AIX using GSC Generic SCSI Interface. */
19
20 #define LONG_PRINT_REQUEST_SENSE  /* sigh! */
21
22 DEVICE_TYPE SCSI_OpenDevice(char *DeviceName)
23 {
24         int DeviceFD = open(DeviceName, 0); 
25
26         if (DeviceFD < 0)
27                 FatalError("cannot open SCSI device '%s' - %m\n", DeviceName);
28         return (DEVICE_TYPE) DeviceFD;
29 }
30
31
32 void SCSI_CloseDevice(char *DeviceName, DEVICE_TYPE DeviceFD)
33 {
34         if (close(DeviceFD) < 0)
35                 FatalError("cannot close SCSI device '%s' - %m\n", DeviceName);
36 }
37
38
39 #define HAS_SCSI_TIMEOUT
40
41 static int timeout = 9 * 60;
42
43 void SCSI_Set_Timeout(int to)
44 {
45         timeout = to;
46 }
47
48 void SCSI_Default_Timeout(void)
49 {
50         timeout = 9 * 60; /* the default */
51 }
52
53 #ifdef DEBUG
54 int SCSI_DumpBuffer(int DataBufferLength, unsigned char *DataBuffer)
55 {
56         int i, j;
57         j = 0;
58
59         for (i = 0; i < DataBufferLength; i++)
60         {
61                 if (j == 25)
62                 {
63                         fprintf(stderr, "\n");
64                         j = 0;
65                 }
66
67                 if (j == 0)
68                 {
69                         fprintf(stderr, "%04x:", i);
70                 }
71
72                 if (j > 0)
73                 {
74                         fprintf(stderr, " ");
75                 }
76
77                 fprintf(stderr, "%02x", (int)DataBuffer[i]);
78                 j++;
79         }
80         fprintf(stderr, "\n");
81 }
82 #endif
83
84
85
86 int SCSI_ExecuteCommand(DEVICE_TYPE DeviceFD,
87                                                 Direction_T Direction,
88                                                 CDB_T *CDB,
89                                                 int CDB_Length,
90                                                 void *DataBuffer,
91                                                 int DataBufferLength,
92                                                 RequestSense_T *RequestSense)
93 {
94         int ioctl_result;
95         char sbyte;
96         scmd_t scmd;
97
98 #ifdef DEBUG_SCSI
99         fprintf(stderr,"------CDB--------\n");
100         SCSI_DumpBuffer(CDB_Length,(char *)CDB);
101 #endif
102
103         /* memset(&scmd, 0, sizeof(struct scmd_t)); */
104         /* memset(RequestSense, 0, sizeof(RequestSense_T)); */
105         switch (Direction)
106         {
107         case Input:
108                 scmd.rw = 1;
109                 if (DataBufferLength > 0)
110                 {
111                         memset(DataBuffer, 0, DataBufferLength);
112                 }
113                 break;
114
115         case Output:
116                 scmd.rw = 2;
117                 break;
118         }
119         /* Set timeout to 5 minutes. */
120 #ifdef DEBUG_TIMEOUT
121         fprintf(stderr,"timeout=%d\n",timeout);
122         fflush(stderr);
123 #endif
124
125         scmd.timeval = timeout;
126
127         scmd.cdb = (caddr_t) CDB;
128         scmd.cdblen = CDB_Length;
129         scmd.data_buf = DataBuffer;
130         scmd.datalen = DataBufferLength;
131         scmd.sense_buf = (caddr_t) RequestSense;
132         scmd.senselen = sizeof(RequestSense_T);
133         scmd.statusp = &sbyte;
134         ioctl_result = ioctl(DeviceFD, GSC_CMD, (caddr_t) &scmd);
135
136         SCSI_Default_Timeout(); /* set it back to default, sigh. */
137
138         if (ioctl_result < 0)
139         {
140 #ifdef DEBUG
141                 perror("mtx");
142 #endif
143                 return ioctl_result;
144         }
145
146         if (sbyte != 0)
147         {
148                 return -1;
149         }
150 #ifdef DEBUG_SCSI
151         if (Direction==Input)
152         {
153                 fprintf(stderr,"--------input data-----------\n");
154                 SCSI_DumpBuffer(DataBufferLength,DataBuffer);
155         }
156 #endif
157         return 0;
158 }