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 dev_name(&device->cdev->dev), 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 dev_name(&device->cdev->dev));
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 dev_name(&device->cdev->dev));
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",
124 dev_name(&device->cdev->dev));
126 DBF_EVENT(3, "%08x: Tape unassigned\n", device->cdev_id);
128 tape_free_request(request);
133 * TAPE390_DISPLAY: Show a string on the tape display.
136 tape_std_display(struct tape_device *device, struct display_struct *disp)
138 struct tape_request *request;
141 request = tape_alloc_request(2, 17);
142 if (IS_ERR(request)) {
143 DBF_EVENT(3, "TAPE: load display failed\n");
144 return PTR_ERR(request);
146 request->op = TO_DIS;
148 *(unsigned char *) request->cpdata = disp->cntrl;
149 DBF_EVENT(5, "TAPE: display cntrl=%04x\n", disp->cntrl);
150 memcpy(((unsigned char *) request->cpdata) + 1, disp->message1, 8);
151 memcpy(((unsigned char *) request->cpdata) + 9, disp->message2, 8);
152 ASCEBC(((unsigned char*) request->cpdata) + 1, 16);
154 tape_ccw_cc(request->cpaddr, LOAD_DISPLAY, 17, request->cpdata);
155 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
157 rc = tape_do_io_interruptible(device, request);
158 tape_free_request(request);
166 tape_std_read_block_id(struct tape_device *device, __u64 *id)
168 struct tape_request *request;
171 request = tape_alloc_request(3, 8);
173 return PTR_ERR(request);
174 request->op = TO_RBI;
176 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
177 tape_ccw_cc(request->cpaddr + 1, READ_BLOCK_ID, 8, request->cpdata);
178 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
180 rc = tape_do_io(device, request);
182 /* Get result from read buffer. */
183 *id = *(__u64 *) request->cpdata;
184 tape_free_request(request);
189 tape_std_terminate_write(struct tape_device *device)
193 if(device->required_tapemarks == 0)
196 DBF_LH(5, "tape%d: terminate write %dxEOF\n", device->first_minor,
197 device->required_tapemarks);
199 rc = tape_mtop(device, MTWEOF, device->required_tapemarks);
203 device->required_tapemarks = 0;
204 return tape_mtop(device, MTBSR, 1);
208 * MTLOAD: Loads the tape.
209 * The default implementation just wait until the tape medium state changes
213 tape_std_mtload(struct tape_device *device, int count)
215 return wait_event_interruptible(device->state_change_wq,
216 (device->medium_state == MS_LOADED));
220 * MTSETBLK: Set block size.
223 tape_std_mtsetblk(struct tape_device *device, int count)
225 struct idal_buffer *new;
227 DBF_LH(6, "tape_std_mtsetblk(%d)\n", count);
230 * Just set block_size to 0. tapechar_read/tapechar_write
231 * will realloc the idal buffer if a bigger one than the
234 device->char_data.block_size = 0;
237 if (device->char_data.idal_buf != NULL &&
238 device->char_data.idal_buf->size == count)
239 /* We already have a idal buffer of that size. */
242 if (count > MAX_BLOCKSIZE) {
243 DBF_EVENT(3, "Invalid block size (%d > %d) given.\n",
244 count, MAX_BLOCKSIZE);
245 PRINT_ERR("Invalid block size (%d > %d) given.\n",
246 count, MAX_BLOCKSIZE);
250 /* Allocate a new idal buffer. */
251 new = idal_buffer_alloc(count, 0);
254 if (device->char_data.idal_buf != NULL)
255 idal_buffer_free(device->char_data.idal_buf);
256 device->char_data.idal_buf = new;
257 device->char_data.block_size = count;
259 DBF_LH(6, "new blocksize is %d\n", device->char_data.block_size);
265 * MTRESET: Set block size to 0.
268 tape_std_mtreset(struct tape_device *device, int count)
270 DBF_EVENT(6, "TCHAR:devreset:\n");
271 device->char_data.block_size = 0;
276 * MTFSF: Forward space over 'count' file marks. The tape is positioned
277 * at the EOT (End of Tape) side of the file mark.
280 tape_std_mtfsf(struct tape_device *device, int mt_count)
282 struct tape_request *request;
285 request = tape_alloc_request(mt_count + 2, 0);
287 return PTR_ERR(request);
288 request->op = TO_FSF;
290 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
291 device->modeset_byte);
292 ccw = tape_ccw_repeat(ccw, FORSPACEFILE, mt_count);
293 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
296 return tape_do_io_free(device, request);
300 * MTFSR: Forward space over 'count' tape blocks (blocksize is set
304 tape_std_mtfsr(struct tape_device *device, int mt_count)
306 struct tape_request *request;
310 request = tape_alloc_request(mt_count + 2, 0);
312 return PTR_ERR(request);
313 request->op = TO_FSB;
315 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
316 device->modeset_byte);
317 ccw = tape_ccw_repeat(ccw, FORSPACEBLOCK, mt_count);
318 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
321 rc = tape_do_io(device, request);
322 if (rc == 0 && request->rescnt > 0) {
323 DBF_LH(3, "FSR over tapemark\n");
326 tape_free_request(request);
332 * MTBSR: Backward space over 'count' tape blocks.
333 * (blocksize is set via MTSETBLK.
336 tape_std_mtbsr(struct tape_device *device, int mt_count)
338 struct tape_request *request;
342 request = tape_alloc_request(mt_count + 2, 0);
344 return PTR_ERR(request);
345 request->op = TO_BSB;
347 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
348 device->modeset_byte);
349 ccw = tape_ccw_repeat(ccw, BACKSPACEBLOCK, mt_count);
350 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
353 rc = tape_do_io(device, request);
354 if (rc == 0 && request->rescnt > 0) {
355 DBF_LH(3, "BSR over tapemark\n");
358 tape_free_request(request);
364 * MTWEOF: Write 'count' file marks at the current position.
367 tape_std_mtweof(struct tape_device *device, int mt_count)
369 struct tape_request *request;
372 request = tape_alloc_request(mt_count + 2, 0);
374 return PTR_ERR(request);
375 request->op = TO_WTM;
377 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
378 device->modeset_byte);
379 ccw = tape_ccw_repeat(ccw, WRITETAPEMARK, mt_count);
380 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
383 return tape_do_io_free(device, request);
387 * MTBSFM: Backward space over 'count' file marks.
388 * The tape is positioned at the BOT (Begin Of Tape) side of the
389 * last skipped file mark.
392 tape_std_mtbsfm(struct tape_device *device, int mt_count)
394 struct tape_request *request;
397 request = tape_alloc_request(mt_count + 2, 0);
399 return PTR_ERR(request);
400 request->op = TO_BSF;
402 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
403 device->modeset_byte);
404 ccw = tape_ccw_repeat(ccw, BACKSPACEFILE, mt_count);
405 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
408 return tape_do_io_free(device, request);
412 * MTBSF: Backward space over 'count' file marks. The tape is positioned at
413 * the EOT (End of Tape) side of the last skipped file mark.
416 tape_std_mtbsf(struct tape_device *device, int mt_count)
418 struct tape_request *request;
422 request = tape_alloc_request(mt_count + 2, 0);
424 return PTR_ERR(request);
425 request->op = TO_BSF;
427 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
428 device->modeset_byte);
429 ccw = tape_ccw_repeat(ccw, BACKSPACEFILE, mt_count);
430 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
432 rc = tape_do_io_free(device, request);
434 rc = tape_mtop(device, MTFSR, 1);
442 * MTFSFM: Forward space over 'count' file marks.
443 * The tape is positioned at the BOT (Begin Of Tape) side
444 * of the last skipped file mark.
447 tape_std_mtfsfm(struct tape_device *device, int mt_count)
449 struct tape_request *request;
453 request = tape_alloc_request(mt_count + 2, 0);
455 return PTR_ERR(request);
456 request->op = TO_FSF;
458 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
459 device->modeset_byte);
460 ccw = tape_ccw_repeat(ccw, FORSPACEFILE, mt_count);
461 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
463 rc = tape_do_io_free(device, request);
465 rc = tape_mtop(device, MTBSR, 1);
474 * MTREW: Rewind the tape.
477 tape_std_mtrew(struct tape_device *device, int mt_count)
479 struct tape_request *request;
481 request = tape_alloc_request(3, 0);
483 return PTR_ERR(request);
484 request->op = TO_REW;
486 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
487 device->modeset_byte);
488 tape_ccw_cc(request->cpaddr + 1, REWIND, 0, NULL);
489 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
492 return tape_do_io_free(device, request);
496 * MTOFFL: Rewind the tape and put the drive off-line.
497 * Implement 'rewind unload'
500 tape_std_mtoffl(struct tape_device *device, int mt_count)
502 struct tape_request *request;
504 request = tape_alloc_request(3, 0);
506 return PTR_ERR(request);
507 request->op = TO_RUN;
509 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
510 tape_ccw_cc(request->cpaddr + 1, REWIND_UNLOAD, 0, NULL);
511 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
514 return tape_do_io_free(device, request);
518 * MTNOP: 'No operation'.
521 tape_std_mtnop(struct tape_device *device, int mt_count)
523 struct tape_request *request;
525 request = tape_alloc_request(2, 0);
527 return PTR_ERR(request);
528 request->op = TO_NOP;
530 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
531 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
533 return tape_do_io_free(device, request);
537 * MTEOM: positions at the end of the portion of the tape already used
538 * for recordind data. MTEOM positions after the last file mark, ready for
539 * appending another file.
542 tape_std_mteom(struct tape_device *device, int mt_count)
547 * Seek from the beginning of tape (rewind).
549 if ((rc = tape_mtop(device, MTREW, 1)) < 0)
553 * The logical end of volume is given by two sewuential tapemarks.
554 * Look for this by skipping to the next file (over one tapemark)
555 * and then test for another one (fsr returns 1 if a tapemark was
559 if ((rc = tape_mtop(device, MTFSF, 1)) < 0)
561 if ((rc = tape_mtop(device, MTFSR, 1)) < 0)
565 return tape_mtop(device, MTBSR, 1);
569 * MTRETEN: Retension the tape, i.e. forward space to end of tape and rewind.
572 tape_std_mtreten(struct tape_device *device, int mt_count)
574 struct tape_request *request;
577 request = tape_alloc_request(4, 0);
579 return PTR_ERR(request);
580 request->op = TO_FSF;
582 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
583 tape_ccw_cc(request->cpaddr + 1,FORSPACEFILE, 0, NULL);
584 tape_ccw_cc(request->cpaddr + 2, NOP, 0, NULL);
585 tape_ccw_end(request->cpaddr + 3, CCW_CMD_TIC, 0, request->cpaddr);
586 /* execute it, MTRETEN rc gets ignored */
587 rc = tape_do_io_interruptible(device, request);
588 tape_free_request(request);
589 return tape_mtop(device, MTREW, 1);
593 * MTERASE: erases the tape.
596 tape_std_mterase(struct tape_device *device, int mt_count)
598 struct tape_request *request;
600 request = tape_alloc_request(6, 0);
602 return PTR_ERR(request);
603 request->op = TO_DSE;
605 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
606 tape_ccw_cc(request->cpaddr + 1, REWIND, 0, NULL);
607 tape_ccw_cc(request->cpaddr + 2, ERASE_GAP, 0, NULL);
608 tape_ccw_cc(request->cpaddr + 3, DATA_SEC_ERASE, 0, NULL);
609 tape_ccw_cc(request->cpaddr + 4, REWIND, 0, NULL);
610 tape_ccw_end(request->cpaddr + 5, NOP, 0, NULL);
613 return tape_do_io_free(device, request);
617 * MTUNLOAD: Rewind the tape and unload it.
620 tape_std_mtunload(struct tape_device *device, int mt_count)
622 return tape_mtop(device, MTOFFL, mt_count);
626 * MTCOMPRESSION: used to enable compression.
627 * Sets the IDRC on/off.
630 tape_std_mtcompression(struct tape_device *device, int mt_count)
632 struct tape_request *request;
634 if (mt_count < 0 || mt_count > 1) {
635 DBF_EXCEPTION(6, "xcom parm\n");
636 if (*device->modeset_byte & 0x08)
637 PRINT_INFO("(%s) Compression is currently on\n",
638 dev_name(&device->cdev->dev));
640 PRINT_INFO("(%s) Compression is currently off\n",
641 dev_name(&device->cdev->dev));
642 PRINT_INFO("Use 1 to switch compression on, 0 to "
646 request = tape_alloc_request(2, 0);
648 return PTR_ERR(request);
649 request->op = TO_NOP;
652 *device->modeset_byte &= ~0x08;
654 *device->modeset_byte |= 0x08;
655 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
656 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
658 return tape_do_io_free(device, request);
664 struct tape_request *
665 tape_std_read_block(struct tape_device *device, size_t count)
667 struct tape_request *request;
670 * We have to alloc 4 ccws in order to be able to transform request
671 * into a read backward request in error case.
673 request = tape_alloc_request(4, 0);
674 if (IS_ERR(request)) {
675 DBF_EXCEPTION(6, "xrbl fail");
678 request->op = TO_RFO;
679 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
680 tape_ccw_end_idal(request->cpaddr + 1, READ_FORWARD,
681 device->char_data.idal_buf);
682 DBF_EVENT(6, "xrbl ccwg\n");
687 * Read Block backward transformation function.
690 tape_std_read_backward(struct tape_device *device, struct tape_request *request)
693 * We have allocated 4 ccws in tape_std_read, so we can now
694 * transform the request to a read backward, followed by a
695 * forward space block.
697 request->op = TO_RBA;
698 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
699 tape_ccw_cc_idal(request->cpaddr + 1, READ_BACKWARD,
700 device->char_data.idal_buf);
701 tape_ccw_cc(request->cpaddr + 2, FORSPACEBLOCK, 0, NULL);
702 tape_ccw_end(request->cpaddr + 3, NOP, 0, NULL);
703 DBF_EVENT(6, "xrop ccwg");}
708 struct tape_request *
709 tape_std_write_block(struct tape_device *device, size_t count)
711 struct tape_request *request;
713 request = tape_alloc_request(2, 0);
714 if (IS_ERR(request)) {
715 DBF_EXCEPTION(6, "xwbl fail\n");
718 request->op = TO_WRI;
719 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
720 tape_ccw_end_idal(request->cpaddr + 1, WRITE_CMD,
721 device->char_data.idal_buf);
722 DBF_EVENT(6, "xwbl ccwg\n");
727 * This routine is called by frontend after an ENOSP on write
730 tape_std_process_eov(struct tape_device *device)
733 * End of volume: We have to backspace the last written record, then
734 * we TRY to write a tapemark and then backspace over the written TM
736 if (tape_mtop(device, MTBSR, 1) == 0 &&
737 tape_mtop(device, MTWEOF, 1) == 0) {
738 tape_mtop(device, MTBSR, 1);
742 EXPORT_SYMBOL(tape_std_assign);
743 EXPORT_SYMBOL(tape_std_unassign);
744 EXPORT_SYMBOL(tape_std_display);
745 EXPORT_SYMBOL(tape_std_read_block_id);
746 EXPORT_SYMBOL(tape_std_mtload);
747 EXPORT_SYMBOL(tape_std_mtsetblk);
748 EXPORT_SYMBOL(tape_std_mtreset);
749 EXPORT_SYMBOL(tape_std_mtfsf);
750 EXPORT_SYMBOL(tape_std_mtfsr);
751 EXPORT_SYMBOL(tape_std_mtbsr);
752 EXPORT_SYMBOL(tape_std_mtweof);
753 EXPORT_SYMBOL(tape_std_mtbsfm);
754 EXPORT_SYMBOL(tape_std_mtbsf);
755 EXPORT_SYMBOL(tape_std_mtfsfm);
756 EXPORT_SYMBOL(tape_std_mtrew);
757 EXPORT_SYMBOL(tape_std_mtoffl);
758 EXPORT_SYMBOL(tape_std_mtnop);
759 EXPORT_SYMBOL(tape_std_mteom);
760 EXPORT_SYMBOL(tape_std_mtreten);
761 EXPORT_SYMBOL(tape_std_mterase);
762 EXPORT_SYMBOL(tape_std_mtunload);
763 EXPORT_SYMBOL(tape_std_mtcompression);
764 EXPORT_SYMBOL(tape_std_read_block);
765 EXPORT_SYMBOL(tape_std_read_backward);
766 EXPORT_SYMBOL(tape_std_write_block);
767 EXPORT_SYMBOL(tape_std_process_eov);