PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / s390 / char / tape_std.c
blob981a99fd8d4285c2b443f81a320c48f8826871cf
1 /*
2 * standard tape device functions for ibm tapes.
4 * S390 and zSeries version
5 * Copyright IBM Corp. 2001, 2002
6 * Author(s): Carsten Otte <cotte@de.ibm.com>
7 * Michael Holzheu <holzheu@de.ibm.com>
8 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
9 * Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Stefan Bader <shbader@de.ibm.com>
13 #define KMSG_COMPONENT "tape"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/stddef.h>
17 #include <linux/kernel.h>
18 #include <linux/bio.h>
19 #include <linux/timer.h>
21 #include <asm/types.h>
22 #include <asm/idals.h>
23 #include <asm/ebcdic.h>
24 #include <asm/tape390.h>
26 #define TAPE_DBF_AREA tape_core_dbf
28 #include "tape.h"
29 #include "tape_std.h"
32 * tape_std_assign
34 static void
35 tape_std_assign_timeout(unsigned long data)
37 struct tape_request * request;
38 struct tape_device * device;
39 int rc;
41 request = (struct tape_request *) data;
42 device = request->device;
43 BUG_ON(!device);
45 DBF_EVENT(3, "%08x: Assignment timeout. Device busy.\n",
46 device->cdev_id);
47 rc = tape_cancel_io(device, request);
48 if(rc)
49 DBF_EVENT(3, "(%08x): Assign timeout: Cancel failed with rc = "
50 "%i\n", device->cdev_id, rc);
53 int
54 tape_std_assign(struct tape_device *device)
56 int rc;
57 struct timer_list timeout;
58 struct tape_request *request;
60 request = tape_alloc_request(2, 11);
61 if (IS_ERR(request))
62 return PTR_ERR(request);
64 request->op = TO_ASSIGN;
65 tape_ccw_cc(request->cpaddr, ASSIGN, 11, request->cpdata);
66 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
69 * The assign command sometimes blocks if the device is assigned
70 * to another host (actually this shouldn't happen but it does).
71 * So we set up a timeout for this call.
73 init_timer_on_stack(&timeout);
74 timeout.function = tape_std_assign_timeout;
75 timeout.data = (unsigned long) request;
76 timeout.expires = jiffies + 2 * HZ;
77 add_timer(&timeout);
79 rc = tape_do_io_interruptible(device, request);
81 del_timer(&timeout);
83 if (rc != 0) {
84 DBF_EVENT(3, "%08x: assign failed - device might be busy\n",
85 device->cdev_id);
86 } else {
87 DBF_EVENT(3, "%08x: Tape assigned\n", device->cdev_id);
89 tape_free_request(request);
90 return rc;
94 * tape_std_unassign
96 int
97 tape_std_unassign (struct tape_device *device)
99 int rc;
100 struct tape_request *request;
102 if (device->tape_state == TS_NOT_OPER) {
103 DBF_EVENT(3, "(%08x): Can't unassign device\n",
104 device->cdev_id);
105 return -EIO;
108 request = tape_alloc_request(2, 11);
109 if (IS_ERR(request))
110 return PTR_ERR(request);
112 request->op = TO_UNASSIGN;
113 tape_ccw_cc(request->cpaddr, UNASSIGN, 11, request->cpdata);
114 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
116 if ((rc = tape_do_io(device, request)) != 0) {
117 DBF_EVENT(3, "%08x: Unassign failed\n", device->cdev_id);
118 } else {
119 DBF_EVENT(3, "%08x: Tape unassigned\n", device->cdev_id);
121 tape_free_request(request);
122 return rc;
126 * TAPE390_DISPLAY: Show a string on the tape display.
129 tape_std_display(struct tape_device *device, struct display_struct *disp)
131 struct tape_request *request;
132 int rc;
134 request = tape_alloc_request(2, 17);
135 if (IS_ERR(request)) {
136 DBF_EVENT(3, "TAPE: load display failed\n");
137 return PTR_ERR(request);
139 request->op = TO_DIS;
141 *(unsigned char *) request->cpdata = disp->cntrl;
142 DBF_EVENT(5, "TAPE: display cntrl=%04x\n", disp->cntrl);
143 memcpy(((unsigned char *) request->cpdata) + 1, disp->message1, 8);
144 memcpy(((unsigned char *) request->cpdata) + 9, disp->message2, 8);
145 ASCEBC(((unsigned char*) request->cpdata) + 1, 16);
147 tape_ccw_cc(request->cpaddr, LOAD_DISPLAY, 17, request->cpdata);
148 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
150 rc = tape_do_io_interruptible(device, request);
151 tape_free_request(request);
152 return rc;
156 * Read block id.
159 tape_std_read_block_id(struct tape_device *device, __u64 *id)
161 struct tape_request *request;
162 int rc;
164 request = tape_alloc_request(3, 8);
165 if (IS_ERR(request))
166 return PTR_ERR(request);
167 request->op = TO_RBI;
168 /* setup ccws */
169 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
170 tape_ccw_cc(request->cpaddr + 1, READ_BLOCK_ID, 8, request->cpdata);
171 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
172 /* execute it */
173 rc = tape_do_io(device, request);
174 if (rc == 0)
175 /* Get result from read buffer. */
176 *id = *(__u64 *) request->cpdata;
177 tape_free_request(request);
178 return rc;
182 tape_std_terminate_write(struct tape_device *device)
184 int rc;
186 if(device->required_tapemarks == 0)
187 return 0;
189 DBF_LH(5, "tape%d: terminate write %dxEOF\n", device->first_minor,
190 device->required_tapemarks);
192 rc = tape_mtop(device, MTWEOF, device->required_tapemarks);
193 if (rc)
194 return rc;
196 device->required_tapemarks = 0;
197 return tape_mtop(device, MTBSR, 1);
201 * MTLOAD: Loads the tape.
202 * The default implementation just wait until the tape medium state changes
203 * to MS_LOADED.
206 tape_std_mtload(struct tape_device *device, int count)
208 return wait_event_interruptible(device->state_change_wq,
209 (device->medium_state == MS_LOADED));
213 * MTSETBLK: Set block size.
216 tape_std_mtsetblk(struct tape_device *device, int count)
218 struct idal_buffer *new;
220 DBF_LH(6, "tape_std_mtsetblk(%d)\n", count);
221 if (count <= 0) {
223 * Just set block_size to 0. tapechar_read/tapechar_write
224 * will realloc the idal buffer if a bigger one than the
225 * current is needed.
227 device->char_data.block_size = 0;
228 return 0;
230 if (device->char_data.idal_buf != NULL &&
231 device->char_data.idal_buf->size == count)
232 /* We already have a idal buffer of that size. */
233 return 0;
235 if (count > MAX_BLOCKSIZE) {
236 DBF_EVENT(3, "Invalid block size (%d > %d) given.\n",
237 count, MAX_BLOCKSIZE);
238 return -EINVAL;
241 /* Allocate a new idal buffer. */
242 new = idal_buffer_alloc(count, 0);
243 if (IS_ERR(new))
244 return -ENOMEM;
245 if (device->char_data.idal_buf != NULL)
246 idal_buffer_free(device->char_data.idal_buf);
247 device->char_data.idal_buf = new;
248 device->char_data.block_size = count;
250 DBF_LH(6, "new blocksize is %d\n", device->char_data.block_size);
252 return 0;
256 * MTRESET: Set block size to 0.
259 tape_std_mtreset(struct tape_device *device, int count)
261 DBF_EVENT(6, "TCHAR:devreset:\n");
262 device->char_data.block_size = 0;
263 return 0;
267 * MTFSF: Forward space over 'count' file marks. The tape is positioned
268 * at the EOT (End of Tape) side of the file mark.
271 tape_std_mtfsf(struct tape_device *device, int mt_count)
273 struct tape_request *request;
274 struct ccw1 *ccw;
276 request = tape_alloc_request(mt_count + 2, 0);
277 if (IS_ERR(request))
278 return PTR_ERR(request);
279 request->op = TO_FSF;
280 /* setup ccws */
281 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
282 device->modeset_byte);
283 ccw = tape_ccw_repeat(ccw, FORSPACEFILE, mt_count);
284 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
286 /* execute it */
287 return tape_do_io_free(device, request);
291 * MTFSR: Forward space over 'count' tape blocks (blocksize is set
292 * via MTSETBLK.
295 tape_std_mtfsr(struct tape_device *device, int mt_count)
297 struct tape_request *request;
298 struct ccw1 *ccw;
299 int rc;
301 request = tape_alloc_request(mt_count + 2, 0);
302 if (IS_ERR(request))
303 return PTR_ERR(request);
304 request->op = TO_FSB;
305 /* setup ccws */
306 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
307 device->modeset_byte);
308 ccw = tape_ccw_repeat(ccw, FORSPACEBLOCK, mt_count);
309 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
311 /* execute it */
312 rc = tape_do_io(device, request);
313 if (rc == 0 && request->rescnt > 0) {
314 DBF_LH(3, "FSR over tapemark\n");
315 rc = 1;
317 tape_free_request(request);
319 return rc;
323 * MTBSR: Backward space over 'count' tape blocks.
324 * (blocksize is set via MTSETBLK.
327 tape_std_mtbsr(struct tape_device *device, int mt_count)
329 struct tape_request *request;
330 struct ccw1 *ccw;
331 int rc;
333 request = tape_alloc_request(mt_count + 2, 0);
334 if (IS_ERR(request))
335 return PTR_ERR(request);
336 request->op = TO_BSB;
337 /* setup ccws */
338 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
339 device->modeset_byte);
340 ccw = tape_ccw_repeat(ccw, BACKSPACEBLOCK, mt_count);
341 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
343 /* execute it */
344 rc = tape_do_io(device, request);
345 if (rc == 0 && request->rescnt > 0) {
346 DBF_LH(3, "BSR over tapemark\n");
347 rc = 1;
349 tape_free_request(request);
351 return rc;
355 * MTWEOF: Write 'count' file marks at the current position.
358 tape_std_mtweof(struct tape_device *device, int mt_count)
360 struct tape_request *request;
361 struct ccw1 *ccw;
363 request = tape_alloc_request(mt_count + 2, 0);
364 if (IS_ERR(request))
365 return PTR_ERR(request);
366 request->op = TO_WTM;
367 /* setup ccws */
368 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
369 device->modeset_byte);
370 ccw = tape_ccw_repeat(ccw, WRITETAPEMARK, mt_count);
371 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
373 /* execute it */
374 return tape_do_io_free(device, request);
378 * MTBSFM: Backward space over 'count' file marks.
379 * The tape is positioned at the BOT (Begin Of Tape) side of the
380 * last skipped file mark.
383 tape_std_mtbsfm(struct tape_device *device, int mt_count)
385 struct tape_request *request;
386 struct ccw1 *ccw;
388 request = tape_alloc_request(mt_count + 2, 0);
389 if (IS_ERR(request))
390 return PTR_ERR(request);
391 request->op = TO_BSF;
392 /* setup ccws */
393 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
394 device->modeset_byte);
395 ccw = tape_ccw_repeat(ccw, BACKSPACEFILE, mt_count);
396 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
398 /* execute it */
399 return tape_do_io_free(device, request);
403 * MTBSF: Backward space over 'count' file marks. The tape is positioned at
404 * the EOT (End of Tape) side of the last skipped file mark.
407 tape_std_mtbsf(struct tape_device *device, int mt_count)
409 struct tape_request *request;
410 struct ccw1 *ccw;
411 int rc;
413 request = tape_alloc_request(mt_count + 2, 0);
414 if (IS_ERR(request))
415 return PTR_ERR(request);
416 request->op = TO_BSF;
417 /* setup ccws */
418 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
419 device->modeset_byte);
420 ccw = tape_ccw_repeat(ccw, BACKSPACEFILE, mt_count);
421 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
422 /* execute it */
423 rc = tape_do_io_free(device, request);
424 if (rc == 0) {
425 rc = tape_mtop(device, MTFSR, 1);
426 if (rc > 0)
427 rc = 0;
429 return rc;
433 * MTFSFM: Forward space over 'count' file marks.
434 * The tape is positioned at the BOT (Begin Of Tape) side
435 * of the last skipped file mark.
438 tape_std_mtfsfm(struct tape_device *device, int mt_count)
440 struct tape_request *request;
441 struct ccw1 *ccw;
442 int rc;
444 request = tape_alloc_request(mt_count + 2, 0);
445 if (IS_ERR(request))
446 return PTR_ERR(request);
447 request->op = TO_FSF;
448 /* setup ccws */
449 ccw = tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
450 device->modeset_byte);
451 ccw = tape_ccw_repeat(ccw, FORSPACEFILE, mt_count);
452 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
453 /* execute it */
454 rc = tape_do_io_free(device, request);
455 if (rc == 0) {
456 rc = tape_mtop(device, MTBSR, 1);
457 if (rc > 0)
458 rc = 0;
461 return rc;
465 * MTREW: Rewind the tape.
468 tape_std_mtrew(struct tape_device *device, int mt_count)
470 struct tape_request *request;
472 request = tape_alloc_request(3, 0);
473 if (IS_ERR(request))
474 return PTR_ERR(request);
475 request->op = TO_REW;
476 /* setup ccws */
477 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1,
478 device->modeset_byte);
479 tape_ccw_cc(request->cpaddr + 1, REWIND, 0, NULL);
480 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
482 /* execute it */
483 return tape_do_io_free(device, request);
487 * MTOFFL: Rewind the tape and put the drive off-line.
488 * Implement 'rewind unload'
491 tape_std_mtoffl(struct tape_device *device, int mt_count)
493 struct tape_request *request;
495 request = tape_alloc_request(3, 0);
496 if (IS_ERR(request))
497 return PTR_ERR(request);
498 request->op = TO_RUN;
499 /* setup ccws */
500 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
501 tape_ccw_cc(request->cpaddr + 1, REWIND_UNLOAD, 0, NULL);
502 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
504 /* execute it */
505 return tape_do_io_free(device, request);
509 * MTNOP: 'No operation'.
512 tape_std_mtnop(struct tape_device *device, int mt_count)
514 struct tape_request *request;
516 request = tape_alloc_request(2, 0);
517 if (IS_ERR(request))
518 return PTR_ERR(request);
519 request->op = TO_NOP;
520 /* setup ccws */
521 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
522 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
523 /* execute it */
524 return tape_do_io_free(device, request);
528 * MTEOM: positions at the end of the portion of the tape already used
529 * for recordind data. MTEOM positions after the last file mark, ready for
530 * appending another file.
533 tape_std_mteom(struct tape_device *device, int mt_count)
535 int rc;
538 * Seek from the beginning of tape (rewind).
540 if ((rc = tape_mtop(device, MTREW, 1)) < 0)
541 return rc;
544 * The logical end of volume is given by two sewuential tapemarks.
545 * Look for this by skipping to the next file (over one tapemark)
546 * and then test for another one (fsr returns 1 if a tapemark was
547 * encountered).
549 do {
550 if ((rc = tape_mtop(device, MTFSF, 1)) < 0)
551 return rc;
552 if ((rc = tape_mtop(device, MTFSR, 1)) < 0)
553 return rc;
554 } while (rc == 0);
556 return tape_mtop(device, MTBSR, 1);
560 * MTRETEN: Retension the tape, i.e. forward space to end of tape and rewind.
563 tape_std_mtreten(struct tape_device *device, int mt_count)
565 struct tape_request *request;
567 request = tape_alloc_request(4, 0);
568 if (IS_ERR(request))
569 return PTR_ERR(request);
570 request->op = TO_FSF;
571 /* setup ccws */
572 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
573 tape_ccw_cc(request->cpaddr + 1,FORSPACEFILE, 0, NULL);
574 tape_ccw_cc(request->cpaddr + 2, NOP, 0, NULL);
575 tape_ccw_end(request->cpaddr + 3, CCW_CMD_TIC, 0, request->cpaddr);
576 /* execute it, MTRETEN rc gets ignored */
577 tape_do_io_interruptible(device, request);
578 tape_free_request(request);
579 return tape_mtop(device, MTREW, 1);
583 * MTERASE: erases the tape.
586 tape_std_mterase(struct tape_device *device, int mt_count)
588 struct tape_request *request;
590 request = tape_alloc_request(6, 0);
591 if (IS_ERR(request))
592 return PTR_ERR(request);
593 request->op = TO_DSE;
594 /* setup ccws */
595 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
596 tape_ccw_cc(request->cpaddr + 1, REWIND, 0, NULL);
597 tape_ccw_cc(request->cpaddr + 2, ERASE_GAP, 0, NULL);
598 tape_ccw_cc(request->cpaddr + 3, DATA_SEC_ERASE, 0, NULL);
599 tape_ccw_cc(request->cpaddr + 4, REWIND, 0, NULL);
600 tape_ccw_end(request->cpaddr + 5, NOP, 0, NULL);
602 /* execute it */
603 return tape_do_io_free(device, request);
607 * MTUNLOAD: Rewind the tape and unload it.
610 tape_std_mtunload(struct tape_device *device, int mt_count)
612 return tape_mtop(device, MTOFFL, mt_count);
616 * MTCOMPRESSION: used to enable compression.
617 * Sets the IDRC on/off.
620 tape_std_mtcompression(struct tape_device *device, int mt_count)
622 struct tape_request *request;
624 if (mt_count < 0 || mt_count > 1) {
625 DBF_EXCEPTION(6, "xcom parm\n");
626 return -EINVAL;
628 request = tape_alloc_request(2, 0);
629 if (IS_ERR(request))
630 return PTR_ERR(request);
631 request->op = TO_NOP;
632 /* setup ccws */
633 if (mt_count == 0)
634 *device->modeset_byte &= ~0x08;
635 else
636 *device->modeset_byte |= 0x08;
637 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
638 tape_ccw_end(request->cpaddr + 1, NOP, 0, NULL);
639 /* execute it */
640 return tape_do_io_free(device, request);
644 * Read Block
646 struct tape_request *
647 tape_std_read_block(struct tape_device *device, size_t count)
649 struct tape_request *request;
652 * We have to alloc 4 ccws in order to be able to transform request
653 * into a read backward request in error case.
655 request = tape_alloc_request(4, 0);
656 if (IS_ERR(request)) {
657 DBF_EXCEPTION(6, "xrbl fail");
658 return request;
660 request->op = TO_RFO;
661 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
662 tape_ccw_end_idal(request->cpaddr + 1, READ_FORWARD,
663 device->char_data.idal_buf);
664 DBF_EVENT(6, "xrbl ccwg\n");
665 return request;
669 * Read Block backward transformation function.
671 void
672 tape_std_read_backward(struct tape_device *device, struct tape_request *request)
675 * We have allocated 4 ccws in tape_std_read, so we can now
676 * transform the request to a read backward, followed by a
677 * forward space block.
679 request->op = TO_RBA;
680 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
681 tape_ccw_cc_idal(request->cpaddr + 1, READ_BACKWARD,
682 device->char_data.idal_buf);
683 tape_ccw_cc(request->cpaddr + 2, FORSPACEBLOCK, 0, NULL);
684 tape_ccw_end(request->cpaddr + 3, NOP, 0, NULL);
685 DBF_EVENT(6, "xrop ccwg");}
688 * Write Block
690 struct tape_request *
691 tape_std_write_block(struct tape_device *device, size_t count)
693 struct tape_request *request;
695 request = tape_alloc_request(2, 0);
696 if (IS_ERR(request)) {
697 DBF_EXCEPTION(6, "xwbl fail\n");
698 return request;
700 request->op = TO_WRI;
701 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
702 tape_ccw_end_idal(request->cpaddr + 1, WRITE_CMD,
703 device->char_data.idal_buf);
704 DBF_EVENT(6, "xwbl ccwg\n");
705 return request;
709 * This routine is called by frontend after an ENOSP on write
711 void
712 tape_std_process_eov(struct tape_device *device)
715 * End of volume: We have to backspace the last written record, then
716 * we TRY to write a tapemark and then backspace over the written TM
718 if (tape_mtop(device, MTBSR, 1) == 0 &&
719 tape_mtop(device, MTWEOF, 1) == 0) {
720 tape_mtop(device, MTBSR, 1);
724 EXPORT_SYMBOL(tape_std_assign);
725 EXPORT_SYMBOL(tape_std_unassign);
726 EXPORT_SYMBOL(tape_std_display);
727 EXPORT_SYMBOL(tape_std_read_block_id);
728 EXPORT_SYMBOL(tape_std_mtload);
729 EXPORT_SYMBOL(tape_std_mtsetblk);
730 EXPORT_SYMBOL(tape_std_mtreset);
731 EXPORT_SYMBOL(tape_std_mtfsf);
732 EXPORT_SYMBOL(tape_std_mtfsr);
733 EXPORT_SYMBOL(tape_std_mtbsr);
734 EXPORT_SYMBOL(tape_std_mtweof);
735 EXPORT_SYMBOL(tape_std_mtbsfm);
736 EXPORT_SYMBOL(tape_std_mtbsf);
737 EXPORT_SYMBOL(tape_std_mtfsfm);
738 EXPORT_SYMBOL(tape_std_mtrew);
739 EXPORT_SYMBOL(tape_std_mtoffl);
740 EXPORT_SYMBOL(tape_std_mtnop);
741 EXPORT_SYMBOL(tape_std_mteom);
742 EXPORT_SYMBOL(tape_std_mtreten);
743 EXPORT_SYMBOL(tape_std_mterase);
744 EXPORT_SYMBOL(tape_std_mtunload);
745 EXPORT_SYMBOL(tape_std_mtcompression);
746 EXPORT_SYMBOL(tape_std_read_block);
747 EXPORT_SYMBOL(tape_std_read_backward);
748 EXPORT_SYMBOL(tape_std_write_block);
749 EXPORT_SYMBOL(tape_std_process_eov);