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
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 timer_list timeout;
57 struct tape_request *request;
59 request = tape_alloc_request(2, 11);
61 return PTR_ERR(request);
63 request->op = TO_ASSIGN;
64 tape_ccw_cc(request->cpaddr, ASSIGN, 11, request->cpdata);
65 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
68 * The assign command sometimes blocks if the device is assigned
69 * to another host (actually this shouldn't happen but it does).
70 * So we set up a timeout for this call.
72 timer_setup(&request->timer, tape_std_assign_timeout, 0);
73 mod_timer(&timeout, jiffies + 2 * HZ);
75 rc = tape_do_io_interruptible(device, request);
77 del_timer_sync(&request->timer);
80 DBF_EVENT(3, "%08x: assign failed - device might be busy\n",
83 DBF_EVENT(3, "%08x: Tape assigned\n", device->cdev_id);
85 tape_free_request(request);
93 tape_std_unassign (struct tape_device *device)
96 struct tape_request *request;
98 if (device->tape_state == TS_NOT_OPER) {
99 DBF_EVENT(3, "(%08x): Can't unassign device\n",
104 request = tape_alloc_request(2, 11);
106 return PTR_ERR(request);
108 request->op = TO_UNASSIGN;
109 tape_ccw_cc(request->cpaddr, UNASSIGN, 11, request->cpdata);
110 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
112 if ((rc = tape_do_io(device, request)) != 0) {
113 DBF_EVENT(3, "%08x: Unassign failed\n", device->cdev_id);
115 DBF_EVENT(3, "%08x: Tape unassigned\n", device->cdev_id);
117 tape_free_request(request);
122 * TAPE390_DISPLAY: Show a string on the tape display.
125 tape_std_display(struct tape_device *device, struct display_struct *disp)
127 struct tape_request *request;
130 request = tape_alloc_request(2, 17);
131 if (IS_ERR(request)) {
132 DBF_EVENT(3, "TAPE: load display failed\n");
133 return PTR_ERR(request);
135 request->op = TO_DIS;
137 *(unsigned char *) request->cpdata = disp->cntrl;
138 DBF_EVENT(5, "TAPE: display cntrl=%04x\n", disp->cntrl);
139 memcpy(((unsigned char *) request->cpdata) + 1, disp->message1, 8);
140 memcpy(((unsigned char *) request->cpdata) + 9, disp->message2, 8);
141 ASCEBC(((unsigned char*) request->cpdata) + 1, 16);
143 tape_ccw_cc(request->cpaddr, LOAD_DISPLAY, 17, request->cpdata);
144 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
146 rc = tape_do_io_interruptible(device, request);
147 tape_free_request(request);
155 tape_std_read_block_id(struct tape_device *device, __u64 *id)
157 struct tape_request *request;
160 request = tape_alloc_request(3, 8);
162 return PTR_ERR(request);
163 request->op = TO_RBI;
165 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
166 tape_ccw_cc(request->cpaddr + 1, READ_BLOCK_ID, 8, request->cpdata);
167 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
169 rc = tape_do_io(device, request);
171 /* Get result from read buffer. */
172 *id = *(__u64 *) request->cpdata;
173 tape_free_request(request);
178 tape_std_terminate_write(struct tape_device *device)
182 if(device->required_tapemarks == 0)
185 DBF_LH(5, "tape%d: terminate write %dxEOF\n", device->first_minor,
186 device->required_tapemarks);
188 rc = tape_mtop(device, MTWEOF, device->required_tapemarks);
192 device->required_tapemarks = 0;
193 return tape_mtop(device, MTBSR, 1);
197 * MTLOAD: Loads the tape.
198 * The default implementation just wait until the tape medium state changes
202 tape_std_mtload(struct tape_device *device, int count)
204 return wait_event_interruptible(device->state_change_wq,
205 (device->medium_state == MS_LOADED));
209 * MTSETBLK: Set block size.
212 tape_std_mtsetblk(struct tape_device *device, int count)
214 struct idal_buffer *new;
216 DBF_LH(6, "tape_std_mtsetblk(%d)\n", count);
219 * Just set block_size to 0. tapechar_read/tapechar_write
220 * will realloc the idal buffer if a bigger one than the
223 device->char_data.block_size = 0;
226 if (device->char_data.idal_buf != NULL &&
227 device->char_data.idal_buf->size == count)
228 /* We already have a idal buffer of that size. */
231 if (count > MAX_BLOCKSIZE) {
232 DBF_EVENT(3, "Invalid block size (%d > %d) given.\n",
233 count, MAX_BLOCKSIZE);
237 /* Allocate a new idal buffer. */
238 new = idal_buffer_alloc(count, 0);
241 if (device->char_data.idal_buf != NULL)
242 idal_buffer_free(device->char_data.idal_buf);
243 device->char_data.idal_buf = new;
244 device->char_data.block_size = count;
246 DBF_LH(6, "new blocksize is %d\n", device->char_data.block_size);
252 * MTRESET: Set block size to 0.
255 tape_std_mtreset(struct tape_device *device, int count)
257 DBF_EVENT(6, "TCHAR:devreset:\n");
258 device->char_data.block_size = 0;
263 * MTFSF: Forward space over 'count' file marks. The tape is positioned
264 * at the EOT (End of Tape) side of the file mark.
267 tape_std_mtfsf(struct tape_device *device, int mt_count)
269 struct tape_request *request;
272 request = tape_alloc_request(mt_count + 2, 0);
274 return PTR_ERR(request);
275 request->op = TO_FSF;
277 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
278 device->modeset_byte);
279 ccw = tape_ccw_repeat(ccw, FORSPACEFILE, mt_count);
280 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
283 return tape_do_io_free(device, request);
287 * MTFSR: Forward space over 'count' tape blocks (blocksize is set
291 tape_std_mtfsr(struct tape_device *device, int mt_count)
293 struct tape_request *request;
297 request = tape_alloc_request(mt_count + 2, 0);
299 return PTR_ERR(request);
300 request->op = TO_FSB;
302 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
303 device->modeset_byte);
304 ccw = tape_ccw_repeat(ccw, FORSPACEBLOCK, mt_count);
305 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
308 rc = tape_do_io(device, request);
309 if (rc == 0 && request->rescnt > 0) {
310 DBF_LH(3, "FSR over tapemark\n");
313 tape_free_request(request);
319 * MTBSR: Backward space over 'count' tape blocks.
320 * (blocksize is set via MTSETBLK.
323 tape_std_mtbsr(struct tape_device *device, int mt_count)
325 struct tape_request *request;
329 request = tape_alloc_request(mt_count + 2, 0);
331 return PTR_ERR(request);
332 request->op = TO_BSB;
334 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
335 device->modeset_byte);
336 ccw = tape_ccw_repeat(ccw, BACKSPACEBLOCK, mt_count);
337 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
340 rc = tape_do_io(device, request);
341 if (rc == 0 && request->rescnt > 0) {
342 DBF_LH(3, "BSR over tapemark\n");
345 tape_free_request(request);
351 * MTWEOF: Write 'count' file marks at the current position.
354 tape_std_mtweof(struct tape_device *device, int mt_count)
356 struct tape_request *request;
359 request = tape_alloc_request(mt_count + 2, 0);
361 return PTR_ERR(request);
362 request->op = TO_WTM;
364 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
365 device->modeset_byte);
366 ccw = tape_ccw_repeat(ccw, WRITETAPEMARK, mt_count);
367 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
370 return tape_do_io_free(device, request);
374 * MTBSFM: Backward space over 'count' file marks.
375 * The tape is positioned at the BOT (Begin Of Tape) side of the
376 * last skipped file mark.
379 tape_std_mtbsfm(struct tape_device *device, int mt_count)
381 struct tape_request *request;
384 request = tape_alloc_request(mt_count + 2, 0);
386 return PTR_ERR(request);
387 request->op = TO_BSF;
389 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
390 device->modeset_byte);
391 ccw = tape_ccw_repeat(ccw, BACKSPACEFILE, mt_count);
392 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
395 return tape_do_io_free(device, request);
399 * MTBSF: Backward space over 'count' file marks. The tape is positioned at
400 * the EOT (End of Tape) side of the last skipped file mark.
403 tape_std_mtbsf(struct tape_device *device, int mt_count)
405 struct tape_request *request;
409 request = tape_alloc_request(mt_count + 2, 0);
411 return PTR_ERR(request);
412 request->op = TO_BSF;
414 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
415 device->modeset_byte);
416 ccw = tape_ccw_repeat(ccw, BACKSPACEFILE, mt_count);
417 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
419 rc = tape_do_io_free(device, request);
421 rc = tape_mtop(device, MTFSR, 1);
429 * MTFSFM: Forward space over 'count' file marks.
430 * The tape is positioned at the BOT (Begin Of Tape) side
431 * of the last skipped file mark.
434 tape_std_mtfsfm(struct tape_device *device, int mt_count)
436 struct tape_request *request;
440 request = tape_alloc_request(mt_count + 2, 0);
442 return PTR_ERR(request);
443 request->op = TO_FSF;
445 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
446 device->modeset_byte);
447 ccw = tape_ccw_repeat(ccw, FORSPACEFILE, mt_count);
448 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
450 rc = tape_do_io_free(device, request);
452 rc = tape_mtop(device, MTBSR, 1);
461 * MTREW: Rewind the tape.
464 tape_std_mtrew(struct tape_device *device, int mt_count)
466 struct tape_request *request;
468 request = tape_alloc_request(3, 0);
470 return PTR_ERR(request);
471 request->op = TO_REW;
473 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
474 device->modeset_byte);
475 tape_ccw_cc(request->cpaddr + 1, REWIND, 0, NULL);
476 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
479 return tape_do_io_free(device, request);
483 * MTOFFL: Rewind the tape and put the drive off-line.
484 * Implement 'rewind unload'
487 tape_std_mtoffl(struct tape_device *device, int mt_count)
489 struct tape_request *request;
491 request = tape_alloc_request(3, 0);
493 return PTR_ERR(request);
494 request->op = TO_RUN;
496 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
497 tape_ccw_cc(request->cpaddr + 1, REWIND_UNLOAD, 0, NULL);
498 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
501 return tape_do_io_free(device, request);
505 * MTNOP: 'No operation'.
508 tape_std_mtnop(struct tape_device *device, int mt_count)
510 struct tape_request *request;
512 request = tape_alloc_request(2, 0);
514 return PTR_ERR(request);
515 request->op = TO_NOP;
517 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
518 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
520 return tape_do_io_free(device, request);
524 * MTEOM: positions at the end of the portion of the tape already used
525 * for recordind data. MTEOM positions after the last file mark, ready for
526 * appending another file.
529 tape_std_mteom(struct tape_device *device, int mt_count)
534 * Seek from the beginning of tape (rewind).
536 if ((rc = tape_mtop(device, MTREW, 1)) < 0)
540 * The logical end of volume is given by two sewuential tapemarks.
541 * Look for this by skipping to the next file (over one tapemark)
542 * and then test for another one (fsr returns 1 if a tapemark was
546 if ((rc = tape_mtop(device, MTFSF, 1)) < 0)
548 if ((rc = tape_mtop(device, MTFSR, 1)) < 0)
552 return tape_mtop(device, MTBSR, 1);
556 * MTRETEN: Retension the tape, i.e. forward space to end of tape and rewind.
559 tape_std_mtreten(struct tape_device *device, int mt_count)
561 struct tape_request *request;
563 request = tape_alloc_request(4, 0);
565 return PTR_ERR(request);
566 request->op = TO_FSF;
568 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
569 tape_ccw_cc(request->cpaddr + 1,FORSPACEFILE, 0, NULL);
570 tape_ccw_cc(request->cpaddr + 2, NOP, 0, NULL);
571 tape_ccw_end(request->cpaddr + 3, CCW_CMD_TIC, 0, request->cpaddr);
572 /* execute it, MTRETEN rc gets ignored */
573 tape_do_io_interruptible(device, request);
574 tape_free_request(request);
575 return tape_mtop(device, MTREW, 1);
579 * MTERASE: erases the tape.
582 tape_std_mterase(struct tape_device *device, int mt_count)
584 struct tape_request *request;
586 request = tape_alloc_request(6, 0);
588 return PTR_ERR(request);
589 request->op = TO_DSE;
591 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
592 tape_ccw_cc(request->cpaddr + 1, REWIND, 0, NULL);
593 tape_ccw_cc(request->cpaddr + 2, ERASE_GAP, 0, NULL);
594 tape_ccw_cc(request->cpaddr + 3, DATA_SEC_ERASE, 0, NULL);
595 tape_ccw_cc(request->cpaddr + 4, REWIND, 0, NULL);
596 tape_ccw_end(request->cpaddr + 5, NOP, 0, NULL);
599 return tape_do_io_free(device, request);
603 * MTUNLOAD: Rewind the tape and unload it.
606 tape_std_mtunload(struct tape_device *device, int mt_count)
608 return tape_mtop(device, MTOFFL, mt_count);
612 * MTCOMPRESSION: used to enable compression.
613 * Sets the IDRC on/off.
616 tape_std_mtcompression(struct tape_device *device, int mt_count)
618 struct tape_request *request;
620 if (mt_count < 0 || mt_count > 1) {
621 DBF_EXCEPTION(6, "xcom parm\n");
624 request = tape_alloc_request(2, 0);
626 return PTR_ERR(request);
627 request->op = TO_NOP;
630 *device->modeset_byte &= ~0x08;
632 *device->modeset_byte |= 0x08;
633 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
634 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
636 return tape_do_io_free(device, request);
642 struct tape_request *
643 tape_std_read_block(struct tape_device *device, size_t count)
645 struct tape_request *request;
648 * We have to alloc 4 ccws in order to be able to transform request
649 * into a read backward request in error case.
651 request = tape_alloc_request(4, 0);
652 if (IS_ERR(request)) {
653 DBF_EXCEPTION(6, "xrbl fail");
656 request->op = TO_RFO;
657 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
658 tape_ccw_end_idal(request->cpaddr + 1, READ_FORWARD,
659 device->char_data.idal_buf);
660 DBF_EVENT(6, "xrbl ccwg\n");
665 * Read Block backward transformation function.
668 tape_std_read_backward(struct tape_device *device, struct tape_request *request)
671 * We have allocated 4 ccws in tape_std_read, so we can now
672 * transform the request to a read backward, followed by a
673 * forward space block.
675 request->op = TO_RBA;
676 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
677 tape_ccw_cc_idal(request->cpaddr + 1, READ_BACKWARD,
678 device->char_data.idal_buf);
679 tape_ccw_cc(request->cpaddr + 2, FORSPACEBLOCK, 0, NULL);
680 tape_ccw_end(request->cpaddr + 3, NOP, 0, NULL);
681 DBF_EVENT(6, "xrop ccwg");}
686 struct tape_request *
687 tape_std_write_block(struct tape_device *device, size_t count)
689 struct tape_request *request;
691 request = tape_alloc_request(2, 0);
692 if (IS_ERR(request)) {
693 DBF_EXCEPTION(6, "xwbl fail\n");
696 request->op = TO_WRI;
697 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
698 tape_ccw_end_idal(request->cpaddr + 1, WRITE_CMD,
699 device->char_data.idal_buf);
700 DBF_EVENT(6, "xwbl ccwg\n");
705 * This routine is called by frontend after an ENOSP on write
708 tape_std_process_eov(struct tape_device *device)
711 * End of volume: We have to backspace the last written record, then
712 * we TRY to write a tapemark and then backspace over the written TM
714 if (tape_mtop(device, MTBSR, 1) == 0 &&
715 tape_mtop(device, MTWEOF, 1) == 0) {
716 tape_mtop(device, MTBSR, 1);
720 EXPORT_SYMBOL(tape_std_assign);
721 EXPORT_SYMBOL(tape_std_unassign);
722 EXPORT_SYMBOL(tape_std_display);
723 EXPORT_SYMBOL(tape_std_read_block_id);
724 EXPORT_SYMBOL(tape_std_mtload);
725 EXPORT_SYMBOL(tape_std_mtsetblk);
726 EXPORT_SYMBOL(tape_std_mtreset);
727 EXPORT_SYMBOL(tape_std_mtfsf);
728 EXPORT_SYMBOL(tape_std_mtfsr);
729 EXPORT_SYMBOL(tape_std_mtbsr);
730 EXPORT_SYMBOL(tape_std_mtweof);
731 EXPORT_SYMBOL(tape_std_mtbsfm);
732 EXPORT_SYMBOL(tape_std_mtbsf);
733 EXPORT_SYMBOL(tape_std_mtfsfm);
734 EXPORT_SYMBOL(tape_std_mtrew);
735 EXPORT_SYMBOL(tape_std_mtoffl);
736 EXPORT_SYMBOL(tape_std_mtnop);
737 EXPORT_SYMBOL(tape_std_mteom);
738 EXPORT_SYMBOL(tape_std_mtreten);
739 EXPORT_SYMBOL(tape_std_mterase);
740 EXPORT_SYMBOL(tape_std_mtunload);
741 EXPORT_SYMBOL(tape_std_mtcompression);
742 EXPORT_SYMBOL(tape_std_read_block);
743 EXPORT_SYMBOL(tape_std_read_backward);
744 EXPORT_SYMBOL(tape_std_write_block);
745 EXPORT_SYMBOL(tape_std_process_eov);