bd010667520414e007f6e1216eea7865ea2d3d26
[debian/amanda] / changer-src / scsi-bsd.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-bsd.c,v 1.17 2005/10/15 13:20:47 martinea Exp $
28  *
29  * Interface to execute SCSI commands on an BSD System (FreeBSD)
30  *
31  * Copyright (c) Thomes Hepper th@ant.han.de
32  */
33
34
35 #include <amanda.h>
36
37 #ifdef HAVE_BSD_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/scsiio.h>
57 #include <sys/mtio.h>
58
59 #include <scsi-defs.h>
60
61 void SCSI_OS_Version()
62 {
63 #ifndef lint
64    static char rcsid[] = "$Id: scsi-bsd.c,v 1.17 2005/10/15 13:20:47 martinea Exp $";
65    DebugPrint(DEBUG_INFO, SECTION_INFO, "scsi-os-layer: %s\n",rcsid);
66 #endif
67 }
68
69
70 /*
71  * Check if the device is already open,
72  * if no open it and save it in the list 
73  * of open files.
74  * 
75  * Return:
76  * 0  -> device not opened
77  * 1  -> sucess , device open
78  */
79 int SCSI_OpenDevice(int ip)
80 {
81   extern OpenFiles_T *pDev;
82   int i;
83   char * DeviceName;
84
85
86   /*
87    * If the SCSI inquiry was not done lets try to get
88    * some infos about the device
89    */
90   if (pDev[ip].inqdone == 0) {
91     pDev[ip].inqdone = 1;                                                     /* Set it to 1, so the inq is done */
92     pDev[ip].SCSI = 0;                                                        /* This will only be set if the inquiry works */
93     pDev[ip].inquiry = (SCSIInquiry_T *)malloc(INQUIRY_SIZE);
94     
95     if (( pDev[ip].fd = open(pDev[ip].dev, O_RDWR)) >= 0)                      /* We need the device in read/write mode */
96       {
97         pDev[ip].devopen = 1;                                                 /* The device is open for use */
98         pDev[ip].avail = 1;                                                   /* And it is available, it could be opened */
99         if (SCSI_Inquiry(ip, pDev[ip].inquiry, INQUIRY_SIZE) == 0)            /* Lets try to get the result of an SCSI inquiry */
100           {
101             if (pDev[ip].inquiry->type == TYPE_TAPE || pDev[ip].inquiry->type == TYPE_CHANGER)  /* If it worked and we got an type of */
102                                                                                                 /* either tape or changer continue */
103               {
104                 for (i=0;i < 16 ;i++)                                         /* Copy the product ident to the pDev struct */
105                   pDev[ip].ident[i] = pDev[ip].inquiry->prod_ident[i];
106                 for (i=15; i >= 0 && !isalnum(pDev[ip].ident[i]); i--)        /* And terminate it with an \0, remove all white space */
107                   {
108                     pDev[ip].ident[i] = '\0';
109                   }
110                 pDev[ip].SCSI = 1;                                            /* OK, its an SCSI device ... */
111
112                   if (pDev[ip].inquiry->type == TYPE_TAPE)
113                   {
114                           pDev[ip].type = stralloc("tape");
115                   }
116
117                   if (pDev[ip].inquiry->type == TYPE_CHANGER)
118                   {
119                           pDev[ip].type = stralloc("changer");
120                   }
121
122                 PrintInquiry(pDev[ip].inquiry);                               /* Some debug output */
123                 return(1);                                                    /* All done */
124               } else {
125                 free(pDev[ip].inquiry);                                       /* The SCSI was ok, but not an TAPE/CHANGER device ... */
126                 pDev[ip].devopen = 0;
127                 pDev[ip].avail = 0;
128                 close(pDev[ip].fd); 
129                 return(0);                                                    /*Might be an ChgExit is better */
130               }
131           } else { /* if SCSI_Inquiry */                                      /* The inquiry failed */
132             free(pDev[ip].inquiry);                                           /* free the allocated memory */
133             pDev[ip].inquiry = NULL;
134             return(1);                                                        /* Its not an SCSI device, but can be used for read/write */
135           }
136       }  /* open() */
137     return(0);                                                                /* Open failed .... */
138   } else { /* pDev[ip].inqdone */                                             /* OK this is the way we go if the device */
139     if (( pDev[ip].fd = open(pDev[ip].dev, O_RDWR)) >= 0)                      /* was opened successfull before */
140       {
141         pDev[ip].devopen = 1;
142         return(1);
143       } 
144   }
145   return(0);                                                                 /* Default, return device not available */
146 }
147
148 /*
149  * Close the device 
150  * abd set the flags in the device struct 
151  *
152  */
153 int SCSI_CloseDevice(int DeviceFD)
154 {
155   int ret;
156   extern OpenFiles_T *pDev;
157   
158   ret = close(pDev[DeviceFD].fd) ;
159   pDev[DeviceFD].devopen = 0;
160   return(ret);
161 }
162
163 int SCSI_ExecuteCommand(int DeviceFD,
164                         Direction_T Direction,
165                         CDB_T CDB,
166                         int CDB_Length,
167                         void *DataBuffer,
168                         int DataBufferLength,
169                         char *pRequestSense,
170                         int RequestSenseLength)
171 {
172   extern OpenFiles_T *pDev;
173   ExtendedRequestSense_T ExtendedRequestSense;
174   scsireq_t ds;
175   int Zero = 0, Result;
176   int retries = 5;
177   extern int errno;
178   
179   if (pDev[DeviceFD].avail == 0)
180     {
181       return(SCSI_ERROR);
182     }
183
184   memset(&ds, 0, sizeof(scsireq_t));
185   memset(pRequestSense, 0, RequestSenseLength);
186   memset(&ExtendedRequestSense, 0 , sizeof(ExtendedRequestSense_T)); 
187   
188   ds.flags = SCCMD_ESCAPE; 
189   /* Timeout */
190   ds.timeout = 120000;
191   /* Set the cmd */
192   memcpy(ds.cmd, CDB, CDB_Length);
193   ds.cmdlen = CDB_Length;
194   /* Data buffer for results */
195   if (DataBufferLength > 0)
196     {
197       ds.databuf = (caddr_t)DataBuffer;
198       ds.datalen = DataBufferLength;
199     }
200   /* Sense Buffer */
201   /*
202     ds.sense = (u_char)pRequestSense;
203   */
204   ds.senselen = RequestSenseLength;
205     
206   switch (Direction) 
207     {
208     case Input:
209       ds.flags = ds.flags | SCCMD_READ;
210       break;
211     case Output:
212       ds.flags = ds.flags | SCCMD_WRITE;
213       break;
214     }
215     
216   while (--retries > 0) {
217     
218     if (pDev[DeviceFD].devopen == 0)
219         if (SCSI_OpenDevice(DeviceFD) == 0)
220             return(SCSI_ERROR);
221     Result = ioctl(pDev[DeviceFD].fd, SCIOCCOMMAND, &ds);
222     SCSI_CloseDevice(DeviceFD);
223    
224     memcpy(pRequestSense, ds.sense, RequestSenseLength);
225     if (Result < 0)
226       {
227         dbprintf(("errno : %d\n",errno));
228         return (SCSI_ERROR);
229       }
230     dbprintf(("SCSI_ExecuteCommand(BSD) %02X STATUS(%02X) \n", CDB[0], ds.retsts));
231     switch (ds.retsts)
232       {
233       case SCCMD_BUSY:                /*  BUSY */
234         break;
235       case SCCMD_OK:                /*  GOOD */
236         return(SCSI_OK);
237         break;
238       case SCCMD_SENSE:               /*  CHECK CONDITION */
239         return(SCSI_SENSE);
240         break;
241       default:
242         continue;
243       }
244   }   
245   return(SCSI_SENSE);
246 }
247
248 /*
249  * Send the command to the device with the
250  * ioctl interface
251  */
252 int Tape_Ioctl( int DeviceFD, int command)
253 {
254   extern OpenFiles_T *pDev;
255   struct mtop mtop;
256   int ret = 0;
257
258   if (pDev[DeviceFD].devopen == 0)
259       if (SCSI_OpenDevice(DeviceFD) == 0)
260           return(-1);
261
262   switch (command)
263     {
264     case IOCTL_EJECT:
265       mtop.mt_op = MTOFFL;
266       mtop.mt_count = 1;
267       break;
268     default:
269       break;
270     }
271
272   if (ioctl(pDev[DeviceFD].fd , MTIOCTOP, &mtop) != 0)
273     {
274       dbprintf(("Tape_Ioctl error ioctl %d\n",errno));
275       SCSI_CloseDevice(DeviceFD);
276       return(-1);
277     }
278
279   SCSI_CloseDevice(DeviceFD);
280   return(ret);  
281 }
282
283 int Tape_Status( int DeviceFD)
284 {
285 /* 
286   Not yet
287 */
288   return(-1);
289 }
290
291 int ScanBus(int print)
292 {
293 /*
294   Not yet
295 */
296   return(-1);
297 }
298
299 #endif
300 /*
301  * Local variables:
302  * indent-tabs-mode: nil
303  * c-file-style: gnu
304  * End:
305  */