File: scsi_linux.c

package info (click to toggle)
mtx 1.2.17rel-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 692 kB
  • ctags: 696
  • sloc: ansic: 4,387; sh: 3,191; python: 203; makefile: 129; perl: 117
file content (401 lines) | stat: -rw-r--r-- 12,859 bytes parent folder | download | duplicates (2)
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
/* Copyright 1997, 1998 Leonard Zubkoff <lnz@dandelion.com>
   Changes in Feb 2000 Eric Green <eric@estinc.com>

$Date: 2001/06/19 21:51:32 $
$Revision: 1.2 $

  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.

*/

#include <linux/param.h>
#include <scsi/sg.h>

/* this is the SCSI commands for Linux. Note that <eric@estinc.com> changed 
 * it from using SCSI_IOCTL_SEND_COMMAND to using the SCSI generic interface.
 */

#ifndef HZ
#warning "HZ is not defined, mtx might not work correctly!"
#define HZ 100
#endif

/* These are copied out of BRU 16.1, with all the boolean masks changed
 * to our bitmasks.
*/
#define S_NO_SENSE(s) ((s)->SenseKey == 0x0)
#define S_RECOVERED_ERROR(s) ((s)->SenseKey == 0x1)

#define S_NOT_READY(s) ((s)->SenseKey == 0x2)
#define S_MEDIUM_ERROR(s) ((s)->SenseKey == 0x3)
#define S_HARDWARE_ERROR(s) ((s)->SenseKey == 0x4)
#define S_UNIT_ATTENTION(s) ((s)->SenseKey == 0x6)
#define S_BLANK_CHECK(s) ((s)->SenseKey == 0x8)
#define S_VOLUME_OVERFLOW(s) ((s)->SenseKey == 0xd)

#define DEFAULT_TIMEOUT 3*60  /* 3 minutes here */

/* Sigh, the T-10 SSC spec says all of the following is needed to
 * detect a short read while in variable block mode, and that even
 * though we got a BLANK_CHECK or MEDIUM_ERROR, it's still a valid read.
 */

#define HIT_FILEMARK(s) (S_NO_SENSE((s)) && (s)->Filemark && (s)->Valid)
#define SHORT_READ(s) (S_NO_SENSE((s)) && (s)->ILI && (s)->Valid &&  (s)->AdditionalSenseCode==0  && (s)->AdditionalSenseCodeQualifier==0)
#define HIT_EOD(s) (S_BLANK_CHECK((s)) && (s)->Valid)
#define HIT_EOP(s) (S_MEDIUM_ERROR((s)) && (s)->EOM && (s)->Valid)
#define HIT_EOM(s) ((s)->EOM && (s)->Valid)

#define STILL_A_VALID_READ(s) (HIT_FILEMARK(s) || SHORT_READ(s) || HIT_EOD(s) || HIT_EOP(s) || HIT_EOM(s))


#define SG_SCSI_DEFAULT_TIMEOUT HZ*60*5			/* 5 minutes */

DEVICE_TYPE SCSI_OpenDevice(char *DeviceName)
{
  int timeout=SG_SCSI_DEFAULT_TIMEOUT;
  int DeviceFD = open(DeviceName, O_RDWR);
  if (DeviceFD < 0)
    FatalError("cannot open SCSI device '%s' - %m\n", DeviceName);

  if(ioctl(DeviceFD, SG_SET_TIMEOUT, &timeout)) {
    FatalError("failed to set sg timeout - %m\n");
  }

  return (DEVICE_TYPE) DeviceFD;
}

static int sg_timeout = SG_SCSI_DEFAULT_TIMEOUT ;

void SCSI_Set_Timeout(int secs) {
  sg_timeout=secs*HZ;
}
 
void SCSI_Default_Timeout(void) {
  sg_timeout=SG_SCSI_DEFAULT_TIMEOUT;
}

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


/* Added by Eric Green <eric@estinc.com> to deal with burping
 * Seagate autoloader (hopefully!). 
 */
/* Get the SCSI ID and LUN... */
scsi_id_t *SCSI_GetIDLun(DEVICE_TYPE fd) {
  int status;
  scsi_id_t *retval;
  
  struct my_scsi_idlun {
    int word1;
    int word2;
  } idlun;

  status=ioctl(fd,SCSI_IOCTL_GET_IDLUN,&idlun);
  if (status) {
    return NULL; /* sorry! */
  }
  
  retval=(scsi_id_t *)xmalloc(sizeof(scsi_id_t));
  retval->id=idlun.word1 & 0xff;
  retval->lun=idlun.word1 >> 8 & 0xff;
#ifdef DEBUG
  fprintf(stderr,"SCSI:ID=%d LUN=%d\n",retval->id,retval->lun);
#endif
  return retval;
}
  

/* Changed February 2000 by Eric Green <eric@estinc.com> to 
 * use the SCSI generic interface rather than SCSI_IOCTL_SEND_COMMAND
 * so that we can get more than PAGE_SIZE data....
 *
 * Note that the SCSI generic interface abuses READ and WRITE calls to serve
 * the same purpose as IOCTL calls, i.e., for "writes", the contents of the
 * buffer that you send as the argument to the write() call are actually
 * altered to fill in result status and sense data (if needed). 
 * Also note that this brain-dead interface does not have any sort of 
 * provisions for expanding the sg_header struct in a backward-compatible
 * manner. This sucks. But sucks less than SCSI_IOCTL_SEND_COMMAND, sigh.
 */

#ifndef OLD_EXECUTE_COMMAND_STUFF

static void slow_memcopy(unsigned char *src, unsigned char *dest, int numbytes)
{
  while (numbytes--) {
    *dest++ = *src++;
  }
}


int SCSI_ExecuteCommand(DEVICE_TYPE DeviceFD,
			       Direction_T Direction,
			       CDB_T *CDB,
			       int CDB_Length,
			       void *DataBuffer,
			       int DataBufferLength,
			       RequestSense_T *RequestSense)
{

  unsigned char *Command=NULL;   /* the command data struct sent to them... */
  unsigned char *ResultBuf=NULL; /* the data we read in return...         */

  unsigned char *src;       /* for copying stuff, sigh. */
  unsigned char *dest;      /* for copy stuff, again, sigh. */

  int write_length = sizeof(struct sg_header)+CDB_Length;
  int i;                  /* a random index...          */
  int result;             /* the result of the write... */ 
  
  
  struct sg_header *Header; /* we actually point this into Command... */
  struct sg_header *ResultHeader; /* we point this into ResultBuf... */


  /* First, see if we need to set our SCSI timeout to something different */
  if (sg_timeout != SG_SCSI_DEFAULT_TIMEOUT) {
    /* if not default, set it: */
#ifdef DEBUG_TIMEOUT
    fprintf(stderr,"Setting timeout to %d\n", sg_timeout);
    fflush(stderr);
#endif
    if(ioctl(DeviceFD, SG_SET_TIMEOUT, &sg_timeout)) {
      FatalError("failed to set sg timeout - %m\n");
    }
  }

  if (Direction == Output) {  /* if we're writing, our length is longer... */
    write_length += DataBufferLength; 
  }
  
  /* allocate some memory... enough for the command plus the header +
   *  any other data that we may need here...
   */
  
  Command=(unsigned char *)xmalloc(write_length);
  Header = (struct sg_header *) Command;  /* make it point to start of buf */

  dest=Command; /* now to copy the CDB... from start of buffer,*/
  dest+= sizeof(struct sg_header); /* increment it past the header. */

  slow_memcopy((char *)CDB,dest,CDB_Length);

  /* if we are writing additional data, tack it on here! */
  if (Direction == Output) {
    dest += CDB_Length;
    slow_memcopy(DataBuffer,dest,DataBufferLength); /* copy to end of command */
  }
  /* Now to fill in the Header struct: */
  Header->reply_len=DataBufferLength+sizeof(struct sg_header);
#ifdef DEBUG
  fprintf(stderr,"sg:reply_len(sent)=%d\n",Header->reply_len);
#endif
  Header->twelve_byte = CDB_Length == 12; 
  Header->result = 0;
  Header->pack_len = write_length; /* # of bytes written... */
  Header->pack_id = 0;             /* not used              */
  Header->other_flags = 0;         /* not used.             */
  Header->sense_buffer[0]=0;      /* used? */

  /* Now to do the write... */
  result=write(DeviceFD,Command,write_length);
  
  /* Now to check the result :-(. */
  /* Note that we don't have any request sense here. So we have no
   * idea what's going on. 
   */ 
  if ( (result < 0) || (result != write_length) || Header->result ||
       Header->sense_buffer[0] ) {
#ifdef DEBUG_SCSI
    fprintf(stderr,"scsi:result=%d Header->result=%d Header->sense_buffer[0]=%d\n",
	    result,Header->result,Header->sense_buffer[0]);
#endif  
    /* we don't have any real sense data, sigh :-(. */
    if (Header->sense_buffer[0]) {
      /* well, I guess we DID have some! eep! copy the sense data! */
      slow_memcopy((char *)Header->sense_buffer,(char *)RequestSense,
		   sizeof(Header->sense_buffer));
    } else {
      dest=(unsigned char *)RequestSense;
      *dest=(unsigned char)Header->result; /* may chop, sigh... */
    }

    /* okay, now, we may or may not need to find a non-zero value to return.
     * For tape drives, we may get a BLANK_CHECK or MEDIUM_ERROR and find
     * that it's *STILL* a good read! Use the STILL_A_VALID_READ macro
     * that calls all those macros I cribbed from Richard. 
     */
    
    if (!STILL_A_VALID_READ(RequestSense)) {
      free(Command); /* zap memory leak, sigh */
      /* okay, find us a non-zero value to return :-(. */
      if (result) {
	return result;
      } else if (Header->result) {
	return Header->result;
      } else {
	return -1;  /* sigh */
      }
    } else {
      result=-1;
    }
  } else {
    result=0; /* we're okay! */
  }
    
  
  /* now to allocate the new block.... */
  ResultBuf=(unsigned char *)xmalloc(Header->reply_len);
  /* now to clear ResultBuf... */
  slow_bzero(ResultBuf,Header->reply_len); 

  ResultHeader=(struct sg_header *)ResultBuf;
  
  /* copy the original Header... */
  ResultHeader->result=0;
  ResultHeader->pack_id=0;
  ResultHeader->other_flags=0;
  ResultHeader->reply_len=Header->reply_len;
  ResultHeader->twelve_byte = CDB_Length == 12; 
  ResultHeader->pack_len = write_length; /* # of bytes written... */
  ResultHeader->sense_buffer[0]=0; /* whoops! Zero that! */
#ifdef DEBUG
  fprintf(stderr,"sg:Reading %d bytes from DeviceFD\n",Header->reply_len);
  fflush(stderr);
#endif
  result=read(DeviceFD,ResultBuf,Header->reply_len);
#ifdef DEBUG
  fprintf(stderr,"sg:result=%d ResultHeader->result=%d\n",
	  result,ResultHeader->result);
  fflush(stderr);
#endif
  /* New: added check to see if the result block is still all zeros! */
  if ( (result < 0) || (result != Header->reply_len) || ResultHeader->result ||
       ResultHeader->sense_buffer[0] ) {
#ifdef DEBUG
    fprintf(stderr,
	    "scsi: result=%d Header->reply_len=%d ResultHeader->result=%d ResultHeader->sense_buffer[0]=%d\n",
	    result,
	    Header->reply_len,
	    ResultHeader->result,
	    ResultHeader->sense_buffer[0]);
#endif
    /* eep! copy the sense data! */
    slow_memcopy((char *)ResultHeader->sense_buffer,(char *)RequestSense,
		 sizeof(ResultHeader->sense_buffer));
    /* sense data copied, now find us a non-zero value to return :-(. */
    /* NOTE: Some commands return sense data even though they validly
     * executed! We catch a few of those with the macro STILL_A_VALID_READ.
     */

    if (!STILL_A_VALID_READ(RequestSense)) {
      free(Command);
      if (result) {
	free(ResultBuf);
	return result;
      } else if (ResultHeader->result) {
	free(ResultBuf);
	return ResultHeader->result;
      } else {
	free(ResultBuf);
	return -1; /* sigh! */
      } 
    } else {
      result=-1; /* if it was a valid read, still have -1 result. */
    }
  } else {
    result=0;
  }
  
  /* See if we need to reset our SCSI timeout */
  if (sg_timeout != SG_SCSI_DEFAULT_TIMEOUT) {
    sg_timeout = SG_SCSI_DEFAULT_TIMEOUT; /* reset it back to default */
#ifdef DEBUG_TIMEOUT
    fprintf(stderr,"Setting timeout to %d\n", sg_timeout);
    fflush(stderr);
#endif
    /* if not default, set it: */
    if(ioctl(DeviceFD, SG_SET_TIMEOUT, &sg_timeout)) {
      FatalError("failed to set sg timeout - %m\n");
    }
  }
  
  
  /* now for the crowning moment: copying any result into the DataBuffer! */
  /* (but only if it were an input command and not an output command :-}  */
  if (Direction == Input) {
#ifdef DEBUG
    fprintf(stderr,"Header->reply_len=%d,ResultHeader->reply_len=%d\n",
	    Header->reply_len,ResultHeader->reply_len);
#endif
    src=ResultBuf+sizeof(struct sg_header);
    dest=DataBuffer;
    for (i=0;i< (ResultHeader->reply_len);i++) {
      if (i>=DataBufferLength) break;  /* eep! */
      *dest++=*src++;
    }
  }
  
  /* and return! */
  free(Command);    /* clean up memory leak... */
  free(ResultBuf);
  return result; /* good stuff ! */
}

#endif  
  

#ifdef OLD_EXECUTE_COMMAND_STUFF

int SCSI_ExecuteCommand(int DeviceFD,
			       Direction_T Direction,
			       CDB_T CDB,
			       int CDB_Length,
			       void *DataBuffer,
			       int DataBufferLength,
			       RequestSense_T *RequestSense)
{
  unsigned char *Command;
  int Zero = 0, Result;
  memset(RequestSense, 0, sizeof(RequestSense_T));
  switch (Direction)
    {
    case Input:
      Command = (unsigned char *)
	xmalloc(8 + max(DataBufferLength, sizeof(RequestSense_T)));
      memcpy(&Command[0], &Zero, 4);
      memcpy(&Command[4], &DataBufferLength, 4);
      memcpy(&Command[8], CDB, CDB_Length);
      break;
    case Output:
      Command = (unsigned char *)
	xmalloc(8 + max(CDB_Length + DataBufferLength, sizeof(RequestSense_T)));
      memcpy(&Command[0], &DataBufferLength, 4);
      memcpy(&Command[4], &Zero, 4);
      memcpy(&Command[8], CDB, CDB_Length);
      memcpy(&Command[8 + CDB_Length], DataBuffer, DataBufferLength);
      break;
    }
  Result = ioctl(DeviceFD, SCSI_IOCTL_SEND_COMMAND, Command);
  if (Result != 0)
    memcpy(RequestSense, &Command[8], sizeof(RequestSense_T));
  else if (Direction == Input)
    memcpy(DataBuffer, &Command[8], DataBufferLength);
  free(Command);
  return Result;
}

#endif