1e635c9e46811a7053969a896b19c0a4e45ad6a9
[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.1.2.13.4.1.2.5 2003/01/26 19:20:57 martinea 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.1.2.13.4.1.2.5 2003/01/26 19:20:57 martinea 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             } else { /* inquiry failed */
120               close(DeviceFD);
121               free(pDev[ip].inquiry);
122               pDev[ip].inquiry = NULL;
123               pDev[ip].avail = 0;
124               return(0);
125             }
126
127           /*
128            * Open ok, but no SCSI communication available 
129            */
130
131           free(pDev[ip].inquiry);
132           pDev[ip].inquiry = NULL;
133           close(DeviceFD);
134           pDev[ip].avail = 0;
135           return(0);
136         }
137     } else {
138       if ((DeviceFD = open(pDev[ip].dev, O_RDWR | O_EXCL)) > 0)
139         {
140           pDev[ip].fd = DeviceFD;
141           pDev[ip].devopen = 1;
142           return(1);
143         } else {
144           pDev[ip].devopen = 0;
145           return(0);
146         }
147     }
148   return(0); 
149 }
150
151 int SCSI_CloseDevice(int DeviceFD)
152 {
153   extern OpenFiles_T *pDev;
154   int ret = 0;
155   
156   if (pDev[DeviceFD].devopen == 1)
157     {
158       pDev[DeviceFD].devopen = 0;
159       ret = close(pDev[DeviceFD].fd);
160     }
161
162   return(ret);
163 }
164
165 int SCSI_ExecuteCommand(int DeviceFD,
166                         Direction_T Direction,
167                         CDB_T CDB,
168                         int CDB_Length,
169                         void *DataBuffer,
170                         int DataBufferLength,
171                         char *pRequestSense,
172                         int RequestSenseLength)
173 {
174   extern OpenFiles_T *pDev;
175   ExtendedRequestSense_T ExtendedRequestSense;
176   struct dsreq ds;
177   int Result;
178   int retries = 5;
179   
180   if (pDev[DeviceFD].avail == 0)
181     {
182       return(SCSI_ERROR);
183     }
184   
185   memset(&ds, 0, sizeof(struct dsreq));
186   memset(pRequestSense, 0, RequestSenseLength);
187   memset(&ExtendedRequestSense, 0 , sizeof(ExtendedRequestSense_T)); 
188   
189   ds.ds_flags = DSRQ_SENSE|DSRQ_TRACE|DSRQ_PRINT; 
190   /* Timeout */
191   ds.ds_time = 120000;
192   /* Set the cmd */
193   ds.ds_cmdbuf = (caddr_t)CDB;
194   ds.ds_cmdlen = CDB_Length;
195   /* Data buffer for results */
196   ds.ds_databuf = (caddr_t)DataBuffer;
197   ds.ds_datalen = DataBufferLength;
198   /* Sense Buffer */
199   ds.ds_sensebuf = (caddr_t)pRequestSense;
200   ds.ds_senselen = RequestSenseLength;
201   
202   switch (Direction) 
203     {
204     case Input:
205       ds.ds_flags = ds.ds_flags | DSRQ_READ;
206       break;
207     case Output:
208       ds.ds_flags = ds.ds_flags | DSRQ_WRITE;
209       break;
210     }
211   
212   while (--retries > 0) {
213     if (pDev[DeviceFD].devopen == 0)
214       {
215         SCSI_OpenDevice(DeviceFD);
216       }
217     Result = ioctl(pDev[DeviceFD].fd, DS_ENTER, &ds);
218     SCSI_CloseDevice(DeviceFD);
219
220     if (Result < 0)
221       {
222         RET(&ds) = DSRT_DEVSCSI;
223         SCSI_CloseDevice(DeviceFD);
224         return (SCSI_ERROR);
225       }
226     DecodeSCSI(CDB, "SCSI_ExecuteCommand : ");
227     dbprintf(("\t\t\tSTATUS(%02X) RET(%02X)\n", STATUS(&ds), RET(&ds)));
228     switch (STATUS(&ds))
229       {
230       case ST_BUSY:                /*  BUSY */
231         break;
232       case STA_RESERV:             /*  RESERV CONFLICT */
233         if (retries > 0)
234           sleep(2);
235         continue;
236       case ST_GOOD:                /*  GOOD 0x00 */
237         switch (RET(&ds))
238           {
239           case DSRT_SENSE:
240             return(SCSI_SENSE);
241             break;
242           case DSRT_SHORT:
243             return(SCSI_OK);
244             break;
245           case DSRT_OK:
246           default:
247             return(SCSI_OK);
248           }
249       case ST_CHECK:               /*  CHECK CONDITION 0x02 */ 
250         switch (RET(&ds))
251           {
252           case DSRT_SENSE:
253             return(SCSI_SENSE);
254             break;
255           default:
256             return(SCSI_CHECK);
257             break;
258           }
259         return(SCSI_CHECK);
260         break;
261       case ST_COND_MET:            /*  INTERM/GOOD 0x10 */
262       default:
263         continue;
264       }
265   }     
266   return(SCSI_ERROR);
267 }
268
269 int Tape_Ioctl ( int DeviceFD, int command)
270 {
271   extern OpenFiles_T *pDev;
272   struct mtop mtop;
273   
274   if (pDev[DeviceFD].devopen == 0)
275     {
276       SCSI_OpenDevice(DeviceFD);
277     }
278   
279   switch (command)
280     {
281     case IOCTL_EJECT:
282       mtop.mt_op = MTUNLOAD;
283       mtop.mt_count = 1;
284       break;
285     default:
286       break;
287     }
288   
289   ioctl(pDev[DeviceFD].fd, MTIOCTOP, &mtop);
290   SCSI_CloseDevice(DeviceFD);
291   return(0);
292 }
293
294 int Tape_Status( int DeviceFD)
295 {
296   extern OpenFiles_T *pDev;
297   struct mtget mtget;
298   int ret = 0;
299
300   if (pDev[DeviceFD].devopen == 0)
301     {
302       SCSI_OpenDevice(DeviceFD);
303     }
304
305   if (ioctl(pDev[DeviceFD].fd , MTIOCGET, &mtget) != 0)
306     {
307       dbprintf(("Tape_Status error ioctl %d\n",errno));
308       SCSI_CloseDevice(DeviceFD);
309       return(-1);
310     }
311   
312   switch(mtget.mt_dposn)
313     {
314     case MT_EOT:
315       ret = ret | TAPE_EOT;
316       break;
317     case MT_BOT:
318       ret = ret | TAPE_BOT;
319       break;
320     case MT_WPROT:
321       ret = ret | TAPE_WR_PROT;
322       break;
323     case MT_ONL:
324       ret = TAPE_ONLINE;
325       break;
326     case MT_EOD:
327       break;
328     case MT_FMK:
329       break;
330     default:
331       break;
332     }
333
334   SCSI_CloseDevice(DeviceFD);
335   return(ret); 
336 }
337
338 int ScanBus(int print)
339 {
340   DIR *dir;
341   struct dirent *dirent;
342   extern OpenFiles_T *pDev;
343   extern int errno;
344   int count = 0;
345
346   dir = opendir("/dev/scsi");
347
348   while ((dirent = readdir(dir)) != NULL)
349     {
350       if (strstr(dirent->d_name, "sc") != NULL)
351       {
352         pDev[count].dev = malloc(10);
353         pDev[count].inqdone = 0;
354         sprintf(pDev[count].dev,"/dev/scsi/%s", dirent->d_name);
355         if (OpenDevice(count,pDev[count].dev, "Scan", NULL ))
356           {
357             SCSI_CloseDevice(count);
358             pDev[count].inqdone = 0;
359             
360             if (print)
361               {
362                 printf("name /dev/scsi/%s ", dirent->d_name);
363                 
364                 switch (pDev[count].inquiry->type)
365                   {
366                   case TYPE_DISK:
367                     printf("Disk");
368                     break;
369                   case TYPE_TAPE:
370                     printf("Tape");
371                     break;
372                   case TYPE_PRINTER:
373                     printf("Printer");
374                     break;
375                   case TYPE_PROCESSOR:
376                     printf("Processor");
377                     break;
378                   case TYPE_WORM:
379                     printf("Worm");
380                     break;
381                   case TYPE_CDROM:
382                     printf("Cdrom");
383                     break;
384                   case TYPE_SCANNER:
385                     printf("Scanner");
386                     break;
387                   case TYPE_OPTICAL:
388                     printf("Optical");
389                     break;
390                   case TYPE_CHANGER:
391                     printf("Changer");
392                     break;
393                   case TYPE_COMM:
394                     printf("Comm");
395                     break;
396                   default:
397                     printf("unknown %d",pDev[count].inquiry->type);
398                     break;
399                   }
400                 printf("\n");
401               }
402             count++;
403             printf("Count %d\n",count);
404           } else {
405             free(pDev[count].dev);
406             pDev[count].dev=NULL;
407           }
408       }
409     }
410   return 0;
411 }
412
413 #endif
414 /*
415  * Local variables:
416  * indent-tabs-mode: nil
417  * c-file-style: gnu
418  * End:
419  */