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