/
ndmos_freebsd.c
326 lines (273 loc) · 7.75 KB
/
ndmos_freebsd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (c) 1998,2001
* Traakan, Inc., Los Altos, CA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Project: NDMJOB
* Ident: $Id: $
*
* Description:
* This contains the O/S (Operating System) specific
* portions of NDMJOBLIB for the FreeBSD platform.
*
* This file is #include'd by ndmos.c when
* selected by #ifdef's of NDMOS_ID.
*
* There are four major portions:
* 1) Misc support routines: password check, local info, etc
* 2) Non-blocking I/O support routines
* 3) Tape interfacs ndmos_tape_xxx()
* 4) OS Specific NDMP request dispatcher which intercepts
* requests implemented here, such as SCSI operations
* and system configuration queries.
*/
/*
* #include "ndmagents.h" already done in ndmos.c
* Additional #include's, not needed in ndmos_freebsd.h, yet needed here.
*/
#include <sys/utsname.h>
#include <camlib.h>
#include <cam/scsi/scsi_message.h>
/*
* Select common code fragments from ndmos_common.c
*/
#define NDMOS_COMMON_SYNC_CONFIG_INFO /* from config file (ndmjob.conf) */
#define NDMOS_COMMON_OK_NAME_PASSWORD
#define NDMOS_COMMON_MD5
#define NDMOS_COMMON_NONBLOCKING_IO_SUPPORT
#define NDMOS_COMMON_TAPE_INTERFACE /* uses tape simulator */
#define NDMOS_COMMON_SCSI_INTERFACE /* use scsi simulator */
#define NDMOS_COMMON_DISPATCH_REQUEST /* no-op */
#include "ndmos_common.c"
#ifdef NDMOS_COMMON_SCSI_INTERFACE
#ifndef NDMOS_OPTION_NO_ROBOT_AGENT /* Surrounds all SCSI intfs */
#ifndef NDMOS_OPTION_ROBOT_SIMULATOR
/*
* SCSI INTERFACE
****************************************************************
*/
int
ndmos_scsi_initialize (struct ndm_session *sess)
{
sess->robot_acb->camdev = 0;
return 0;
}
void
ndmos_scsi_sync_state (struct ndm_session *sess)
{
struct ndm_robot_agent * robot = sess->robot_acb;
struct cam_device * camdev = robot->camdev;
ndmp9_scsi_get_state_reply * state = &robot->scsi_state;
if (robot->camdev) {
state->error = NDMP9_NO_ERR;
state->target_controller = camdev->path_id;
state->target_id = camdev->target_id;
state->target_lun = camdev->target_lun;
} else {
state->error = NDMP9_DEV_NOT_OPEN_ERR;
state->target_controller = -1;
state->target_id = -1;
state->target_lun = -1;
}
}
ndmp9_error
ndmos_scsi_open (struct ndm_session *sess, char *name)
{
struct ndm_robot_agent * robot = sess->robot_acb;
struct cam_device * camdev = robot->camdev;
/* redundant test */
if (camdev) {
return NDMP9_DEVICE_OPENED_ERR;
}
camdev = cam_open_pass(name, 2, 0);
if (!camdev) {
/* could sniff errno */
switch (errno) {
default:
case ENOENT:
return NDMP9_NO_DEVICE_ERR;
case EACCES:
case EPERM:
return NDMP9_PERMISSION_ERR;
case EBUSY:
return NDMP9_DEVICE_BUSY_ERR;
}
}
robot->camdev = camdev;
ndmos_scsi_sync_state (sess);
return NDMP9_NO_ERR;
}
ndmp9_error
ndmos_scsi_close (struct ndm_session *sess)
{
struct ndm_robot_agent * robot = sess->robot_acb;
struct cam_device * camdev = robot->camdev;
/* redundant test */
if (!camdev) {
return NDMP9_DEV_NOT_OPEN_ERR;
}
robot->camdev = 0;
ndmos_scsi_sync_state (sess);
cam_close_device (camdev);
return NDMP9_NO_ERR;
}
/* deprecated */
ndmp9_error
ndmos_scsi_set_target (struct ndm_session *sess)
{
return NDMP9_NOT_SUPPORTED_ERR;
}
ndmp9_error
ndmos_scsi_reset_device (struct ndm_session *sess)
{
return NDMP9_NOT_SUPPORTED_ERR;
}
/* deprecated */
ndmp9_error
ndmos_scsi_reset_bus (struct ndm_session *sess)
{
return NDMP9_NOT_SUPPORTED_ERR;
}
ndmp9_error
ndmos_scsi_execute_cdb (struct ndm_session *sess,
ndmp9_execute_cdb_request *request,
ndmp9_execute_cdb_reply *reply)
{
struct ndm_robot_agent *robot = sess->robot_acb;
struct cam_device * camdev = robot->camdev;
union ccb * ccb;
u_int32_t flags;
u_int8_t * data_ptr = 0;
u_int8_t * data_in_ptr = 0;
u_int32_t data_len = 0;
u_int32_t data_done;
int rc;
NDMOS_MACRO_ZEROFILL (reply);
reply->error = NDMP9_IO_ERR; /* pessimistic */
ccb = cam_getccb(camdev);
if (!ccb) {
reply->error = NDMP9_NO_MEM_ERR;
return reply->error;
}
switch (request->data_dir) {
case NDMP9_SCSI_DATA_DIR_NONE:
flags = CAM_DIR_NONE;
break;
case NDMP9_SCSI_DATA_DIR_IN:
if (data_len > 1024*1024) {
reply->error = NDMP9_ILLEGAL_ARGS_ERR;
goto out;
}
data_len = request->datain_len;
data_in_ptr = (u_int8_t *)malloc (data_len);
if (!data_in_ptr) {
reply->error = NDMP9_NO_MEM_ERR;
goto out;
}
data_ptr = data_in_ptr;
flags = CAM_DIR_IN;
break;
case NDMP9_SCSI_DATA_DIR_OUT:
data_len = request->dataout.dataout_len;
data_ptr = (u_int8_t *)request->dataout.dataout_val;
flags = CAM_DIR_OUT;
break;
default:
return NDMP9_ILLEGAL_ARGS_ERR;
break;
}
bcopy(request->cdb.cdb_val, &ccb->csio.cdb_io.cdb_bytes,
request->cdb.cdb_len);
cam_fill_csio(&ccb->csio,
/*retries*/ 1,
/*cbfcnp*/ NULL,
/*flags*/ flags,
/*tag_action*/ MSG_SIMPLE_Q_TAG,
/*data_ptr*/ data_ptr,
/*dxfer_len*/ data_len,
/*sense_len*/ SSD_FULL_SIZE,
/*cdb_len*/ request->cdb.cdb_len,
/*timeout*/ request->timeout);
rc = cam_send_ccb (camdev, ccb);
if (rc != 0) {
reply->error = NDMP9_IO_ERR;
goto out;
}
switch (ccb->csio.ccb_h.status & CAM_STATUS_MASK) {
case CAM_REQ_CMP: /* completed */
reply->error = NDMP9_NO_ERR;
break;
case CAM_SEL_TIMEOUT:
case CAM_CMD_TIMEOUT:
reply->error = NDMP9_TIMEOUT_ERR;
break;
case CAM_SCSI_STATUS_ERROR:
if (ccb->csio.ccb_h.status & CAM_AUTOSNS_VALID) {
int n_sense;
n_sense = ccb->csio.sense_len - ccb->csio.sense_resid;
reply->ext_sense.ext_sense_val = (char *)malloc (n_sense);
if (reply->ext_sense.ext_sense_val) {
bcopy (&ccb->csio.sense_data,
reply->ext_sense.ext_sense_val,
n_sense);
reply->ext_sense.ext_sense_len = n_sense;
}
}
reply->error = NDMP9_NO_ERR;
break;
default:
reply->error = NDMP9_IO_ERR;
break;
}
out:
if (reply->error == NDMP9_NO_ERR) {
reply->status = ccb->csio.scsi_status;
data_done = data_len - ccb->csio.resid;
switch (request->data_dir) {
case NDMP9_SCSI_DATA_DIR_NONE:
break;
case NDMP9_SCSI_DATA_DIR_IN:
reply->datain.datain_val = (char *)data_in_ptr;
reply->datain.datain_len = data_len;
break;
case NDMP9_SCSI_DATA_DIR_OUT:
reply->dataout_len = data_len;
break;
default:
break;
}
} else {
if (data_in_ptr) {
free (data_in_ptr);
data_in_ptr = 0;
}
}
cam_freeccb (ccb);
return reply->error;
}
#endif /* NDMOS_OPTION_ROBOT_SIMULATOR */
#endif /* NDMOS_OPTION_NO_ROBOT_AGENT Surrounds all SCSI intfs */
#endif /* NDMOS_COMMON_SCSI_INTERFACE */