2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-2000 University of Maryland at College Park
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.
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.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: scsi-solaris.c,v 1.25 2005/10/15 13:20:47 martinea Exp $
29 * Interface to execute SCSI commands on an Sun Workstation
31 * Copyright (c) Thomas Hepper th@ant.han.de
37 #ifdef HAVE_SOLARIS_LIKE_SCSI
45 #ifdef HAVE_SYS_TYPES_H
46 #include <sys/types.h>
48 #ifdef HAVE_SYS_STAT_H
55 #include <sys/scsi/impl/uscsi.h>
57 #include <scsi-defs.h>
60 void SCSI_OS_Version()
63 static char rcsid[] = "$Id: scsi-solaris.c,v 1.25 2005/10/15 13:20:47 martinea Exp $";
64 DebugPrint(DEBUG_INFO, SECTION_INFO, "scsi-os-layer: %s\n",rcsid);
68 int SCSI_OpenDevice(int ip)
72 extern OpenFiles_T *pDev;
74 if (pDev[ip].inqdone == 0)
77 if ((DeviceFD = open(pDev[ip].dev, O_RDWR| O_NONBLOCK)) >= 0)
80 pDev[ip].fd = DeviceFD;
83 pDev[ip].inquiry = (SCSIInquiry_T *)malloc(INQUIRY_SIZE);
85 if (SCSI_Inquiry(ip, pDev[ip].inquiry, INQUIRY_SIZE) == 0)
87 if (pDev[ip].inquiry->type == TYPE_TAPE || pDev[ip].inquiry->type == TYPE_CHANGER)
90 pDev[ip].ident[i] = pDev[ip].inquiry->prod_ident[i];
91 for (i=15; i >= 0 && !isalnum((int)pDev[ip].ident[i]) ; i--)
93 pDev[ip].ident[i] = '\0';
97 if (pDev[ip].inquiry->type == TYPE_TAPE)
99 pDev[ip].type = stralloc("tape");
102 if (pDev[ip].inquiry->type == TYPE_CHANGER)
104 pDev[ip].type = stralloc("changer");
107 PrintInquiry(pDev[ip].inquiry);
111 free(pDev[ip].inquiry);
115 free(pDev[ip].inquiry);
116 pDev[ip].inquiry = NULL;
121 dbprintf(("SCSI_OpenDevice %s failed\n", pDev[ip].dev));
125 if ((DeviceFD = open(pDev[ip].dev, O_RDWR| O_NDELAY)) >= 0)
127 pDev[ip].fd = DeviceFD;
128 pDev[ip].devopen = 1;
136 int SCSI_CloseDevice(int DeviceFD)
139 extern OpenFiles_T *pDev;
141 ret = close(pDev[DeviceFD].fd);
142 pDev[DeviceFD].devopen = 0;
147 int SCSI_ExecuteCommand(int DeviceFD,
148 Direction_T Direction,
152 int DataBufferLength,
154 int RequestSenseLength)
156 extern OpenFiles_T *pDev;
157 extern FILE * debug_file;
161 struct uscsi_cmd Command;
163 ExtendedRequestSense_T pExtendedRequestSense;
165 static int depth = 0;
167 if (pDev[DeviceFD].avail == 0)
175 SCSI_CloseDevice(DeviceFD);
178 memset(&Command, 0, sizeof(struct uscsi_cmd));
179 memset(pRequestSense, 0, RequestSenseLength);
183 if (DataBufferLength > 0)
184 memset(DataBuffer, 0, DataBufferLength);
186 /* Command.uscsi_flags = USCSI_READ | USCSI_RQENABLE; */
187 Command.uscsi_flags = USCSI_DIAGNOSE | USCSI_ISOLATE
188 | USCSI_READ | USCSI_RQENABLE;
191 /* Command.uscsi_flags = USCSI_WRITE | USCSI_RQENABLE; */
192 Command.uscsi_flags = USCSI_DIAGNOSE | USCSI_ISOLATE
193 | USCSI_WRITE | USCSI_RQENABLE;
196 /* Set timeout to 5 minutes. */
197 Command.uscsi_timeout = 300;
198 Command.uscsi_cdb = (caddr_t) CDB;
199 Command.uscsi_cdblen = CDB_Length;
201 if (DataBufferLength > 0)
203 Command.uscsi_bufaddr = DataBuffer;
204 Command.uscsi_buflen = DataBufferLength;
207 * If there is no data buffer force the direction to write, read with
208 * a null buffer will fail (errno 22)
210 Command.uscsi_flags = USCSI_DIAGNOSE | USCSI_ISOLATE
211 | USCSI_WRITE | USCSI_RQENABLE;
214 Command.uscsi_rqbuf = (caddr_t) pRequestSense;
215 Command.uscsi_rqlen = RequestSenseLength;
216 DecodeSCSI(CDB, "SCSI_ExecuteCommand : ");
219 if (pDev[DeviceFD].devopen == 0)
220 if (SCSI_OpenDevice(DeviceFD) == 0)
226 if ((ret = ioctl(pDev[DeviceFD].fd, USCSICMD, &Command)) >= 0)
228 ret = Command.uscsi_status;
231 dbprintf(("ioctl on %d failed, errno %d, ret %d\n",pDev[DeviceFD].fd, errno, ret));
233 RequestSense(DeviceFD, &pExtendedRequestSense, 0);
235 DecodeSense((RequestSense_T *)pRequestSense,
236 "SCSI_ExecuteCommand:", debug_file);
240 SCSI_CloseDevice(DeviceFD);
245 DebugPrint(DEBUG_INFO, SECTION_SCSI,"ioctl ret (%d)\n",ret);
252 * Send the command to the device with the
255 int Tape_Ioctl( int DeviceFD, int command)
257 extern OpenFiles_T *pDev;
261 if (pDev[DeviceFD].devopen == 0)
262 if (SCSI_OpenDevice(DeviceFD) == 0)
275 if (ioctl(pDev[DeviceFD].fd , MTIOCTOP, &mtop) != 0)
277 dbprintf(("Tape_Ioctl error ioctl %d\n",errno));
278 SCSI_CloseDevice(DeviceFD);
282 SCSI_CloseDevice(DeviceFD);
286 int Tape_Status( int DeviceFD)
288 extern OpenFiles_T *pDev;
292 if (pDev[DeviceFD].devopen == 0)
293 if (SCSI_OpenDevice(DeviceFD) == 0)
296 if (ioctl(pDev[DeviceFD].fd , MTIOCGET, &mtget) != 0)
298 dbprintf(("Tape_Status error ioctl %d\n",errno));
299 SCSI_CloseDevice(DeviceFD);
304 * I have no idea what is the meaning of the bits in mt_erreg
305 * I assume that nothing set is tape loaded
306 * 0x2 is no tape online
309 DebugPrint(DEBUG_INFO, SECTION_TAPE, "ioctl result for mt_dsreg (%d)\n", mtget.mt_dsreg);
310 DebugPrint(DEBUG_INFO, SECTION_TAPE, "ioctl result for mt_erreg (%d)\n", mtget.mt_erreg);
312 if (mtget.mt_erreg == 0)
314 ret = ret | TAPE_ONLINE;
317 if (mtget.mt_erreg & 0x2)
319 ret = ret | TAPE_NOT_LOADED;
322 SCSI_CloseDevice(DeviceFD);
327 int ScanBus(int print)
335 * indent-tabs-mode: nil