1 // SPDX-License-Identifier: GPL-2.0
3 * standard tape device functions for ibm tapes.
5 * S390 and zSeries version
6 * Copyright IBM Corp. 2001, 2002
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Michael Holzheu <holzheu@de.ibm.com>
9 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
10 * Martin Schwidefsky <schwidefsky@de.ibm.com>
11 * Stefan Bader <shbader@de.ibm.com>
14 #define KMSG_COMPONENT "tape"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17 #include <linux/stddef.h>
18 #include <linux/kernel.h>
19 #include <linux/bio.h>
20 #include <linux/timer.h>
22 #include <asm/types.h>
23 #include <asm/idals.h>
24 #include <asm/ebcdic.h>
25 #include <asm/tape390.h>
27 #define TAPE_DBF_AREA tape_core_dbf
36 tape_std_assign_timeout(struct timer_list
*t
)
38 struct tape_request
* request
= from_timer(request
, t
, timer
);
39 struct tape_device
* device
= request
->device
;
44 DBF_EVENT(3, "%08x: Assignment timeout. Device busy.\n",
46 rc
= tape_cancel_io(device
, request
);
48 DBF_EVENT(3, "(%08x): Assign timeout: Cancel failed with rc = "
49 "%i\n", device
->cdev_id
, rc
);
53 tape_std_assign(struct tape_device
*device
)
56 struct tape_request
*request
;
58 request
= tape_alloc_request(2, 11);
60 return PTR_ERR(request
);
62 request
->op
= TO_ASSIGN
;
63 tape_ccw_cc(request
->cpaddr
, ASSIGN
, 11, request
->cpdata
);
64 tape_ccw_end(request
->cpaddr
+ 1, NOP
, 0, NULL
);
67 * The assign command sometimes blocks if the device is assigned
68 * to another host (actually this shouldn't happen but it does).
69 * So we set up a timeout for this call.
71 timer_setup(&request
->timer
, tape_std_assign_timeout
, 0);
72 mod_timer(&request
->timer
, jiffies
+ msecs_to_jiffies(2000));
74 rc
= tape_do_io_interruptible(device
, request
);
76 del_timer_sync(&request
->timer
);
79 DBF_EVENT(3, "%08x: assign failed - device might be busy\n",
82 DBF_EVENT(3, "%08x: Tape assigned\n", device
->cdev_id
);
84 tape_free_request(request
);
92 tape_std_unassign (struct tape_device
*device
)
95 struct tape_request
*request
;
97 if (device
->tape_state
== TS_NOT_OPER
) {
98 DBF_EVENT(3, "(%08x): Can't unassign device\n",
103 request
= tape_alloc_request(2, 11);
105 return PTR_ERR(request
);
107 request
->op
= TO_UNASSIGN
;
108 tape_ccw_cc(request
->cpaddr
, UNASSIGN
, 11, request
->cpdata
);
109 tape_ccw_end(request
->cpaddr
+ 1, NOP
, 0, NULL
);
111 if ((rc
= tape_do_io(device
, request
)) != 0) {
112 DBF_EVENT(3, "%08x: Unassign failed\n", device
->cdev_id
);
114 DBF_EVENT(3, "%08x: Tape unassigned\n", device
->cdev_id
);
116 tape_free_request(request
);
121 * TAPE390_DISPLAY: Show a string on the tape display.
124 tape_std_display(struct tape_device
*device
, struct display_struct
*disp
)
126 struct tape_request
*request
;
129 request
= tape_alloc_request(2, 17);
130 if (IS_ERR(request
)) {
131 DBF_EVENT(3, "TAPE: load display failed\n");
132 return PTR_ERR(request
);
134 request
->op
= TO_DIS
;
136 *(unsigned char *) request
->cpdata
= disp
->cntrl
;
137 DBF_EVENT(5, "TAPE: display cntrl=%04x\n", disp
->cntrl
);
138 memcpy(((unsigned char *) request
->cpdata
) + 1, disp
->message1
, 8);
139 memcpy(((unsigned char *) request
->cpdata
) + 9, disp
->message2
, 8);
140 ASCEBC(((unsigned char*) request
->cpdata
) + 1, 16);
142 tape_ccw_cc(request
->cpaddr
, LOAD_DISPLAY
, 17, request
->cpdata
);
143 tape_ccw_end(request
->cpaddr
+ 1, NOP
, 0, NULL
);
145 rc
= tape_do_io_interruptible(device
, request
);
146 tape_free_request(request
);
154 tape_std_read_block_id(struct tape_device
*device
, __u64
*id
)
156 struct tape_request
*request
;
159 request
= tape_alloc_request(3, 8);
161 return PTR_ERR(request
);
162 request
->op
= TO_RBI
;
164 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
165 tape_ccw_cc(request
->cpaddr
+ 1, READ_BLOCK_ID
, 8, request
->cpdata
);
166 tape_ccw_end(request
->cpaddr
+ 2, NOP
, 0, NULL
);
168 rc
= tape_do_io(device
, request
);
170 /* Get result from read buffer. */
171 *id
= *(__u64
*) request
->cpdata
;
172 tape_free_request(request
);
177 tape_std_terminate_write(struct tape_device
*device
)
181 if(device
->required_tapemarks
== 0)
184 DBF_LH(5, "tape%d: terminate write %dxEOF\n", device
->first_minor
,
185 device
->required_tapemarks
);
187 rc
= tape_mtop(device
, MTWEOF
, device
->required_tapemarks
);
191 device
->required_tapemarks
= 0;
192 return tape_mtop(device
, MTBSR
, 1);
196 * MTLOAD: Loads the tape.
197 * The default implementation just wait until the tape medium state changes
201 tape_std_mtload(struct tape_device
*device
, int count
)
203 return wait_event_interruptible(device
->state_change_wq
,
204 (device
->medium_state
== MS_LOADED
));
208 * MTSETBLK: Set block size.
211 tape_std_mtsetblk(struct tape_device
*device
, int count
)
213 struct idal_buffer
*new;
215 DBF_LH(6, "tape_std_mtsetblk(%d)\n", count
);
218 * Just set block_size to 0. tapechar_read/tapechar_write
219 * will realloc the idal buffer if a bigger one than the
222 device
->char_data
.block_size
= 0;
225 if (device
->char_data
.idal_buf
!= NULL
&&
226 device
->char_data
.idal_buf
->size
== count
)
227 /* We already have a idal buffer of that size. */
230 if (count
> MAX_BLOCKSIZE
) {
231 DBF_EVENT(3, "Invalid block size (%d > %d) given.\n",
232 count
, MAX_BLOCKSIZE
);
236 /* Allocate a new idal buffer. */
237 new = idal_buffer_alloc(count
, 0);
240 if (device
->char_data
.idal_buf
!= NULL
)
241 idal_buffer_free(device
->char_data
.idal_buf
);
242 device
->char_data
.idal_buf
= new;
243 device
->char_data
.block_size
= count
;
245 DBF_LH(6, "new blocksize is %d\n", device
->char_data
.block_size
);
251 * MTRESET: Set block size to 0.
254 tape_std_mtreset(struct tape_device
*device
, int count
)
256 DBF_EVENT(6, "TCHAR:devreset:\n");
257 device
->char_data
.block_size
= 0;
262 * MTFSF: Forward space over 'count' file marks. The tape is positioned
263 * at the EOT (End of Tape) side of the file mark.
266 tape_std_mtfsf(struct tape_device
*device
, int mt_count
)
268 struct tape_request
*request
;
271 request
= tape_alloc_request(mt_count
+ 2, 0);
273 return PTR_ERR(request
);
274 request
->op
= TO_FSF
;
276 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
277 device
->modeset_byte
);
278 ccw
= tape_ccw_repeat(ccw
, FORSPACEFILE
, mt_count
);
279 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
282 return tape_do_io_free(device
, request
);
286 * MTFSR: Forward space over 'count' tape blocks (blocksize is set
290 tape_std_mtfsr(struct tape_device
*device
, int mt_count
)
292 struct tape_request
*request
;
296 request
= tape_alloc_request(mt_count
+ 2, 0);
298 return PTR_ERR(request
);
299 request
->op
= TO_FSB
;
301 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
302 device
->modeset_byte
);
303 ccw
= tape_ccw_repeat(ccw
, FORSPACEBLOCK
, mt_count
);
304 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
307 rc
= tape_do_io(device
, request
);
308 if (rc
== 0 && request
->rescnt
> 0) {
309 DBF_LH(3, "FSR over tapemark\n");
312 tape_free_request(request
);
318 * MTBSR: Backward space over 'count' tape blocks.
319 * (blocksize is set via MTSETBLK.
322 tape_std_mtbsr(struct tape_device
*device
, int mt_count
)
324 struct tape_request
*request
;
328 request
= tape_alloc_request(mt_count
+ 2, 0);
330 return PTR_ERR(request
);
331 request
->op
= TO_BSB
;
333 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
334 device
->modeset_byte
);
335 ccw
= tape_ccw_repeat(ccw
, BACKSPACEBLOCK
, mt_count
);
336 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
339 rc
= tape_do_io(device
, request
);
340 if (rc
== 0 && request
->rescnt
> 0) {
341 DBF_LH(3, "BSR over tapemark\n");
344 tape_free_request(request
);
350 * MTWEOF: Write 'count' file marks at the current position.
353 tape_std_mtweof(struct tape_device
*device
, int mt_count
)
355 struct tape_request
*request
;
358 request
= tape_alloc_request(mt_count
+ 2, 0);
360 return PTR_ERR(request
);
361 request
->op
= TO_WTM
;
363 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
364 device
->modeset_byte
);
365 ccw
= tape_ccw_repeat(ccw
, WRITETAPEMARK
, mt_count
);
366 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
369 return tape_do_io_free(device
, request
);
373 * MTBSFM: Backward space over 'count' file marks.
374 * The tape is positioned at the BOT (Begin Of Tape) side of the
375 * last skipped file mark.
378 tape_std_mtbsfm(struct tape_device
*device
, int mt_count
)
380 struct tape_request
*request
;
383 request
= tape_alloc_request(mt_count
+ 2, 0);
385 return PTR_ERR(request
);
386 request
->op
= TO_BSF
;
388 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
389 device
->modeset_byte
);
390 ccw
= tape_ccw_repeat(ccw
, BACKSPACEFILE
, mt_count
);
391 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
394 return tape_do_io_free(device
, request
);
398 * MTBSF: Backward space over 'count' file marks. The tape is positioned at
399 * the EOT (End of Tape) side of the last skipped file mark.
402 tape_std_mtbsf(struct tape_device
*device
, int mt_count
)
404 struct tape_request
*request
;
408 request
= tape_alloc_request(mt_count
+ 2, 0);
410 return PTR_ERR(request
);
411 request
->op
= TO_BSF
;
413 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
414 device
->modeset_byte
);
415 ccw
= tape_ccw_repeat(ccw
, BACKSPACEFILE
, mt_count
);
416 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
418 rc
= tape_do_io_free(device
, request
);
420 rc
= tape_mtop(device
, MTFSR
, 1);
428 * MTFSFM: Forward space over 'count' file marks.
429 * The tape is positioned at the BOT (Begin Of Tape) side
430 * of the last skipped file mark.
433 tape_std_mtfsfm(struct tape_device
*device
, int mt_count
)
435 struct tape_request
*request
;
439 request
= tape_alloc_request(mt_count
+ 2, 0);
441 return PTR_ERR(request
);
442 request
->op
= TO_FSF
;
444 ccw
= tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
445 device
->modeset_byte
);
446 ccw
= tape_ccw_repeat(ccw
, FORSPACEFILE
, mt_count
);
447 ccw
= tape_ccw_end(ccw
, NOP
, 0, NULL
);
449 rc
= tape_do_io_free(device
, request
);
451 rc
= tape_mtop(device
, MTBSR
, 1);
460 * MTREW: Rewind the tape.
463 tape_std_mtrew(struct tape_device
*device
, int mt_count
)
465 struct tape_request
*request
;
467 request
= tape_alloc_request(3, 0);
469 return PTR_ERR(request
);
470 request
->op
= TO_REW
;
472 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1,
473 device
->modeset_byte
);
474 tape_ccw_cc(request
->cpaddr
+ 1, REWIND
, 0, NULL
);
475 tape_ccw_end(request
->cpaddr
+ 2, NOP
, 0, NULL
);
478 return tape_do_io_free(device
, request
);
482 * MTOFFL: Rewind the tape and put the drive off-line.
483 * Implement 'rewind unload'
486 tape_std_mtoffl(struct tape_device
*device
, int mt_count
)
488 struct tape_request
*request
;
490 request
= tape_alloc_request(3, 0);
492 return PTR_ERR(request
);
493 request
->op
= TO_RUN
;
495 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
496 tape_ccw_cc(request
->cpaddr
+ 1, REWIND_UNLOAD
, 0, NULL
);
497 tape_ccw_end(request
->cpaddr
+ 2, NOP
, 0, NULL
);
500 return tape_do_io_free(device
, request
);
504 * MTNOP: 'No operation'.
507 tape_std_mtnop(struct tape_device
*device
, int mt_count
)
509 struct tape_request
*request
;
511 request
= tape_alloc_request(2, 0);
513 return PTR_ERR(request
);
514 request
->op
= TO_NOP
;
516 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
517 tape_ccw_end(request
->cpaddr
+ 1, NOP
, 0, NULL
);
519 return tape_do_io_free(device
, request
);
523 * MTEOM: positions at the end of the portion of the tape already used
524 * for recordind data. MTEOM positions after the last file mark, ready for
525 * appending another file.
528 tape_std_mteom(struct tape_device
*device
, int mt_count
)
533 * Seek from the beginning of tape (rewind).
535 if ((rc
= tape_mtop(device
, MTREW
, 1)) < 0)
539 * The logical end of volume is given by two sewuential tapemarks.
540 * Look for this by skipping to the next file (over one tapemark)
541 * and then test for another one (fsr returns 1 if a tapemark was
545 if ((rc
= tape_mtop(device
, MTFSF
, 1)) < 0)
547 if ((rc
= tape_mtop(device
, MTFSR
, 1)) < 0)
551 return tape_mtop(device
, MTBSR
, 1);
555 * MTRETEN: Retension the tape, i.e. forward space to end of tape and rewind.
558 tape_std_mtreten(struct tape_device
*device
, int mt_count
)
560 struct tape_request
*request
;
562 request
= tape_alloc_request(4, 0);
564 return PTR_ERR(request
);
565 request
->op
= TO_FSF
;
567 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
568 tape_ccw_cc(request
->cpaddr
+ 1,FORSPACEFILE
, 0, NULL
);
569 tape_ccw_cc(request
->cpaddr
+ 2, NOP
, 0, NULL
);
570 tape_ccw_end(request
->cpaddr
+ 3, CCW_CMD_TIC
, 0, request
->cpaddr
);
571 /* execute it, MTRETEN rc gets ignored */
572 tape_do_io_interruptible(device
, request
);
573 tape_free_request(request
);
574 return tape_mtop(device
, MTREW
, 1);
578 * MTERASE: erases the tape.
581 tape_std_mterase(struct tape_device
*device
, int mt_count
)
583 struct tape_request
*request
;
585 request
= tape_alloc_request(6, 0);
587 return PTR_ERR(request
);
588 request
->op
= TO_DSE
;
590 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
591 tape_ccw_cc(request
->cpaddr
+ 1, REWIND
, 0, NULL
);
592 tape_ccw_cc(request
->cpaddr
+ 2, ERASE_GAP
, 0, NULL
);
593 tape_ccw_cc(request
->cpaddr
+ 3, DATA_SEC_ERASE
, 0, NULL
);
594 tape_ccw_cc(request
->cpaddr
+ 4, REWIND
, 0, NULL
);
595 tape_ccw_end(request
->cpaddr
+ 5, NOP
, 0, NULL
);
598 return tape_do_io_free(device
, request
);
602 * MTUNLOAD: Rewind the tape and unload it.
605 tape_std_mtunload(struct tape_device
*device
, int mt_count
)
607 return tape_mtop(device
, MTOFFL
, mt_count
);
611 * MTCOMPRESSION: used to enable compression.
612 * Sets the IDRC on/off.
615 tape_std_mtcompression(struct tape_device
*device
, int mt_count
)
617 struct tape_request
*request
;
619 if (mt_count
< 0 || mt_count
> 1) {
620 DBF_EXCEPTION(6, "xcom parm\n");
623 request
= tape_alloc_request(2, 0);
625 return PTR_ERR(request
);
626 request
->op
= TO_NOP
;
629 *device
->modeset_byte
&= ~0x08;
631 *device
->modeset_byte
|= 0x08;
632 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
633 tape_ccw_end(request
->cpaddr
+ 1, NOP
, 0, NULL
);
635 return tape_do_io_free(device
, request
);
641 struct tape_request
*
642 tape_std_read_block(struct tape_device
*device
, size_t count
)
644 struct tape_request
*request
;
647 * We have to alloc 4 ccws in order to be able to transform request
648 * into a read backward request in error case.
650 request
= tape_alloc_request(4, 0);
651 if (IS_ERR(request
)) {
652 DBF_EXCEPTION(6, "xrbl fail");
655 request
->op
= TO_RFO
;
656 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
657 tape_ccw_end_idal(request
->cpaddr
+ 1, READ_FORWARD
,
658 device
->char_data
.idal_buf
);
659 DBF_EVENT(6, "xrbl ccwg\n");
664 * Read Block backward transformation function.
667 tape_std_read_backward(struct tape_device
*device
, struct tape_request
*request
)
670 * We have allocated 4 ccws in tape_std_read, so we can now
671 * transform the request to a read backward, followed by a
672 * forward space block.
674 request
->op
= TO_RBA
;
675 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
676 tape_ccw_cc_idal(request
->cpaddr
+ 1, READ_BACKWARD
,
677 device
->char_data
.idal_buf
);
678 tape_ccw_cc(request
->cpaddr
+ 2, FORSPACEBLOCK
, 0, NULL
);
679 tape_ccw_end(request
->cpaddr
+ 3, NOP
, 0, NULL
);
680 DBF_EVENT(6, "xrop ccwg");}
685 struct tape_request
*
686 tape_std_write_block(struct tape_device
*device
, size_t count
)
688 struct tape_request
*request
;
690 request
= tape_alloc_request(2, 0);
691 if (IS_ERR(request
)) {
692 DBF_EXCEPTION(6, "xwbl fail\n");
695 request
->op
= TO_WRI
;
696 tape_ccw_cc(request
->cpaddr
, MODE_SET_DB
, 1, device
->modeset_byte
);
697 tape_ccw_end_idal(request
->cpaddr
+ 1, WRITE_CMD
,
698 device
->char_data
.idal_buf
);
699 DBF_EVENT(6, "xwbl ccwg\n");
704 * This routine is called by frontend after an ENOSP on write
707 tape_std_process_eov(struct tape_device
*device
)
710 * End of volume: We have to backspace the last written record, then
711 * we TRY to write a tapemark and then backspace over the written TM
713 if (tape_mtop(device
, MTBSR
, 1) == 0 &&
714 tape_mtop(device
, MTWEOF
, 1) == 0) {
715 tape_mtop(device
, MTBSR
, 1);
719 EXPORT_SYMBOL(tape_std_assign
);
720 EXPORT_SYMBOL(tape_std_unassign
);
721 EXPORT_SYMBOL(tape_std_display
);
722 EXPORT_SYMBOL(tape_std_read_block_id
);
723 EXPORT_SYMBOL(tape_std_mtload
);
724 EXPORT_SYMBOL(tape_std_mtsetblk
);
725 EXPORT_SYMBOL(tape_std_mtreset
);
726 EXPORT_SYMBOL(tape_std_mtfsf
);
727 EXPORT_SYMBOL(tape_std_mtfsr
);
728 EXPORT_SYMBOL(tape_std_mtbsr
);
729 EXPORT_SYMBOL(tape_std_mtweof
);
730 EXPORT_SYMBOL(tape_std_mtbsfm
);
731 EXPORT_SYMBOL(tape_std_mtbsf
);
732 EXPORT_SYMBOL(tape_std_mtfsfm
);
733 EXPORT_SYMBOL(tape_std_mtrew
);
734 EXPORT_SYMBOL(tape_std_mtoffl
);
735 EXPORT_SYMBOL(tape_std_mtnop
);
736 EXPORT_SYMBOL(tape_std_mteom
);
737 EXPORT_SYMBOL(tape_std_mtreten
);
738 EXPORT_SYMBOL(tape_std_mterase
);
739 EXPORT_SYMBOL(tape_std_mtunload
);
740 EXPORT_SYMBOL(tape_std_mtcompression
);
741 EXPORT_SYMBOL(tape_std_read_block
);
742 EXPORT_SYMBOL(tape_std_read_backward
);
743 EXPORT_SYMBOL(tape_std_write_block
);
744 EXPORT_SYMBOL(tape_std_process_eov
);