Imported Upstream version 2.5.1
[debian/amanda] / changer-src / scsi-irix.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-2000 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: scsi-irix.c,v 1.23 2006/05/25 01:47:08 johnfranks Exp $
28  *
29  * Interface to execute SCSI commands on an SGI Workstation
30  *
31  * Copyright (c) Thomas Hepper th@ant.han.de
32  */
33
34
35 #include <amanda.h>
36
37 #ifdef HAVE_IRIX_LIKE_SCSI
38
39 /*
40 #ifdef HAVE_STDIO_H
41 */
42 #include <stdio.h>
43 /*
44 #endif
45 */
46 #ifdef HAVE_SYS_TYPES_H
47 #include <sys/types.h>
48 #endif
49 #ifdef HAVE_SYS_STAT_H
50 #include <sys/stat.h>
51 #endif
52 #ifdef HAVE_FCNTL_H
53 #include <fcntl.h>
54 #endif
55
56 #include <sys/scsi.h>
57 #include <sys/dsreq.h>
58 #include <sys/mtio.h>
59
60 #include <scsi-defs.h>
61
62 void SCSI_OS_Version()
63 {
64 #ifndef lint
65    static char rcsid[] = "$Id: scsi-irix.c,v 1.23 2006/05/25 01:47:08 johnfranks Exp $";
66    DebugPrint(DEBUG_INFO, SECTION_INFO, "scsi-os-layer: %s\n",rcsid);
67 #endif
68 }
69
70
71 /*
72  */
73 int SCSI_OpenDevice(int ip)
74 {
75   extern OpenFiles_T *pDev;
76   int DeviceFD;
77   int i;
78   
79   if (pDev[ip].inqdone == 0)
80     {
81       if ((DeviceFD = open(pDev[ip].dev, O_RDWR | O_EXCL)) >= 0)
82         {
83           pDev[ip].inqdone = 1;          pDev[ip].SCSI = 0;
84           pDev[ip].avail = 1;
85           pDev[ip].fd = DeviceFD;
86           pDev[ip].inquiry = (SCSIInquiry_T *)malloc(INQUIRY_SIZE);
87           if (SCSI_Inquiry(ip, pDev[ip].inquiry, INQUIRY_SIZE) == 0)
88             {
89               if (pDev[ip].inquiry->type == TYPE_TAPE || pDev[ip].inquiry->type == TYPE_CHANGER)
90                 {
91                   for (i=0;i < 16 ;i++)
92                     pDev[ip].ident[i] = pDev[ip].inquiry->prod_ident[i];
93                   for (i=15; i >= 0 && !isalnum((int)pDev[ip].ident[i]) ; i--)
94                     {
95                       pDev[ip].ident[i] = '\0';
96                     }
97                   pDev[ip].SCSI = 1;
98                   close(DeviceFD);
99
100                   if (pDev[ip].inquiry->type == TYPE_TAPE)
101                   {
102                           pDev[ip].type = stralloc("tape");
103                   }
104
105                   if (pDev[ip].inquiry->type == TYPE_CHANGER)
106                   {
107                           pDev[ip].type = stralloc("changer");
108                   }
109
110                   PrintInquiry(pDev[ip].inquiry);
111                   return(1);
112                 } else { /* ! TYPE_TAPE ! TYPE_CHANGER */
113                   close(DeviceFD);
114                   free(pDev[ip].inquiry);
115                   pDev[ip].inquiry = NULL;
116                   pDev[ip].avail = 0;
117                   return(0);
118                 }
119             }
120             /* inquiry failed or no SCSI communication available */
121             close(DeviceFD);
122             free(pDev[ip].inquiry);
123             pDev[ip].inquiry = NULL;
124             pDev[ip].avail = 0;
125             return(0);
126         }
127     } else {
128       if ((DeviceFD = open(pDev[ip].dev, O_RDWR | O_EXCL)) >= 0)
129         {
130           pDev[ip].fd = DeviceFD;
131           pDev[ip].devopen = 1;
132           return(1);
133         } else {
134           pDev[ip].devopen = 0;
135           return(0);
136         }
137     }
138   return(0); 
139 }
140
141 int SCSI_CloseDevice(int DeviceFD)
142 {
143   extern OpenFiles_T *pDev;
144   int ret = 0;
145   
146   if (pDev[DeviceFD].devopen == 1)
147     {
148       pDev[DeviceFD].devopen = 0;
149       ret = close(pDev[DeviceFD].fd);
150     }
151
152   return(ret);
153 }
154
155 int SCSI_ExecuteCommand(int DeviceFD,
156                         Direction_T Direction,
157                         CDB_T CDB,
158                         size_t CDB_Length,
159                         void *DataBuffer,
160                         size_t DataBufferLength,
161                         RequestSense_T *pRequestSense,
162                         size_t RequestSenseLength)
163 {
164   extern OpenFiles_T *pDev;
165   ExtendedRequestSense_T ExtendedRequestSense;
166   struct dsreq ds;
167   int Result;
168   int retries = 5;
169   
170   /* Basic sanity checks */
171   assert(CDB_Length <= UCHAR_MAX);
172   assert(RequestSenseLength <= UCHAR_MAX);
173
174   /* Clear buffer for cases where sense is not returned */
175   memset(pRequestSense, 0, RequestSenseLength);
176
177   if (pDev[DeviceFD].avail == 0)
178     {
179       return(SCSI_ERROR);
180     }
181   
182   memset(&ds, 0, SIZEOF(struct dsreq));
183   memset(pRequestSense, 0, RequestSenseLength);
184   memset(&ExtendedRequestSense, 0 , SIZEOF(ExtendedRequestSense_T)); 
185   
186   ds.ds_flags = DSRQ_SENSE|DSRQ_TRACE|DSRQ_PRINT; 
187   /* Timeout */
188   ds.ds_time = 120000;
189   /* Set the cmd */
190   ds.ds_cmdbuf = (caddr_t)CDB;
191   ds.ds_cmdlen = CDB_Length;
192   /* Data buffer for results */
193   ds.ds_databuf = (caddr_t)DataBuffer;
194   ds.ds_datalen = DataBufferLength;
195   /* Sense Buffer */
196   ds.ds_sensebuf = (caddr_t)pRequestSense;
197   ds.ds_senselen = RequestSenseLength;
198   
199   switch (Direction) 
200     {
201     case Input:
202       ds.ds_flags = ds.ds_flags | DSRQ_READ;
203       break;
204     case Output:
205       ds.ds_flags = ds.ds_flags | DSRQ_WRITE;
206       break;
207     }
208   
209   while (--retries > 0) {
210     if (pDev[DeviceFD].devopen == 0)
211       {
212         if (SCSI_OpenDevice(DeviceFD) == 0)
213           {
214             dbprintf(("SCSI_ExecuteCommand could not open %s: %s\n",
215                       pDev[DeviceFD].dev,
216                       strerror(errno)));
217             sleep(1); /* Give device a little time befor retry */
218             continue;
219           }
220       }
221     Result = ioctl(pDev[DeviceFD].fd, DS_ENTER, &ds);
222     SCSI_CloseDevice(DeviceFD);
223
224     if (Result < 0)
225       {
226         RET(&ds) = DSRT_DEVSCSI;
227         SCSI_CloseDevice(DeviceFD);
228         return (SCSI_ERROR);
229       }
230     DecodeSCSI(CDB, "SCSI_ExecuteCommand : ");
231     dbprintf(("\t\t\tSTATUS(%02X) RET(%02X)\n", STATUS(&ds), RET(&ds)));
232     switch (STATUS(&ds))
233       {
234       case ST_BUSY:                /*  BUSY */
235         break;
236
237       case STA_RESERV:             /*  RESERV CONFLICT */
238         if (retries > 0)
239           sleep(2);
240         continue;
241
242       case ST_GOOD:                /*  GOOD 0x00 */
243         switch (RET(&ds))
244           {
245           case DSRT_SENSE:
246             return(SCSI_SENSE);
247           }
248           return(SCSI_OK);
249
250       case ST_CHECK:               /*  CHECK CONDITION 0x02 */ 
251         switch (RET(&ds))
252           {
253           case DSRT_SENSE:
254             return(SCSI_SENSE);
255           }
256         return(SCSI_CHECK);
257
258       case ST_COND_MET:            /*  INTERM/GOOD 0x10 */
259       default:
260         continue;
261       }
262   }     
263   return(SCSI_ERROR);
264 }
265
266 int Tape_Ioctl ( int DeviceFD, int command)
267 {
268   extern OpenFiles_T *pDev;
269   struct mtop mtop;
270   
271   if (pDev[DeviceFD].devopen == 0)
272     {
273       if (SCSI_OpenDevice(DeviceFD) == 0)
274           return(-1);
275     }
276   
277   switch (command)
278     {
279     case IOCTL_EJECT:
280       mtop.mt_op = MTUNLOAD;
281       mtop.mt_count = 1;
282       break;
283     default:
284       break;
285     }
286   
287   ioctl(pDev[DeviceFD].fd, MTIOCTOP, &mtop);
288   SCSI_CloseDevice(DeviceFD);
289   return(0);
290 }
291
292 int Tape_Status( int DeviceFD)
293 {
294   extern OpenFiles_T *pDev;
295   struct mtget mtget;
296   int ret = 0;
297
298   if (pDev[DeviceFD].devopen == 0)
299     {
300       if (SCSI_OpenDevice(DeviceFD) == 0)
301           return(-1);
302     }
303
304   if (ioctl(pDev[DeviceFD].fd , MTIOCGET, &mtget) != 0)
305     {
306       dbprintf(("Tape_Status error ioctl %s\n",strerror(errno)));
307       SCSI_CloseDevice(DeviceFD);
308       return(-1);
309     }
310   
311   switch(mtget.mt_dposn)
312     {
313     case MT_EOT:
314       ret = ret | TAPE_EOT;
315       break;
316     case MT_BOT:
317       ret = ret | TAPE_BOT;
318       break;
319     case MT_WPROT:
320       ret = ret | TAPE_WR_PROT;
321       break;
322     case MT_ONL:
323       ret = TAPE_ONLINE;
324       break;
325     case MT_EOD:
326       break;
327     case MT_FMK:
328       break;
329     default:
330       break;
331     }
332
333   SCSI_CloseDevice(DeviceFD);
334   return(ret); 
335 }
336
337 int ScanBus(int print)
338 {
339   DIR *dir;
340   struct dirent *dirent;
341   extern OpenFiles_T *pDev;
342   extern int errno;
343   int count = 0;
344
345   if ((dir = opendir("/dev/scsi")) == NULL)
346     {
347       dbprintf(("Can not read /dev/scsi: %s", strerror(errno)));
348       return 0;
349     }
350
351   while ((dirent = readdir(dir)) != NULL)
352     {
353       if (strstr(dirent->d_name, "sc") != NULL)
354       {
355         pDev[count].dev = malloc(10);
356         pDev[count].inqdone = 0;
357         sprintf(pDev[count].dev,"/dev/scsi/%s", dirent->d_name);
358         if (OpenDevice(count,pDev[count].dev, "Scan", NULL ))
359           {
360             SCSI_CloseDevice(count);
361             pDev[count].inqdone = 0;
362             
363             if (print)
364               {
365                 printf("name /dev/scsi/%s ", dirent->d_name);
366                 
367                 switch (pDev[count].inquiry->type)
368                   {
369                   case TYPE_DISK:
370                     printf("Disk");
371                     break;
372                   case TYPE_TAPE:
373                     printf("Tape");
374                     break;
375                   case TYPE_PRINTER:
376                     printf("Printer");
377                     break;
378                   case TYPE_PROCESSOR:
379                     printf("Processor");
380                     break;
381                   case TYPE_WORM:
382                     printf("Worm");
383                     break;
384                   case TYPE_CDROM:
385                     printf("Cdrom");
386                     break;
387                   case TYPE_SCANNER:
388                     printf("Scanner");
389                     break;
390                   case TYPE_OPTICAL:
391                     printf("Optical");
392                     break;
393                   case TYPE_CHANGER:
394                     printf("Changer");
395                     break;
396                   case TYPE_COMM:
397                     printf("Comm");
398                     break;
399                   default:
400                     printf("unknown %d",pDev[count].inquiry->type);
401                     break;
402                   }
403                 printf("\n");
404               }
405             count++;
406             printf("Count %d\n",count);
407           } else {
408             free(pDev[count].dev);
409             pDev[count].dev=NULL;
410           }
411       }
412     }
413   return 0;
414 }
415
416 #endif
417 /*
418  * Local variables:
419  * indent-tabs-mode: nil
420  * c-file-style: gnu
421  * End:
422  */