summaryrefslogtreecommitdiff
path: root/usr/src/cmd/ndmpd/ndmp/ndmpd_scsi.c
blob: 5b4331f483b5df5379ea3eb93f6c4ec14ca3d47e (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * BSD 3 Clause License
 *
 * Copyright (c) 2007, The Storage Networking Industry Association.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 	- Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 *
 * 	- 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.
 *
 *	- Neither the name of The Storage Networking Industry Association (SNIA)
 *	  nor the names of its contributors may be used to endorse or promote
 *	  products derived from this software without specific prior written
 *	  permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
 */
/* Copyright (c) 2007, The Storage Networking Industry Association. */
/* Copyright (c) 1996, 1997 PDC, Network Appliance. All Rights Reserved */

#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include "ndmpd_common.h"
#include "ndmpd.h"
#include <string.h>
#include <sys/scsi/impl/uscsi.h>
#include <sys/scsi/scsi.h>

static void scsi_open_send_reply(ndmp_connection_t *connection, int err);
static void common_open(ndmp_connection_t *connection, char *devname);
static void common_set_target(ndmp_connection_t *connection, char *device,
    ushort_t controller, ushort_t sid, ushort_t lun);


/*
 * ************************************************************************
 * NDMP V2 HANDLERS
 * ************************************************************************
 */

/*
 * ndmpd_scsi_open_v2
 *
 * This handler opens the specified SCSI device.
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   body       (input) - request message body.
 *
 * Returns:
 *   void
 */
void
ndmpd_scsi_open_v2(ndmp_connection_t *connection, void *body)
{
	ndmp_scsi_open_request_v2 *request = (ndmp_scsi_open_request_v2 *)body;

	common_open(connection, request->device.name);
}


/*
 * ndmpd_scsi_close_v2
 *
 * This handler closes the currently open SCSI device.
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   body       (input) - request message body.
 *
 * Returns:
 *   void
 */
/*ARGSUSED*/
void
ndmpd_scsi_close_v2(ndmp_connection_t *connection, void *body)
{
	ndmp_scsi_close_reply reply;
	ndmpd_session_t *session = ndmp_get_client_data(connection);

	if (session->ns_scsi.sd_is_open == -1) {
		NDMP_LOG(LOG_ERR, "SCSI device is not open.");
		reply.error = NDMP_DEV_NOT_OPEN_ERR;
		ndmp_send_reply(connection, (void *) &reply,
		    "sending scsi_close reply");
		return;
	}
	(void) ndmp_open_list_del(session->ns_scsi.sd_adapter_name,
	    session->ns_scsi.sd_sid,
	    session->ns_scsi.sd_lun);
	(void) close(session->ns_scsi.sd_devid);

	session->ns_scsi.sd_is_open = -1;
	session->ns_scsi.sd_devid = -1;
	session->ns_scsi.sd_sid = 0;
	session->ns_scsi.sd_lun = 0;
	session->ns_scsi.sd_valid_target_set = FALSE;
	(void) memset(session->ns_scsi.sd_adapter_name, 0,
	    sizeof (session->ns_scsi.sd_adapter_name));

	reply.error = NDMP_NO_ERR;
	ndmp_send_reply(connection, (void *) &reply,
	    "sending scsi_close reply");
}


/*
 * ndmpd_scsi_get_state_v2
 *
 * This handler returns state information for the currently open SCSI device.
 * Since the implementation only supports the opening of a specific SCSI
 * device, as opposed to a device that can talk to multiple SCSI targets,
 * this request is not supported. This request is only appropriate for
 * implementations that support device files that can target multiple
 * SCSI devices.
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   body       (input) - request message body.
 *
 * Returns:
 *   void
 */
/*ARGSUSED*/
void
ndmpd_scsi_get_state_v2(ndmp_connection_t *connection, void *body)
{
	ndmp_scsi_get_state_reply reply;
	ndmpd_session_t *session = ndmp_get_client_data(connection);

	if (session->ns_scsi.sd_is_open == -1)
		reply.error = NDMP_DEV_NOT_OPEN_ERR;
	else if (!session->ns_scsi.sd_valid_target_set) {
		reply.error = NDMP_NO_ERR;
		reply.target_controller = -1;
		reply.target_id = -1;
		reply.target_lun = -1;
	} else {
		reply.error = NDMP_NO_ERR;
		reply.target_controller = 0;
		reply.target_id = session->ns_scsi.sd_sid;
		reply.target_lun = session->ns_scsi.sd_lun;
	}

	ndmp_send_reply(connection, (void *) &reply,
	    "sending scsi_get_state reply");
}


/*
 * ndmpd_scsi_set_target_v2
 *
 * This handler sets the SCSI target of the SCSI device.
 * It is only valid to use this request if the opened SCSI device
 * is capable of talking to multiple SCSI targets.
 * Since the implementation only supports the opening of a specific SCSI
 * device, as opposed to a device that can talk to multiple SCSI targets,
 * this request is not supported. This request is only appropriate for
 * implementations that support device files that can target multiple
 * SCSI devices.
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   body       (input) - request message body.
 *
 * Returns:
 *   void
 */
void
ndmpd_scsi_set_target_v2(ndmp_connection_t *connection, void *body)
{
	ndmp_scsi_set_target_request_v2 *request;

	request = (ndmp_scsi_set_target_request_v2 *) body;

	common_set_target(connection, request->device.name,
	    request->target_controller, request->target_id,
	    request->target_lun);
}


/*
 * ndmpd_scsi_reset_device_v2
 *
 * This handler resets the currently targeted SCSI device.
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   body       (input) - request message body.
 *
 * Returns:
 *   void
 */
/*ARGSUSED*/
void
ndmpd_scsi_reset_device_v2(ndmp_connection_t *connection, void *body)
{
	ndmp_scsi_reset_device_reply reply;


	ndmpd_session_t *session = ndmp_get_client_data(connection);
	struct uscsi_cmd  cmd;

	if (session->ns_scsi.sd_devid == -1) {
		NDMP_LOG(LOG_ERR, "SCSI device is not open.");
		reply.error = NDMP_DEV_NOT_OPEN_ERR;
	} else {
		reply.error = NDMP_NO_ERR;
		(void) memset((void*)&cmd, 0, sizeof (cmd));
		cmd.uscsi_flags |= USCSI_RESET;
		if (ioctl(session->ns_scsi.sd_devid, USCSICMD, &cmd) < 0) {
			NDMP_LOG(LOG_ERR, "USCSI reset failed: %m.");
			NDMP_LOG(LOG_DEBUG,
			    "ioctl(USCSICMD) USCSI_RESET failed: %m.");
			reply.error = NDMP_IO_ERR;
		}
	}

	ndmp_send_reply(connection, (void *) &reply,
	    "sending scsi_reset_device reply");
}


/*
 * ndmpd_scsi_reset_bus_v2
 *
 * This handler resets the currently targeted SCSI bus.
 *
 * Request not yet supported.
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   body       (input) - request message body.
 *
 * Returns:
 *   void
 */
/*ARGSUSED*/
void
ndmpd_scsi_reset_bus_v2(ndmp_connection_t *connection, void *body)
{
	ndmp_scsi_reset_bus_reply reply;

	NDMP_LOG(LOG_DEBUG, "request not supported");
	reply.error = NDMP_NOT_SUPPORTED_ERR;

	ndmp_send_reply(connection, (void *) &reply,
	    "sending scsi_reset_bus reply");
}


/*
 * ndmpd_scsi_execute_cdb_v2
 *
 * This handler sends the CDB to the currently targeted SCSI device.
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   body       (input) - request message body.
 *
 * Returns:
 *   void
 */
void
ndmpd_scsi_execute_cdb_v2(ndmp_connection_t *connection, void *body)
{
	ndmp_execute_cdb_request *request = (ndmp_execute_cdb_request *) body;
	ndmp_execute_cdb_reply reply;
	ndmpd_session_t *session = ndmp_get_client_data(connection);

	if (session->ns_scsi.sd_is_open == -1 ||
	    !session->ns_scsi.sd_valid_target_set) {
		(void) memset((void *) &reply, 0, sizeof (reply));

		NDMP_LOG(LOG_ERR, "SCSI device is not open.");
		reply.error = NDMP_DEV_NOT_OPEN_ERR;
		ndmp_send_reply(connection, (void *) &reply,
		    "sending scsi_execute_cdb reply");
	} else {
		ndmp_execute_cdb(session, session->ns_scsi.sd_adapter_name,
		    session->ns_scsi.sd_sid, session->ns_scsi.sd_lun, request);
	}
}


/*
 * ************************************************************************
 * NDMP V3 HANDLERS
 * ************************************************************************
 */

/*
 * ndmpd_scsi_open_v3
 *
 * This handler opens the specified SCSI device.
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   body       (input) - request message body.
 *
 * Returns:
 *   void
 */
void
ndmpd_scsi_open_v3(ndmp_connection_t *connection, void *body)
{
	ndmp_scsi_open_request_v3 *request = (ndmp_scsi_open_request_v3 *)body;

	common_open(connection, request->device);
}


/*
 * ndmpd_scsi_set_target_v3
 *
 * This handler sets the SCSI target of the SCSI device.
 * It is only valid to use this request if the opened SCSI device
 * is capable of talking to multiple SCSI targets.
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   body       (input) - request message body.
 *
 * Returns:
 *   void
 */
void
ndmpd_scsi_set_target_v3(ndmp_connection_t *connection, void *body)
{
	ndmp_scsi_set_target_request_v3 *request;

	request = (ndmp_scsi_set_target_request_v3 *) body;

	common_set_target(connection, request->device,
	    request->target_controller, request->target_id,
	    request->target_lun);
}


/*
 * ************************************************************************
 * NDMP V4 HANDLERS
 * ************************************************************************
 */

/*
 * ************************************************************************
 * LOCALS
 * ************************************************************************
 */


/*
 * scsi_open_send_reply
 *
 * Send a reply for SCSI open command
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   err        (input) - ndmp error code
 *
 * Returns:
 *   void
 */
static void
scsi_open_send_reply(ndmp_connection_t *connection, int err)
{
	ndmp_scsi_open_reply reply;

	reply.error = err;
	ndmp_send_reply(connection, (void *) &reply, "sending scsi_open reply");
}


/*
 * common_open
 *
 * Common SCSI open function for all NDMP versions
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   devname (input) - device name to open.
 *
 * Returns:
 *   void
 */
static void
common_open(ndmp_connection_t *connection, char *devname)
{
	ndmpd_session_t *session = ndmp_get_client_data(connection);
	char adptnm[SCSI_MAX_NAME];
	int sid, lun;
	int err;
	scsi_adapter_t *sa;
	int devid;

	err = NDMP_NO_ERR;

	if (session->ns_tape.td_fd != -1 || session->ns_scsi.sd_is_open != -1) {
		NDMP_LOG(LOG_ERR,
		    "Session already has a tape or scsi device open.");
		err = NDMP_DEVICE_OPENED_ERR;
	} else if ((sa = scsi_get_adapter(0)) != NULL) {
		NDMP_LOG(LOG_DEBUG, "Adapter device found: %s", devname);
		(void) strlcpy(adptnm, devname, SCSI_MAX_NAME-2);
		adptnm[SCSI_MAX_NAME-1] = '\0';
		sid = lun = -1;

		scsi_find_sid_lun(sa, devname, &sid, &lun);
		if (ndmp_open_list_find(devname, sid, lun) == NULL &&
		    (devid = open(devname, O_RDWR | O_NDELAY)) < 0) {
			NDMP_LOG(LOG_ERR, "Failed to open device %s: %m.",
			    devname);
			err = NDMP_NO_DEVICE_ERR;
		}
	} else {
		NDMP_LOG(LOG_ERR, "%s: No such SCSI adapter.", devname);
		err = NDMP_NO_DEVICE_ERR;
	}

	if (err != NDMP_NO_ERR) {
		scsi_open_send_reply(connection, err);
		return;
	}

	switch (ndmp_open_list_add(connection, adptnm, sid, lun, devid)) {
	case 0:
		/* OK */
		break;
	case EBUSY:
		err = NDMP_DEVICE_BUSY_ERR;
		break;
	case ENOMEM:
		err = NDMP_NO_MEM_ERR;
		break;
	default:
		err = NDMP_IO_ERR;
	}
	if (err != NDMP_NO_ERR) {
		scsi_open_send_reply(connection, err);
		return;
	}

	(void) strlcpy(session->ns_scsi.sd_adapter_name, adptnm, SCSI_MAX_NAME);
	session->ns_scsi.sd_is_open = 1;
	session->ns_scsi.sd_devid = devid;
	if (sid != -1) {
		session->ns_scsi.sd_sid = sid;
		session->ns_scsi.sd_lun = lun;
		session->ns_scsi.sd_valid_target_set = TRUE;
	} else {
		session->ns_scsi.sd_sid = session->ns_scsi.sd_lun = -1;
		session->ns_scsi.sd_valid_target_set = FALSE;
	}

	scsi_open_send_reply(connection, err);
}


/*
 * common_set_target
 *
 * Set the SCSI target (SCSI number, LUN number, controller number)
 *
 * Parameters:
 *   connection (input) - connection handle.
 *   device (input) - device name.
 *   controller (input) - controller number.
 *   sid (input) - SCSI target ID.
 *   lun (input) - LUN number.
 *
 * Returns:
 *   0: on success
 *  -1: otherwise
 */
/*ARGSUSED*/
static void
common_set_target(ndmp_connection_t *connection, char *device,
    ushort_t controller, ushort_t sid, ushort_t lun)
{
	ndmp_scsi_set_target_reply reply;
	ndmpd_session_t *session = ndmp_get_client_data(connection);
	int type;

	reply.error = NDMP_NO_ERR;

	if (session->ns_scsi.sd_is_open == -1) {
		reply.error = NDMP_DEV_NOT_OPEN_ERR;
	} else if (!scsi_dev_exists(session->ns_scsi.sd_adapter_name, sid,
	    lun)) {
		NDMP_LOG(LOG_ERR, "No such SCSI device: target %d lun %d.",
		    sid, lun);
		reply.error = NDMP_NO_DEVICE_ERR;
	} else {
		type = scsi_get_devtype(session->ns_scsi.sd_adapter_name, sid,
		    lun);
		if (type != DTYPE_SEQUENTIAL && type != DTYPE_CHANGER) {
			NDMP_LOG(LOG_ERR,
			    "Not a tape or robot device: target %d lun %d.",
			    sid, lun);
			reply.error = NDMP_ILLEGAL_ARGS_ERR;
		}
	}

	if (reply.error != NDMP_NO_ERR) {
		ndmp_send_reply(connection, (void *) &reply,
		    "sending scsi_set_target reply");
		return;
	}

	/*
	 * The open_list must be updated if the SID or LUN are going to be
	 * changed.  Close uses the same SID & LUN for removing the entry
	 * from the open_list.
	 */
	if (sid != session->ns_scsi.sd_sid || lun != session->ns_scsi.sd_lun) {
		switch (ndmp_open_list_add(connection,
		    session->ns_scsi.sd_adapter_name, sid, lun, 0)) {
		case 0:
			(void) ndmp_open_list_del(session->
			    ns_scsi.sd_adapter_name, session->ns_scsi.sd_sid,
			    session->ns_scsi.sd_lun);
			break;
		case EBUSY:
			reply.error = NDMP_DEVICE_BUSY_ERR;
			break;
		case ENOMEM:
			reply.error = NDMP_NO_MEM_ERR;
			break;
		default:
			reply.error = NDMP_IO_ERR;
		}
	}

	if (reply.error == NDMP_NO_ERR) {
		NDMP_LOG(LOG_DEBUG, "Updated sid %d lun %d", sid, lun);
		session->ns_scsi.sd_sid = sid;
		session->ns_scsi.sd_lun = lun;
		session->ns_scsi.sd_valid_target_set = TRUE;
	}

	ndmp_send_reply(connection, (void *) &reply,
	    "sending scsi_set_target reply");
}

/*
 * scsi_find_sid_lun
 *
 * gets the adapter, and returns the sid and lun number
 */
void
scsi_find_sid_lun(scsi_adapter_t *sa, char *devname, int *sid, int *lun)
{
	scsi_link_t *sl;
	char *name;

	for (sl = sa->sa_link_head.sl_next; sl && sl != &sa->sa_link_head;
	    sl = sl->sl_next) {
		name = sasd_slink_name(sl);
		if (strcmp(devname, name) == 0) {
			*sid = sl->sl_sid;
			*lun = sl->sl_lun;
			return;
		}
	}

	*sid = -1;
	*lun = -1;
}