Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / s390 / char / tape_34xx.c
blobd4f2da7380784850d354515a29434b16fe457b86
1 /*
2 * drivers/s390/char/tape_34xx.c
3 * tape device discipline for 3480/3490 tapes.
5 * Copyright (C) IBM Corp. 2001,2006
6 * Author(s): Carsten Otte <cotte@de.ibm.com>
7 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
9 */
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/bio.h>
15 #include <linux/workqueue.h>
17 #define TAPE_DBF_AREA tape_34xx_dbf
19 #include "tape.h"
20 #include "tape_std.h"
22 #define PRINTK_HEADER "TAPE_34XX: "
25 * Pointer to debug area.
27 debug_info_t *TAPE_DBF_AREA = NULL;
28 EXPORT_SYMBOL(TAPE_DBF_AREA);
30 #define TAPE34XX_FMT_3480 0
31 #define TAPE34XX_FMT_3480_2_XF 1
32 #define TAPE34XX_FMT_3480_XF 2
34 struct tape_34xx_block_id {
35 unsigned int wrap : 1;
36 unsigned int segment : 7;
37 unsigned int format : 2;
38 unsigned int block : 22;
42 * A list of block ID's is used to faster seek blocks.
44 struct tape_34xx_sbid {
45 struct list_head list;
46 struct tape_34xx_block_id bid;
49 static void tape_34xx_delete_sbid_from(struct tape_device *, int);
52 * Medium sense for 34xx tapes. There is no 'real' medium sense call.
53 * So we just do a normal sense.
55 static int
56 tape_34xx_medium_sense(struct tape_device *device)
58 struct tape_request *request;
59 unsigned char *sense;
60 int rc;
62 request = tape_alloc_request(1, 32);
63 if (IS_ERR(request)) {
64 DBF_EXCEPTION(6, "MSEN fail\n");
65 return PTR_ERR(request);
68 request->op = TO_MSEN;
69 tape_ccw_end(request->cpaddr, SENSE, 32, request->cpdata);
71 rc = tape_do_io_interruptible(device, request);
72 if (request->rc == 0) {
73 sense = request->cpdata;
76 * This isn't quite correct. But since INTERVENTION_REQUIRED
77 * means that the drive is 'neither ready nor on-line' it is
78 * only slightly inaccurate to say there is no tape loaded if
79 * the drive isn't online...
81 if (sense[0] & SENSE_INTERVENTION_REQUIRED)
82 tape_med_state_set(device, MS_UNLOADED);
83 else
84 tape_med_state_set(device, MS_LOADED);
86 if (sense[1] & SENSE_WRITE_PROTECT)
87 device->tape_generic_status |= GMT_WR_PROT(~0);
88 else
89 device->tape_generic_status &= ~GMT_WR_PROT(~0);
90 } else {
91 DBF_EVENT(4, "tape_34xx: medium sense failed with rc=%d\n",
92 request->rc);
94 tape_free_request(request);
96 return rc;
100 * These functions are currently used only to schedule a medium_sense for
101 * later execution. This is because we get an interrupt whenever a medium
102 * is inserted but cannot call tape_do_io* from an interrupt context.
103 * Maybe that's useful for other actions we want to start from the
104 * interrupt handler.
106 static void
107 tape_34xx_work_handler(void *data)
109 struct {
110 struct tape_device *device;
111 enum tape_op op;
112 struct work_struct work;
113 } *p = data;
115 switch(p->op) {
116 case TO_MSEN:
117 tape_34xx_medium_sense(p->device);
118 break;
119 default:
120 DBF_EVENT(3, "T34XX: internal error: unknown work\n");
123 p->device = tape_put_device(p->device);
124 kfree(p);
127 static int
128 tape_34xx_schedule_work(struct tape_device *device, enum tape_op op)
130 struct {
131 struct tape_device *device;
132 enum tape_op op;
133 struct work_struct work;
134 } *p;
136 if ((p = kmalloc(sizeof(*p), GFP_ATOMIC)) == NULL)
137 return -ENOMEM;
139 memset(p, 0, sizeof(*p));
140 INIT_WORK(&p->work, tape_34xx_work_handler, p);
142 p->device = tape_get_device_reference(device);
143 p->op = op;
145 schedule_work(&p->work);
146 return 0;
150 * Done Handler is called when dev stat = DEVICE-END (successful operation)
152 static inline int
153 tape_34xx_done(struct tape_request *request)
155 DBF_EVENT(6, "%s done\n", tape_op_verbose[request->op]);
157 switch (request->op) {
158 case TO_DSE:
159 case TO_RUN:
160 case TO_WRI:
161 case TO_WTM:
162 case TO_ASSIGN:
163 case TO_UNASSIGN:
164 tape_34xx_delete_sbid_from(request->device, 0);
165 break;
166 default:
169 return TAPE_IO_SUCCESS;
172 static inline int
173 tape_34xx_erp_failed(struct tape_request *request, int rc)
175 DBF_EVENT(3, "Error recovery failed for %s (RC=%d)\n",
176 tape_op_verbose[request->op], rc);
177 return rc;
180 static inline int
181 tape_34xx_erp_succeeded(struct tape_request *request)
183 DBF_EVENT(3, "Error Recovery successful for %s\n",
184 tape_op_verbose[request->op]);
185 return tape_34xx_done(request);
188 static inline int
189 tape_34xx_erp_retry(struct tape_request *request)
191 DBF_EVENT(3, "xerp retr %s\n", tape_op_verbose[request->op]);
192 return TAPE_IO_RETRY;
196 * This function is called, when no request is outstanding and we get an
197 * interrupt
199 static int
200 tape_34xx_unsolicited_irq(struct tape_device *device, struct irb *irb)
202 if (irb->scsw.dstat == 0x85 /* READY */) {
203 /* A medium was inserted in the drive. */
204 DBF_EVENT(6, "xuud med\n");
205 tape_34xx_delete_sbid_from(device, 0);
206 tape_34xx_schedule_work(device, TO_MSEN);
207 } else {
208 DBF_EVENT(3, "unsol.irq! dev end: %08x\n", device->cdev_id);
209 PRINT_WARN("Unsolicited IRQ (Device End) caught.\n");
210 tape_dump_sense(device, NULL, irb);
212 return TAPE_IO_SUCCESS;
216 * Read Opposite Error Recovery Function:
217 * Used, when Read Forward does not work
219 static int
220 tape_34xx_erp_read_opposite(struct tape_device *device,
221 struct tape_request *request)
223 if (request->op == TO_RFO) {
225 * We did read forward, but the data could not be read
226 * *correctly*. We transform the request to a read backward
227 * and try again.
229 tape_std_read_backward(device, request);
230 return tape_34xx_erp_retry(request);
232 if (request->op != TO_RBA)
233 PRINT_ERR("read_opposite called with state:%s\n",
234 tape_op_verbose[request->op]);
236 * We tried to read forward and backward, but hat no
237 * success -> failed.
239 return tape_34xx_erp_failed(request, -EIO);
242 static int
243 tape_34xx_erp_bug(struct tape_device *device, struct tape_request *request,
244 struct irb *irb, int no)
246 if (request->op != TO_ASSIGN) {
247 PRINT_WARN("An unexpected condition #%d was caught in "
248 "tape error recovery.\n", no);
249 PRINT_WARN("Please report this incident.\n");
250 if (request)
251 PRINT_WARN("Operation of tape:%s\n",
252 tape_op_verbose[request->op]);
253 tape_dump_sense(device, request, irb);
255 return tape_34xx_erp_failed(request, -EIO);
259 * Handle data overrun between cu and drive. The channel speed might
260 * be too slow.
262 static int
263 tape_34xx_erp_overrun(struct tape_device *device, struct tape_request *request,
264 struct irb *irb)
266 if (irb->ecw[3] == 0x40) {
267 PRINT_WARN ("Data overrun error between control-unit "
268 "and drive. Use a faster channel connection, "
269 "if possible! \n");
270 return tape_34xx_erp_failed(request, -EIO);
272 return tape_34xx_erp_bug(device, request, irb, -1);
276 * Handle record sequence error.
278 static int
279 tape_34xx_erp_sequence(struct tape_device *device,
280 struct tape_request *request, struct irb *irb)
282 if (irb->ecw[3] == 0x41) {
284 * cu detected incorrect block-id sequence on tape.
286 PRINT_WARN("Illegal block-id sequence found!\n");
287 return tape_34xx_erp_failed(request, -EIO);
290 * Record sequence error bit is set, but erpa does not
291 * show record sequence error.
293 return tape_34xx_erp_bug(device, request, irb, -2);
297 * This function analyses the tape's sense-data in case of a unit-check.
298 * If possible, it tries to recover from the error. Else the user is
299 * informed about the problem.
301 static int
302 tape_34xx_unit_check(struct tape_device *device, struct tape_request *request,
303 struct irb *irb)
305 int inhibit_cu_recovery;
306 __u8* sense;
308 inhibit_cu_recovery = (*device->modeset_byte & 0x80) ? 1 : 0;
309 sense = irb->ecw;
311 #ifdef CONFIG_S390_TAPE_BLOCK
312 if (request->op == TO_BLOCK) {
314 * Recovery for block device requests. Set the block_position
315 * to something invalid and retry.
317 device->blk_data.block_position = -1;
318 if (request->retries-- <= 0)
319 return tape_34xx_erp_failed(request, -EIO);
320 else
321 return tape_34xx_erp_retry(request);
323 #endif
325 if (
326 sense[0] & SENSE_COMMAND_REJECT &&
327 sense[1] & SENSE_WRITE_PROTECT
329 if (
330 request->op == TO_DSE ||
331 request->op == TO_WRI ||
332 request->op == TO_WTM
334 /* medium is write protected */
335 return tape_34xx_erp_failed(request, -EACCES);
336 } else {
337 return tape_34xx_erp_bug(device, request, irb, -3);
342 * Special cases for various tape-states when reaching
343 * end of recorded area
345 * FIXME: Maybe a special case of the special case:
346 * sense[0] == SENSE_EQUIPMENT_CHECK &&
347 * sense[1] == SENSE_DRIVE_ONLINE &&
348 * sense[3] == 0x47 (Volume Fenced)
350 * This was caused by continued FSF or FSR after an
351 * 'End Of Data'.
353 if ((
354 sense[0] == SENSE_DATA_CHECK ||
355 sense[0] == SENSE_EQUIPMENT_CHECK ||
356 sense[0] == SENSE_EQUIPMENT_CHECK + SENSE_DEFERRED_UNIT_CHECK
357 ) && (
358 sense[1] == SENSE_DRIVE_ONLINE ||
359 sense[1] == SENSE_BEGINNING_OF_TAPE + SENSE_WRITE_MODE
360 )) {
361 switch (request->op) {
363 * sense[0] == SENSE_DATA_CHECK &&
364 * sense[1] == SENSE_DRIVE_ONLINE
365 * sense[3] == 0x36 (End Of Data)
367 * Further seeks might return a 'Volume Fenced'.
369 case TO_FSF:
370 case TO_FSB:
371 /* Trying to seek beyond end of recorded area */
372 return tape_34xx_erp_failed(request, -ENOSPC);
373 case TO_BSB:
374 return tape_34xx_erp_retry(request);
377 * sense[0] == SENSE_DATA_CHECK &&
378 * sense[1] == SENSE_DRIVE_ONLINE &&
379 * sense[3] == 0x36 (End Of Data)
381 case TO_LBL:
382 /* Block could not be located. */
383 tape_34xx_delete_sbid_from(device, 0);
384 return tape_34xx_erp_failed(request, -EIO);
386 case TO_RFO:
387 /* Read beyond end of recorded area -> 0 bytes read */
388 return tape_34xx_erp_failed(request, 0);
391 * sense[0] == SENSE_EQUIPMENT_CHECK &&
392 * sense[1] == SENSE_DRIVE_ONLINE &&
393 * sense[3] == 0x38 (Physical End Of Volume)
395 case TO_WRI:
396 /* Writing at physical end of volume */
397 return tape_34xx_erp_failed(request, -ENOSPC);
398 default:
399 PRINT_ERR("Invalid op in %s:%i\n",
400 __FUNCTION__, __LINE__);
401 return tape_34xx_erp_failed(request, 0);
405 /* Sensing special bits */
406 if (sense[0] & SENSE_BUS_OUT_CHECK)
407 return tape_34xx_erp_retry(request);
409 if (sense[0] & SENSE_DATA_CHECK) {
411 * hardware failure, damaged tape or improper
412 * operating conditions
414 switch (sense[3]) {
415 case 0x23:
416 /* a read data check occurred */
417 if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
418 inhibit_cu_recovery)
419 // data check is not permanent, may be
420 // recovered. We always use async-mode with
421 // cu-recovery, so this should *never* happen.
422 return tape_34xx_erp_bug(device, request,
423 irb, -4);
425 /* data check is permanent, CU recovery has failed */
426 PRINT_WARN("Permanent read error\n");
427 return tape_34xx_erp_failed(request, -EIO);
428 case 0x25:
429 // a write data check occurred
430 if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
431 inhibit_cu_recovery)
432 // data check is not permanent, may be
433 // recovered. We always use async-mode with
434 // cu-recovery, so this should *never* happen.
435 return tape_34xx_erp_bug(device, request,
436 irb, -5);
438 // data check is permanent, cu-recovery has failed
439 PRINT_WARN("Permanent write error\n");
440 return tape_34xx_erp_failed(request, -EIO);
441 case 0x26:
442 /* Data Check (read opposite) occurred. */
443 return tape_34xx_erp_read_opposite(device, request);
444 case 0x28:
445 /* ID-Mark at tape start couldn't be written */
446 PRINT_WARN("ID-Mark could not be written.\n");
447 return tape_34xx_erp_failed(request, -EIO);
448 case 0x31:
449 /* Tape void. Tried to read beyond end of device. */
450 PRINT_WARN("Read beyond end of recorded area.\n");
451 return tape_34xx_erp_failed(request, -ENOSPC);
452 case 0x41:
453 /* Record sequence error. */
454 PRINT_WARN("Invalid block-id sequence found.\n");
455 return tape_34xx_erp_failed(request, -EIO);
456 default:
457 /* all data checks for 3480 should result in one of
458 * the above erpa-codes. For 3490, other data-check
459 * conditions do exist. */
460 if (device->cdev->id.driver_info == tape_3480)
461 return tape_34xx_erp_bug(device, request,
462 irb, -6);
466 if (sense[0] & SENSE_OVERRUN)
467 return tape_34xx_erp_overrun(device, request, irb);
469 if (sense[1] & SENSE_RECORD_SEQUENCE_ERR)
470 return tape_34xx_erp_sequence(device, request, irb);
472 /* Sensing erpa codes */
473 switch (sense[3]) {
474 case 0x00:
475 /* Unit check with erpa code 0. Report and ignore. */
476 PRINT_WARN("Non-error sense was found. "
477 "Unit-check will be ignored.\n");
478 return TAPE_IO_SUCCESS;
479 case 0x21:
481 * Data streaming not operational. CU will switch to
482 * interlock mode. Reissue the command.
484 PRINT_WARN("Data streaming not operational. "
485 "Switching to interlock-mode.\n");
486 return tape_34xx_erp_retry(request);
487 case 0x22:
489 * Path equipment check. Might be drive adapter error, buffer
490 * error on the lower interface, internal path not usable,
491 * or error during cartridge load.
493 PRINT_WARN("A path equipment check occurred. One of the "
494 "following conditions occurred:\n");
495 PRINT_WARN("drive adapter error, buffer error on the lower "
496 "interface, internal path not usable, error "
497 "during cartridge load.\n");
498 return tape_34xx_erp_failed(request, -EIO);
499 case 0x24:
501 * Load display check. Load display was command was issued,
502 * but the drive is displaying a drive check message. Can
503 * be threated as "device end".
505 return tape_34xx_erp_succeeded(request);
506 case 0x27:
508 * Command reject. May indicate illegal channel program or
509 * buffer over/underrun. Since all channel programs are
510 * issued by this driver and ought be correct, we assume a
511 * over/underrun situation and retry the channel program.
513 return tape_34xx_erp_retry(request);
514 case 0x29:
516 * Function incompatible. Either the tape is idrc compressed
517 * but the hardware isn't capable to do idrc, or a perform
518 * subsystem func is issued and the CU is not on-line.
520 PRINT_WARN ("Function incompatible. Try to switch off idrc\n");
521 return tape_34xx_erp_failed(request, -EIO);
522 case 0x2a:
524 * Unsolicited environmental data. An internal counter
525 * overflows, we can ignore this and reissue the cmd.
527 return tape_34xx_erp_retry(request);
528 case 0x2b:
530 * Environmental data present. Indicates either unload
531 * completed ok or read buffered log command completed ok.
533 if (request->op == TO_RUN) {
534 /* Rewind unload completed ok. */
535 tape_med_state_set(device, MS_UNLOADED);
536 return tape_34xx_erp_succeeded(request);
538 /* tape_34xx doesn't use read buffered log commands. */
539 return tape_34xx_erp_bug(device, request, irb, sense[3]);
540 case 0x2c:
542 * Permanent equipment check. CU has tried recovery, but
543 * did not succeed.
545 return tape_34xx_erp_failed(request, -EIO);
546 case 0x2d:
547 /* Data security erase failure. */
548 if (request->op == TO_DSE)
549 return tape_34xx_erp_failed(request, -EIO);
550 /* Data security erase failure, but no such command issued. */
551 return tape_34xx_erp_bug(device, request, irb, sense[3]);
552 case 0x2e:
554 * Not capable. This indicates either that the drive fails
555 * reading the format id mark or that that format specified
556 * is not supported by the drive.
558 PRINT_WARN("Drive not capable processing the tape format!\n");
559 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
560 case 0x30:
561 /* The medium is write protected. */
562 PRINT_WARN("Medium is write protected!\n");
563 return tape_34xx_erp_failed(request, -EACCES);
564 case 0x32:
565 // Tension loss. We cannot recover this, it's an I/O error.
566 PRINT_WARN("The drive lost tape tension.\n");
567 return tape_34xx_erp_failed(request, -EIO);
568 case 0x33:
570 * Load Failure. The cartridge was not inserted correctly or
571 * the tape is not threaded correctly.
573 PRINT_WARN("Cartridge load failure. Reload the cartridge "
574 "and try again.\n");
575 tape_34xx_delete_sbid_from(device, 0);
576 return tape_34xx_erp_failed(request, -EIO);
577 case 0x34:
579 * Unload failure. The drive cannot maintain tape tension
580 * and control tape movement during an unload operation.
582 PRINT_WARN("Failure during cartridge unload. "
583 "Please try manually.\n");
584 if (request->op == TO_RUN)
585 return tape_34xx_erp_failed(request, -EIO);
586 return tape_34xx_erp_bug(device, request, irb, sense[3]);
587 case 0x35:
589 * Drive equipment check. One of the following:
590 * - cu cannot recover from a drive detected error
591 * - a check code message is shown on drive display
592 * - the cartridge loader does not respond correctly
593 * - a failure occurs during an index, load, or unload cycle
595 PRINT_WARN("Equipment check! Please check the drive and "
596 "the cartridge loader.\n");
597 return tape_34xx_erp_failed(request, -EIO);
598 case 0x36:
599 if (device->cdev->id.driver_info == tape_3490)
600 /* End of data. */
601 return tape_34xx_erp_failed(request, -EIO);
602 /* This erpa is reserved for 3480 */
603 return tape_34xx_erp_bug(device, request, irb, sense[3]);
604 case 0x37:
606 * Tape length error. The tape is shorter than reported in
607 * the beginning-of-tape data.
609 PRINT_WARN("Tape length error.\n");
610 return tape_34xx_erp_failed(request, -EIO);
611 case 0x38:
613 * Physical end of tape. A read/write operation reached
614 * the physical end of tape.
616 if (request->op==TO_WRI ||
617 request->op==TO_DSE ||
618 request->op==TO_WTM)
619 return tape_34xx_erp_failed(request, -ENOSPC);
620 return tape_34xx_erp_failed(request, -EIO);
621 case 0x39:
622 /* Backward at Beginning of tape. */
623 return tape_34xx_erp_failed(request, -EIO);
624 case 0x3a:
625 /* Drive switched to not ready. */
626 PRINT_WARN("Drive not ready. Turn the ready/not ready switch "
627 "to ready position and try again.\n");
628 return tape_34xx_erp_failed(request, -EIO);
629 case 0x3b:
630 /* Manual rewind or unload. This causes an I/O error. */
631 PRINT_WARN("Medium was rewound or unloaded manually.\n");
632 tape_34xx_delete_sbid_from(device, 0);
633 return tape_34xx_erp_failed(request, -EIO);
634 case 0x42:
636 * Degraded mode. A condition that can cause degraded
637 * performance is detected.
639 PRINT_WARN("Subsystem is running in degraded mode.\n");
640 return tape_34xx_erp_retry(request);
641 case 0x43:
642 /* Drive not ready. */
643 tape_34xx_delete_sbid_from(device, 0);
644 tape_med_state_set(device, MS_UNLOADED);
645 /* Some commands commands are successful even in this case */
646 if (sense[1] & SENSE_DRIVE_ONLINE) {
647 switch(request->op) {
648 case TO_ASSIGN:
649 case TO_UNASSIGN:
650 case TO_DIS:
651 case TO_NOP:
652 return tape_34xx_done(request);
653 break;
654 default:
655 break;
658 PRINT_WARN("The drive is not ready.\n");
659 return tape_34xx_erp_failed(request, -ENOMEDIUM);
660 case 0x44:
661 /* Locate Block unsuccessful. */
662 if (request->op != TO_BLOCK && request->op != TO_LBL)
663 /* No locate block was issued. */
664 return tape_34xx_erp_bug(device, request,
665 irb, sense[3]);
666 return tape_34xx_erp_failed(request, -EIO);
667 case 0x45:
668 /* The drive is assigned to a different channel path. */
669 PRINT_WARN("The drive is assigned elsewhere.\n");
670 return tape_34xx_erp_failed(request, -EIO);
671 case 0x46:
673 * Drive not on-line. Drive may be switched offline,
674 * the power supply may be switched off or
675 * the drive address may not be set correctly.
677 PRINT_WARN("The drive is not on-line.");
678 return tape_34xx_erp_failed(request, -EIO);
679 case 0x47:
680 /* Volume fenced. CU reports volume integrity is lost. */
681 PRINT_WARN("Volume fenced. The volume integrity is lost.\n");
682 tape_34xx_delete_sbid_from(device, 0);
683 return tape_34xx_erp_failed(request, -EIO);
684 case 0x48:
685 /* Log sense data and retry request. */
686 return tape_34xx_erp_retry(request);
687 case 0x49:
688 /* Bus out check. A parity check error on the bus was found. */
689 PRINT_WARN("Bus out check. A data transfer over the bus "
690 "has been corrupted.\n");
691 return tape_34xx_erp_failed(request, -EIO);
692 case 0x4a:
693 /* Control unit erp failed. */
694 PRINT_WARN("The control unit I/O error recovery failed.\n");
695 return tape_34xx_erp_failed(request, -EIO);
696 case 0x4b:
698 * CU and drive incompatible. The drive requests micro-program
699 * patches, which are not available on the CU.
701 PRINT_WARN("The drive needs microprogram patches from the "
702 "control unit, which are not available.\n");
703 return tape_34xx_erp_failed(request, -EIO);
704 case 0x4c:
706 * Recovered Check-One failure. Cu develops a hardware error,
707 * but is able to recover.
709 return tape_34xx_erp_retry(request);
710 case 0x4d:
711 if (device->cdev->id.driver_info == tape_3490)
713 * Resetting event received. Since the driver does
714 * not support resetting event recovery (which has to
715 * be handled by the I/O Layer), retry our command.
717 return tape_34xx_erp_retry(request);
718 /* This erpa is reserved for 3480. */
719 return tape_34xx_erp_bug(device, request, irb, sense[3]);
720 case 0x4e:
721 if (device->cdev->id.driver_info == tape_3490) {
723 * Maximum block size exceeded. This indicates, that
724 * the block to be written is larger than allowed for
725 * buffered mode.
727 PRINT_WARN("Maximum block size for buffered "
728 "mode exceeded.\n");
729 return tape_34xx_erp_failed(request, -ENOBUFS);
731 /* This erpa is reserved for 3480. */
732 return tape_34xx_erp_bug(device, request, irb, sense[3]);
733 case 0x50:
735 * Read buffered log (Overflow). CU is running in extended
736 * buffered log mode, and a counter overflows. This should
737 * never happen, since we're never running in extended
738 * buffered log mode.
740 return tape_34xx_erp_retry(request);
741 case 0x51:
743 * Read buffered log (EOV). EOF processing occurs while the
744 * CU is in extended buffered log mode. This should never
745 * happen, since we're never running in extended buffered
746 * log mode.
748 return tape_34xx_erp_retry(request);
749 case 0x52:
750 /* End of Volume complete. Rewind unload completed ok. */
751 if (request->op == TO_RUN) {
752 tape_med_state_set(device, MS_UNLOADED);
753 tape_34xx_delete_sbid_from(device, 0);
754 return tape_34xx_erp_succeeded(request);
756 return tape_34xx_erp_bug(device, request, irb, sense[3]);
757 case 0x53:
758 /* Global command intercept. */
759 return tape_34xx_erp_retry(request);
760 case 0x54:
761 /* Channel interface recovery (temporary). */
762 return tape_34xx_erp_retry(request);
763 case 0x55:
764 /* Channel interface recovery (permanent). */
765 PRINT_WARN("A permanent channel interface error occurred.\n");
766 return tape_34xx_erp_failed(request, -EIO);
767 case 0x56:
768 /* Channel protocol error. */
769 PRINT_WARN("A channel protocol error occurred.\n");
770 return tape_34xx_erp_failed(request, -EIO);
771 case 0x57:
772 if (device->cdev->id.driver_info == tape_3480) {
773 /* Attention intercept. */
774 PRINT_WARN("An attention intercept occurred, "
775 "which will be recovered.\n");
776 return tape_34xx_erp_retry(request);
777 } else {
778 /* Global status intercept. */
779 PRINT_WARN("An global status intercept was received, "
780 "which will be recovered.\n");
781 return tape_34xx_erp_retry(request);
783 case 0x5a:
785 * Tape length incompatible. The tape inserted is too long,
786 * which could cause damage to the tape or the drive.
788 PRINT_WARN("Tape Length Incompatible\n");
789 PRINT_WARN("Tape length exceeds IBM enhanced capacity "
790 "cartdridge length or a medium\n");
791 PRINT_WARN("with EC-CST identification mark has been mounted "
792 "in a device that writes\n");
793 PRINT_WARN("3480 or 3480 XF format.\n");
794 return tape_34xx_erp_failed(request, -EIO);
795 case 0x5b:
796 /* Format 3480 XF incompatible */
797 if (sense[1] & SENSE_BEGINNING_OF_TAPE)
798 /* The tape will get overwritten. */
799 return tape_34xx_erp_retry(request);
800 PRINT_WARN("Format 3480 XF Incompatible\n");
801 PRINT_WARN("Medium has been created in 3480 format. "
802 "To change the format writes\n");
803 PRINT_WARN("must be issued at BOT.\n");
804 return tape_34xx_erp_failed(request, -EIO);
805 case 0x5c:
806 /* Format 3480-2 XF incompatible */
807 PRINT_WARN("Format 3480-2 XF Incompatible\n");
808 PRINT_WARN("Device can only read 3480 or 3480 XF format.\n");
809 return tape_34xx_erp_failed(request, -EIO);
810 case 0x5d:
811 /* Tape length violation. */
812 PRINT_WARN("Tape Length Violation\n");
813 PRINT_WARN("The mounted tape exceeds IBM Enhanced Capacity "
814 "Cartdridge System Tape length.\n");
815 PRINT_WARN("This may cause damage to the drive or tape when "
816 "processing to the EOV\n");
817 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
818 case 0x5e:
819 /* Compaction algorithm incompatible. */
820 PRINT_WARN("Compaction Algorithm Incompatible\n");
821 PRINT_WARN("The volume is recorded using an incompatible "
822 "compaction algorithm,\n");
823 PRINT_WARN("which is not supported by the device.\n");
824 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
826 /* The following erpas should have been covered earlier. */
827 case 0x23: /* Read data check. */
828 case 0x25: /* Write data check. */
829 case 0x26: /* Data check (read opposite). */
830 case 0x28: /* Write id mark check. */
831 case 0x31: /* Tape void. */
832 case 0x40: /* Overrun error. */
833 case 0x41: /* Record sequence error. */
834 /* All other erpas are reserved for future use. */
835 default:
836 return tape_34xx_erp_bug(device, request, irb, sense[3]);
841 * 3480/3490 interrupt handler
843 static int
844 tape_34xx_irq(struct tape_device *device, struct tape_request *request,
845 struct irb *irb)
847 if (request == NULL)
848 return tape_34xx_unsolicited_irq(device, irb);
850 if ((irb->scsw.dstat & DEV_STAT_UNIT_EXCEP) &&
851 (irb->scsw.dstat & DEV_STAT_DEV_END) &&
852 (request->op == TO_WRI)) {
853 /* Write at end of volume */
854 PRINT_INFO("End of volume\n"); /* XXX */
855 return tape_34xx_erp_failed(request, -ENOSPC);
858 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
859 return tape_34xx_unit_check(device, request, irb);
861 if (irb->scsw.dstat & DEV_STAT_DEV_END) {
863 * A unit exception occurs on skipping over a tapemark block.
865 if (irb->scsw.dstat & DEV_STAT_UNIT_EXCEP) {
866 if (request->op == TO_BSB || request->op == TO_FSB)
867 request->rescnt++;
868 else
869 DBF_EVENT(5, "Unit Exception!\n");
871 return tape_34xx_done(request);
874 DBF_EVENT(6, "xunknownirq\n");
875 PRINT_ERR("Unexpected interrupt.\n");
876 PRINT_ERR("Current op is: %s", tape_op_verbose[request->op]);
877 tape_dump_sense(device, request, irb);
878 return TAPE_IO_STOP;
882 * ioctl_overload
884 static int
885 tape_34xx_ioctl(struct tape_device *device, unsigned int cmd, unsigned long arg)
887 if (cmd == TAPE390_DISPLAY) {
888 struct display_struct disp;
890 if (copy_from_user(&disp, (char __user *) arg, sizeof(disp)) != 0)
891 return -EFAULT;
893 return tape_std_display(device, &disp);
894 } else
895 return -EINVAL;
898 static inline void
899 tape_34xx_append_new_sbid(struct tape_34xx_block_id bid, struct list_head *l)
901 struct tape_34xx_sbid * new_sbid;
903 new_sbid = kmalloc(sizeof(*new_sbid), GFP_ATOMIC);
904 if (!new_sbid)
905 return;
907 new_sbid->bid = bid;
908 list_add(&new_sbid->list, l);
912 * Build up the search block ID list. The block ID consists of a logical
913 * block number and a hardware specific part. The hardware specific part
914 * helps the tape drive to speed up searching for a specific block.
916 static void
917 tape_34xx_add_sbid(struct tape_device *device, struct tape_34xx_block_id bid)
919 struct list_head * sbid_list;
920 struct tape_34xx_sbid * sbid;
921 struct list_head * l;
924 * immediately return if there is no list at all or the block to add
925 * is located in segment 1 of wrap 0 because this position is used
926 * if no hardware position data is supplied.
928 sbid_list = (struct list_head *) device->discdata;
929 if (!sbid_list || (bid.segment < 2 && bid.wrap == 0))
930 return;
933 * Search the position where to insert the new entry. Hardware
934 * acceleration uses only the segment and wrap number. So we
935 * need only one entry for a specific wrap/segment combination.
936 * If there is a block with a lower number but the same hard-
937 * ware position data we just update the block number in the
938 * existing entry.
940 list_for_each(l, sbid_list) {
941 sbid = list_entry(l, struct tape_34xx_sbid, list);
943 if (
944 (sbid->bid.segment == bid.segment) &&
945 (sbid->bid.wrap == bid.wrap)
947 if (bid.block < sbid->bid.block)
948 sbid->bid = bid;
949 else return;
950 break;
953 /* Sort in according to logical block number. */
954 if (bid.block < sbid->bid.block) {
955 tape_34xx_append_new_sbid(bid, l->prev);
956 break;
959 /* List empty or new block bigger than last entry. */
960 if (l == sbid_list)
961 tape_34xx_append_new_sbid(bid, l->prev);
963 DBF_LH(4, "Current list is:\n");
964 list_for_each(l, sbid_list) {
965 sbid = list_entry(l, struct tape_34xx_sbid, list);
966 DBF_LH(4, "%d:%03d@%05d\n",
967 sbid->bid.wrap,
968 sbid->bid.segment,
969 sbid->bid.block
975 * Delete all entries from the search block ID list that belong to tape blocks
976 * equal or higher than the given number.
978 static void
979 tape_34xx_delete_sbid_from(struct tape_device *device, int from)
981 struct list_head * sbid_list;
982 struct tape_34xx_sbid * sbid;
983 struct list_head * l;
984 struct list_head * n;
986 sbid_list = (struct list_head *) device->discdata;
987 if (!sbid_list)
988 return;
990 list_for_each_safe(l, n, sbid_list) {
991 sbid = list_entry(l, struct tape_34xx_sbid, list);
992 if (sbid->bid.block >= from) {
993 DBF_LH(4, "Delete sbid %d:%03d@%05d\n",
994 sbid->bid.wrap,
995 sbid->bid.segment,
996 sbid->bid.block
998 list_del(l);
999 kfree(sbid);
1005 * Merge hardware position data into a block id.
1007 static void
1008 tape_34xx_merge_sbid(
1009 struct tape_device * device,
1010 struct tape_34xx_block_id * bid
1012 struct tape_34xx_sbid * sbid;
1013 struct tape_34xx_sbid * sbid_to_use;
1014 struct list_head * sbid_list;
1015 struct list_head * l;
1017 sbid_list = (struct list_head *) device->discdata;
1018 bid->wrap = 0;
1019 bid->segment = 1;
1021 if (!sbid_list || list_empty(sbid_list))
1022 return;
1024 sbid_to_use = NULL;
1025 list_for_each(l, sbid_list) {
1026 sbid = list_entry(l, struct tape_34xx_sbid, list);
1028 if (sbid->bid.block >= bid->block)
1029 break;
1030 sbid_to_use = sbid;
1032 if (sbid_to_use) {
1033 bid->wrap = sbid_to_use->bid.wrap;
1034 bid->segment = sbid_to_use->bid.segment;
1035 DBF_LH(4, "Use %d:%03d@%05d for %05d\n",
1036 sbid_to_use->bid.wrap,
1037 sbid_to_use->bid.segment,
1038 sbid_to_use->bid.block,
1039 bid->block
1044 static int
1045 tape_34xx_setup_device(struct tape_device * device)
1047 int rc;
1048 struct list_head * discdata;
1050 DBF_EVENT(6, "34xx device setup\n");
1051 if ((rc = tape_std_assign(device)) == 0) {
1052 if ((rc = tape_34xx_medium_sense(device)) != 0) {
1053 DBF_LH(3, "34xx medium sense returned %d\n", rc);
1056 discdata = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1057 if (discdata) {
1058 INIT_LIST_HEAD(discdata);
1059 device->discdata = discdata;
1062 return rc;
1065 static void
1066 tape_34xx_cleanup_device(struct tape_device *device)
1068 tape_std_unassign(device);
1070 if (device->discdata) {
1071 tape_34xx_delete_sbid_from(device, 0);
1072 kfree(device->discdata);
1073 device->discdata = NULL;
1079 * MTTELL: Tell block. Return the number of block relative to current file.
1081 static int
1082 tape_34xx_mttell(struct tape_device *device, int mt_count)
1084 struct {
1085 struct tape_34xx_block_id cbid;
1086 struct tape_34xx_block_id dbid;
1087 } __attribute__ ((packed)) block_id;
1088 int rc;
1090 rc = tape_std_read_block_id(device, (__u64 *) &block_id);
1091 if (rc)
1092 return rc;
1094 tape_34xx_add_sbid(device, block_id.cbid);
1095 return block_id.cbid.block;
1099 * MTSEEK: seek to the specified block.
1101 static int
1102 tape_34xx_mtseek(struct tape_device *device, int mt_count)
1104 struct tape_request *request;
1105 struct tape_34xx_block_id * bid;
1107 if (mt_count > 0x3fffff) {
1108 DBF_EXCEPTION(6, "xsee parm\n");
1109 return -EINVAL;
1111 request = tape_alloc_request(3, 4);
1112 if (IS_ERR(request))
1113 return PTR_ERR(request);
1115 /* setup ccws */
1116 request->op = TO_LBL;
1117 bid = (struct tape_34xx_block_id *) request->cpdata;
1118 bid->format = (*device->modeset_byte & 0x08) ?
1119 TAPE34XX_FMT_3480_XF : TAPE34XX_FMT_3480;
1120 bid->block = mt_count;
1121 tape_34xx_merge_sbid(device, bid);
1123 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
1124 tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
1125 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
1127 /* execute it */
1128 return tape_do_io_free(device, request);
1131 #ifdef CONFIG_S390_TAPE_BLOCK
1133 * Tape block read for 34xx.
1135 static struct tape_request *
1136 tape_34xx_bread(struct tape_device *device, struct request *req)
1138 struct tape_request *request;
1139 struct ccw1 *ccw;
1140 int count = 0, i;
1141 unsigned off;
1142 char *dst;
1143 struct bio_vec *bv;
1144 struct bio *bio;
1145 struct tape_34xx_block_id * start_block;
1147 DBF_EVENT(6, "xBREDid:");
1149 /* Count the number of blocks for the request. */
1150 rq_for_each_bio(bio, req) {
1151 bio_for_each_segment(bv, bio, i) {
1152 count += bv->bv_len >> (TAPEBLOCK_HSEC_S2B + 9);
1156 /* Allocate the ccw request. */
1157 request = tape_alloc_request(3+count+1, 8);
1158 if (IS_ERR(request))
1159 return request;
1161 /* Setup ccws. */
1162 request->op = TO_BLOCK;
1163 start_block = (struct tape_34xx_block_id *) request->cpdata;
1164 start_block->block = req->sector >> TAPEBLOCK_HSEC_S2B;
1165 DBF_EVENT(6, "start_block = %i\n", start_block->block);
1167 ccw = request->cpaddr;
1168 ccw = tape_ccw_cc(ccw, MODE_SET_DB, 1, device->modeset_byte);
1171 * We always setup a nop after the mode set ccw. This slot is
1172 * used in tape_std_check_locate to insert a locate ccw if the
1173 * current tape position doesn't match the start block to be read.
1174 * The second nop will be filled with a read block id which is in
1175 * turn used by tape_34xx_free_bread to populate the segment bid
1176 * table.
1178 ccw = tape_ccw_cc(ccw, NOP, 0, NULL);
1179 ccw = tape_ccw_cc(ccw, NOP, 0, NULL);
1181 rq_for_each_bio(bio, req) {
1182 bio_for_each_segment(bv, bio, i) {
1183 dst = kmap(bv->bv_page) + bv->bv_offset;
1184 for (off = 0; off < bv->bv_len;
1185 off += TAPEBLOCK_HSEC_SIZE) {
1186 ccw->flags = CCW_FLAG_CC;
1187 ccw->cmd_code = READ_FORWARD;
1188 ccw->count = TAPEBLOCK_HSEC_SIZE;
1189 set_normalized_cda(ccw, (void*) __pa(dst));
1190 ccw++;
1191 dst += TAPEBLOCK_HSEC_SIZE;
1196 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
1197 DBF_EVENT(6, "xBREDccwg\n");
1198 return request;
1201 static void
1202 tape_34xx_free_bread (struct tape_request *request)
1204 struct ccw1* ccw;
1206 ccw = request->cpaddr;
1207 if ((ccw + 2)->cmd_code == READ_BLOCK_ID) {
1208 struct {
1209 struct tape_34xx_block_id cbid;
1210 struct tape_34xx_block_id dbid;
1211 } __attribute__ ((packed)) *rbi_data;
1213 rbi_data = request->cpdata;
1215 if (request->device)
1216 tape_34xx_add_sbid(request->device, rbi_data->cbid);
1219 /* Last ccw is a nop and doesn't need clear_normalized_cda */
1220 for (; ccw->flags & CCW_FLAG_CC; ccw++)
1221 if (ccw->cmd_code == READ_FORWARD)
1222 clear_normalized_cda(ccw);
1223 tape_free_request(request);
1227 * check_locate is called just before the tape request is passed to
1228 * the common io layer for execution. It has to check the current
1229 * tape position and insert a locate ccw if it doesn't match the
1230 * start block for the request.
1232 static void
1233 tape_34xx_check_locate(struct tape_device *device, struct tape_request *request)
1235 struct tape_34xx_block_id * start_block;
1237 start_block = (struct tape_34xx_block_id *) request->cpdata;
1238 if (start_block->block == device->blk_data.block_position)
1239 return;
1241 DBF_LH(4, "Block seek(%06d+%06d)\n", start_block->block, device->bof);
1242 start_block->wrap = 0;
1243 start_block->segment = 1;
1244 start_block->format = (*device->modeset_byte & 0x08) ?
1245 TAPE34XX_FMT_3480_XF :
1246 TAPE34XX_FMT_3480;
1247 start_block->block = start_block->block + device->bof;
1248 tape_34xx_merge_sbid(device, start_block);
1249 tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
1250 tape_ccw_cc(request->cpaddr + 2, READ_BLOCK_ID, 8, request->cpdata);
1252 #endif
1255 * List of 3480/3490 magnetic tape commands.
1257 static tape_mtop_fn tape_34xx_mtop[TAPE_NR_MTOPS] = {
1258 [MTRESET] = tape_std_mtreset,
1259 [MTFSF] = tape_std_mtfsf,
1260 [MTBSF] = tape_std_mtbsf,
1261 [MTFSR] = tape_std_mtfsr,
1262 [MTBSR] = tape_std_mtbsr,
1263 [MTWEOF] = tape_std_mtweof,
1264 [MTREW] = tape_std_mtrew,
1265 [MTOFFL] = tape_std_mtoffl,
1266 [MTNOP] = tape_std_mtnop,
1267 [MTRETEN] = tape_std_mtreten,
1268 [MTBSFM] = tape_std_mtbsfm,
1269 [MTFSFM] = tape_std_mtfsfm,
1270 [MTEOM] = tape_std_mteom,
1271 [MTERASE] = tape_std_mterase,
1272 [MTRAS1] = NULL,
1273 [MTRAS2] = NULL,
1274 [MTRAS3] = NULL,
1275 [MTSETBLK] = tape_std_mtsetblk,
1276 [MTSETDENSITY] = NULL,
1277 [MTSEEK] = tape_34xx_mtseek,
1278 [MTTELL] = tape_34xx_mttell,
1279 [MTSETDRVBUFFER] = NULL,
1280 [MTFSS] = NULL,
1281 [MTBSS] = NULL,
1282 [MTWSM] = NULL,
1283 [MTLOCK] = NULL,
1284 [MTUNLOCK] = NULL,
1285 [MTLOAD] = tape_std_mtload,
1286 [MTUNLOAD] = tape_std_mtunload,
1287 [MTCOMPRESSION] = tape_std_mtcompression,
1288 [MTSETPART] = NULL,
1289 [MTMKPART] = NULL
1293 * Tape discipline structure for 3480 and 3490.
1295 static struct tape_discipline tape_discipline_34xx = {
1296 .owner = THIS_MODULE,
1297 .setup_device = tape_34xx_setup_device,
1298 .cleanup_device = tape_34xx_cleanup_device,
1299 .process_eov = tape_std_process_eov,
1300 .irq = tape_34xx_irq,
1301 .read_block = tape_std_read_block,
1302 .write_block = tape_std_write_block,
1303 #ifdef CONFIG_S390_TAPE_BLOCK
1304 .bread = tape_34xx_bread,
1305 .free_bread = tape_34xx_free_bread,
1306 .check_locate = tape_34xx_check_locate,
1307 #endif
1308 .ioctl_fn = tape_34xx_ioctl,
1309 .mtop_array = tape_34xx_mtop
1312 static struct ccw_device_id tape_34xx_ids[] = {
1313 { CCW_DEVICE_DEVTYPE(0x3480, 0, 0x3480, 0), driver_info: tape_3480},
1314 { CCW_DEVICE_DEVTYPE(0x3490, 0, 0x3490, 0), driver_info: tape_3490},
1315 { /* end of list */ }
1318 static int
1319 tape_34xx_online(struct ccw_device *cdev)
1321 return tape_generic_online(
1322 cdev->dev.driver_data,
1323 &tape_discipline_34xx
1327 static int
1328 tape_34xx_offline(struct ccw_device *cdev)
1330 return tape_generic_offline(cdev->dev.driver_data);
1333 static struct ccw_driver tape_34xx_driver = {
1334 .name = "tape_34xx",
1335 .owner = THIS_MODULE,
1336 .ids = tape_34xx_ids,
1337 .probe = tape_generic_probe,
1338 .remove = tape_generic_remove,
1339 .set_online = tape_34xx_online,
1340 .set_offline = tape_34xx_offline,
1343 static int
1344 tape_34xx_init (void)
1346 int rc;
1348 TAPE_DBF_AREA = debug_register ( "tape_34xx", 2, 2, 4*sizeof(long));
1349 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1350 #ifdef DBF_LIKE_HELL
1351 debug_set_level(TAPE_DBF_AREA, 6);
1352 #endif
1354 DBF_EVENT(3, "34xx init\n");
1355 /* Register driver for 3480/3490 tapes. */
1356 rc = ccw_driver_register(&tape_34xx_driver);
1357 if (rc)
1358 DBF_EVENT(3, "34xx init failed\n");
1359 else
1360 DBF_EVENT(3, "34xx registered\n");
1361 return rc;
1364 static void
1365 tape_34xx_exit(void)
1367 ccw_driver_unregister(&tape_34xx_driver);
1369 debug_unregister(TAPE_DBF_AREA);
1372 MODULE_DEVICE_TABLE(ccw, tape_34xx_ids);
1373 MODULE_AUTHOR("(C) 2001-2002 IBM Deutschland Entwicklung GmbH");
1374 MODULE_DESCRIPTION("Linux on zSeries channel attached 3480 tape device driver");
1375 MODULE_LICENSE("GPL");
1377 module_init(tape_34xx_init);
1378 module_exit(tape_34xx_exit);