Imported Upstream version 3.1.0
[debian/amanda] / ndmp-src / ndml_scsi.c
1 /*
2  * Copyright (c) 1998,1999,2000
3  *      Traakan, Inc., Los Altos, CA
4  *      All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 /*
30  * Project:  NDMJOB
31  * Ident:    $Id: $
32  *
33  * Description:
34  *
35  */
36
37
38 #include "ndmlib.h"
39
40
41 /*
42  * NAME[,[CNUM,]SID[,LUN]
43  *
44  * The foregoing pattern is ambiguous. Here's the disambiguating rules:
45  * 1) If just a name is given, controller, sid, and lun are all
46  *    set to invalid (-1)
47  * 2) If one number comes after the device name (,sid) it is the
48  *    SID; controller is set to -1, lun is set to 0
49  * 3) If two numbers come after the device name (,sid,lun), they are
50  *    the SID and LUN; controller is set to -1
51  * 4) Three numbers after the device name are all three number fields.
52  */
53
54
55 int
56 ndmscsi_target_from_str (struct ndmscsi_target *targ, char *str)
57 {
58         char *          p;
59         int             n1, n2, n3;
60
61         NDMOS_MACRO_ZEROFILL (targ);
62
63         p = strchr (str, ',');
64         if (p) {
65                 *p++ = 0;
66         }
67
68         if (strlen (str) >= PATH_MAX) {
69                 if (p) p[-1] = ',';
70                 return -2;
71         }
72
73         strcpy (targ->dev_name, str);
74
75         if (!p) {
76                 targ->controller = -1;
77                 targ->sid = -1;
78                 targ->lun = -1;
79
80                 return 0;
81         }
82
83         p[-1] = ',';
84
85         if (*p < '0' || '9' < *p) {
86                 return -3;
87         }
88         n1 = strtol (p, &p, 0);
89         if (*p != 0 && *p != ',') {
90                 return -4;
91         }
92         if (*p == 0) {
93                 targ->controller = -1;
94                 targ->sid = n1;
95                 targ->lun = 0;
96
97                 return 0;
98         }
99
100         p++;
101
102         if (*p < '0' || '9' < *p) {
103                 return -5;
104         }
105         n2 = strtol (p, &p, 0);
106
107         if (*p == 0) {
108                 /* SID,LUN */
109                 targ->controller = -1;
110                 targ->sid = n1;
111                 targ->lun = n2;
112         } else {
113                 if (*p != ',') {
114                         return -6;
115                 }
116                 p++;
117
118                 if (*p < '0' || '9' < *p) {
119                         return -7;
120                 }
121                 n3 = strtol (p, &p, 0);
122
123                 if (*p != 0) {
124                         return -8;
125                 }
126
127                 targ->controller = n1;
128                 targ->sid = n2;
129                 targ->lun = n3;
130         }
131
132         return 0;
133 }
134
135
136 int
137 ndmscsi_open (struct ndmconn *conn, char *dev_name)
138 {
139         int             rc;
140
141         NDMC_WITH(ndmp9_scsi_open, NDMP9VER)
142                 request->device = dev_name;
143                 rc = NDMC_CALL(conn);
144         NDMC_ENDWITH
145
146         return rc;
147 }
148
149 int
150 ndmscsi_close (struct ndmconn *conn)
151 {
152         int             rc;
153
154         NDMC_WITH_VOID_REQUEST(ndmp9_scsi_close, NDMP9VER)
155                 rc = NDMC_CALL(conn);
156         NDMC_ENDWITH
157
158         return rc;
159 }
160
161 int
162 ndmscsi_get_state (struct ndmconn *conn, struct ndmscsi_target *targ)
163 {
164         int             rc;
165
166         NDMOS_MACRO_ZEROFILL (targ);
167
168         NDMC_WITH_VOID_REQUEST(ndmp9_scsi_get_state, NDMP9VER)
169                 rc = NDMC_CALL(conn);
170                 targ->controller = reply->target_controller;
171                 targ->sid = reply->target_id;
172                 targ->lun = reply->target_lun;
173         NDMC_ENDWITH
174
175         return rc;
176 }
177
178 int
179 ndmscsi_set_target (struct ndmconn *conn, struct ndmscsi_target *targ)
180 {
181         int             rc;
182
183         NDMC_WITH(ndmp9_scsi_set_target, NDMP9VER)
184                 request->device = targ->dev_name;
185                 request->target_controller = targ->controller;
186                 request->target_id = targ->sid;
187                 request->target_lun = targ->lun;
188                 rc = NDMC_CALL(conn);
189         NDMC_ENDWITH
190
191         return rc;
192 }
193
194 int
195 ndmscsi_use (struct ndmconn *conn, struct ndmscsi_target *targ)
196 {
197         int             rc;
198
199 #if 0
200         rc = ndmscsi_close (conn);
201         /* error ignored */
202 #endif
203
204         rc = ndmscsi_open (conn, targ->dev_name);
205         if (rc) return rc;
206
207         if (targ->controller != -1 || targ->sid != -1 || targ->lun != -1) {
208 #ifndef NDMOS_OPTION_NO_NDMP4
209                 if (conn->protocol_version == NDMP4VER) {
210                         return -1;      /* can't set target */
211                 }
212 #endif /* !NDMOS_OPTION_NO_NDMP4 */
213
214                 rc = ndmscsi_set_target (conn, targ);
215                 if (rc) {
216                         ndmscsi_close (conn);   /* best effort */
217                         return rc;
218                 }
219         }
220
221         return 0;
222 }
223
224 int
225 ndmscsi_execute (struct ndmconn *conn,
226   struct ndmscsi_request *req,
227   struct ndmscsi_target *targ)
228 {
229         int             rc;
230
231         if (targ) {
232                 rc = ndmscsi_use (conn, targ);
233                 if (rc) return rc;
234         }
235
236         NDMC_WITH(ndmp9_scsi_execute_cdb, NDMP9VER)
237                 request->cdb.cdb_len = req->n_cmd;
238                 request->cdb.cdb_val = (char*)req->cmd;
239
240                 switch (req->data_dir) {
241                 case NDMSCSI_DD_NONE:
242                         request->data_dir = NDMP9_SCSI_DATA_DIR_NONE;
243                         break;
244
245                 case NDMSCSI_DD_IN:
246                         request->data_dir = NDMP9_SCSI_DATA_DIR_IN;
247                         request->datain_len = req->n_data_avail;
248                         break;
249
250                 case NDMSCSI_DD_OUT:
251                         request->data_dir = NDMP9_SCSI_DATA_DIR_OUT;
252                         request->dataout.dataout_len = req->n_data_avail;
253                         request->dataout.dataout_val = (char*)req->data;
254                         break;
255                 }
256                 request->timeout = 300000;      /* five minutes */
257
258                 rc = NDMC_CALL (conn);
259                 if (rc) {
260                         req->completion_status = NDMSCSI_CS_FAIL;
261                         return rc;
262                 }
263
264                 req->status_byte = reply->status;
265                 req->n_data_done = 0;
266                 req->n_sense_data = 0;
267
268                 rc = reply->ext_sense.ext_sense_len;
269                 if (rc > 0) {
270                         if (rc > NDMSCSI_MAX_SENSE_DATA)
271                                 rc = NDMSCSI_MAX_SENSE_DATA;
272
273                         req->n_sense_data = rc;
274                         NDMOS_API_BCOPY (reply->ext_sense.ext_sense_val,
275                                         req->sense_data, rc);
276                 }
277
278                 switch (req->data_dir) {
279                 case NDMSCSI_DD_IN:
280                         req->n_data_done = reply->datain.datain_len;
281                         if (req->n_data_done > 0) {
282                                 NDMOS_API_BCOPY (reply->datain.datain_val,
283                                         req->data, req->n_data_done);
284                         }
285                         break;
286
287                 case NDMSCSI_DD_OUT:
288                         req->n_data_done = reply->dataout_len;
289                         break;
290                 }
291                 req->completion_status = NDMSCSI_CS_GOOD;
292
293                 NDMC_FREE_REPLY();
294         NDMC_ENDWITH
295
296         return 0;
297 }