2 * drivers/s390/char/tape_std.c
3 * standard tape device functions for ibm tapes.
5 * S390 and zSeries version
6 * Copyright (C) 2001,2002 IBM Deutschland Entwicklung GmbH, IBM Corporation
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 #include <linux/stddef.h>
15 #include <linux/kernel.h>
16 #include <linux/bio.h>
17 #include <linux/timer.h>
19 #include <asm/types.h>
20 #include <asm/idals.h>
21 #include <asm/ebcdic.h>
22 #include <asm/tape390.h>
24 #define TAPE_DBF_AREA tape_core_dbf
29 #define PRINTK_HEADER "TAPE_STD: "
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 if ((device = request->device) == NULL)
45 DBF_EVENT(3, "%08x: Assignment timeout. Device busy.\n",
47 rc = tape_cancel_io(device, request);
49 PRINT_ERR("(%s): Assign timeout: Cancel failed with rc = %i\n",
50 device->cdev->dev.bus_id, rc);
55 tape_std_assign(struct tape_device *device)
58 struct timer_list timeout;
59 struct tape_request *request;
61 request = tape_alloc_request(2, 11);
63 return PTR_ERR(request);
65 request->op = TO_ASSIGN;
66 tape_ccw_cc(request->cpaddr, ASSIGN, 11, request->cpdata);
67 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
70 * The assign command sometimes blocks if the device is assigned
71 * to another host (actually this shouldn't happen but it does).
72 * So we set up a timeout for this call.
75 timeout.function = tape_std_assign_timeout;
76 timeout.data = (unsigned long) request;
77 timeout.expires = jiffies + 2 * HZ;
80 rc = tape_do_io_interruptible(device, request);
85 PRINT_WARN("%s: assign failed - device might be busy\n",
86 device->cdev->dev.bus_id);
87 DBF_EVENT(3, "%08x: assign failed - device might be busy\n",
90 DBF_EVENT(3, "%08x: Tape assigned\n", device->cdev_id);
92 tape_free_request(request);
100 tape_std_unassign (struct tape_device *device)
103 struct tape_request *request;
105 if (device->tape_state == TS_NOT_OPER) {
106 DBF_EVENT(3, "(%08x): Can't unassign device\n",
108 PRINT_WARN("(%s): Can't unassign device - device gone\n",
109 device->cdev->dev.bus_id);
113 request = tape_alloc_request(2, 11);
115 return PTR_ERR(request);
117 request->op = TO_UNASSIGN;
118 tape_ccw_cc(request->cpaddr, UNASSIGN, 11, request->cpdata);
119 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
121 if ((rc = tape_do_io(device, request)) != 0) {
122 DBF_EVENT(3, "%08x: Unassign failed\n", device->cdev_id);
123 PRINT_WARN("%s: Unassign failed\n", device->cdev->dev.bus_id);
125 DBF_EVENT(3, "%08x: Tape unassigned\n", device->cdev_id);
127 tape_free_request(request);
132 * TAPE390_DISPLAY: Show a string on the tape display.
135 tape_std_display(struct tape_device *device, struct display_struct *disp)
137 struct tape_request *request;
140 request = tape_alloc_request(2, 17);
141 if (IS_ERR(request)) {
142 DBF_EVENT(3, "TAPE: load display failed\n");
143 return PTR_ERR(request);
145 request->op = TO_DIS;
147 *(unsigned char *) request->cpdata = disp->cntrl;
148 DBF_EVENT(5, "TAPE: display cntrl=%04x\n", disp->cntrl);
149 memcpy(((unsigned char *) request->cpdata) + 1, disp->message1, 8);
150 memcpy(((unsigned char *) request->cpdata) + 9, disp->message2, 8);
151 ASCEBC(((unsigned char*) request->cpdata) + 1, 16);
153 tape_ccw_cc(request->cpaddr, LOAD_DISPLAY, 17, request->cpdata);
154 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
156 rc = tape_do_io_interruptible(device, request);
157 tape_free_request(request);
165 tape_std_read_block_id(struct tape_device *device, __u64 *id)
167 struct tape_request *request;
170 request = tape_alloc_request(3, 8);
172 return PTR_ERR(request);
173 request->op = TO_RBI;
175 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
176 tape_ccw_cc(request->cpaddr + 1, READ_BLOCK_ID, 8, request->cpdata);
177 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
179 rc = tape_do_io(device, request);
181 /* Get result from read buffer. */
182 *id = *(__u64 *) request->cpdata;
183 tape_free_request(request);
188 tape_std_terminate_write(struct tape_device *device)
192 if(device->required_tapemarks == 0)
195 DBF_LH(5, "tape%d: terminate write %dxEOF\n", device->first_minor,
196 device->required_tapemarks);
198 rc = tape_mtop(device, MTWEOF, device->required_tapemarks);
202 device->required_tapemarks = 0;
203 return tape_mtop(device, MTBSR, 1);
207 * MTLOAD: Loads the tape.
208 * The default implementation just wait until the tape medium state changes
212 tape_std_mtload(struct tape_device *device, int count)
214 return wait_event_interruptible(device->state_change_wq,
215 (device->medium_state == MS_LOADED));
219 * MTSETBLK: Set block size.
222 tape_std_mtsetblk(struct tape_device *device, int count)
224 struct idal_buffer *new;
226 DBF_LH(6, "tape_std_mtsetblk(%d)\n", count);
229 * Just set block_size to 0. tapechar_read/tapechar_write
230 * will realloc the idal buffer if a bigger one than the
233 device->char_data.block_size = 0;
236 if (device->char_data.idal_buf != NULL &&
237 device->char_data.idal_buf->size == count)
238 /* We already have a idal buffer of that size. */
241 if (count > MAX_BLOCKSIZE) {
242 DBF_EVENT(3, "Invalid block size (%d > %d) given.\n",
243 count, MAX_BLOCKSIZE);
244 PRINT_ERR("Invalid block size (%d > %d) given.\n",
245 count, MAX_BLOCKSIZE);
249 /* Allocate a new idal buffer. */
250 new = idal_buffer_alloc(count, 0);
253 if (device->char_data.idal_buf != NULL)
254 idal_buffer_free(device->char_data.idal_buf);
255 device->char_data.idal_buf = new;
256 device->char_data.block_size = count;
258 DBF_LH(6, "new blocksize is %d\n", device->char_data.block_size);
264 * MTRESET: Set block size to 0.
267 tape_std_mtreset(struct tape_device *device, int count)
269 DBF_EVENT(6, "TCHAR:devreset:\n");
270 device->char_data.block_size = 0;
275 * MTFSF: Forward space over 'count' file marks. The tape is positioned
276 * at the EOT (End of Tape) side of the file mark.
279 tape_std_mtfsf(struct tape_device *device, int mt_count)
281 struct tape_request *request;
284 request = tape_alloc_request(mt_count + 2, 0);
286 return PTR_ERR(request);
287 request->op = TO_FSF;
289 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
290 device->modeset_byte);
291 ccw = tape_ccw_repeat(ccw, FORSPACEFILE, mt_count);
292 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
295 return tape_do_io_free(device, request);
299 * MTFSR: Forward space over 'count' tape blocks (blocksize is set
303 tape_std_mtfsr(struct tape_device *device, int mt_count)
305 struct tape_request *request;
309 request = tape_alloc_request(mt_count + 2, 0);
311 return PTR_ERR(request);
312 request->op = TO_FSB;
314 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
315 device->modeset_byte);
316 ccw = tape_ccw_repeat(ccw, FORSPACEBLOCK, mt_count);
317 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
320 rc = tape_do_io(device, request);
321 if (rc == 0 && request->rescnt > 0) {
322 DBF_LH(3, "FSR over tapemark\n");
325 tape_free_request(request);
331 * MTBSR: Backward space over 'count' tape blocks.
332 * (blocksize is set via MTSETBLK.
335 tape_std_mtbsr(struct tape_device *device, int mt_count)
337 struct tape_request *request;
341 request = tape_alloc_request(mt_count + 2, 0);
343 return PTR_ERR(request);
344 request->op = TO_BSB;
346 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
347 device->modeset_byte);
348 ccw = tape_ccw_repeat(ccw, BACKSPACEBLOCK, mt_count);
349 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
352 rc = tape_do_io(device, request);
353 if (rc == 0 && request->rescnt > 0) {
354 DBF_LH(3, "BSR over tapemark\n");
357 tape_free_request(request);
363 * MTWEOF: Write 'count' file marks at the current position.
366 tape_std_mtweof(struct tape_device *device, int mt_count)
368 struct tape_request *request;
371 request = tape_alloc_request(mt_count + 2, 0);
373 return PTR_ERR(request);
374 request->op = TO_WTM;
376 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
377 device->modeset_byte);
378 ccw = tape_ccw_repeat(ccw, WRITETAPEMARK, mt_count);
379 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
382 return tape_do_io_free(device, request);
386 * MTBSFM: Backward space over 'count' file marks.
387 * The tape is positioned at the BOT (Begin Of Tape) side of the
388 * last skipped file mark.
391 tape_std_mtbsfm(struct tape_device *device, int mt_count)
393 struct tape_request *request;
396 request = tape_alloc_request(mt_count + 2, 0);
398 return PTR_ERR(request);
399 request->op = TO_BSF;
401 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
402 device->modeset_byte);
403 ccw = tape_ccw_repeat(ccw, BACKSPACEFILE, mt_count);
404 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
407 return tape_do_io_free(device, request);
411 * MTBSF: Backward space over 'count' file marks. The tape is positioned at
412 * the EOT (End of Tape) side of the last skipped file mark.
415 tape_std_mtbsf(struct tape_device *device, int mt_count)
417 struct tape_request *request;
421 request = tape_alloc_request(mt_count + 2, 0);
423 return PTR_ERR(request);
424 request->op = TO_BSF;
426 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
427 device->modeset_byte);
428 ccw = tape_ccw_repeat(ccw, BACKSPACEFILE, mt_count);
429 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
431 rc = tape_do_io_free(device, request);
433 rc = tape_mtop(device, MTFSR, 1);
441 * MTFSFM: Forward space over 'count' file marks.
442 * The tape is positioned at the BOT (Begin Of Tape) side
443 * of the last skipped file mark.
446 tape_std_mtfsfm(struct tape_device *device, int mt_count)
448 struct tape_request *request;
452 request = tape_alloc_request(mt_count + 2, 0);
454 return PTR_ERR(request);
455 request->op = TO_FSF;
457 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
458 device->modeset_byte);
459 ccw = tape_ccw_repeat(ccw, FORSPACEFILE, mt_count);
460 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
462 rc = tape_do_io_free(device, request);
464 rc = tape_mtop(device, MTBSR, 1);
473 * MTREW: Rewind the tape.
476 tape_std_mtrew(struct tape_device *device, int mt_count)
478 struct tape_request *request;
480 request = tape_alloc_request(3, 0);
482 return PTR_ERR(request);
483 request->op = TO_REW;
485 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
486 device->modeset_byte);
487 tape_ccw_cc(request->cpaddr + 1, REWIND, 0, NULL);
488 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
491 return tape_do_io_free(device, request);
495 * MTOFFL: Rewind the tape and put the drive off-line.
496 * Implement 'rewind unload'
499 tape_std_mtoffl(struct tape_device *device, int mt_count)
501 struct tape_request *request;
503 request = tape_alloc_request(3, 0);
505 return PTR_ERR(request);
506 request->op = TO_RUN;
508 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
509 tape_ccw_cc(request->cpaddr + 1, REWIND_UNLOAD, 0, NULL);
510 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
513 return tape_do_io_free(device, request);
517 * MTNOP: 'No operation'.
520 tape_std_mtnop(struct tape_device *device, int mt_count)
522 struct tape_request *request;
524 request = tape_alloc_request(2, 0);
526 return PTR_ERR(request);
527 request->op = TO_NOP;
529 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
530 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
532 return tape_do_io_free(device, request);
536 * MTEOM: positions at the end of the portion of the tape already used
537 * for recordind data. MTEOM positions after the last file mark, ready for
538 * appending another file.
541 tape_std_mteom(struct tape_device *device, int mt_count)
546 * Seek from the beginning of tape (rewind).
548 if ((rc = tape_mtop(device, MTREW, 1)) < 0)
552 * The logical end of volume is given by two sewuential tapemarks.
553 * Look for this by skipping to the next file (over one tapemark)
554 * and then test for another one (fsr returns 1 if a tapemark was
558 if ((rc = tape_mtop(device, MTFSF, 1)) < 0)
560 if ((rc = tape_mtop(device, MTFSR, 1)) < 0)
564 return tape_mtop(device, MTBSR, 1);
568 * MTRETEN: Retension the tape, i.e. forward space to end of tape and rewind.
571 tape_std_mtreten(struct tape_device *device, int mt_count)
573 struct tape_request *request;
576 request = tape_alloc_request(4, 0);
578 return PTR_ERR(request);
579 request->op = TO_FSF;
581 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
582 tape_ccw_cc(request->cpaddr + 1,FORSPACEFILE, 0, NULL);
583 tape_ccw_cc(request->cpaddr + 2, NOP, 0, NULL);
584 tape_ccw_end(request->cpaddr + 3, CCW_CMD_TIC, 0, request->cpaddr);
585 /* execute it, MTRETEN rc gets ignored */
586 rc = tape_do_io_interruptible(device, request);
587 tape_free_request(request);
588 return tape_mtop(device, MTREW, 1);
592 * MTERASE: erases the tape.
595 tape_std_mterase(struct tape_device *device, int mt_count)
597 struct tape_request *request;
599 request = tape_alloc_request(6, 0);
601 return PTR_ERR(request);
602 request->op = TO_DSE;
604 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
605 tape_ccw_cc(request->cpaddr + 1, REWIND, 0, NULL);
606 tape_ccw_cc(request->cpaddr + 2, ERASE_GAP, 0, NULL);
607 tape_ccw_cc(request->cpaddr + 3, DATA_SEC_ERASE, 0, NULL);
608 tape_ccw_cc(request->cpaddr + 4, REWIND, 0, NULL);
609 tape_ccw_end(request->cpaddr + 5, NOP, 0, NULL);
612 return tape_do_io_free(device, request);
616 * MTUNLOAD: Rewind the tape and unload it.
619 tape_std_mtunload(struct tape_device *device, int mt_count)
621 return tape_mtop(device, MTOFFL, mt_count);
625 * MTCOMPRESSION: used to enable compression.
626 * Sets the IDRC on/off.
629 tape_std_mtcompression(struct tape_device *device, int mt_count)
631 struct tape_request *request;
633 if (mt_count < 0 || mt_count > 1) {
634 DBF_EXCEPTION(6, "xcom parm\n");
635 if (*device->modeset_byte & 0x08)
636 PRINT_INFO("(%s) Compression is currently on\n",
637 device->cdev->dev.bus_id);
639 PRINT_INFO("(%s) Compression is currently off\n",
640 device->cdev->dev.bus_id);
641 PRINT_INFO("Use 1 to switch compression on, 0 to "
645 request = tape_alloc_request(2, 0);
647 return PTR_ERR(request);
648 request->op = TO_NOP;
650 *device->modeset_byte = (mt_count == 0) ? 0x00 : 0x08;
651 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
652 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
654 return tape_do_io_free(device, request);
660 struct tape_request *
661 tape_std_read_block(struct tape_device *device, size_t count)
663 struct tape_request *request;
666 * We have to alloc 4 ccws in order to be able to transform request
667 * into a read backward request in error case.
669 request = tape_alloc_request(4, 0);
670 if (IS_ERR(request)) {
671 DBF_EXCEPTION(6, "xrbl fail");
674 request->op = TO_RFO;
675 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
676 tape_ccw_end_idal(request->cpaddr + 1, READ_FORWARD,
677 device->char_data.idal_buf);
678 DBF_EVENT(6, "xrbl ccwg\n");
683 * Read Block backward transformation function.
686 tape_std_read_backward(struct tape_device *device, struct tape_request *request)
689 * We have allocated 4 ccws in tape_std_read, so we can now
690 * transform the request to a read backward, followed by a
691 * forward space block.
693 request->op = TO_RBA;
694 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
695 tape_ccw_cc_idal(request->cpaddr + 1, READ_BACKWARD,
696 device->char_data.idal_buf);
697 tape_ccw_cc(request->cpaddr + 2, FORSPACEBLOCK, 0, NULL);
698 tape_ccw_end(request->cpaddr + 3, NOP, 0, NULL);
699 DBF_EVENT(6, "xrop ccwg");}
704 struct tape_request *
705 tape_std_write_block(struct tape_device *device, size_t count)
707 struct tape_request *request;
709 request = tape_alloc_request(2, 0);
710 if (IS_ERR(request)) {
711 DBF_EXCEPTION(6, "xwbl fail\n");
714 request->op = TO_WRI;
715 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
716 tape_ccw_end_idal(request->cpaddr + 1, WRITE_CMD,
717 device->char_data.idal_buf);
718 DBF_EVENT(6, "xwbl ccwg\n");
723 * This routine is called by frontend after an ENOSP on write
726 tape_std_process_eov(struct tape_device *device)
729 * End of volume: We have to backspace the last written record, then
730 * we TRY to write a tapemark and then backspace over the written TM
732 if (tape_mtop(device, MTBSR, 1) == 0 &&
733 tape_mtop(device, MTWEOF, 1) == 0) {
734 tape_mtop(device, MTBSR, 1);
738 EXPORT_SYMBOL(tape_std_assign);
739 EXPORT_SYMBOL(tape_std_unassign);
740 EXPORT_SYMBOL(tape_std_display);
741 EXPORT_SYMBOL(tape_std_read_block_id);
742 EXPORT_SYMBOL(tape_std_mtload);
743 EXPORT_SYMBOL(tape_std_mtsetblk);
744 EXPORT_SYMBOL(tape_std_mtreset);
745 EXPORT_SYMBOL(tape_std_mtfsf);
746 EXPORT_SYMBOL(tape_std_mtfsr);
747 EXPORT_SYMBOL(tape_std_mtbsr);
748 EXPORT_SYMBOL(tape_std_mtweof);
749 EXPORT_SYMBOL(tape_std_mtbsfm);
750 EXPORT_SYMBOL(tape_std_mtbsf);
751 EXPORT_SYMBOL(tape_std_mtfsfm);
752 EXPORT_SYMBOL(tape_std_mtrew);
753 EXPORT_SYMBOL(tape_std_mtoffl);
754 EXPORT_SYMBOL(tape_std_mtnop);
755 EXPORT_SYMBOL(tape_std_mteom);
756 EXPORT_SYMBOL(tape_std_mtreten);
757 EXPORT_SYMBOL(tape_std_mterase);
758 EXPORT_SYMBOL(tape_std_mtunload);
759 EXPORT_SYMBOL(tape_std_mtcompression);
760 EXPORT_SYMBOL(tape_std_read_block);
761 EXPORT_SYMBOL(tape_std_read_backward);
762 EXPORT_SYMBOL(tape_std_write_block);
763 EXPORT_SYMBOL(tape_std_process_eov);