Lines Matching refs:STp
246 struct scsi_tape *STp = NULL;
251 STp = idr_find(&st_index_idr, dev);
252 if (!STp) goto out;
254 kref_get(&STp->kref);
256 if (!STp->device)
259 if (scsi_device_get(STp->device))
265 kref_put(&STp->kref, scsi_tape_release);
266 STp = NULL;
270 return STp;
273 static void scsi_tape_put(struct scsi_tape *STp)
275 struct scsi_device *sdev = STp->device;
278 kref_put(&STp->kref, scsi_tape_release);
359 static int st_chk_result(struct scsi_tape *STp, struct st_request * SRpnt)
364 char *name = STp->name;
370 cmdstatp = &STp->buffer->cmdstat;
374 scode = STp->buffer->cmdstat.sense_hdr.sense_key;
380 st_printk(ST_DEB_MSG, STp,
385 __scsi_print_sense(STp->device, name,
390 st_printk(KERN_WARNING, STp,
402 __scsi_print_sense(STp->device, name,
408 STp->cln_mode >= EXTENDED_SENSE_START) { /* Only fixed format sense */
409 if (STp->cln_sense_value)
410 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
411 STp->cln_sense_mask) == STp->cln_sense_value);
413 STp->cleaning_req |= ((SRpnt->sense[STp->cln_mode] &
414 STp->cln_sense_mask) != 0);
418 STp->cleaning_req = 1; /* ASC and ASCQ => cleaning requested */
420 STp->pos_unknown = 1; /* ASC => power on / reset */
422 STp->pos_unknown |= STp->device->was_reset;
431 STp->recover_count++;
432 STp->recover_reg++;
442 st_printk(ST_DEB_MSG, STp,
444 stp, STp->recover_count);
477 static void st_do_stats(struct scsi_tape *STp, struct request *req)
484 now = ktime_sub(now, STp->stats->write_time);
485 atomic64_add(ktime_to_ns(now), &STp->stats->tot_write_time);
486 atomic64_add(ktime_to_ns(now), &STp->stats->tot_io_time);
487 atomic64_inc(&STp->stats->write_cnt);
489 atomic64_add(atomic_read(&STp->stats->last_write_size)
490 - STp->buffer->cmdstat.residual,
491 &STp->stats->write_byte_cnt);
492 if (STp->buffer->cmdstat.residual > 0)
493 atomic64_inc(&STp->stats->resid_cnt);
495 atomic64_add(atomic_read(&STp->stats->last_write_size),
496 &STp->stats->write_byte_cnt);
498 now = ktime_sub(now, STp->stats->read_time);
499 atomic64_add(ktime_to_ns(now), &STp->stats->tot_read_time);
500 atomic64_add(ktime_to_ns(now), &STp->stats->tot_io_time);
501 atomic64_inc(&STp->stats->read_cnt);
503 atomic64_add(atomic_read(&STp->stats->last_read_size)
504 - STp->buffer->cmdstat.residual,
505 &STp->stats->read_byte_cnt);
506 if (STp->buffer->cmdstat.residual > 0)
507 atomic64_inc(&STp->stats->resid_cnt);
509 atomic64_add(atomic_read(&STp->stats->last_read_size),
510 &STp->stats->read_byte_cnt);
512 now = ktime_sub(now, STp->stats->other_time);
513 atomic64_add(ktime_to_ns(now), &STp->stats->tot_io_time);
514 atomic64_inc(&STp->stats->other_cnt);
516 atomic64_dec(&STp->stats->in_flight);
524 struct scsi_tape *STp = SRpnt->stp;
527 STp->buffer->cmdstat.midlevel_result = SRpnt->result = scmd->result;
528 STp->buffer->cmdstat.residual = scmd->resid_len;
530 st_do_stats(STp, req);
550 struct scsi_tape *STp = SRpnt->stp;
572 atomic64_inc(&STp->stats->in_flight);
574 atomic_set(&STp->stats->last_write_size, bufflen);
575 STp->stats->write_time = ktime_get();
577 atomic_set(&STp->stats->last_read_size, bufflen);
578 STp->stats->read_time = ktime_get();
580 STp->stats->other_time = ktime_get();
599 st_do_scsi(struct st_request * SRpnt, struct scsi_tape * STp, unsigned char *cmd,
603 struct rq_map_data *mdata = &STp->buffer->map_data;
607 if (!do_wait && ((STp->buffer)->last_SRpnt)) {
608 st_printk(KERN_ERR, STp,
611 (STp->buffer)->syscall_result = (-EINTR);
613 (STp->buffer)->syscall_result = (-EBUSY);
618 SRpnt = st_allocate_request(STp);
626 (STp->buffer)->last_SRpnt = SRpnt;
628 waiting = &STp->wait;
632 if (STp->buffer->do_dio) {
634 mdata->nr_entries = STp->buffer->sg_segs;
635 mdata->pages = STp->buffer->mapped_pages;
637 mdata->page_order = STp->buffer->reserved_page_order;
640 mdata->pages = STp->buffer->reserved_pages;
645 STp->buffer->cmdstat.have_sense = 0;
646 STp->buffer->syscall_result = 0;
652 (STp->buffer)->syscall_result = (-EBUSY);
653 (STp->buffer)->last_SRpnt = NULL;
657 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
668 static int write_behind_check(struct scsi_tape * STp)
676 STbuffer = STp->buffer;
681 if (STp->write_pending)
682 STp->nbr_waits++;
684 STp->nbr_finished++;
687 wait_for_completion(&(STp->wait));
692 (STp->buffer)->syscall_result = st_chk_result(STp, SRpnt);
696 STps = &(STp->ps[STp->partition]);
698 if (STp->block_size == 0)
701 STps->drv_block += STbuffer->writing / STp->block_size;
722 st_printk(ST_DEB_MSG, STp,
732 static int cross_eof(struct scsi_tape * STp, int forward)
746 DEBC_printk(STp, "Stepping over filemark %s.\n",
749 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
750 STp->device->request_queue->rq_timeout,
753 return (STp->buffer)->syscall_result;
758 if ((STp->buffer)->cmdstat.midlevel_result != 0)
759 st_printk(KERN_ERR, STp,
763 return (STp->buffer)->syscall_result;
768 static int st_flush_write_buffer(struct scsi_tape * STp)
776 result = write_behind_check(STp);
781 if (STp->dirty == 1) {
783 transfer = STp->buffer->buffer_bytes;
784 DEBC_printk(STp, "Flushing %d bytes.\n", transfer);
789 blks = transfer / STp->block_size;
794 SRpnt = st_do_scsi(NULL, STp, cmd, transfer, DMA_TO_DEVICE,
795 STp->device->request_queue->rq_timeout,
798 return (STp->buffer)->syscall_result;
800 STps = &(STp->ps[STp->partition]);
801 if ((STp->buffer)->syscall_result != 0) {
802 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
810 STp->dirty = 0;
811 (STp->buffer)->buffer_bytes = 0;
816 st_printk(KERN_ERR, STp, "Error on flush.\n");
823 STp->dirty = 0;
824 (STp->buffer)->buffer_bytes = 0;
835 static int flush_buffer(struct scsi_tape *STp, int seek_next)
844 if (STp->pos_unknown)
847 if (STp->ready != ST_READY)
849 STps = &(STp->ps[STp->partition]);
851 return st_flush_write_buffer(STp);
853 if (STp->block_size == 0)
856 backspace = ((STp->buffer)->buffer_bytes +
857 (STp->buffer)->read_pointer) / STp->block_size -
858 ((STp->buffer)->read_pointer + STp->block_size - 1) /
859 STp->block_size;
860 (STp->buffer)->buffer_bytes = 0;
861 (STp->buffer)->read_pointer = 0;
865 result = cross_eof(STp, 0); /* Back over the EOF hit */
875 result = st_int_ioctl(STp, MTBSR, backspace);
888 static int set_mode_densblk(struct scsi_tape * STp, struct st_modedef * STm)
893 if (!STp->density_changed &&
895 STm->default_density != STp->density) {
899 arg = STp->density;
901 if (!STp->blksize_changed &&
903 STm->default_blksize != STp->block_size) {
907 arg |= STp->block_size;
909 st_int_ioctl(STp, SET_DENS_AND_BLK, arg)) {
910 st_printk(KERN_WARNING, STp,
922 static int do_door_lock(struct scsi_tape * STp, int do_lock)
926 DEBC_printk(STp, "%socking drive door.\n", do_lock ? "L" : "Unl");
928 retval = scsi_set_medium_removal(STp->device,
931 STp->door_locked = do_lock ? ST_LOCKED_EXPLICIT : ST_UNLOCKED;
933 STp->door_locked = ST_LOCK_FAILS;
939 static void reset_state(struct scsi_tape *STp)
944 STp->pos_unknown = 0;
946 STps = &(STp->ps[i]);
954 if (STp->can_partitions) {
955 STp->partition = find_partition(STp);
956 if (STp->partition < 0)
957 STp->partition = 0;
958 STp->new_partition = STp->partition;
972 static int test_ready(struct scsi_tape *STp, int do_wait)
978 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
985 SRpnt = st_do_scsi(SRpnt, STp, cmd, 0, DMA_NONE,
986 STp->long_timeout, MAX_READY_RETRIES, 1);
989 retval = (STp->buffer)->syscall_result;
1019 if ((STp->device)->scsi_level >= SCSI_2 &&
1029 retval = (STp->buffer)->syscall_result;
1046 static int check_tape(struct scsi_tape *STp, struct file *filp)
1057 STp->ready = ST_READY;
1059 if (mode != STp->current_mode) {
1060 DEBC_printk(STp, "Mode change from %d to %d.\n",
1061 STp->current_mode, mode);
1063 STp->current_mode = mode;
1065 STm = &(STp->modes[STp->current_mode]);
1067 saved_cleaning = STp->cleaning_req;
1068 STp->cleaning_req = 0;
1071 retval = test_ready(STp, do_wait);
1077 STp->pos_unknown = 0;
1078 STp->partition = STp->new_partition = 0;
1079 if (STp->can_partitions)
1080 STp->nbr_partitions = 1; /* This guess will be updated later
1083 STps = &(STp->ps[i]);
1094 STp->cleaning_req |= saved_cleaning;
1098 STp->ready = ST_NO_TAPE;
1100 STp->ready = ST_NOT_READY;
1102 STp->density = 0; /* Clear the erroneous "residue" */
1103 STp->write_prot = 0;
1104 STp->block_size = 0;
1105 STp->ps[0].drv_file = STp->ps[0].drv_block = (-1);
1106 STp->partition = STp->new_partition = 0;
1107 STp->door_locked = ST_UNLOCKED;
1112 if (STp->omit_blklims)
1113 STp->min_block = STp->max_block = (-1);
1118 SRpnt = st_do_scsi(SRpnt, STp, cmd, 6, DMA_FROM_DEVICE,
1119 STp->device->request_queue->rq_timeout,
1122 retval = (STp->buffer)->syscall_result;
1126 if (!SRpnt->result && !STp->buffer->cmdstat.have_sense) {
1127 STp->max_block = ((STp->buffer)->b_data[1] << 16) |
1128 ((STp->buffer)->b_data[2] << 8) | (STp->buffer)->b_data[3];
1129 STp->min_block = ((STp->buffer)->b_data[4] << 8) |
1130 (STp->buffer)->b_data[5];
1131 if ( DEB( debugging || ) !STp->inited)
1132 st_printk(KERN_INFO, STp,
1134 STp->min_block, STp->max_block);
1136 STp->min_block = STp->max_block = (-1);
1137 DEBC_printk(STp, "Can't read block limits.\n");
1145 SRpnt = st_do_scsi(SRpnt, STp, cmd, 12, DMA_FROM_DEVICE,
1146 STp->device->request_queue->rq_timeout,
1149 retval = (STp->buffer)->syscall_result;
1153 if ((STp->buffer)->syscall_result != 0) {
1154 DEBC_printk(STp, "No Mode Sense.\n");
1155 STp->block_size = ST_DEFAULT_BLOCK; /* Educated guess (?) */
1156 (STp->buffer)->syscall_result = 0; /* Prevent error propagation */
1157 STp->drv_write_prot = 0;
1159 DEBC_printk(STp,"Mode sense. Length %d, "
1161 (STp->buffer)->b_data[0],
1162 (STp->buffer)->b_data[1],
1163 (STp->buffer)->b_data[2],
1164 (STp->buffer)->b_data[3]);
1166 if ((STp->buffer)->b_data[3] >= 8) {
1167 STp->drv_buffer = ((STp->buffer)->b_data[2] >> 4) & 7;
1168 STp->density = (STp->buffer)->b_data[4];
1169 STp->block_size = (STp->buffer)->b_data[9] * 65536 +
1170 (STp->buffer)->b_data[10] * 256 + (STp->buffer)->b_data[11];
1171 DEBC_printk(STp, "Density %x, tape length: %x, "
1173 STp->density,
1174 (STp->buffer)->b_data[5] * 65536 +
1175 (STp->buffer)->b_data[6] * 256 +
1176 (STp->buffer)->b_data[7],
1177 STp->drv_buffer);
1179 STp->drv_write_prot = ((STp->buffer)->b_data[2] & 0x80) != 0;
1180 if (!STp->drv_buffer && STp->immediate_filemark) {
1181 st_printk(KERN_WARNING, STp,
1184 STp->immediate_filemark = 0;
1189 STp->inited = 1;
1191 if (STp->block_size > 0)
1192 (STp->buffer)->buffer_blocks =
1193 (STp->buffer)->buffer_size / STp->block_size;
1195 (STp->buffer)->buffer_blocks = 1;
1196 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
1198 DEBC_printk(STp, "Block size: %d, buffer size: %d (%d blocks).\n",
1199 STp->block_size, (STp->buffer)->buffer_size,
1200 (STp->buffer)->buffer_blocks);
1202 if (STp->drv_write_prot) {
1203 STp->write_prot = 1;
1205 DEBC_printk(STp, "Write protected\n");
1215 if (STp->can_partitions && STp->nbr_partitions < 1) {
1219 DEBC_printk(STp, "Updating partition number in status.\n");
1220 if ((STp->partition = find_partition(STp)) < 0) {
1221 retval = STp->partition;
1224 STp->new_partition = STp->partition;
1225 STp->nbr_partitions = 1; /* This guess will be updated when necessary */
1229 STp->density_changed = STp->blksize_changed = 0;
1230 STp->compression_changed = 0;
1232 (retval = set_mode_densblk(STp, STm)) < 0)
1235 if (STp->default_drvbuffer != 0xff) {
1236 if (st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer))
1237 st_printk(KERN_WARNING, STp,
1240 STp->default_drvbuffer);
1258 struct scsi_tape *STp;
1269 if (!(STp = scsi_tape_get(dev))) {
1273 filp->private_data = STp;
1276 if (STp->in_use) {
1278 DEBC_printk(STp, "Device already in use.\n");
1279 scsi_tape_put(STp);
1283 STp->in_use = 1;
1285 STp->rew_at_close = STp->autorew_dev = (iminor(inode) & 0x80) == 0;
1287 if (scsi_autopm_get_device(STp->device) < 0) {
1292 if (!scsi_block_when_processing_errors(STp->device)) {
1298 if (!enlarge_buffer(STp->buffer, PAGE_SIZE)) {
1299 st_printk(KERN_WARNING, STp,
1305 (STp->buffer)->cleared = 0;
1306 (STp->buffer)->writing = 0;
1307 (STp->buffer)->syscall_result = 0;
1309 STp->write_prot = ((filp->f_flags & O_ACCMODE) == O_RDONLY);
1311 STp->dirty = 0;
1313 STps = &(STp->ps[i]);
1316 STp->try_dio_now = STp->try_dio;
1317 STp->recover_count = 0;
1318 DEB( STp->nbr_waits = STp->nbr_finished = 0;
1319 STp->nbr_requests = STp->nbr_dio = STp->nbr_pages = 0; )
1321 retval = check_tape(STp, filp);
1326 if (STp->ready == NO_TAPE)
1335 normalize_buffer(STp->buffer);
1337 STp->in_use = 0;
1340 scsi_autopm_put_device(STp->device);
1341 scsi_tape_put(STp);
1354 struct scsi_tape *STp = filp->private_data;
1355 struct st_modedef *STm = &(STp->modes[STp->current_mode]);
1356 struct st_partstat *STps = &(STp->ps[STp->partition]);
1361 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1362 result = st_flush_write_buffer(STp);
1367 if (STp->can_partitions &&
1368 (result2 = switch_partition(STp)) < 0) {
1369 DEBC_printk(STp, "switch_partition at close failed.\n");
1375 DEBC( if (STp->nbr_requests)
1376 st_printk(KERN_DEBUG, STp,
1378 "pages %d.\n", STp->nbr_requests, STp->nbr_dio,
1379 STp->nbr_pages));
1381 if (STps->rw == ST_WRITING && !STp->pos_unknown) {
1382 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1385 DEBC_printk(STp, "Async write waits %d, finished %d.\n",
1386 STp->nbr_waits, STp->nbr_finished);
1390 if (STp->immediate_filemark)
1392 cmd[4] = 1 + STp->two_fm;
1394 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
1395 STp->device->request_queue->rq_timeout,
1398 result = (STp->buffer)->syscall_result;
1402 if (STp->buffer->syscall_result == 0 ||
1414 if (STp->two_fm)
1415 cross_eof(STp, 0);
1421 st_printk(KERN_ERR, STp,
1427 DEBC_printk(STp, "Buffer flushed, %d EOF(s) written\n", cmd[4]);
1428 } else if (!STp->rew_at_close) {
1429 STps = &(STp->ps[STp->partition]);
1431 if (STp->can_bsr)
1432 result = flush_buffer(STp, 0);
1434 result = cross_eof(STp, 0);
1444 !(result = cross_eof(STp, 1))) ||
1454 if (STp->rew_at_close) {
1455 result2 = st_int_ioctl(STp, MTREW, 1);
1467 struct scsi_tape *STp = filp->private_data;
1469 if (STp->door_locked == ST_LOCKED_AUTO)
1470 do_door_lock(STp, 0);
1472 normalize_buffer(STp->buffer);
1474 STp->in_use = 0;
1476 scsi_autopm_put_device(STp->device);
1477 scsi_tape_put(STp);
1483 static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count)
1493 if (!scsi_block_when_processing_errors(STp->device)) {
1498 if (STp->ready != ST_READY) {
1499 if (STp->ready == ST_NO_TAPE)
1506 if (! STp->modes[STp->current_mode].defined) {
1516 if (STp->pos_unknown) {
1525 if (!STp->in_use) {
1526 st_printk(ST_DEB_MSG, STp,
1532 if (STp->can_partitions &&
1533 (retval = switch_partition(STp)) < 0)
1536 if (STp->block_size == 0 && STp->max_block > 0 &&
1537 (count < STp->min_block || count > STp->max_block)) {
1542 if (STp->do_auto_lock && STp->door_locked == ST_UNLOCKED &&
1543 !do_door_lock(STp, 1))
1544 STp->door_locked = ST_LOCKED_AUTO;
1551 static int setup_buffering(struct scsi_tape *STp, const char __user *buf,
1555 struct st_buffer *STbp = STp->buffer;
1558 i = STp->try_dio_now && try_rdio;
1560 i = STp->try_dio_now && try_wdio;
1563 STp->device->request_queue)) == 0) {
1575 STp->nbr_dio++;
1576 STp->nbr_pages += STbp->do_dio;
1581 DEB( STp->nbr_requests++; )
1584 if (STp->block_size)
1585 bufsize = STp->block_size > st_fixed_buffer_size ?
1586 STp->block_size : st_fixed_buffer_size;
1591 if (is_read && STp->sili && !STbp->cleared)
1597 st_printk(KERN_WARNING, STp,
1603 if (STp->block_size)
1604 STbp->buffer_blocks = bufsize / STp->block_size;
1613 static void release_buffering(struct scsi_tape *STp, int is_read)
1617 STbp = STp->buffer;
1638 struct scsi_tape *STp = filp->private_data;
1643 if (mutex_lock_interruptible(&STp->lock))
1646 retval = rw_checks(STp, filp, count);
1651 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
1652 st_printk(KERN_WARNING, STp,
1658 STm = &(STp->modes[STp->current_mode]);
1659 STps = &(STp->ps[STp->partition]);
1661 if (STp->write_prot) {
1668 retval = flush_buffer(STp, 0);
1674 if ((retval = set_mode_densblk(STp, STm)) < 0)
1677 !(STp->compression_changed)) {
1678 if (st_compression(STp, (STm->default_compression == ST_YES))) {
1679 st_printk(KERN_WARNING, STp,
1689 STbp = STp->buffer;
1690 i = write_behind_check(STp);
1710 if (STp->block_size != 0 &&
1718 retval = setup_buffering(STp, buf, count, 0);
1726 cmd[1] = (STp->block_size != 0);
1737 if (STp->block_size == 0)
1740 do_count = STbp->buffer_blocks * STp->block_size -
1755 async_write = STp->block_size == 0 && !STbp->do_dio &&
1758 if (STp->block_size != 0 && STm->do_buffer_writes &&
1759 !(STp->try_dio_now && try_wdio) && STps->eof < ST_EOM_OK &&
1761 STp->dirty = 1;
1768 if (STp->block_size == 0)
1775 blks /= STp->block_size;
1776 transfer = blks * STp->block_size;
1782 SRpnt = st_do_scsi(SRpnt, STp, cmd, transfer, DMA_TO_DEVICE,
1783 STp->device->request_queue->rq_timeout,
1791 STp->dirty = !(STbp->writing ==
1794 DEB( STp->write_pending = 1; )
1799 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1801 DEBC_printk(STp, "Error on write:\n");
1806 else if (STp->block_size == 0 &&
1811 if (STp->block_size != 0)
1812 undone *= STp->block_size;
1818 if (STp->block_size)
1819 blks = (transfer - undone) / STp->block_size;
1825 if (STp->block_size == 0 ||
1828 DEBC_printk(STp, "EOM with %d "
1836 move_buffer_data(STp->buffer, transfer - undone);
1840 STp->block_size;
1843 DEBC_printk(STp, "Retry "
1846 STp->buffer->buffer_bytes);
1857 DEBC_printk(STp, "EOM with "
1870 if (STp->block_size == 0)
1877 STp->dirty = 0;
1895 release_buffering(STp, 0);
1896 mutex_unlock(&STp->lock);
1908 static long read_tape(struct scsi_tape *STp, long count,
1922 STm = &(STp->modes[STp->current_mode]);
1923 STps = &(STp->ps[STp->partition]);
1926 STbp = STp->buffer;
1928 if (STp->block_size == 0)
1931 if (!(STp->try_dio_now && try_rdio) && STm->do_read_ahead) {
1932 blks = (STp->buffer)->buffer_blocks;
1933 bytes = blks * STp->block_size;
1936 if (!STbp->do_dio && bytes > (STp->buffer)->buffer_size)
1937 bytes = (STp->buffer)->buffer_size;
1938 blks = bytes / STp->block_size;
1939 bytes = blks * STp->block_size;
1945 cmd[1] = (STp->block_size != 0);
1946 if (!cmd[1] && STp->sili)
1953 SRpnt = st_do_scsi(SRpnt, STp, cmd, bytes, DMA_FROM_DEVICE,
1954 STp->device->request_queue->rq_timeout,
1956 release_buffering(STp, 1);
1966 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
1969 DEBC_printk(STp,
1987 if (STp->block_size == 0)
1994 if (STp->block_size == 0 &&
1996 st_printk(KERN_NOTICE, STp,
2006 } else if (STp->block_size == 0) {
2012 st_printk(KERN_NOTICE, STp,
2017 st_int_ioctl(STp, MTBSR, 1);
2022 STp->block_size;
2023 DEBC_printk(STp, "ILI but "
2030 if (st_int_ioctl(STp, MTBSR, 1))
2038 if (STp->block_size == 0)
2042 bytes - transfer * STp->block_size;
2043 DEBC_printk(STp, "EOF detected (%d "
2051 if (STp->block_size == 0)
2055 bytes - transfer * STp->block_size;
2057 DEBC_printk(STp, "EOM detected (%d "
2064 DEBC_printk(STp, "Tape error while reading.\n");
2068 DEBC_printk(STp, "Zero returned for "
2088 if (STp->sili) /* In fixed block mode residual is always zero here */
2089 STbp->buffer_bytes -= STp->buffer->cmdstat.residual;
2093 if (STp->block_size == 0)
2096 STps->drv_block += STbp->buffer_bytes / STp->block_size;
2112 struct scsi_tape *STp = filp->private_data;
2115 struct st_buffer *STbp = STp->buffer;
2117 if (mutex_lock_interruptible(&STp->lock))
2120 retval = rw_checks(STp, filp, count);
2124 STm = &(STp->modes[STp->current_mode]);
2125 if (STp->block_size != 0 && (count % STp->block_size) != 0) {
2130 STp->try_dio_now = 0; /* Direct i/o can't handle split blocks */
2133 STps = &(STp->ps[STp->partition]);
2135 retval = flush_buffer(STp, 0);
2142 st_printk(ST_DEB_MSG, STp,
2147 retval = setup_buffering(STp, buf, count, 1);
2183 special = read_tape(STp, count - total, &SRpnt);
2194 st_printk(ST_DEB_MSG, STp,
2212 if (STp->block_size == 0)
2242 release_buffering(STp, 1);
2245 mutex_unlock(&STp->lock);
2255 static void st_log_options(struct scsi_tape * STp, struct st_modedef * STm)
2258 st_printk(KERN_INFO, STp,
2261 STp->current_mode, STm->do_buffer_writes,
2263 st_printk(KERN_INFO, STp,
2266 STp->can_bsr, STp->two_fm, STp->fast_mteom,
2267 STp->do_auto_lock);
2268 st_printk(KERN_INFO, STp,
2271 STm->defaults_for_writes, STp->omit_blklims,
2272 STp->can_partitions, STp->scsi2_logical);
2273 st_printk(KERN_INFO, STp,
2276 STm->sysv, STp->immediate, STp->sili,
2277 STp->immediate_filemark);
2278 st_printk(KERN_INFO, STp, " debugging: %d\n", debugging);
2284 static int st_set_options(struct scsi_tape *STp, long options)
2292 STm = &(STp->modes[STp->current_mode]);
2298 memcpy(STm, &(STp->modes[0]), sizeof(struct st_modedef));
2304 DEBC_printk(STp, "Initialized mode %d definition from mode 0\n",
2305 STp->current_mode);
2314 STp->two_fm = (options & MT_ST_TWO_FM) != 0;
2315 STp->fast_mteom = (options & MT_ST_FAST_MTEOM) != 0;
2316 STp->do_auto_lock = (options & MT_ST_AUTO_LOCK) != 0;
2317 STp->can_bsr = (options & MT_ST_CAN_BSR) != 0;
2318 STp->omit_blklims = (options & MT_ST_NO_BLKLIMS) != 0;
2319 if ((STp->device)->scsi_level >= SCSI_2)
2320 STp->can_partitions = (options & MT_ST_CAN_PARTITIONS) != 0;
2321 STp->scsi2_logical = (options & MT_ST_SCSI2LOGICAL) != 0;
2322 STp->immediate = (options & MT_ST_NOWAIT) != 0;
2323 STp->immediate_filemark = (options & MT_ST_NOWAIT_EOF) != 0;
2325 STp->sili = (options & MT_ST_SILI) != 0;
2327 st_log_options(STp, STm); )
2339 STp->two_fm = value;
2341 STp->fast_mteom = value;
2343 STp->do_auto_lock = value;
2345 STp->can_bsr = value;
2347 STp->omit_blklims = value;
2348 if ((STp->device)->scsi_level >= SCSI_2 &&
2350 STp->can_partitions = value;
2352 STp->scsi2_logical = value;
2354 STp->immediate = value;
2356 STp->immediate_filemark = value;
2360 STp->sili = value;
2364 st_log_options(STp, STm); )
2371 DEBC_printk(STp, "Default block size disabled.\n");
2374 DEBC_printk(STp,"Default block size set to "
2376 if (STp->ready == ST_READY) {
2377 STp->blksize_changed = 0;
2378 set_mode_densblk(STp, STm);
2384 STp->long_timeout = (value & ~MT_ST_SET_LONG_TIMEOUT) * HZ;
2385 DEBC_printk(STp, "Long timeout set to %d seconds.\n",
2388 blk_queue_rq_timeout(STp->device->request_queue,
2390 DEBC_printk(STp, "Normal timeout set to %d seconds.\n",
2399 STp->cln_mode = value;
2400 STp->cln_sense_mask = (options >> 8) & 0xff;
2401 STp->cln_sense_value = (options >> 16) & 0xff;
2402 st_printk(KERN_INFO, STp,
2404 value, STp->cln_sense_mask, STp->cln_sense_value);
2411 DEBC_printk(STp,
2415 DEBC_printk(STp, "Density default set to %x\n",
2417 if (STp->ready == ST_READY) {
2418 STp->density_changed = 0;
2419 set_mode_densblk(STp, STm);
2424 STp->default_drvbuffer = 0xff;
2425 DEBC_printk(STp,
2428 STp->default_drvbuffer = value & 7;
2429 DEBC_printk(STp,
2431 STp->default_drvbuffer);
2432 if (STp->ready == ST_READY)
2433 st_int_ioctl(STp, MTSETDRVBUFFER, STp->default_drvbuffer);
2438 DEBC_printk(STp,
2442 STp->c_algo = (value & 0xff00) >> 8;
2443 DEBC_printk(STp, "Compression "
2445 STp->c_algo);
2449 DEBC_printk(STp, "Compression default "
2452 if (STp->ready == ST_READY) {
2453 STp->compression_changed = 0;
2454 st_compression(STp, (STm->default_compression == ST_YES));
2488 static int read_mode_page(struct scsi_tape *STp, int page, int omit_block_descs)
2500 SRpnt = st_do_scsi(NULL, STp, cmd, cmd[4], DMA_FROM_DEVICE,
2501 STp->device->request_queue->rq_timeout, 0, 1);
2503 return (STp->buffer)->syscall_result;
2507 return STp->buffer->syscall_result;
2513 static int write_mode_page(struct scsi_tape *STp, int page, int slow)
2523 pgo = MODE_HEADER_LENGTH + (STp->buffer)->b_data[MH_OFF_BDESCS_LENGTH];
2524 cmd[4] = pgo + (STp->buffer)->b_data[pgo + MP_OFF_PAGE_LENGTH] + 2;
2527 (STp->buffer)->b_data[MH_OFF_DATA_LENGTH] = 0;
2528 (STp->buffer)->b_data[MH_OFF_MEDIUM_TYPE] = 0;
2529 (STp->buffer)->b_data[MH_OFF_DEV_SPECIFIC] &= ~MH_BIT_WP;
2530 (STp->buffer)->b_data[pgo + MP_OFF_PAGE_NBR] &= MP_MSK_PAGE_NBR;
2533 STp->long_timeout : STp->device->request_queue->rq_timeout;
2534 SRpnt = st_do_scsi(NULL, STp, cmd, cmd[4], DMA_TO_DEVICE,
2537 return (STp->buffer)->syscall_result;
2541 return STp->buffer->syscall_result;
2562 static int st_compression(struct scsi_tape * STp, int state)
2566 unsigned char *b_data = (STp->buffer)->b_data;
2568 if (STp->ready != ST_READY)
2572 retval = read_mode_page(STp, COMPRESSION_PAGE, 0);
2574 DEBC_printk(STp, "Compression mode page not supported.\n");
2579 DEBC_printk(STp, "Compression state is %d.\n",
2584 DEBC_printk(STp, "Compression not supported.\n");
2591 if (STp->c_algo != 0)
2592 b_data[mpoffs + CP_OFF_C_ALGO] = STp->c_algo;
2596 if (STp->c_algo != 0)
2600 retval = write_mode_page(STp, COMPRESSION_PAGE, 0);
2602 DEBC_printk(STp, "Compression change failed.\n");
2605 DEBC_printk(STp, "Compression state changed to %d.\n", state);
2607 STp->compression_changed = 1;
2613 static int do_load_unload(struct scsi_tape *STp, struct file *filp, int load_code)
2620 if (STp->ready != ST_READY && !load_code) {
2621 if (STp->ready == ST_NO_TAPE)
2636 DEBC_printk(STp, " Enhanced %sload slot %2d.\n",
2641 if (STp->immediate) {
2643 timeout = STp->device->request_queue->rq_timeout;
2646 timeout = STp->long_timeout;
2650 st_printk(ST_DEB_MSG, STp, "Unloading tape.\n");
2652 st_printk(ST_DEB_MSG, STp, "Loading tape.\n");
2655 SRpnt = st_do_scsi(NULL, STp, cmd, 0, DMA_NONE,
2658 return (STp->buffer)->syscall_result;
2660 retval = (STp->buffer)->syscall_result;
2666 STp->rew_at_close = 0;
2667 STp->ready = ST_NO_TAPE;
2670 STp->rew_at_close = STp->autorew_dev;
2671 retval = check_tape(STp, filp);
2677 STps = &(STp->ps[STp->partition]);
2688 static void deb_space_print(struct scsi_tape *STp, int direction, char *units, unsigned char *cmd)
2698 st_printk(ST_DEB_MSG, STp, "Spacing tape %s over %d %s.\n",
2704 static void deb_space_print(struct scsi_tape *STp, int direction, char *units, unsigned char *cmd) {}
2709 static int st_int_ioctl(struct scsi_tape *STp, unsigned int cmd_in, unsigned long arg)
2721 WARN_ON(STp->buffer->do_dio != 0);
2722 if (STp->ready != ST_READY) {
2723 if (STp->ready == ST_NO_TAPE)
2728 timeout = STp->long_timeout;
2729 STps = &(STp->ps[STp->partition]);
2745 deb_space_print(STp, ST_DEB_FORWARD, "filemarks", cmd);
2761 deb_space_print(STp, ST_DEB_BACKWARD, "filemarks", cmd);
2773 deb_space_print(STp, ST_DEB_FORWARD, "blocks", cmd);
2785 deb_space_print(STp, ST_DEB_BACKWARD, "blocks", cmd);
2796 deb_space_print(STp, ST_DEB_FORWARD, "setmarks", cmd);
2809 deb_space_print(STp, ST_DEB_BACKWARD, "setmarks", cmd);
2818 if (STp->write_prot)
2824 (cmd_in == MTWEOF && STp->immediate_filemark))
2829 timeout = STp->device->request_queue->rq_timeout;
2832 st_printk(ST_DEB_MSG, STp,
2838 st_printk(ST_DEB_MSG, STp,
2851 if (STp->immediate) {
2853 timeout = STp->device->request_queue->rq_timeout;
2855 DEBC_printk(STp, "Rewinding tape.\n");
2859 DEBC_printk(STp, "No op on tape.\n");
2863 if (STp->immediate) {
2865 timeout = STp->device->request_queue->rq_timeout;
2868 DEBC_printk(STp, "Retensioning tape.\n");
2872 if (!STp->fast_mteom) {
2874 ioctl_result = st_int_ioctl(STp, MTFSF, 0x7fffff);
2886 DEBC_printk(STp, "Spacing to end of recorded medium.\n");
2891 if (STp->write_prot)
2895 if (STp->immediate) {
2897 timeout = STp->device->request_queue->rq_timeout;
2900 timeout = STp->long_timeout * 8;
2902 DEBC_printk(STp, "Erasing tape.\n");
2910 if (STp->dirty || (STp->buffer)->buffer_bytes != 0)
2914 STp->max_block > 0 &&
2915 ((arg & MT_ST_BLKSIZE_MASK) < STp->min_block ||
2916 (arg & MT_ST_BLKSIZE_MASK) > STp->max_block)) {
2917 st_printk(KERN_WARNING, STp, "Illegal block size.\n");
2921 if ((STp->use_pf & USE_PF))
2926 memset((STp->buffer)->b_data, 0, 12);
2928 (STp->buffer)->b_data[2] = (arg & 7) << 4;
2930 (STp->buffer)->b_data[2] =
2931 STp->drv_buffer << 4;
2932 (STp->buffer)->b_data[3] = 8; /* block descriptor length */
2934 (STp->buffer)->b_data[4] = arg;
2935 STp->density_changed = 1; /* At least we tried ;-) */
2937 (STp->buffer)->b_data[4] = arg >> 24;
2939 (STp->buffer)->b_data[4] = STp->density;
2943 STp->blksize_changed = 1; /* At least we tried ;-) */
2945 ltmp = STp->block_size;
2946 (STp->buffer)->b_data[9] = (ltmp >> 16);
2947 (STp->buffer)->b_data[10] = (ltmp >> 8);
2948 (STp->buffer)->b_data[11] = ltmp;
2949 timeout = STp->device->request_queue->rq_timeout;
2952 st_printk(ST_DEB_MSG, STp,
2954 (STp->buffer)->b_data[9] * 65536 +
2955 (STp->buffer)->b_data[10] * 256 +
2956 (STp->buffer)->b_data[11]);
2958 st_printk(ST_DEB_MSG, STp,
2960 (STp->buffer)->b_data[4]);
2962 st_printk(ST_DEB_MSG, STp,
2964 ((STp->buffer)->b_data[2] >> 4) & 7);
2971 SRpnt = st_do_scsi(NULL, STp, cmd, datalen, direction,
2974 return (STp->buffer)->syscall_result;
2976 ioctl_result = (STp->buffer)->syscall_result;
2986 ioctl_result = st_int_ioctl(STp, MTFSF, 1);
2988 ioctl_result = st_int_ioctl(STp, MTBSF, 1);
2991 STp->block_size = arg & MT_ST_BLKSIZE_MASK;
2992 if (STp->block_size != 0) {
2993 (STp->buffer)->buffer_blocks =
2994 (STp->buffer)->buffer_size / STp->block_size;
2996 (STp->buffer)->buffer_bytes = (STp->buffer)->read_pointer = 0;
2998 STp->density = arg >> MT_ST_DENSITY_SHIFT;
3000 STp->drv_buffer = (arg & 7);
3002 STp->density = arg;
3015 struct st_cmdstatus *cmdstatp = &STp->buffer->cmdstat;
3090 !(STp->use_pf & PF_TESTED)) {
3093 STp->use_pf = (STp->use_pf ^ USE_PF) | PF_TESTED;
3096 return st_int_ioctl(STp, cmd_in, arg);
3116 static int get_location(struct scsi_tape *STp, unsigned int *block, int *partition,
3123 if (STp->ready != ST_READY)
3127 if ((STp->device)->scsi_level < SCSI_2) {
3132 if (!logical && !STp->scsi2_logical)
3135 SRpnt = st_do_scsi(NULL, STp, scmd, 20, DMA_FROM_DEVICE,
3136 STp->device->request_queue->rq_timeout,
3139 return (STp->buffer)->syscall_result;
3141 if ((STp->buffer)->syscall_result != 0 ||
3142 (STp->device->scsi_level >= SCSI_2 &&
3143 ((STp->buffer)->b_data[0] & 4) != 0)) {
3145 DEBC_printk(STp, " Can't read tape position.\n");
3149 if ((STp->device)->scsi_level < SCSI_2) {
3150 *block = ((STp->buffer)->b_data[0] << 16)
3151 + ((STp->buffer)->b_data[1] << 8)
3152 + (STp->buffer)->b_data[2];
3155 *block = ((STp->buffer)->b_data[4] << 24)
3156 + ((STp->buffer)->b_data[5] << 16)
3157 + ((STp->buffer)->b_data[6] << 8)
3158 + (STp->buffer)->b_data[7];
3159 *partition = (STp->buffer)->b_data[1];
3160 if (((STp->buffer)->b_data[0] & 0x80) &&
3161 (STp->buffer)->b_data[1] == 0) /* BOP of partition 0 */
3162 STp->ps[0].drv_block = STp->ps[0].drv_file = 0;
3164 DEBC_printk(STp, "Got tape pos. blk %d part %d.\n",
3176 static int set_location(struct scsi_tape *STp, unsigned int block, int partition,
3186 if (STp->ready != ST_READY)
3188 timeout = STp->long_timeout;
3189 STps = &(STp->ps[STp->partition]);
3191 DEBC_printk(STp, "Setting block to %d and partition to %d.\n",
3197 if ((!STp->can_partitions && partition != 0) ||
3200 if (partition != STp->partition) {
3201 if (get_location(STp, &blk, &p, 1))
3206 DEBC_printk(STp, "Visited block %d for "
3208 blk, STp->partition);
3213 if ((STp->device)->scsi_level < SCSI_2) {
3225 if (!logical && !STp->scsi2_logical)
3227 if (STp->partition != partition) {
3230 DEBC_printk(STp, "Trying to change partition "
3231 "from %d to %d\n", STp->partition,
3235 if (STp->immediate) {
3237 timeout = STp->device->request_queue->rq_timeout;
3240 SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE,
3243 return (STp->buffer)->syscall_result;
3247 if ((STp->buffer)->syscall_result != 0) {
3249 if (STp->can_partitions &&
3250 (STp->device)->scsi_level >= SCSI_2 &&
3251 (p = find_partition(STp)) >= 0)
3252 STp->partition = p;
3254 if (STp->can_partitions) {
3255 STp->partition = partition;
3256 STps = &(STp->ps[partition]);
3278 static int find_partition(struct scsi_tape *STp)
3283 if ((i = get_location(STp, &block, &partition, 1)) < 0)
3292 static int switch_partition(struct scsi_tape *STp)
3296 if (STp->partition == STp->new_partition)
3298 STps = &(STp->ps[STp->new_partition]);
3301 return set_location(STp, STps->last_block_visited, STp->new_partition, 1);
3324 static int nbr_partitions(struct scsi_tape *STp)
3328 if (STp->ready != ST_READY)
3331 result = read_mode_page(STp, PART_PAGE, 1);
3334 DEBC_printk(STp, "Can't read medium partition page.\n");
3337 result = (STp->buffer)->b_data[MODE_HEADER_LENGTH +
3339 DEBC_printk(STp, "Number of partitions %d.\n", result);
3346 static int format_medium(struct scsi_tape *STp, int format)
3349 int timeout = STp->long_timeout;
3356 if (STp->immediate) {
3358 timeout = STp->device->request_queue->rq_timeout;
3360 DEBC_printk(STp, "Sending FORMAT MEDIUM\n");
3361 SRpnt = st_do_scsi(NULL, STp, scmd, 0, DMA_NONE,
3364 result = STp->buffer->syscall_result;
3390 static int partition_tape(struct scsi_tape *STp, int size)
3394 bool scsi3 = STp->device->scsi_level >= SCSI_3, needs_format = false;
3399 result = read_mode_page(STp, PART_PAGE, 0);
3401 DEBC_printk(STp, "Can't read partition mode page.\n");
3411 bp = (STp->buffer)->b_data;
3413 DEBC_printk(STp, "Partition page length is %d bytes.\n",
3424 DEBC_printk(STp, "Formatting tape with one partition.\n");
3425 result = format_medium(STp, 0);
3467 DEBC_printk(STp, "psd_cnt %d, max.parts %d, nbr_parts %d\n",
3475 DEBC_printk(STp, "Formatting tape with one partition.\n");
3484 DEBC_printk(STp,
3499 result = write_mode_page(STp, PART_PAGE, 1);
3502 result = format_medium(STp, 1);
3505 st_printk(KERN_INFO, STp, "Partitioning of tape failed.\n");
3523 struct scsi_tape *STp = file->private_data;
3527 if (mutex_lock_interruptible(&STp->lock))
3531 if (debugging && !STp->in_use) {
3532 st_printk(ST_DEB_MSG, STp, "Incorrect device.\n");
3537 STm = &(STp->modes[STp->current_mode]);
3538 STps = &(STp->ps[STp->partition]);
3546 retval = scsi_ioctl_block_when_processing_errors(STp->device, cmd_in,
3569 st_printk(KERN_WARNING, STp,
3581 if (!STp->pos_unknown) {
3599 i = !STp->can_partitions ||
3600 (STp->new_partition != STp->partition);
3609 i = flush_buffer(STp, i);
3618 i = st_int_ioctl(STp, MTWEOF, 1);
3643 reset_state(STp);
3645 STp->device->was_reset = 0;
3653 if (mtc.mt_op == MTOFFL && STp->door_locked != ST_UNLOCKED)
3654 do_door_lock(STp, 0); /* Ignore result! */
3658 retval = st_set_options(STp, mtc.mt_count);
3663 if (!STp->can_partitions ||
3668 if (mtc.mt_count >= STp->nbr_partitions &&
3669 (STp->nbr_partitions = nbr_partitions(STp)) < 0) {
3673 if (mtc.mt_count >= STp->nbr_partitions) {
3677 STp->new_partition = mtc.mt_count;
3683 if (!STp->can_partitions) {
3687 i = do_load_unload(STp, file, 1);
3692 i = partition_tape(STp, mtc.mt_count);
3698 STp->ps[i].rw = ST_IDLE;
3699 STp->ps[i].at_sm = 0;
3700 STp->ps[i].last_block_valid = 0;
3702 STp->partition = STp->new_partition = 0;
3703 STp->nbr_partitions = mtc.mt_count != 0 ? 2 : 1;
3710 i = set_location(STp, mtc.mt_count, STp->new_partition, 0);
3711 if (!STp->can_partitions)
3712 STp->ps[0].rw = ST_IDLE;
3718 retval = do_load_unload(STp, file, 0);
3723 retval = do_load_unload(STp, file, max(1, mtc.mt_count));
3728 retval = do_door_lock(STp, (mtc.mt_op == MTLOCK));
3732 if (STp->can_partitions && STp->ready == ST_READY &&
3733 (i = switch_partition(STp)) < 0) {
3739 retval = st_compression(STp, (mtc.mt_count & 1));
3741 retval = st_int_ioctl(STp, mtc.mt_op, mtc.mt_count);
3749 if ((i = flush_buffer(STp, 0)) < 0) {
3753 if (STp->can_partitions &&
3754 (i = switch_partition(STp)) < 0) {
3767 mt_status.mt_type = STp->tape_type;
3769 ((STp->block_size << MT_ST_BLKSIZE_SHIFT) & MT_ST_BLKSIZE_MASK) |
3770 ((STp->density << MT_ST_DENSITY_SHIFT) & MT_ST_DENSITY_MASK);
3773 if (STp->block_size != 0) {
3776 (STp->buffer)->buffer_bytes / STp->block_size;
3779 ((STp->buffer)->buffer_bytes +
3780 STp->block_size - 1) / STp->block_size;
3784 if (STp->drv_write_prot)
3792 mt_status.mt_erreg = (STp->recover_reg << MT_ST_SOFTERR_SHIFT);
3793 mt_status.mt_resid = STp->partition;
3798 if (STp->density == 1)
3800 else if (STp->density == 2)
3802 else if (STp->density == 3)
3804 if (STp->ready == ST_READY)
3806 if (STp->ready == ST_NO_TAPE)
3811 (STm->do_buffer_writes && STp->block_size != 0) ||
3812 STp->drv_buffer != 0)
3814 if (STp->cleaning_req)
3821 STp->recover_reg = 0; /* Clear after read */
3830 if ((i = get_location(STp, &blk, &bt, 0)) < 0) {
3838 mutex_unlock(&STp->lock);
3851 retval = scsi_ioctl(STp->device, file->f_mode & FMODE_WRITE, cmd_in, p);
3854 STp->rew_at_close = 0;
3855 STp->ready = ST_NO_TAPE;
3860 mutex_unlock(&STp->lock);
4636 struct scsi_tape *STp = STm->tape;
4644 options |= STp->two_fm ? MT_ST_TWO_FM : 0;
4645 options |= STp->fast_mteom ? MT_ST_FAST_MTEOM : 0;
4647 options |= STp->can_bsr ? MT_ST_CAN_BSR : 0;
4648 options |= STp->omit_blklims ? MT_ST_NO_BLKLIMS : 0;
4649 options |= STp->can_partitions ? MT_ST_CAN_PARTITIONS : 0;
4650 options |= STp->scsi2_logical ? MT_ST_SCSI2LOGICAL : 0;
4652 options |= STp->immediate ? MT_ST_NOWAIT : 0;
4653 options |= STp->immediate_filemark ? MT_ST_NOWAIT_EOF : 0;
4654 options |= STp->sili ? MT_ST_SILI : 0;