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-cam.c,v 1.15 2006/05/25 01:47:07 johnfranks Exp $
29 * Interface to execute SCSI commands on an system with cam support
30 * Current support is for FreeBSD 4.x
32 * Copyright (c) Thomes Hepper th@ant.han.de
38 #ifdef HAVE_CAM_LIKE_SCSI
47 #ifdef HAVE_SYS_TYPES_H
48 #include <sys/types.h>
50 #ifdef HAVE_SYS_STAT_H
61 #include <cam/scsi/scsi_message.h>
63 #ifdef HAVE_SYS_MTIO_H
67 #include <scsi-defs.h>
69 extern OpenFiles_T *pChangerDev;
70 extern OpenFiles_T *pTapeDev;
71 extern OpenFiles_T *pTapeDevCtl;
72 extern FILE *debug_file;
75 void SCSI_OS_Version()
78 static char rcsid[] = "$Id: scsi-cam.c,v 1.15 2006/05/25 01:47:07 johnfranks Exp $";
79 DebugPrint(DEBUG_INFO, SECTION_INFO, "scsi-os-layer: %s\n",rcsid);
83 /* parse string of format 1:2:3 and fill in path, target, lun
84 returns 0 if it doesn't look like btl
85 returns 1 if parse successful
86 calls ChgExit if it looks like btl but formatted improperly
89 int parse_btl(char *DeviceName,
90 path_id_t *path, target_id_t *target, lun_id_t *lun)
93 if (strstr(DeviceName, ":") == NULL)
96 p = strtok(DeviceName, ":");
97 if (sscanf(p,"%d", path) != 1) {
99 ChgExit("SCSI_OpenDevice",
100 "Path conversion error. Digits expected", FATAL);
103 if ((p = strtok(NULL,":")) == NULL) {
105 ChgExit("SCSI_OpenDevice", "target in Device Name not found", FATAL);
108 if (sscanf(p,"%d", target) != 1) {
110 ChgExit("SCSI_OpenDevice",
111 "Target conversion error. Digits expected", FATAL);
114 if ((p = strtok(NULL,":")) == NULL) {
116 ChgExit("SCSI_OpenDevice", "lun in Device Name not found", FATAL);
118 if (sscanf(p,"%d", lun) != 1) {
120 ChgExit("SCSI_OpenDevice",
121 "LUN conversion error. Digits expected", FATAL);
128 * Check if the device is already open,
129 * if no open it and save it in the list
131 * DeviceName can be an device name, /dev/nrsa0 for example
132 * or an bus:target:lun path, 0:4:0 for bus 0 target 4 lun 0
135 int SCSI_OpenDevice(int ip)
137 extern OpenFiles_T *pDev;
145 DeviceName = stralloc(pDev[ip].dev);
147 if (pDev[ip].inqdone == 0) {
148 pDev[ip].inqdone = 1;
150 pDev[ip].inquiry = (SCSIInquiry_T *)malloc(INQUIRY_SIZE);
151 if (parse_btl(DeviceName, &path, &target, &lun))
152 pDev[ip].curdev = cam_open_btl(path, target, lun, O_RDWR, NULL);
154 pDev[ip].curdev = cam_open_device(DeviceName, O_RDWR);
157 if (pDev[ip].curdev) {
160 pDev[ip].devopen = 1;
161 if (SCSI_Inquiry(ip, pDev[ip].inquiry, INQUIRY_SIZE) == 0) {
162 if (pDev[ip].inquiry->type == TYPE_TAPE || pDev[ip].inquiry->type == TYPE_CHANGER) {
164 pDev[ip].ident[i] = pDev[ip].inquiry->prod_ident[i];
166 for (i=15; i >= 0 && !isalnum(pDev[ip].ident[i]); i--) {
167 pDev[ip].ident[i] = '\0';
171 if (pDev[ip].inquiry->type == TYPE_TAPE)
173 pDev[ip].type = stralloc("tape");
176 if (pDev[ip].inquiry->type == TYPE_CHANGER)
178 pDev[ip].type = stralloc("changer");
181 PrintInquiry(pDev[ip].inquiry);
184 free(pDev[ip].inquiry);
189 free(pDev[ip].inquiry);
190 pDev[ip].inquiry = NULL;
193 } else { /* Device open failed */
194 DebugPrint(DEBUG_INFO, SECTION_SCSI,"##### STOP SCSI_OpenDevice open failed\n");
198 if (parse_btl(DeviceName, &path, &target, &lun))
199 pDev[ip].curdev = cam_open_btl(path, target, lun, O_RDWR, NULL);
201 pDev[ip].curdev = cam_open_device(DeviceName, O_RDWR);
205 if (pDev[ip].curdev) {
206 pDev[ip].devopen = 1;
213 int SCSI_CloseDevice(int DeviceFD)
216 extern OpenFiles_T *pDev;
218 if (pDev[DeviceFD].SCSI == 1)
220 cam_close_device(pDev[DeviceFD].curdev);
221 pDev[DeviceFD].devopen = 0;
223 close(pDev[DeviceFD].fd);
228 int SCSI_ExecuteCommand(int DeviceFD,
229 Direction_T Direction,
233 size_t DataBufferLength,
234 RequestSense_T *pRequestSense,
235 size_t RequestSenseLength)
237 ExtendedRequestSense_T ExtendedRequestSense;
238 extern OpenFiles_T *pDev;
242 OpenFiles_T *pwork = NULL;
244 /* Basic sanity checks */
245 assert(CDB_Length <= UCHAR_MAX);
246 assert(RequestSenseLength <= UCHAR_MAX);
248 /* Clear buffer for cases where sense is not returned */
249 memset(pRequestSense, 0, RequestSenseLength);
251 if (pDev[DeviceFD].avail == 0)
257 * CLear the SENSE buffer
259 bzero(pRequestSense, RequestSenseLength);
261 DecodeSCSI(CDB, "SCSI_ExecuteCommand : ");
263 ccb = cam_getccb(pDev[DeviceFD].curdev);
266 bzero(&(&ccb->ccb_h)[1], SIZEOF(struct ccb_scsiio));
267 bcopy(&CDB[0], &ccb->csio.cdb_io.cdb_bytes, CDB_Length);
272 if (DataBufferLength == 0)
274 ccb_flags = CAM_DIR_NONE;
276 ccb_flags = CAM_DIR_IN;
280 if (DataBufferLength == 0)
282 ccb_flags = CAM_DIR_NONE;
284 ccb_flags = CAM_DIR_OUT;
288 ccb_flags = CAM_DIR_NONE;
292 cam_fill_csio(&ccb->csio,
295 /* flags */ ccb_flags,
296 /* tag_action */ MSG_SIMPLE_Q_TAG,
297 /* data_ptr */ (u_int8_t*)DataBuffer,
298 /* dxfer_len */ DataBufferLength,
299 /* sense_len */ SSD_FULL_SIZE,
300 /* cdb_len */ CDB_Length,
301 /* timeout */ 600 * 1000);
304 if (pDev[DeviceFD].devopen == 0)
306 if (SCSI_OpenDevice(DeviceFD) == 0)
313 ret = cam_send_ccb(pDev[DeviceFD].curdev, ccb);
314 SCSI_CloseDevice(DeviceFD);
323 * copy the SENSE data to the Sense Buffer !!
325 memcpy(pRequestSense, &ccb->csio.sense_data, RequestSenseLength);
327 /* ToDo add error handling */
328 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
330 dbprintf(("SCSI_ExecuteCommand return %d\n", (ccb->ccb_h.status & CAM_STATUS_MASK)));
339 * Send the command to the device with the
342 int Tape_Ioctl( int DeviceFD, int command)
344 extern OpenFiles_T *pDev;
348 if (pDev[DeviceFD].devopen == 0)
349 if (SCSI_OpenDevice(DeviceFD) == 0)
362 if (ioctl(pDev[DeviceFD].fd , MTIOCTOP, &mtop) != 0)
364 dbprintf(("Tape_Ioctl error ioctl %s\n", strerror(errno)));
365 SCSI_CloseDevice(DeviceFD);
369 SCSI_CloseDevice(DeviceFD);
373 int Tape_Status( int DeviceFD)
375 extern OpenFiles_T *pDev;
379 if (pDev[DeviceFD].devopen == 0)
380 if (SCSI_OpenDevice(DeviceFD) == 0)
383 if (ioctl(pDev[DeviceFD].fd , MTIOCGET, &mtget) != 0)
385 dbprintf(("Tape_Status error ioctl %s\n", strerror(errno)));
386 SCSI_CloseDevice(DeviceFD);
390 dbprintf(("ioctl -> mtget.mt_dsreg %lX\n",mtget.mt_dsreg));
391 dbprintf(("ioctl -> mtget.mt_erreg %lX\n",mtget.mt_erreg));
394 * I have no idea what is the meaning of the bits in mt_erreg
395 * I assume that nothing set is tape loaded
396 * 0x2 is no tape online
398 if (mtget.mt_erreg == 0)
400 ret = ret | TAPE_ONLINE;
403 if (mtget.mt_erreg & 0x2)
405 ret = ret | TAPE_NOT_LOADED;
408 SCSI_CloseDevice(DeviceFD);
413 * Scans the bus for device with the type 'tape' or 'robot'
416 int ScanBus(int print)
420 extern OpenFiles_T *pDev;
422 for (bus = 0;bus < 3; bus++)
424 pDev[count].dev = malloc(10);
425 for (target = 0;target < 8; target++)
427 for (lun = 0; lun < 8; lun++)
429 sprintf(pDev[count].dev, "%d:%d:%d", bus, target, lun);
430 pDev[count].inqdone = 0;
431 if (OpenDevice(count, pDev[count].dev, "Scan", NULL))
433 if (pDev[count].inquiry->type == TYPE_TAPE ||
434 pDev[count].inquiry->type == TYPE_CHANGER)
437 pDev[count].dev = malloc(10);
441 printf("bus:target:lun -> %s == ",pDev[count].dev);
443 switch (pDev[count].inquiry->type)
476 printf("unknown %d",pDev[count].inquiry->type);
492 * indent-tabs-mode: nil