fix lintian issues
[debian/mtx] / scsi_freebsd.c
1 /* Copyright 2000 Enhanced Software Technologies Inc. (http://www.estinc.com)
2    Copyright 2007-2008 by Robert Nelson <robertn@the-nelsons.org>
3    Written by Eric Lee Green <eric@badtux.org>
4
5 $Date: 2008-08-19 03:03:38 -0700 (Tue, 19 Aug 2008) $
6 $Revision: 193 $
7
8   This program is free software; you may redistribute and/or modify it under
9   the terms of the GNU General Public License Version 2 as published by the
10   Free Software Foundation.
11
12   This program is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
14   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15   for complete details.
16
17 */
18
19 /* This is the SCSI commands for FreeBSD */
20 DEVICE_TYPE SCSI_OpenDevice(char *DeviceName)
21 {
22         struct cam_device *DeviceFD = cam_open_pass(DeviceName, O_RDWR | O_EXCL, NULL);
23         if (DeviceFD == 0)
24                 FatalError("cannot open SCSI device '%s' - %m\n", DeviceName);
25         return (DEVICE_TYPE) DeviceFD;
26 }
27
28
29 void SCSI_CloseDevice(char *DeviceName, DEVICE_TYPE DeviceFD)
30 {
31         cam_close_device((struct cam_device *) DeviceFD);
32 }
33
34 #define PASS_HZ 1000*60
35 #define PASS_DEFAULT_TIMEOUT 5*PASS_HZ
36 static int pass_timeout = PASS_DEFAULT_TIMEOUT;
37
38 void SCSI_Set_Timeout(int secs)
39 {
40         pass_timeout=secs*PASS_HZ;
41 }
42
43 void SCSI_Default_Timeout(void) {
44         pass_timeout=5*PASS_HZ;
45 }
46
47 int SCSI_ExecuteCommand(DEVICE_TYPE DeviceFD,
48                                                 Direction_T Direction,
49                                                 CDB_T *CDB,
50                                                 int CDB_Length,
51                                                 void *DataBuffer,
52                                                 int DataBufferLength,
53                                                 RequestSense_T *RequestSense)
54 {
55         struct cam_device *dsp = (struct cam_device *) DeviceFD;
56         int retval;
57         union ccb *ccb;
58         CDB_T *cdb;
59         int Result;
60
61         ccb = cam_getccb(dsp);
62         cdb = (CDB_T *) &ccb->csio.cdb_io.cdb_bytes; /* pointer to actual cdb. */
63
64         /* cam_getccb() zeros the CCB header only. So now clear the
65         * payload portion of the ccb.
66         */
67         bzero(&(&ccb->ccb_h)[1], sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
68
69         /* copy the CDB... */
70         memcpy(cdb,CDB,CDB_Length);
71
72         /* set the command control block stuff.... the rather involved
73         * conditional expression sets the direction to NONE if there is no
74         * data to go in or out, and IN or OUT if we want data. Movement
75         * commands will have no data buffer, just a CDB, while INQUIRY and
76         * READ_ELEMENT_STATUS will have input data, and we don't have any 
77         * stuff that outputs data -- yet -- but we may eventually. 
78         */
79         cam_fill_csio(  &ccb->csio,
80                                         1,                                                      /* retries */
81                                         NULL,                                           /* cbfcnp*/
82                                         (DataBufferLength ? 
83                                                 (Direction == Input ? CAM_DIR_IN : CAM_DIR_OUT) : 
84                                                 CAM_DIR_NONE),                  /* flags */
85                                         MSG_SIMPLE_Q_TAG,                       /* tag action */
86                                         DataBuffer,                                     /* data ptr */
87                                         DataBufferLength,                       /* xfer_len */
88                                         SSD_FULL_SIZE,                          /* sense_len */
89                                         CDB_Length,                                     /* cdb_len */
90                                         pass_timeout                            /* timeout */ /* should be 5 minutes or more?! */
91                                         ); 
92
93         pass_timeout = PASS_DEFAULT_TIMEOUT; /* make sure it gets reset. */
94         memset(RequestSense, 0, sizeof(RequestSense_T)); /* clear sense buffer... */
95
96         if (Direction == Input)
97         {
98                 memset(DataBuffer, 0, DataBufferLength);
99         }
100
101         Result = cam_send_ccb(DeviceFD,ccb);
102         if (Result < 0 || 
103                 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
104         {
105                 /* copy our sense data, sigh... */
106                 memcpy(RequestSense,(void *) &ccb->csio.sense_data,
107                 min(sizeof(RequestSense_T), sizeof(struct scsi_sense_data)));
108
109                 cam_freeccb(ccb);
110                 return -1; /* sorry!  */
111         }
112
113         /* okay, we did good, maybe? */
114         cam_freeccb(ccb);
115         return 0; /* and done? */
116 }