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
|
/*
* Mach Operating System
* Copyright (c) 1991,1990 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/*
* File: rz_tape.c
* Author: Alessandro Forin, Carnegie Mellon University
* Date: 10/90
*
* Top layer of the SCSI driver: interface with the MI.
* This file contains operations specific to TAPE-like devices.
*/
#include <mach/std_types.h>
#include <scsi/compat_30.h>
#include <sys/ioctl.h>
#ifdef MACH_KERNEL
#include <device/tape_status.h>
#else /*MACH_KERNEL*/
#include <mips/PMAX/tape_status.h>
#endif /*MACH_KERNEL*/
#include <scsi/scsi.h>
#include <scsi/scsi_defs.h>
#include <scsi/rz.h>
#if (NSCSI > 0)
void sctape_start(); /* forward */
int scsi_tape_timeout = 5*60; /* secs, tk50 is slow when positioning far apart */
int sctape_open(tgt, req)
target_info_t *tgt;
io_req_t req;
{
io_return_t ret;
io_req_t ior;
int i;
scsi_mode_sense_data_t *mod;
#ifdef MACH_KERNEL
req->io_device->flag |= D_EXCL_OPEN;
#endif /*MACH_KERNEL*/
/* Preferably allow tapes to disconnect */
if (BGET(scsi_might_disconnect,(unsigned char)tgt->masterno,tgt->target_id))
BSET(scsi_should_disconnect,(unsigned char)tgt->masterno,tgt->target_id);
/*
* Dummy ior for proper sync purposes
*/
io_req_alloc(ior,0);
ior->io_count = 0;
/*
* Do a mode sense first, some drives might be picky
* about changing params [even if the standard might
* say otherwise, sigh.]
*/
do {
ior->io_op = IO_INTERNAL;
ior->io_next = 0;
ior->io_error = 0;
ret = scsi_mode_sense(tgt, 0, 32, ior);
} while (ret == SCSI_RET_RETRY);
mod = (scsi_mode_sense_data_t *)tgt->cmd_ptr;
if (scsi_debug) {
int p[5];
bcopy((char*)mod, (char*)p, sizeof(p));
printf("[modsns(%x): x%x x%x x%x x%x x%x]", ret,
p[0], p[1], p[2], p[3], p[4]);
}
if (ret == SCSI_RET_DEVICE_DOWN)
goto out;
if (ret == SCSI_RET_SUCCESS) {
tgt->dev_info.tape.read_only = mod->wp;
tgt->dev_info.tape.speed = mod->speed;
tgt->dev_info.tape.density = mod->bdesc[0].density_code;
} /* else they all default sensibly, using zeroes */
/* Some tapes have limits on record-length */
again:
ior->io_op = IO_INTERNAL;
ior->io_next = 0;
ior->io_error = 0;
ret = scsi_read_block_limits( tgt, ior);
if (ret == SCSI_RET_RETRY) goto again;
if (!ior->io_error && (ret == SCSI_RET_SUCCESS)) {
scsi_blimits_data_t *lim;
int maxl;
lim = (scsi_blimits_data_t *) tgt->cmd_ptr;
tgt->block_size = (lim->minlen_msb << 8) |
lim->minlen_lsb;
maxl = (lim->maxlen_msb << 16) |
(lim->maxlen_sb << 8) |
lim->maxlen_lsb;
if (maxl == 0)
maxl = (unsigned)-1;
tgt->dev_info.tape.maxreclen = maxl;
tgt->dev_info.tape.fixed_size = (maxl == tgt->block_size);
} else {
/* let the user worry about it */
/* default: tgt->block_size = 1; */
tgt->dev_info.tape.maxreclen = (unsigned)-1;
tgt->dev_info.tape.fixed_size = FALSE;
}
/* Try hard to do a mode select */
for (i = 0; i < 5; i++) {
ior->io_op = IO_INTERNAL;
ior->io_error = 0;
ret = sctape_mode_select(tgt, 0, 0, FALSE, ior);
if (ret == SCSI_RET_SUCCESS)
break;
}
if (scsi_watchdog_period < scsi_tape_timeout)
scsi_watchdog_period += scsi_tape_timeout;
#if 0 /* this might imply rewind, which we do not want, although yes, .. */
/* we want the tape loaded */
ior->io_op = IO_INTERNAL;
ior->io_next = 0;
ior->io_error = 0;
ret = scsi_start_unit(tgt, SCSI_CMD_SS_START, ior);
#endif
req->io_device->bsize = tgt->block_size;
out:
io_req_free(ior);
return ret;
}
io_return_t sctape_close(tgt)
target_info_t *tgt;
{
io_return_t ret = SCSI_RET_SUCCESS;
io_req_t ior;
/*
* Dummy ior for proper sync purposes
*/
io_req_alloc(ior,0);
ior->io_op = IO_INTERNAL;
ior->io_next = 0;
ior->io_count = 0;
if (tgt->ior) printf("TAPE: Close with pending requests ?? \n");
/* write a filemark if we xtnded/truncated the tape */
if (tgt->flags & TGT_WRITTEN_TO) {
tgt->ior = ior;
ior->io_error = 0;
ret = scsi_write_filemarks(tgt, 2, ior);
if (ret != SCSI_RET_SUCCESS)
printf("%s%d: wfmark failed x%x\n",
(*tgt->dev_ops->driver_name)(TRUE), tgt->target_id, ret);
/*
* Don't bother repositioning if we'll rewind it
*/
if (tgt->flags & TGT_REWIND_ON_CLOSE)
goto rew;
retry:
tgt->ior = ior;
ior->io_op = IO_INTERNAL;
ior->io_error = 0;
ior->io_next = 0;
ret = scsi_space(tgt, SCSI_CMD_SP_FIL, -1, ior);
if (ret != SCSI_RET_SUCCESS) {
if (ret == SCSI_RET_RETRY) {
timeout(wakeup, tgt, hz);
await(tgt);
goto retry;
}
printf("%s%d: bspfile failed x%x\n",
(*tgt->dev_ops->driver_name)(TRUE), tgt->target_id, ret);
}
}
rew:
if (tgt->flags & TGT_REWIND_ON_CLOSE) {
/* Rewind tape */
ior->io_error = 0;
ior->io_op = IO_INTERNAL;
ior->io_error = 0;
tgt->ior = ior;
(void) scsi_rewind(tgt, ior, FALSE);
iowait(ior);
if (tgt->done == SCSI_RET_RETRY) {
timeout(wakeup, tgt, 5*hz);
await(tgt);
goto rew;
}
}
io_req_free(ior);
tgt->flags &= ~(TGT_ONLINE|TGT_WRITTEN_TO|TGT_REWIND_ON_CLOSE);
return ret;
}
int sctape_strategy(ior)
register io_req_t ior;
{
target_info_t *tgt;
register int i = ior->io_unit;
tgt = scsi_softc[rzcontroller(i)]->target[rzslave(i)];
if (((ior->io_op & IO_READ) == 0) &&
tgt->dev_info.tape.read_only) {
ior->io_error = D_INVALID_OPERATION;
ior->io_op |= IO_ERROR;
ior->io_residual = ior->io_count;
iodone(ior);
return ior->io_error;
}
return rz_simpleq_strategy( ior, sctape_start);
}
static void
do_residue(ior, sns, bsize)
io_req_t ior;
scsi_sense_data_t *sns;
int bsize;
{
int residue;
/* Not an error situation */
ior->io_error = 0;
ior->io_op &= ~IO_ERROR;
if (!sns->addr_valid) {
ior->io_residual = ior->io_count;
return;
}
residue = sns->u.xtended.info0 << 24 |
sns->u.xtended.info1 << 16 |
sns->u.xtended.info2 << 8 |
sns->u.xtended.info3;
/* fixed ? */
residue *= bsize;
/*
* NOTE: residue == requested - actual
* We only care if > 0
*/
if (residue < 0) residue = 0;/* sanity */
ior->io_residual += residue;
}
void sctape_start( tgt, done)
target_info_t *tgt;
boolean_t done;
{
io_req_t head, ior = tgt->ior;
if (ior == 0)
return;
if (done) {
/* see if we must retry */
if ((tgt->done == SCSI_RET_RETRY) &&
((ior->io_op & IO_INTERNAL) == 0)) {
delay(1000000);/*XXX*/
goto start;
} else
/* got a bus reset ? ouch, that hurts */
if (tgt->done == (SCSI_RET_ABORTED|SCSI_RET_RETRY)) {
/*
* we really cannot retry because the tape position
* is lost.
*/
printf("Lost tape position\n");
ior->io_error = D_IO_ERROR;
ior->io_op |= IO_ERROR;
} else
/* check completion status */
if (tgt->cur_cmd == SCSI_CMD_REQUEST_SENSE) {
scsi_sense_data_t *sns;
ior->io_op = ior->io_temporary;
ior->io_error = D_IO_ERROR;
ior->io_op |= IO_ERROR;
sns = (scsi_sense_data_t *)tgt->cmd_ptr;
if (scsi_debug)
scsi_print_sense_data(sns);
if (scsi_check_sense_data(tgt, sns)) {
if (sns->u.xtended.ili) {
if (ior->io_op & IO_READ) {
do_residue(ior, sns, tgt->block_size);
if (scsi_debug)
printf("Tape Short Read (%d)\n",
ior->io_residual);
}
} else if (sns->u.xtended.eom) {
do_residue(ior, sns, tgt->block_size);
if (scsi_debug)
printf("End of Physical Tape!\n");
} else if (sns->u.xtended.fm) {
do_residue(ior, sns, tgt->block_size);
if (scsi_debug)
printf("File Mark\n");
}
}
}
else if (tgt->done != SCSI_RET_SUCCESS) {
if (tgt->done == SCSI_RET_NEED_SENSE) {
ior->io_temporary = ior->io_op;
ior->io_op = IO_INTERNAL;
if (scsi_debug)
printf("[NeedSns x%x x%x]", ior->io_residual, ior->io_count);
scsi_request_sense(tgt, ior, 0);
return;
} else if (tgt->done == SCSI_RET_RETRY) {
/* only retry here READs and WRITEs */
if ((ior->io_op & IO_INTERNAL) == 0) {
ior->io_residual = 0;
goto start;
} else{
ior->io_error = D_WOULD_BLOCK;
ior->io_op |= IO_ERROR;
}
} else {
ior->io_error = D_IO_ERROR;
ior->io_op |= IO_ERROR;
}
}
if (scsi_debug)
printf("[Resid x%x]", ior->io_residual);
/* dequeue next one */
head = ior;
simple_lock(&tgt->target_lock);
ior = head->io_next;
tgt->ior = ior;
if (ior)
ior->io_prev = head->io_prev;
simple_unlock(&tgt->target_lock);
iodone(head);
if (ior == 0)
return;
}
ior->io_residual = 0;
start:
if (ior->io_op & IO_READ) {
tgt->flags &= ~TGT_WRITTEN_TO;
sctape_read( tgt, ior );
} else if ((ior->io_op & IO_INTERNAL) == 0) {
tgt->flags |= TGT_WRITTEN_TO;
sctape_write( tgt, ior );
}
}
io_return_t
sctape_get_status( dev, tgt, flavor, status, status_count)
int dev;
target_info_t *tgt;
dev_flavor_t flavor;
dev_status_t status;
natural_t *status_count;
{
switch (flavor) {
case DEV_GET_SIZE:
status[DEV_GET_SIZE_DEVICE_SIZE] = 0;
status[DEV_GET_SIZE_RECORD_SIZE] = tgt->block_size;
*status_count = DEV_GET_SIZE_COUNT;
break;
case TAPE_STATUS: {
struct tape_status *ts = (struct tape_status *) status;
ts->mt_type = MT_ISSCSI;
ts->speed = tgt->dev_info.tape.speed;
ts->density = tgt->dev_info.tape.density;
ts->flags = (tgt->flags & TGT_REWIND_ON_CLOSE) ?
TAPE_FLG_REWIND : 0;
if (tgt->dev_info.tape.read_only)
ts->flags |= TAPE_FLG_WP;
#ifdef MACH_KERNEL
*status_count = TAPE_STATUS_COUNT;
#endif
break;
}
/* U*x compat */
case MTIOCGET: {
struct mtget *g = (struct mtget *) status;
bzero(g, sizeof(struct mtget));
g->mt_type = 0x7; /* Ultrix compat */
#ifdef MACH_KERNEL
*status_count = sizeof(struct mtget)/sizeof(int);
#endif
break;
}
default:
return D_INVALID_OPERATION;
}
return D_SUCCESS;
}
io_return_t
sctape_set_status( dev, tgt, flavor, status, status_count)
int dev;
target_info_t *tgt;
dev_flavor_t flavor;
dev_status_t status;
natural_t status_count;
{
scsi_ret_t ret;
switch (flavor) {
case TAPE_STATUS: {
struct tape_status *ts = (struct tape_status *) status;
if (ts->flags & TAPE_FLG_REWIND)
tgt->flags |= TGT_REWIND_ON_CLOSE;
else
tgt->flags &= ~TGT_REWIND_ON_CLOSE;
if (ts->speed || ts->density) {
unsigned int ospeed, odensity;
io_req_t ior;
io_req_alloc(ior,0);
ior->io_op = IO_INTERNAL;
ior->io_error = 0;
ior->io_next = 0;
ior->io_count = 0;
ospeed = tgt->dev_info.tape.speed;
odensity = tgt->dev_info.tape.density;
tgt->dev_info.tape.speed = ts->speed;
tgt->dev_info.tape.density = ts->density;
ret = sctape_mode_select(tgt, 0, 0, (ospeed == ts->speed), ior);
if (ret != SCSI_RET_SUCCESS) {
tgt->dev_info.tape.speed = ospeed;
tgt->dev_info.tape.density = odensity;
}
io_req_free(ior);
}
break;
}
/* U*x compat */
case MTIOCTOP: {
struct tape_params *mt = (struct tape_params *) status;
io_req_t ior;
if (scsi_debug)
printf("[sctape_sstatus: %x %x %x]\n",
flavor, mt->mt_operation, mt->mt_repeat_count);
io_req_alloc(ior,0);
retry:
ior->io_count = 0;
ior->io_op = IO_INTERNAL;
ior->io_error = 0;
ior->io_next = 0;
tgt->ior = ior;
/* compat: in U*x it is a short */
switch ((short)(mt->mt_operation)) {
case MTWEOF: /* write an end-of-file record */
ret = scsi_write_filemarks(tgt, mt->mt_repeat_count, ior);
break;
case MTFSF: /* forward space file */
ret = scsi_space(tgt, SCSI_CMD_SP_FIL, mt->mt_repeat_count, ior);
break;
case MTBSF: /* backward space file */
ret = scsi_space(tgt, SCSI_CMD_SP_FIL, -mt->mt_repeat_count,ior);
break;
case MTFSR: /* forward space record */
ret = scsi_space(tgt, SCSI_CMD_SP_BLOCKS, mt->mt_repeat_count, ior);
break;
case MTBSR: /* backward space record */
ret = scsi_space(tgt, SCSI_CMD_SP_BLOCKS, -mt->mt_repeat_count, ior);
break;
case MTREW: /* rewind */
case MTOFFL: /* rewind and put the drive offline */
ret = scsi_rewind(tgt, ior, TRUE);
iowait(ior);
if ((short)(mt->mt_operation) == MTREW) break;
ior->io_op = 0;
ior->io_next = 0;
ior->io_error = 0;
(void) scsi_start_unit(tgt, 0, ior);
break;
case MTNOP: /* no operation, sets status only */
case MTCACHE: /* enable controller cache */
case MTNOCACHE: /* disable controller cache */
ret = SCSI_RET_SUCCESS;
break;
default:
tgt->ior = 0;
io_req_free(ior);
return D_INVALID_OPERATION;
}
if (ret == SCSI_RET_RETRY) {
timeout(wakeup, ior, 5*hz);
await(ior);
goto retry;
}
io_req_free(ior);
if (ret != SCSI_RET_SUCCESS)
return D_IO_ERROR;
break;
}
case MTIOCIEOT:
case MTIOCEEOT:
default:
return D_INVALID_OPERATION;
}
return D_SUCCESS;
}
#endif /* NSCSI > 0 */
|