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.26 2006/05/25 01:47:10 johnfranks Exp $
29 * Interface to execute SCSI commands on an Sun Workstation
31 * Copyright (c) Thomas Hepper th@ant.han.de
37 #include <sys/scsi/impl/uscsi.h>
39 #include <scsi-defs.h>
42 void SCSI_OS_Version(void)
45 static char rcsid[] = "$Id: scsi-solaris.c,v 1.26 2006/05/25 01:47:10 johnfranks Exp $";
46 DebugPrint(DEBUG_INFO, SECTION_INFO, "scsi-os-layer: %s\n",rcsid);
50 int SCSI_OpenDevice(int ip)
54 extern OpenFiles_T *pDev;
56 if (pDev[ip].inqdone == 0)
59 if ((DeviceFD = open(pDev[ip].dev, O_RDWR| O_NONBLOCK)) >= 0)
62 pDev[ip].fd = DeviceFD;
65 pDev[ip].inquiry = (SCSIInquiry_T *)malloc(INQUIRY_SIZE);
67 if (SCSI_Inquiry(ip, pDev[ip].inquiry, (unsigned char)INQUIRY_SIZE) == 0)
69 if (pDev[ip].inquiry->type == TYPE_TAPE || pDev[ip].inquiry->type == TYPE_CHANGER)
72 pDev[ip].ident[i] = pDev[ip].inquiry->prod_ident[i];
73 for (i=15; i >= 0 && !isalnum((int)pDev[ip].ident[i]) ; i--)
75 pDev[ip].ident[i] = '\0';
79 if (pDev[ip].inquiry->type == TYPE_TAPE)
81 pDev[ip].type = stralloc("tape");
84 if (pDev[ip].inquiry->type == TYPE_CHANGER)
86 pDev[ip].type = stralloc("changer");
89 PrintInquiry(pDev[ip].inquiry);
93 free(pDev[ip].inquiry);
97 free(pDev[ip].inquiry);
98 pDev[ip].inquiry = NULL;
101 dbprintf(_("SCSI_OpenDevice %s failed\n"), pDev[ip].dev);
105 if ((DeviceFD = open(pDev[ip].dev, O_RDWR| O_NDELAY)) >= 0)
107 pDev[ip].fd = DeviceFD;
108 pDev[ip].devopen = 1;
116 int SCSI_CloseDevice(int DeviceFD)
119 extern OpenFiles_T *pDev;
121 ret = close(pDev[DeviceFD].fd);
122 pDev[DeviceFD].devopen = 0;
127 int SCSI_ExecuteCommand(int DeviceFD,
128 Direction_T Direction,
132 size_t DataBufferLength,
133 RequestSense_T *RequestSense,
134 size_t RequestSenseLength)
136 extern OpenFiles_T *pDev;
137 extern FILE * debug_file;
140 struct uscsi_cmd Command;
141 static int depth = 0;
143 /* Basic sanity checks */
144 assert(CDB_Length <= UCHAR_MAX);
145 assert(RequestSenseLength <= UCHAR_MAX);
147 /* Clear buffer for cases where sense is not returned */
148 memset(RequestSense, 0, RequestSenseLength);
150 if (pDev[DeviceFD].avail == 0)
158 SCSI_CloseDevice(DeviceFD);
161 memset(&Command, 0, SIZEOF(struct uscsi_cmd));
162 memset(RequestSense, 0, RequestSenseLength);
166 if (DataBufferLength > 0)
167 memset(DataBuffer, 0, DataBufferLength);
169 /* Command.uscsi_flags = USCSI_READ | USCSI_RQENABLE; */
170 Command.uscsi_flags = USCSI_DIAGNOSE | USCSI_ISOLATE
171 | USCSI_READ | USCSI_RQENABLE;
174 /* Command.uscsi_flags = USCSI_WRITE | USCSI_RQENABLE; */
175 Command.uscsi_flags = USCSI_DIAGNOSE | USCSI_ISOLATE
176 | USCSI_WRITE | USCSI_RQENABLE;
179 /* Set timeout to 5 minutes. */
180 Command.uscsi_timeout = 300;
181 Command.uscsi_cdb = (caddr_t) CDB;
182 Command.uscsi_cdblen = (u_char)CDB_Length;
184 if (DataBufferLength > 0)
186 Command.uscsi_bufaddr = DataBuffer;
187 Command.uscsi_buflen = DataBufferLength;
190 * If there is no data buffer force the direction to write, read with
191 * a null buffer will fail (errno 22)
193 Command.uscsi_flags = USCSI_DIAGNOSE | USCSI_ISOLATE
194 | USCSI_WRITE | USCSI_RQENABLE;
197 Command.uscsi_rqbuf = (caddr_t)RequestSense;
198 Command.uscsi_rqlen = (u_char)RequestSenseLength;
199 DecodeSCSI(CDB, "SCSI_ExecuteCommand : ");
202 if (pDev[DeviceFD].devopen == 0)
203 if (SCSI_OpenDevice(DeviceFD) == 0)
209 if ((ret = ioctl(pDev[DeviceFD].fd, USCSICMD, &Command)) >= 0)
211 ret = Command.uscsi_status;
214 dbprintf(_("ioctl on %d failed, errno %s, ret %d\n"),
215 pDev[DeviceFD].fd, strerror(errno), ret);
217 RequestSense(DeviceFD, &pExtendedRequestSense, 0);
219 DecodeSense(RequestSense, "SCSI_ExecuteCommand:", debug_file);
223 SCSI_CloseDevice(DeviceFD);
225 DebugPrint(DEBUG_INFO, SECTION_SCSI,_("ioctl ret (%d)\n"),ret);
230 * Send the command to the device with the
233 int Tape_Ioctl( int DeviceFD, int command)
235 extern OpenFiles_T *pDev;
239 if (pDev[DeviceFD].devopen == 0)
240 if (SCSI_OpenDevice(DeviceFD) == 0)
253 if (ioctl(pDev[DeviceFD].fd , MTIOCTOP, &mtop) != 0)
255 dbprintf(_("Tape_Ioctl error ioctl %s\n"), strerror(errno));
256 SCSI_CloseDevice(DeviceFD);
260 SCSI_CloseDevice(DeviceFD);
264 int Tape_Status( int DeviceFD)
266 extern OpenFiles_T *pDev;
270 memset(&mtget, 0, SIZEOF(mtget));
271 if (pDev[DeviceFD].devopen == 0)
272 if (SCSI_OpenDevice(DeviceFD) == 0)
275 if (ioctl(pDev[DeviceFD].fd , MTIOCGET, &mtget) != 0)
277 dbprintf(_("Tape_Status error ioctl %s\n"), strerror(errno));
278 SCSI_CloseDevice(DeviceFD);
283 * I have no idea what is the meaning of the bits in mt_erreg
284 * I assume that nothing set is tape loaded
285 * 0x2 is no tape online
288 DebugPrint(DEBUG_INFO, SECTION_TAPE, _("ioctl result for mt_dsreg (%d)\n"), mtget.mt_dsreg);
289 DebugPrint(DEBUG_INFO, SECTION_TAPE, _("ioctl result for mt_erreg (%d)\n"), mtget.mt_erreg);
291 if (mtget.mt_erreg == 0)
293 ret = ret | TAPE_ONLINE;
296 if (mtget.mt_erreg & 0x2)
298 ret = ret | TAPE_NOT_LOADED;
301 SCSI_CloseDevice(DeviceFD);
306 int ScanBus(int print)
308 (void)print; /* Quiet unused parameter warning */
314 * indent-tabs-mode: nil