2 * standard tape device functions for ibm tapes.
4 * S390 and zSeries version
5 * Copyright IBM Corp. 2001, 2002
6 * Author(s): Carsten Otte <cotte@de.ibm.com>
7 * Michael Holzheu <holzheu@de.ibm.com>
8 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
9 * Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Stefan Bader <shbader@de.ibm.com>
13 #define KMSG_COMPONENT "tape"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/stddef.h>
17 #include <linux/kernel.h>
18 #include <linux/bio.h>
19 #include <linux/timer.h>
21 #include <asm/types.h>
22 #include <asm/idals.h>
23 #include <asm/ebcdic.h>
24 #include <asm/tape390.h>
26 #define TAPE_DBF_AREA tape_core_dbf
35 tape_std_assign_timeout(unsigned long data
)
37 struct tape_request
* request
;
38 struct tape_device
* device
;
41 request
= (struct tape_request
*) data
;
42 device
= request
->device
;
45 DBF_EVENT(3, "%08x: Assignment timeout. Device busy.\n",
47 rc
= tape_cancel_io(device
, request
);
49 DBF_EVENT(3, "(%08x): Assign timeout: Cancel failed with rc = "
50 "%i\n", device
->cdev_id
, rc
);
54 tape_std_assign(struct tape_device
*device
)
57 struct timer_list timeout
;
58 struct tape_request
*request
;
60 request
= tape_alloc_request(2, 11);
62 return PTR_ERR(request
);
64 request
->op
= TO_ASSIGN
;
65 tape_ccw_cc(request
->cpaddr
, ASSIGN
, 11, request
->cpdata
);
66 tape_ccw_end(request
->cpaddr
+ 1, NOP
, 0, NULL
);
69 * The assign command sometimes blocks if the device is assigned
70 * to another host (actually this shouldn't happen but it does).
71 * So we set up a timeout for this call.
73 init_timer_on_stack(&timeout
);
74 timeout
.function
= tape_std_assign_timeout
;
75 timeout
.data
= (unsigned long) request
;
76 timeout
.expires
= jiffies
+ 2 * HZ
;
79 rc
= tape_do_io_interruptible(device
, request
);
84 DBF_EVENT(3, "%08x: assign failed - device might be busy\n",
87 DBF_EVENT(3, "%08x: Tape assigned\n", device
->cdev_id
);
89 tape_free_request(request
);
97 tape_std_unassign (struct tape_device
*device
)
100 struct tape_request
*request
;
102 if (device
->tape_state
== TS_NOT_OPER
) {
103 DBF_EVENT(3, "(%08x): Can't unassign device\n",
108 request
= tape_alloc_request(2, 11);
110 return PTR_ERR(request
);
112 request
->op
= TO_UNASSIGN
;
113 tape_ccw_cc(request
->cpaddr
, UNASSIGN
, 11, request
->cpdata
);
114 tape_ccw_end(request
->cpaddr
+ 1, NOP
, 0, NULL
);
116 if ((rc
= tape_do_io(device
, request
)) != 0) {
117 DBF_EVENT(3, "%08x: Unassign failed\n", device
->cdev_id
);
119 DBF_EVENT(3, "%08x: Tape unassigned\n", device
->cdev_id
);
121 tape_free_request(request
);
126 * TAPE390_DISPLAY: Show a string on the tape display.
129 tape_std_display(struct tape_device
*device
, struct display_struct
*disp
)
131 struct tape_request
*request
;
134 request
= tape_alloc_request(2, 17);
135 if (IS_ERR(request
)) {
136 DBF_EVENT(3, "TAPE: load display failed\n");
137 return PTR_ERR(request
);
139 request
->op
= TO_DIS
;
141 *(unsigned char *) request
->cpdata
= disp
->cntrl
;
142 DBF_EVENT(5, "TAPE: display cntrl=%04x\n", disp
->cntrl
);
143 memcpy(((unsigned char *) request
->cpdata
) + 1, disp
->message1
, 8);
144 memcpy(((unsigned char *) request
->cpdata
) + 9, disp
->message2
, 8);
145 ASCEBC(((unsigned char*) request
->cpdata
) + 1, 16);
147 tape_ccw_cc(request
->cpaddr
, LOAD_DISPLAY
, 17, request
->cpdata
);
148 tape_ccw_end(request
->cpaddr
+ 1, NOP
, 0, NULL
);
150 rc
= tape_do_io_interruptible(device
, request
);
151 tape_free_request(request
);
159 tape_std_read_block_id(struct tape_device
*device
, __u64
*id
)
161 struct tape_request
*request
;
164 request
= tape_alloc_request(3, 8);
166 return PTR_ERR(request
);
167 request
->op
= TO_RBI
;
169 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
170 tape_ccw_cc(request
->cpaddr
+ 1, READ_BLOCK_ID
, 8, request
->cpdata
);
171 tape_ccw_end(request
->cpaddr
+ 2, NOP
, 0, NULL
);
173 rc
= tape_do_io(device
, request
);
175 /* Get result from read buffer. */
176 *id
= *(__u64
*) request
->cpdata
;
177 tape_free_request(request
);
182 tape_std_terminate_write(struct tape_device
*device
)
186 if(device
->required_tapemarks
== 0)
189 DBF_LH(5, "tape%d: terminate write %dxEOF\n", device
->first_minor
,
190 device
->required_tapemarks
);
192 rc
= tape_mtop(device
, MTWEOF
, device
->required_tapemarks
);
196 device
->required_tapemarks
= 0;
197 return tape_mtop(device
, MTBSR
, 1);
201 * MTLOAD: Loads the tape.
202 * The default implementation just wait until the tape medium state changes
206 tape_std_mtload(struct tape_device
*device
, int count
)
208 return wait_event_interruptible(device
->state_change_wq
,
209 (device
->medium_state
== MS_LOADED
));
213 * MTSETBLK: Set block size.
216 tape_std_mtsetblk(struct tape_device
*device
, int count
)
218 struct idal_buffer
*new;
220 DBF_LH(6, "tape_std_mtsetblk(%d)\n", count
);
223 * Just set block_size to 0. tapechar_read/tapechar_write
224 * will realloc the idal buffer if a bigger one than the
227 device
->char_data
.block_size
= 0;
230 if (device
->char_data
.idal_buf
!= NULL
&&
231 device
->char_data
.idal_buf
->size
== count
)
232 /* We already have a idal buffer of that size. */
235 if (count
> MAX_BLOCKSIZE
) {
236 DBF_EVENT(3, "Invalid block size (%d > %d) given.\n",
237 count
, MAX_BLOCKSIZE
);
241 /* Allocate a new idal buffer. */
242 new = idal_buffer_alloc(count
, 0);
245 if (device
->char_data
.idal_buf
!= NULL
)
246 idal_buffer_free(device
->char_data
.idal_buf
);
247 device
->char_data
.idal_buf
= new;
248 device
->char_data
.block_size
= count
;
250 DBF_LH(6, "new blocksize is %d\n", device
->char_data
.block_size
);
256 * MTRESET: Set block size to 0.
259 tape_std_mtreset(struct tape_device
*device
, int count
)
261 DBF_EVENT(6, "TCHAR:devreset:\n");
262 device
->char_data
.block_size
= 0;
267 * MTFSF: Forward space over 'count' file marks. The tape is positioned
268 * at the EOT (End of Tape) side of the file mark.
271 tape_std_mtfsf(struct tape_device
*device
, int mt_count
)
273 struct tape_request
*request
;
276 request
= tape_alloc_request(mt_count
+ 2, 0);
278 return PTR_ERR(request
);
279 request
->op
= TO_FSF
;
281 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
282 device
->modeset_byte
);
283 ccw
= tape_ccw_repeat(ccw
, FORSPACEFILE
, mt_count
);
284 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
287 return tape_do_io_free(device
, request
);
291 * MTFSR: Forward space over 'count' tape blocks (blocksize is set
295 tape_std_mtfsr(struct tape_device
*device
, int mt_count
)
297 struct tape_request
*request
;
301 request
= tape_alloc_request(mt_count
+ 2, 0);
303 return PTR_ERR(request
);
304 request
->op
= TO_FSB
;
306 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
307 device
->modeset_byte
);
308 ccw
= tape_ccw_repeat(ccw
, FORSPACEBLOCK
, mt_count
);
309 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
312 rc
= tape_do_io(device
, request
);
313 if (rc
== 0 && request
->rescnt
> 0) {
314 DBF_LH(3, "FSR over tapemark\n");
317 tape_free_request(request
);
323 * MTBSR: Backward space over 'count' tape blocks.
324 * (blocksize is set via MTSETBLK.
327 tape_std_mtbsr(struct tape_device
*device
, int mt_count
)
329 struct tape_request
*request
;
333 request
= tape_alloc_request(mt_count
+ 2, 0);
335 return PTR_ERR(request
);
336 request
->op
= TO_BSB
;
338 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
339 device
->modeset_byte
);
340 ccw
= tape_ccw_repeat(ccw
, BACKSPACEBLOCK
, mt_count
);
341 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
344 rc
= tape_do_io(device
, request
);
345 if (rc
== 0 && request
->rescnt
> 0) {
346 DBF_LH(3, "BSR over tapemark\n");
349 tape_free_request(request
);
355 * MTWEOF: Write 'count' file marks at the current position.
358 tape_std_mtweof(struct tape_device
*device
, int mt_count
)
360 struct tape_request
*request
;
363 request
= tape_alloc_request(mt_count
+ 2, 0);
365 return PTR_ERR(request
);
366 request
->op
= TO_WTM
;
368 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
369 device
->modeset_byte
);
370 ccw
= tape_ccw_repeat(ccw
, WRITETAPEMARK
, mt_count
);
371 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
374 return tape_do_io_free(device
, request
);
378 * MTBSFM: Backward space over 'count' file marks.
379 * The tape is positioned at the BOT (Begin Of Tape) side of the
380 * last skipped file mark.
383 tape_std_mtbsfm(struct tape_device
*device
, int mt_count
)
385 struct tape_request
*request
;
388 request
= tape_alloc_request(mt_count
+ 2, 0);
390 return PTR_ERR(request
);
391 request
->op
= TO_BSF
;
393 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
394 device
->modeset_byte
);
395 ccw
= tape_ccw_repeat(ccw
, BACKSPACEFILE
, mt_count
);
396 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
399 return tape_do_io_free(device
, request
);
403 * MTBSF: Backward space over 'count' file marks. The tape is positioned at
404 * the EOT (End of Tape) side of the last skipped file mark.
407 tape_std_mtbsf(struct tape_device
*device
, int mt_count
)
409 struct tape_request
*request
;
413 request
= tape_alloc_request(mt_count
+ 2, 0);
415 return PTR_ERR(request
);
416 request
->op
= TO_BSF
;
418 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
419 device
->modeset_byte
);
420 ccw
= tape_ccw_repeat(ccw
, BACKSPACEFILE
, mt_count
);
421 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
423 rc
= tape_do_io_free(device
, request
);
425 rc
= tape_mtop(device
, MTFSR
, 1);
433 * MTFSFM: Forward space over 'count' file marks.
434 * The tape is positioned at the BOT (Begin Of Tape) side
435 * of the last skipped file mark.
438 tape_std_mtfsfm(struct tape_device
*device
, int mt_count
)
440 struct tape_request
*request
;
444 request
= tape_alloc_request(mt_count
+ 2, 0);
446 return PTR_ERR(request
);
447 request
->op
= TO_FSF
;
449 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
450 device
->modeset_byte
);
451 ccw
= tape_ccw_repeat(ccw
, FORSPACEFILE
, mt_count
);
452 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
454 rc
= tape_do_io_free(device
, request
);
456 rc
= tape_mtop(device
, MTBSR
, 1);
465 * MTREW: Rewind the tape.
468 tape_std_mtrew(struct tape_device
*device
, int mt_count
)
470 struct tape_request
*request
;
472 request
= tape_alloc_request(3, 0);
474 return PTR_ERR(request
);
475 request
->op
= TO_REW
;
477 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
478 device
->modeset_byte
);
479 tape_ccw_cc(request
->cpaddr
+ 1, REWIND
, 0, NULL
);
480 tape_ccw_end(request
->cpaddr
+ 2, NOP
, 0, NULL
);
483 return tape_do_io_free(device
, request
);
487 * MTOFFL: Rewind the tape and put the drive off-line.
488 * Implement 'rewind unload'
491 tape_std_mtoffl(struct tape_device
*device
, int mt_count
)
493 struct tape_request
*request
;
495 request
= tape_alloc_request(3, 0);
497 return PTR_ERR(request
);
498 request
->op
= TO_RUN
;
500 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
501 tape_ccw_cc(request
->cpaddr
+ 1, REWIND_UNLOAD
, 0, NULL
);
502 tape_ccw_end(request
->cpaddr
+ 2, NOP
, 0, NULL
);
505 return tape_do_io_free(device
, request
);
509 * MTNOP: 'No operation'.
512 tape_std_mtnop(struct tape_device
*device
, int mt_count
)
514 struct tape_request
*request
;
516 request
= tape_alloc_request(2, 0);
518 return PTR_ERR(request
);
519 request
->op
= TO_NOP
;
521 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
522 tape_ccw_end(request
->cpaddr
+ 1, NOP
, 0, NULL
);
524 return tape_do_io_free(device
, request
);
528 * MTEOM: positions at the end of the portion of the tape already used
529 * for recordind data. MTEOM positions after the last file mark, ready for
530 * appending another file.
533 tape_std_mteom(struct tape_device
*device
, int mt_count
)
538 * Seek from the beginning of tape (rewind).
540 if ((rc
= tape_mtop(device
, MTREW
, 1)) < 0)
544 * The logical end of volume is given by two sewuential tapemarks.
545 * Look for this by skipping to the next file (over one tapemark)
546 * and then test for another one (fsr returns 1 if a tapemark was
550 if ((rc
= tape_mtop(device
, MTFSF
, 1)) < 0)
552 if ((rc
= tape_mtop(device
, MTFSR
, 1)) < 0)
556 return tape_mtop(device
, MTBSR
, 1);
560 * MTRETEN: Retension the tape, i.e. forward space to end of tape and rewind.
563 tape_std_mtreten(struct tape_device
*device
, int mt_count
)
565 struct tape_request
*request
;
567 request
= tape_alloc_request(4, 0);
569 return PTR_ERR(request
);
570 request
->op
= TO_FSF
;
572 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
573 tape_ccw_cc(request
->cpaddr
+ 1,FORSPACEFILE
, 0, NULL
);
574 tape_ccw_cc(request
->cpaddr
+ 2, NOP
, 0, NULL
);
575 tape_ccw_end(request
->cpaddr
+ 3, CCW_CMD_TIC
, 0, request
->cpaddr
);
576 /* execute it, MTRETEN rc gets ignored */
577 tape_do_io_interruptible(device
, request
);
578 tape_free_request(request
);
579 return tape_mtop(device
, MTREW
, 1);
583 * MTERASE: erases the tape.
586 tape_std_mterase(struct tape_device
*device
, int mt_count
)
588 struct tape_request
*request
;
590 request
= tape_alloc_request(6, 0);
592 return PTR_ERR(request
);
593 request
->op
= TO_DSE
;
595 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
596 tape_ccw_cc(request
->cpaddr
+ 1, REWIND
, 0, NULL
);
597 tape_ccw_cc(request
->cpaddr
+ 2, ERASE_GAP
, 0, NULL
);
598 tape_ccw_cc(request
->cpaddr
+ 3, DATA_SEC_ERASE
, 0, NULL
);
599 tape_ccw_cc(request
->cpaddr
+ 4, REWIND
, 0, NULL
);
600 tape_ccw_end(request
->cpaddr
+ 5, NOP
, 0, NULL
);
603 return tape_do_io_free(device
, request
);
607 * MTUNLOAD: Rewind the tape and unload it.
610 tape_std_mtunload(struct tape_device
*device
, int mt_count
)
612 return tape_mtop(device
, MTOFFL
, mt_count
);
616 * MTCOMPRESSION: used to enable compression.
617 * Sets the IDRC on/off.
620 tape_std_mtcompression(struct tape_device
*device
, int mt_count
)
622 struct tape_request
*request
;
624 if (mt_count
< 0 || mt_count
> 1) {
625 DBF_EXCEPTION(6, "xcom parm\n");
628 request
= tape_alloc_request(2, 0);
630 return PTR_ERR(request
);
631 request
->op
= TO_NOP
;
634 *device
->modeset_byte
&= ~0x08;
636 *device
->modeset_byte
|= 0x08;
637 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
638 tape_ccw_end(request
->cpaddr
+ 1, NOP
, 0, NULL
);
640 return tape_do_io_free(device
, request
);
646 struct tape_request
*
647 tape_std_read_block(struct tape_device
*device
, size_t count
)
649 struct tape_request
*request
;
652 * We have to alloc 4 ccws in order to be able to transform request
653 * into a read backward request in error case.
655 request
= tape_alloc_request(4, 0);
656 if (IS_ERR(request
)) {
657 DBF_EXCEPTION(6, "xrbl fail");
660 request
->op
= TO_RFO
;
661 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
662 tape_ccw_end_idal(request
->cpaddr
+ 1, READ_FORWARD
,
663 device
->char_data
.idal_buf
);
664 DBF_EVENT(6, "xrbl ccwg\n");
669 * Read Block backward transformation function.
672 tape_std_read_backward(struct tape_device
*device
, struct tape_request
*request
)
675 * We have allocated 4 ccws in tape_std_read, so we can now
676 * transform the request to a read backward, followed by a
677 * forward space block.
679 request
->op
= TO_RBA
;
680 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
681 tape_ccw_cc_idal(request
->cpaddr
+ 1, READ_BACKWARD
,
682 device
->char_data
.idal_buf
);
683 tape_ccw_cc(request
->cpaddr
+ 2, FORSPACEBLOCK
, 0, NULL
);
684 tape_ccw_end(request
->cpaddr
+ 3, NOP
, 0, NULL
);
685 DBF_EVENT(6, "xrop ccwg");}
690 struct tape_request
*
691 tape_std_write_block(struct tape_device
*device
, size_t count
)
693 struct tape_request
*request
;
695 request
= tape_alloc_request(2, 0);
696 if (IS_ERR(request
)) {
697 DBF_EXCEPTION(6, "xwbl fail\n");
700 request
->op
= TO_WRI
;
701 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
702 tape_ccw_end_idal(request
->cpaddr
+ 1, WRITE_CMD
,
703 device
->char_data
.idal_buf
);
704 DBF_EVENT(6, "xwbl ccwg\n");
709 * This routine is called by frontend after an ENOSP on write
712 tape_std_process_eov(struct tape_device
*device
)
715 * End of volume: We have to backspace the last written record, then
716 * we TRY to write a tapemark and then backspace over the written TM
718 if (tape_mtop(device
, MTBSR
, 1) == 0 &&
719 tape_mtop(device
, MTWEOF
, 1) == 0) {
720 tape_mtop(device
, MTBSR
, 1);
724 EXPORT_SYMBOL(tape_std_assign
);
725 EXPORT_SYMBOL(tape_std_unassign
);
726 EXPORT_SYMBOL(tape_std_display
);
727 EXPORT_SYMBOL(tape_std_read_block_id
);
728 EXPORT_SYMBOL(tape_std_mtload
);
729 EXPORT_SYMBOL(tape_std_mtsetblk
);
730 EXPORT_SYMBOL(tape_std_mtreset
);
731 EXPORT_SYMBOL(tape_std_mtfsf
);
732 EXPORT_SYMBOL(tape_std_mtfsr
);
733 EXPORT_SYMBOL(tape_std_mtbsr
);
734 EXPORT_SYMBOL(tape_std_mtweof
);
735 EXPORT_SYMBOL(tape_std_mtbsfm
);
736 EXPORT_SYMBOL(tape_std_mtbsf
);
737 EXPORT_SYMBOL(tape_std_mtfsfm
);
738 EXPORT_SYMBOL(tape_std_mtrew
);
739 EXPORT_SYMBOL(tape_std_mtoffl
);
740 EXPORT_SYMBOL(tape_std_mtnop
);
741 EXPORT_SYMBOL(tape_std_mteom
);
742 EXPORT_SYMBOL(tape_std_mtreten
);
743 EXPORT_SYMBOL(tape_std_mterase
);
744 EXPORT_SYMBOL(tape_std_mtunload
);
745 EXPORT_SYMBOL(tape_std_mtcompression
);
746 EXPORT_SYMBOL(tape_std_read_block
);
747 EXPORT_SYMBOL(tape_std_read_backward
);
748 EXPORT_SYMBOL(tape_std_write_block
);
749 EXPORT_SYMBOL(tape_std_process_eov
);