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/config.h>
15 #include <linux/stddef.h>
16 #include <linux/kernel.h>
17 #include <linux/bio.h>
18 #include <linux/timer.h>
20 #include <asm/types.h>
21 #include <asm/idals.h>
22 #include <asm/ebcdic.h>
23 #include <asm/tape390.h>
25 #define TAPE_DBF_AREA tape_core_dbf
30 #define PRINTK_HEADER "TAPE_STD: "
36 tape_std_assign_timeout(unsigned long data)
38 struct tape_request * request;
39 struct tape_device * device;
42 request = (struct tape_request *) data;
43 if ((device = request->device) == NULL)
46 DBF_EVENT(3, "%08x: Assignment timeout. Device busy.\n",
48 rc = tape_cancel_io(device, request);
50 PRINT_ERR("(%s): Assign timeout: Cancel failed with rc = %i\n",
51 device->cdev->dev.bus_id, rc);
56 tape_std_assign(struct tape_device *device)
59 struct timer_list timeout;
60 struct tape_request *request;
62 request = tape_alloc_request(2, 11);
64 return PTR_ERR(request);
66 request->op = TO_ASSIGN;
67 tape_ccw_cc(request->cpaddr, ASSIGN, 11, request->cpdata);
68 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
71 * The assign command sometimes blocks if the device is assigned
72 * to another host (actually this shouldn't happen but it does).
73 * So we set up a timeout for this call.
76 timeout.function = tape_std_assign_timeout;
77 timeout.data = (unsigned long) request;
78 timeout.expires = jiffies + 2 * HZ;
81 rc = tape_do_io_interruptible(device, request);
86 PRINT_WARN("%s: assign failed - device might be busy\n",
87 device->cdev->dev.bus_id);
88 DBF_EVENT(3, "%08x: assign failed - device might be busy\n",
91 DBF_EVENT(3, "%08x: Tape assigned\n", device->cdev_id);
93 tape_free_request(request);
101 tape_std_unassign (struct tape_device *device)
104 struct tape_request *request;
106 if (device->tape_state == TS_NOT_OPER) {
107 DBF_EVENT(3, "(%08x): Can't unassign device\n",
109 PRINT_WARN("(%s): Can't unassign device - device gone\n",
110 device->cdev->dev.bus_id);
114 request = tape_alloc_request(2, 11);
116 return PTR_ERR(request);
118 request->op = TO_UNASSIGN;
119 tape_ccw_cc(request->cpaddr, UNASSIGN, 11, request->cpdata);
120 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
122 if ((rc = tape_do_io(device, request)) != 0) {
123 DBF_EVENT(3, "%08x: Unassign failed\n", device->cdev_id);
124 PRINT_WARN("%s: Unassign failed\n", device->cdev->dev.bus_id);
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 device->cdev->dev.bus_id);
640 PRINT_INFO("(%s) Compression is currently off\n",
641 device->cdev->dev.bus_id);
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;
651 *device->modeset_byte = (mt_count == 0) ? 0x00 : 0x08;
652 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
653 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
655 return tape_do_io_free(device, request);
661 struct tape_request *
662 tape_std_read_block(struct tape_device *device, size_t count)
664 struct tape_request *request;
667 * We have to alloc 4 ccws in order to be able to transform request
668 * into a read backward request in error case.
670 request = tape_alloc_request(4, 0);
671 if (IS_ERR(request)) {
672 DBF_EXCEPTION(6, "xrbl fail");
675 request->op = TO_RFO;
676 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
677 tape_ccw_end_idal(request->cpaddr + 1, READ_FORWARD,
678 device->char_data.idal_buf);
679 DBF_EVENT(6, "xrbl ccwg\n");
684 * Read Block backward transformation function.
687 tape_std_read_backward(struct tape_device *device, struct tape_request *request)
690 * We have allocated 4 ccws in tape_std_read, so we can now
691 * transform the request to a read backward, followed by a
692 * forward space block.
694 request->op = TO_RBA;
695 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
696 tape_ccw_cc_idal(request->cpaddr + 1, READ_BACKWARD,
697 device->char_data.idal_buf);
698 tape_ccw_cc(request->cpaddr + 2, FORSPACEBLOCK, 0, NULL);
699 tape_ccw_end(request->cpaddr + 3, NOP, 0, NULL);
700 DBF_EVENT(6, "xrop ccwg");}
705 struct tape_request *
706 tape_std_write_block(struct tape_device *device, size_t count)
708 struct tape_request *request;
710 request = tape_alloc_request(2, 0);
711 if (IS_ERR(request)) {
712 DBF_EXCEPTION(6, "xwbl fail\n");
715 request->op = TO_WRI;
716 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
717 tape_ccw_end_idal(request->cpaddr + 1, WRITE_CMD,
718 device->char_data.idal_buf);
719 DBF_EVENT(6, "xwbl ccwg\n");
724 * This routine is called by frontend after an ENOSP on write
727 tape_std_process_eov(struct tape_device *device)
730 * End of volume: We have to backspace the last written record, then
731 * we TRY to write a tapemark and then backspace over the written TM
733 if (tape_mtop(device, MTBSR, 1) == 0 &&
734 tape_mtop(device, MTWEOF, 1) == 0) {
735 tape_mtop(device, MTBSR, 1);
739 EXPORT_SYMBOL(tape_std_assign);
740 EXPORT_SYMBOL(tape_std_unassign);
741 EXPORT_SYMBOL(tape_std_display);
742 EXPORT_SYMBOL(tape_std_read_block_id);
743 EXPORT_SYMBOL(tape_std_mtload);
744 EXPORT_SYMBOL(tape_std_mtsetblk);
745 EXPORT_SYMBOL(tape_std_mtreset);
746 EXPORT_SYMBOL(tape_std_mtfsf);
747 EXPORT_SYMBOL(tape_std_mtfsr);
748 EXPORT_SYMBOL(tape_std_mtbsr);
749 EXPORT_SYMBOL(tape_std_mtweof);
750 EXPORT_SYMBOL(tape_std_mtbsfm);
751 EXPORT_SYMBOL(tape_std_mtbsf);
752 EXPORT_SYMBOL(tape_std_mtfsfm);
753 EXPORT_SYMBOL(tape_std_mtrew);
754 EXPORT_SYMBOL(tape_std_mtoffl);
755 EXPORT_SYMBOL(tape_std_mtnop);
756 EXPORT_SYMBOL(tape_std_mteom);
757 EXPORT_SYMBOL(tape_std_mtreten);
758 EXPORT_SYMBOL(tape_std_mterase);
759 EXPORT_SYMBOL(tape_std_mtunload);
760 EXPORT_SYMBOL(tape_std_mtcompression);
761 EXPORT_SYMBOL(tape_std_read_block);
762 EXPORT_SYMBOL(tape_std_read_backward);
763 EXPORT_SYMBOL(tape_std_write_block);
764 EXPORT_SYMBOL(tape_std_process_eov);