V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / s390 / char / tape_34xx.c
blob5b47e9cce75f6b277985ae9308692880722122a8
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/module.h>
12 #include <linux/init.h>
13 #include <linux/bio.h>
14 #include <linux/workqueue.h>
16 #define TAPE_DBF_AREA tape_34xx_dbf
18 #include "tape.h"
19 #include "tape_std.h"
21 #define PRINTK_HEADER "TAPE_34XX: "
24 * Pointer to debug area.
26 debug_info_t *TAPE_DBF_AREA = NULL;
27 EXPORT_SYMBOL(TAPE_DBF_AREA);
29 #define TAPE34XX_FMT_3480 0
30 #define TAPE34XX_FMT_3480_2_XF 1
31 #define TAPE34XX_FMT_3480_XF 2
33 struct tape_34xx_block_id {
34 unsigned int wrap : 1;
35 unsigned int segment : 7;
36 unsigned int format : 2;
37 unsigned int block : 22;
41 * A list of block ID's is used to faster seek blocks.
43 struct tape_34xx_sbid {
44 struct list_head list;
45 struct tape_34xx_block_id bid;
48 static void tape_34xx_delete_sbid_from(struct tape_device *, int);
51 * Medium sense for 34xx tapes. There is no 'real' medium sense call.
52 * So we just do a normal sense.
54 static int
55 tape_34xx_medium_sense(struct tape_device *device)
57 struct tape_request *request;
58 unsigned char *sense;
59 int rc;
61 request = tape_alloc_request(1, 32);
62 if (IS_ERR(request)) {
63 DBF_EXCEPTION(6, "MSEN fail\n");
64 return PTR_ERR(request);
67 request->op = TO_MSEN;
68 tape_ccw_end(request->cpaddr, SENSE, 32, request->cpdata);
70 rc = tape_do_io_interruptible(device, request);
71 if (request->rc == 0) {
72 sense = request->cpdata;
75 * This isn't quite correct. But since INTERVENTION_REQUIRED
76 * means that the drive is 'neither ready nor on-line' it is
77 * only slightly inaccurate to say there is no tape loaded if
78 * the drive isn't online...
80 if (sense[0] & SENSE_INTERVENTION_REQUIRED)
81 tape_med_state_set(device, MS_UNLOADED);
82 else
83 tape_med_state_set(device, MS_LOADED);
85 if (sense[1] & SENSE_WRITE_PROTECT)
86 device->tape_generic_status |= GMT_WR_PROT(~0);
87 else
88 device->tape_generic_status &= ~GMT_WR_PROT(~0);
89 } else {
90 DBF_EVENT(4, "tape_34xx: medium sense failed with rc=%d\n",
91 request->rc);
93 tape_free_request(request);
95 return rc;
98 struct tape_34xx_work {
99 struct tape_device *device;
100 enum tape_op op;
101 struct work_struct work;
105 * These functions are currently used only to schedule a medium_sense for
106 * later execution. This is because we get an interrupt whenever a medium
107 * is inserted but cannot call tape_do_io* from an interrupt context.
108 * Maybe that's useful for other actions we want to start from the
109 * interrupt handler.
111 static void
112 tape_34xx_work_handler(struct work_struct *work)
114 struct tape_34xx_work *p =
115 container_of(work, struct tape_34xx_work, work);
117 switch(p->op) {
118 case TO_MSEN:
119 tape_34xx_medium_sense(p->device);
120 break;
121 default:
122 DBF_EVENT(3, "T34XX: internal error: unknown work\n");
125 p->device = tape_put_device(p->device);
126 kfree(p);
129 static int
130 tape_34xx_schedule_work(struct tape_device *device, enum tape_op op)
132 struct tape_34xx_work *p;
134 if ((p = kzalloc(sizeof(*p), GFP_ATOMIC)) == NULL)
135 return -ENOMEM;
137 INIT_WORK(&p->work, tape_34xx_work_handler);
139 p->device = tape_get_device_reference(device);
140 p->op = op;
142 schedule_work(&p->work);
143 return 0;
147 * Done Handler is called when dev stat = DEVICE-END (successful operation)
149 static inline int
150 tape_34xx_done(struct tape_request *request)
152 DBF_EVENT(6, "%s done\n", tape_op_verbose[request->op]);
154 switch (request->op) {
155 case TO_DSE:
156 case TO_RUN:
157 case TO_WRI:
158 case TO_WTM:
159 case TO_ASSIGN:
160 case TO_UNASSIGN:
161 tape_34xx_delete_sbid_from(request->device, 0);
162 break;
163 default:
166 return TAPE_IO_SUCCESS;
169 static inline int
170 tape_34xx_erp_failed(struct tape_request *request, int rc)
172 DBF_EVENT(3, "Error recovery failed for %s (RC=%d)\n",
173 tape_op_verbose[request->op], rc);
174 return rc;
177 static inline int
178 tape_34xx_erp_succeeded(struct tape_request *request)
180 DBF_EVENT(3, "Error Recovery successful for %s\n",
181 tape_op_verbose[request->op]);
182 return tape_34xx_done(request);
185 static inline int
186 tape_34xx_erp_retry(struct tape_request *request)
188 DBF_EVENT(3, "xerp retr %s\n", tape_op_verbose[request->op]);
189 return TAPE_IO_RETRY;
193 * This function is called, when no request is outstanding and we get an
194 * interrupt
196 static int
197 tape_34xx_unsolicited_irq(struct tape_device *device, struct irb *irb)
199 if (irb->scsw.dstat == 0x85 /* READY */) {
200 /* A medium was inserted in the drive. */
201 DBF_EVENT(6, "xuud med\n");
202 tape_34xx_delete_sbid_from(device, 0);
203 tape_34xx_schedule_work(device, TO_MSEN);
204 } else {
205 DBF_EVENT(3, "unsol.irq! dev end: %08x\n", device->cdev_id);
206 PRINT_WARN("Unsolicited IRQ (Device End) caught.\n");
207 tape_dump_sense(device, NULL, irb);
209 return TAPE_IO_SUCCESS;
213 * Read Opposite Error Recovery Function:
214 * Used, when Read Forward does not work
216 static int
217 tape_34xx_erp_read_opposite(struct tape_device *device,
218 struct tape_request *request)
220 if (request->op == TO_RFO) {
222 * We did read forward, but the data could not be read
223 * *correctly*. We transform the request to a read backward
224 * and try again.
226 tape_std_read_backward(device, request);
227 return tape_34xx_erp_retry(request);
229 if (request->op != TO_RBA)
230 PRINT_ERR("read_opposite called with state:%s\n",
231 tape_op_verbose[request->op]);
233 * We tried to read forward and backward, but hat no
234 * success -> failed.
236 return tape_34xx_erp_failed(request, -EIO);
239 static int
240 tape_34xx_erp_bug(struct tape_device *device, struct tape_request *request,
241 struct irb *irb, int no)
243 if (request->op != TO_ASSIGN) {
244 PRINT_WARN("An unexpected condition #%d was caught in "
245 "tape error recovery.\n", no);
246 PRINT_WARN("Please report this incident.\n");
247 if (request)
248 PRINT_WARN("Operation of tape:%s\n",
249 tape_op_verbose[request->op]);
250 tape_dump_sense(device, request, irb);
252 return tape_34xx_erp_failed(request, -EIO);
256 * Handle data overrun between cu and drive. The channel speed might
257 * be too slow.
259 static int
260 tape_34xx_erp_overrun(struct tape_device *device, struct tape_request *request,
261 struct irb *irb)
263 if (irb->ecw[3] == 0x40) {
264 PRINT_WARN ("Data overrun error between control-unit "
265 "and drive. Use a faster channel connection, "
266 "if possible! \n");
267 return tape_34xx_erp_failed(request, -EIO);
269 return tape_34xx_erp_bug(device, request, irb, -1);
273 * Handle record sequence error.
275 static int
276 tape_34xx_erp_sequence(struct tape_device *device,
277 struct tape_request *request, struct irb *irb)
279 if (irb->ecw[3] == 0x41) {
281 * cu detected incorrect block-id sequence on tape.
283 PRINT_WARN("Illegal block-id sequence found!\n");
284 return tape_34xx_erp_failed(request, -EIO);
287 * Record sequence error bit is set, but erpa does not
288 * show record sequence error.
290 return tape_34xx_erp_bug(device, request, irb, -2);
294 * This function analyses the tape's sense-data in case of a unit-check.
295 * If possible, it tries to recover from the error. Else the user is
296 * informed about the problem.
298 static int
299 tape_34xx_unit_check(struct tape_device *device, struct tape_request *request,
300 struct irb *irb)
302 int inhibit_cu_recovery;
303 __u8* sense;
305 inhibit_cu_recovery = (*device->modeset_byte & 0x80) ? 1 : 0;
306 sense = irb->ecw;
308 #ifdef CONFIG_S390_TAPE_BLOCK
309 if (request->op == TO_BLOCK) {
311 * Recovery for block device requests. Set the block_position
312 * to something invalid and retry.
314 device->blk_data.block_position = -1;
315 if (request->retries-- <= 0)
316 return tape_34xx_erp_failed(request, -EIO);
317 else
318 return tape_34xx_erp_retry(request);
320 #endif
322 if (
323 sense[0] & SENSE_COMMAND_REJECT &&
324 sense[1] & SENSE_WRITE_PROTECT
326 if (
327 request->op == TO_DSE ||
328 request->op == TO_WRI ||
329 request->op == TO_WTM
331 /* medium is write protected */
332 return tape_34xx_erp_failed(request, -EACCES);
333 } else {
334 return tape_34xx_erp_bug(device, request, irb, -3);
339 * Special cases for various tape-states when reaching
340 * end of recorded area
342 * FIXME: Maybe a special case of the special case:
343 * sense[0] == SENSE_EQUIPMENT_CHECK &&
344 * sense[1] == SENSE_DRIVE_ONLINE &&
345 * sense[3] == 0x47 (Volume Fenced)
347 * This was caused by continued FSF or FSR after an
348 * 'End Of Data'.
350 if ((
351 sense[0] == SENSE_DATA_CHECK ||
352 sense[0] == SENSE_EQUIPMENT_CHECK ||
353 sense[0] == SENSE_EQUIPMENT_CHECK + SENSE_DEFERRED_UNIT_CHECK
354 ) && (
355 sense[1] == SENSE_DRIVE_ONLINE ||
356 sense[1] == SENSE_BEGINNING_OF_TAPE + SENSE_WRITE_MODE
357 )) {
358 switch (request->op) {
360 * sense[0] == SENSE_DATA_CHECK &&
361 * sense[1] == SENSE_DRIVE_ONLINE
362 * sense[3] == 0x36 (End Of Data)
364 * Further seeks might return a 'Volume Fenced'.
366 case TO_FSF:
367 case TO_FSB:
368 /* Trying to seek beyond end of recorded area */
369 return tape_34xx_erp_failed(request, -ENOSPC);
370 case TO_BSB:
371 return tape_34xx_erp_retry(request);
374 * sense[0] == SENSE_DATA_CHECK &&
375 * sense[1] == SENSE_DRIVE_ONLINE &&
376 * sense[3] == 0x36 (End Of Data)
378 case TO_LBL:
379 /* Block could not be located. */
380 tape_34xx_delete_sbid_from(device, 0);
381 return tape_34xx_erp_failed(request, -EIO);
383 case TO_RFO:
384 /* Read beyond end of recorded area -> 0 bytes read */
385 return tape_34xx_erp_failed(request, 0);
388 * sense[0] == SENSE_EQUIPMENT_CHECK &&
389 * sense[1] == SENSE_DRIVE_ONLINE &&
390 * sense[3] == 0x38 (Physical End Of Volume)
392 case TO_WRI:
393 /* Writing at physical end of volume */
394 return tape_34xx_erp_failed(request, -ENOSPC);
395 default:
396 PRINT_ERR("Invalid op in %s:%i\n",
397 __FUNCTION__, __LINE__);
398 return tape_34xx_erp_failed(request, 0);
402 /* Sensing special bits */
403 if (sense[0] & SENSE_BUS_OUT_CHECK)
404 return tape_34xx_erp_retry(request);
406 if (sense[0] & SENSE_DATA_CHECK) {
408 * hardware failure, damaged tape or improper
409 * operating conditions
411 switch (sense[3]) {
412 case 0x23:
413 /* a read data check occurred */
414 if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
415 inhibit_cu_recovery)
416 // data check is not permanent, may be
417 // recovered. We always use async-mode with
418 // cu-recovery, so this should *never* happen.
419 return tape_34xx_erp_bug(device, request,
420 irb, -4);
422 /* data check is permanent, CU recovery has failed */
423 PRINT_WARN("Permanent read error\n");
424 return tape_34xx_erp_failed(request, -EIO);
425 case 0x25:
426 // a write data check occurred
427 if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
428 inhibit_cu_recovery)
429 // data check is not permanent, may be
430 // recovered. We always use async-mode with
431 // cu-recovery, so this should *never* happen.
432 return tape_34xx_erp_bug(device, request,
433 irb, -5);
435 // data check is permanent, cu-recovery has failed
436 PRINT_WARN("Permanent write error\n");
437 return tape_34xx_erp_failed(request, -EIO);
438 case 0x26:
439 /* Data Check (read opposite) occurred. */
440 return tape_34xx_erp_read_opposite(device, request);
441 case 0x28:
442 /* ID-Mark at tape start couldn't be written */
443 PRINT_WARN("ID-Mark could not be written.\n");
444 return tape_34xx_erp_failed(request, -EIO);
445 case 0x31:
446 /* Tape void. Tried to read beyond end of device. */
447 PRINT_WARN("Read beyond end of recorded area.\n");
448 return tape_34xx_erp_failed(request, -ENOSPC);
449 case 0x41:
450 /* Record sequence error. */
451 PRINT_WARN("Invalid block-id sequence found.\n");
452 return tape_34xx_erp_failed(request, -EIO);
453 default:
454 /* all data checks for 3480 should result in one of
455 * the above erpa-codes. For 3490, other data-check
456 * conditions do exist. */
457 if (device->cdev->id.driver_info == tape_3480)
458 return tape_34xx_erp_bug(device, request,
459 irb, -6);
463 if (sense[0] & SENSE_OVERRUN)
464 return tape_34xx_erp_overrun(device, request, irb);
466 if (sense[1] & SENSE_RECORD_SEQUENCE_ERR)
467 return tape_34xx_erp_sequence(device, request, irb);
469 /* Sensing erpa codes */
470 switch (sense[3]) {
471 case 0x00:
472 /* Unit check with erpa code 0. Report and ignore. */
473 PRINT_WARN("Non-error sense was found. "
474 "Unit-check will be ignored.\n");
475 return TAPE_IO_SUCCESS;
476 case 0x21:
478 * Data streaming not operational. CU will switch to
479 * interlock mode. Reissue the command.
481 PRINT_WARN("Data streaming not operational. "
482 "Switching to interlock-mode.\n");
483 return tape_34xx_erp_retry(request);
484 case 0x22:
486 * Path equipment check. Might be drive adapter error, buffer
487 * error on the lower interface, internal path not usable,
488 * or error during cartridge load.
490 PRINT_WARN("A path equipment check occurred. One of the "
491 "following conditions occurred:\n");
492 PRINT_WARN("drive adapter error, buffer error on the lower "
493 "interface, internal path not usable, error "
494 "during cartridge load.\n");
495 return tape_34xx_erp_failed(request, -EIO);
496 case 0x24:
498 * Load display check. Load display was command was issued,
499 * but the drive is displaying a drive check message. Can
500 * be threated as "device end".
502 return tape_34xx_erp_succeeded(request);
503 case 0x27:
505 * Command reject. May indicate illegal channel program or
506 * buffer over/underrun. Since all channel programs are
507 * issued by this driver and ought be correct, we assume a
508 * over/underrun situation and retry the channel program.
510 return tape_34xx_erp_retry(request);
511 case 0x29:
513 * Function incompatible. Either the tape is idrc compressed
514 * but the hardware isn't capable to do idrc, or a perform
515 * subsystem func is issued and the CU is not on-line.
517 PRINT_WARN ("Function incompatible. Try to switch off idrc\n");
518 return tape_34xx_erp_failed(request, -EIO);
519 case 0x2a:
521 * Unsolicited environmental data. An internal counter
522 * overflows, we can ignore this and reissue the cmd.
524 return tape_34xx_erp_retry(request);
525 case 0x2b:
527 * Environmental data present. Indicates either unload
528 * completed ok or read buffered log command completed ok.
530 if (request->op == TO_RUN) {
531 /* Rewind unload completed ok. */
532 tape_med_state_set(device, MS_UNLOADED);
533 return tape_34xx_erp_succeeded(request);
535 /* tape_34xx doesn't use read buffered log commands. */
536 return tape_34xx_erp_bug(device, request, irb, sense[3]);
537 case 0x2c:
539 * Permanent equipment check. CU has tried recovery, but
540 * did not succeed.
542 return tape_34xx_erp_failed(request, -EIO);
543 case 0x2d:
544 /* Data security erase failure. */
545 if (request->op == TO_DSE)
546 return tape_34xx_erp_failed(request, -EIO);
547 /* Data security erase failure, but no such command issued. */
548 return tape_34xx_erp_bug(device, request, irb, sense[3]);
549 case 0x2e:
551 * Not capable. This indicates either that the drive fails
552 * reading the format id mark or that that format specified
553 * is not supported by the drive.
555 PRINT_WARN("Drive not capable processing the tape format!\n");
556 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
557 case 0x30:
558 /* The medium is write protected. */
559 PRINT_WARN("Medium is write protected!\n");
560 return tape_34xx_erp_failed(request, -EACCES);
561 case 0x32:
562 // Tension loss. We cannot recover this, it's an I/O error.
563 PRINT_WARN("The drive lost tape tension.\n");
564 return tape_34xx_erp_failed(request, -EIO);
565 case 0x33:
567 * Load Failure. The cartridge was not inserted correctly or
568 * the tape is not threaded correctly.
570 PRINT_WARN("Cartridge load failure. Reload the cartridge "
571 "and try again.\n");
572 tape_34xx_delete_sbid_from(device, 0);
573 return tape_34xx_erp_failed(request, -EIO);
574 case 0x34:
576 * Unload failure. The drive cannot maintain tape tension
577 * and control tape movement during an unload operation.
579 PRINT_WARN("Failure during cartridge unload. "
580 "Please try manually.\n");
581 if (request->op == TO_RUN)
582 return tape_34xx_erp_failed(request, -EIO);
583 return tape_34xx_erp_bug(device, request, irb, sense[3]);
584 case 0x35:
586 * Drive equipment check. One of the following:
587 * - cu cannot recover from a drive detected error
588 * - a check code message is shown on drive display
589 * - the cartridge loader does not respond correctly
590 * - a failure occurs during an index, load, or unload cycle
592 PRINT_WARN("Equipment check! Please check the drive and "
593 "the cartridge loader.\n");
594 return tape_34xx_erp_failed(request, -EIO);
595 case 0x36:
596 if (device->cdev->id.driver_info == tape_3490)
597 /* End of data. */
598 return tape_34xx_erp_failed(request, -EIO);
599 /* This erpa is reserved for 3480 */
600 return tape_34xx_erp_bug(device, request, irb, sense[3]);
601 case 0x37:
603 * Tape length error. The tape is shorter than reported in
604 * the beginning-of-tape data.
606 PRINT_WARN("Tape length error.\n");
607 return tape_34xx_erp_failed(request, -EIO);
608 case 0x38:
610 * Physical end of tape. A read/write operation reached
611 * the physical end of tape.
613 if (request->op==TO_WRI ||
614 request->op==TO_DSE ||
615 request->op==TO_WTM)
616 return tape_34xx_erp_failed(request, -ENOSPC);
617 return tape_34xx_erp_failed(request, -EIO);
618 case 0x39:
619 /* Backward at Beginning of tape. */
620 return tape_34xx_erp_failed(request, -EIO);
621 case 0x3a:
622 /* Drive switched to not ready. */
623 PRINT_WARN("Drive not ready. Turn the ready/not ready switch "
624 "to ready position and try again.\n");
625 return tape_34xx_erp_failed(request, -EIO);
626 case 0x3b:
627 /* Manual rewind or unload. This causes an I/O error. */
628 PRINT_WARN("Medium was rewound or unloaded manually.\n");
629 tape_34xx_delete_sbid_from(device, 0);
630 return tape_34xx_erp_failed(request, -EIO);
631 case 0x42:
633 * Degraded mode. A condition that can cause degraded
634 * performance is detected.
636 PRINT_WARN("Subsystem is running in degraded mode.\n");
637 return tape_34xx_erp_retry(request);
638 case 0x43:
639 /* Drive not ready. */
640 tape_34xx_delete_sbid_from(device, 0);
641 tape_med_state_set(device, MS_UNLOADED);
642 /* Some commands commands are successful even in this case */
643 if (sense[1] & SENSE_DRIVE_ONLINE) {
644 switch(request->op) {
645 case TO_ASSIGN:
646 case TO_UNASSIGN:
647 case TO_DIS:
648 case TO_NOP:
649 return tape_34xx_done(request);
650 break;
651 default:
652 break;
655 PRINT_WARN("The drive is not ready.\n");
656 return tape_34xx_erp_failed(request, -ENOMEDIUM);
657 case 0x44:
658 /* Locate Block unsuccessful. */
659 if (request->op != TO_BLOCK && request->op != TO_LBL)
660 /* No locate block was issued. */
661 return tape_34xx_erp_bug(device, request,
662 irb, sense[3]);
663 return tape_34xx_erp_failed(request, -EIO);
664 case 0x45:
665 /* The drive is assigned to a different channel path. */
666 PRINT_WARN("The drive is assigned elsewhere.\n");
667 return tape_34xx_erp_failed(request, -EIO);
668 case 0x46:
670 * Drive not on-line. Drive may be switched offline,
671 * the power supply may be switched off or
672 * the drive address may not be set correctly.
674 PRINT_WARN("The drive is not on-line.");
675 return tape_34xx_erp_failed(request, -EIO);
676 case 0x47:
677 /* Volume fenced. CU reports volume integrity is lost. */
678 PRINT_WARN("Volume fenced. The volume integrity is lost.\n");
679 tape_34xx_delete_sbid_from(device, 0);
680 return tape_34xx_erp_failed(request, -EIO);
681 case 0x48:
682 /* Log sense data and retry request. */
683 return tape_34xx_erp_retry(request);
684 case 0x49:
685 /* Bus out check. A parity check error on the bus was found. */
686 PRINT_WARN("Bus out check. A data transfer over the bus "
687 "has been corrupted.\n");
688 return tape_34xx_erp_failed(request, -EIO);
689 case 0x4a:
690 /* Control unit erp failed. */
691 PRINT_WARN("The control unit I/O error recovery failed.\n");
692 return tape_34xx_erp_failed(request, -EIO);
693 case 0x4b:
695 * CU and drive incompatible. The drive requests micro-program
696 * patches, which are not available on the CU.
698 PRINT_WARN("The drive needs microprogram patches from the "
699 "control unit, which are not available.\n");
700 return tape_34xx_erp_failed(request, -EIO);
701 case 0x4c:
703 * Recovered Check-One failure. Cu develops a hardware error,
704 * but is able to recover.
706 return tape_34xx_erp_retry(request);
707 case 0x4d:
708 if (device->cdev->id.driver_info == tape_3490)
710 * Resetting event received. Since the driver does
711 * not support resetting event recovery (which has to
712 * be handled by the I/O Layer), retry our command.
714 return tape_34xx_erp_retry(request);
715 /* This erpa is reserved for 3480. */
716 return tape_34xx_erp_bug(device, request, irb, sense[3]);
717 case 0x4e:
718 if (device->cdev->id.driver_info == tape_3490) {
720 * Maximum block size exceeded. This indicates, that
721 * the block to be written is larger than allowed for
722 * buffered mode.
724 PRINT_WARN("Maximum block size for buffered "
725 "mode exceeded.\n");
726 return tape_34xx_erp_failed(request, -ENOBUFS);
728 /* This erpa is reserved for 3480. */
729 return tape_34xx_erp_bug(device, request, irb, sense[3]);
730 case 0x50:
732 * Read buffered log (Overflow). CU is running in extended
733 * buffered log mode, and a counter overflows. This should
734 * never happen, since we're never running in extended
735 * buffered log mode.
737 return tape_34xx_erp_retry(request);
738 case 0x51:
740 * Read buffered log (EOV). EOF processing occurs while the
741 * CU is in extended buffered log mode. This should never
742 * happen, since we're never running in extended buffered
743 * log mode.
745 return tape_34xx_erp_retry(request);
746 case 0x52:
747 /* End of Volume complete. Rewind unload completed ok. */
748 if (request->op == TO_RUN) {
749 tape_med_state_set(device, MS_UNLOADED);
750 tape_34xx_delete_sbid_from(device, 0);
751 return tape_34xx_erp_succeeded(request);
753 return tape_34xx_erp_bug(device, request, irb, sense[3]);
754 case 0x53:
755 /* Global command intercept. */
756 return tape_34xx_erp_retry(request);
757 case 0x54:
758 /* Channel interface recovery (temporary). */
759 return tape_34xx_erp_retry(request);
760 case 0x55:
761 /* Channel interface recovery (permanent). */
762 PRINT_WARN("A permanent channel interface error occurred.\n");
763 return tape_34xx_erp_failed(request, -EIO);
764 case 0x56:
765 /* Channel protocol error. */
766 PRINT_WARN("A channel protocol error occurred.\n");
767 return tape_34xx_erp_failed(request, -EIO);
768 case 0x57:
769 if (device->cdev->id.driver_info == tape_3480) {
770 /* Attention intercept. */
771 PRINT_WARN("An attention intercept occurred, "
772 "which will be recovered.\n");
773 return tape_34xx_erp_retry(request);
774 } else {
775 /* Global status intercept. */
776 PRINT_WARN("An global status intercept was received, "
777 "which will be recovered.\n");
778 return tape_34xx_erp_retry(request);
780 case 0x5a:
782 * Tape length incompatible. The tape inserted is too long,
783 * which could cause damage to the tape or the drive.
785 PRINT_WARN("Tape Length Incompatible\n");
786 PRINT_WARN("Tape length exceeds IBM enhanced capacity "
787 "cartdridge length or a medium\n");
788 PRINT_WARN("with EC-CST identification mark has been mounted "
789 "in a device that writes\n");
790 PRINT_WARN("3480 or 3480 XF format.\n");
791 return tape_34xx_erp_failed(request, -EIO);
792 case 0x5b:
793 /* Format 3480 XF incompatible */
794 if (sense[1] & SENSE_BEGINNING_OF_TAPE)
795 /* The tape will get overwritten. */
796 return tape_34xx_erp_retry(request);
797 PRINT_WARN("Format 3480 XF Incompatible\n");
798 PRINT_WARN("Medium has been created in 3480 format. "
799 "To change the format writes\n");
800 PRINT_WARN("must be issued at BOT.\n");
801 return tape_34xx_erp_failed(request, -EIO);
802 case 0x5c:
803 /* Format 3480-2 XF incompatible */
804 PRINT_WARN("Format 3480-2 XF Incompatible\n");
805 PRINT_WARN("Device can only read 3480 or 3480 XF format.\n");
806 return tape_34xx_erp_failed(request, -EIO);
807 case 0x5d:
808 /* Tape length violation. */
809 PRINT_WARN("Tape Length Violation\n");
810 PRINT_WARN("The mounted tape exceeds IBM Enhanced Capacity "
811 "Cartdridge System Tape length.\n");
812 PRINT_WARN("This may cause damage to the drive or tape when "
813 "processing to the EOV\n");
814 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
815 case 0x5e:
816 /* Compaction algorithm incompatible. */
817 PRINT_WARN("Compaction Algorithm Incompatible\n");
818 PRINT_WARN("The volume is recorded using an incompatible "
819 "compaction algorithm,\n");
820 PRINT_WARN("which is not supported by the device.\n");
821 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
823 /* The following erpas should have been covered earlier. */
824 case 0x23: /* Read data check. */
825 case 0x25: /* Write data check. */
826 case 0x26: /* Data check (read opposite). */
827 case 0x28: /* Write id mark check. */
828 case 0x31: /* Tape void. */
829 case 0x40: /* Overrun error. */
830 case 0x41: /* Record sequence error. */
831 /* All other erpas are reserved for future use. */
832 default:
833 return tape_34xx_erp_bug(device, request, irb, sense[3]);
838 * 3480/3490 interrupt handler
840 static int
841 tape_34xx_irq(struct tape_device *device, struct tape_request *request,
842 struct irb *irb)
844 if (request == NULL)
845 return tape_34xx_unsolicited_irq(device, irb);
847 if ((irb->scsw.dstat & DEV_STAT_UNIT_EXCEP) &&
848 (irb->scsw.dstat & DEV_STAT_DEV_END) &&
849 (request->op == TO_WRI)) {
850 /* Write at end of volume */
851 PRINT_INFO("End of volume\n"); /* XXX */
852 return tape_34xx_erp_failed(request, -ENOSPC);
855 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
856 return tape_34xx_unit_check(device, request, irb);
858 if (irb->scsw.dstat & DEV_STAT_DEV_END) {
860 * A unit exception occurs on skipping over a tapemark block.
862 if (irb->scsw.dstat & DEV_STAT_UNIT_EXCEP) {
863 if (request->op == TO_BSB || request->op == TO_FSB)
864 request->rescnt++;
865 else
866 DBF_EVENT(5, "Unit Exception!\n");
868 return tape_34xx_done(request);
871 DBF_EVENT(6, "xunknownirq\n");
872 PRINT_ERR("Unexpected interrupt.\n");
873 PRINT_ERR("Current op is: %s", tape_op_verbose[request->op]);
874 tape_dump_sense(device, request, irb);
875 return TAPE_IO_STOP;
879 * ioctl_overload
881 static int
882 tape_34xx_ioctl(struct tape_device *device, unsigned int cmd, unsigned long arg)
884 if (cmd == TAPE390_DISPLAY) {
885 struct display_struct disp;
887 if (copy_from_user(&disp, (char __user *) arg, sizeof(disp)) != 0)
888 return -EFAULT;
890 return tape_std_display(device, &disp);
891 } else
892 return -EINVAL;
895 static inline void
896 tape_34xx_append_new_sbid(struct tape_34xx_block_id bid, struct list_head *l)
898 struct tape_34xx_sbid * new_sbid;
900 new_sbid = kmalloc(sizeof(*new_sbid), GFP_ATOMIC);
901 if (!new_sbid)
902 return;
904 new_sbid->bid = bid;
905 list_add(&new_sbid->list, l);
909 * Build up the search block ID list. The block ID consists of a logical
910 * block number and a hardware specific part. The hardware specific part
911 * helps the tape drive to speed up searching for a specific block.
913 static void
914 tape_34xx_add_sbid(struct tape_device *device, struct tape_34xx_block_id bid)
916 struct list_head * sbid_list;
917 struct tape_34xx_sbid * sbid;
918 struct list_head * l;
921 * immediately return if there is no list at all or the block to add
922 * is located in segment 1 of wrap 0 because this position is used
923 * if no hardware position data is supplied.
925 sbid_list = (struct list_head *) device->discdata;
926 if (!sbid_list || (bid.segment < 2 && bid.wrap == 0))
927 return;
930 * Search the position where to insert the new entry. Hardware
931 * acceleration uses only the segment and wrap number. So we
932 * need only one entry for a specific wrap/segment combination.
933 * If there is a block with a lower number but the same hard-
934 * ware position data we just update the block number in the
935 * existing entry.
937 list_for_each(l, sbid_list) {
938 sbid = list_entry(l, struct tape_34xx_sbid, list);
940 if (
941 (sbid->bid.segment == bid.segment) &&
942 (sbid->bid.wrap == bid.wrap)
944 if (bid.block < sbid->bid.block)
945 sbid->bid = bid;
946 else return;
947 break;
950 /* Sort in according to logical block number. */
951 if (bid.block < sbid->bid.block) {
952 tape_34xx_append_new_sbid(bid, l->prev);
953 break;
956 /* List empty or new block bigger than last entry. */
957 if (l == sbid_list)
958 tape_34xx_append_new_sbid(bid, l->prev);
960 DBF_LH(4, "Current list is:\n");
961 list_for_each(l, sbid_list) {
962 sbid = list_entry(l, struct tape_34xx_sbid, list);
963 DBF_LH(4, "%d:%03d@%05d\n",
964 sbid->bid.wrap,
965 sbid->bid.segment,
966 sbid->bid.block
972 * Delete all entries from the search block ID list that belong to tape blocks
973 * equal or higher than the given number.
975 static void
976 tape_34xx_delete_sbid_from(struct tape_device *device, int from)
978 struct list_head * sbid_list;
979 struct tape_34xx_sbid * sbid;
980 struct list_head * l;
981 struct list_head * n;
983 sbid_list = (struct list_head *) device->discdata;
984 if (!sbid_list)
985 return;
987 list_for_each_safe(l, n, sbid_list) {
988 sbid = list_entry(l, struct tape_34xx_sbid, list);
989 if (sbid->bid.block >= from) {
990 DBF_LH(4, "Delete sbid %d:%03d@%05d\n",
991 sbid->bid.wrap,
992 sbid->bid.segment,
993 sbid->bid.block
995 list_del(l);
996 kfree(sbid);
1002 * Merge hardware position data into a block id.
1004 static void
1005 tape_34xx_merge_sbid(
1006 struct tape_device * device,
1007 struct tape_34xx_block_id * bid
1009 struct tape_34xx_sbid * sbid;
1010 struct tape_34xx_sbid * sbid_to_use;
1011 struct list_head * sbid_list;
1012 struct list_head * l;
1014 sbid_list = (struct list_head *) device->discdata;
1015 bid->wrap = 0;
1016 bid->segment = 1;
1018 if (!sbid_list || list_empty(sbid_list))
1019 return;
1021 sbid_to_use = NULL;
1022 list_for_each(l, sbid_list) {
1023 sbid = list_entry(l, struct tape_34xx_sbid, list);
1025 if (sbid->bid.block >= bid->block)
1026 break;
1027 sbid_to_use = sbid;
1029 if (sbid_to_use) {
1030 bid->wrap = sbid_to_use->bid.wrap;
1031 bid->segment = sbid_to_use->bid.segment;
1032 DBF_LH(4, "Use %d:%03d@%05d for %05d\n",
1033 sbid_to_use->bid.wrap,
1034 sbid_to_use->bid.segment,
1035 sbid_to_use->bid.block,
1036 bid->block
1041 static int
1042 tape_34xx_setup_device(struct tape_device * device)
1044 int rc;
1045 struct list_head * discdata;
1047 DBF_EVENT(6, "34xx device setup\n");
1048 if ((rc = tape_std_assign(device)) == 0) {
1049 if ((rc = tape_34xx_medium_sense(device)) != 0) {
1050 DBF_LH(3, "34xx medium sense returned %d\n", rc);
1053 discdata = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1054 if (discdata) {
1055 INIT_LIST_HEAD(discdata);
1056 device->discdata = discdata;
1059 return rc;
1062 static void
1063 tape_34xx_cleanup_device(struct tape_device *device)
1065 tape_std_unassign(device);
1067 if (device->discdata) {
1068 tape_34xx_delete_sbid_from(device, 0);
1069 kfree(device->discdata);
1070 device->discdata = NULL;
1076 * MTTELL: Tell block. Return the number of block relative to current file.
1078 static int
1079 tape_34xx_mttell(struct tape_device *device, int mt_count)
1081 struct {
1082 struct tape_34xx_block_id cbid;
1083 struct tape_34xx_block_id dbid;
1084 } __attribute__ ((packed)) block_id;
1085 int rc;
1087 rc = tape_std_read_block_id(device, (__u64 *) &block_id);
1088 if (rc)
1089 return rc;
1091 tape_34xx_add_sbid(device, block_id.cbid);
1092 return block_id.cbid.block;
1096 * MTSEEK: seek to the specified block.
1098 static int
1099 tape_34xx_mtseek(struct tape_device *device, int mt_count)
1101 struct tape_request *request;
1102 struct tape_34xx_block_id * bid;
1104 if (mt_count > 0x3fffff) {
1105 DBF_EXCEPTION(6, "xsee parm\n");
1106 return -EINVAL;
1108 request = tape_alloc_request(3, 4);
1109 if (IS_ERR(request))
1110 return PTR_ERR(request);
1112 /* setup ccws */
1113 request->op = TO_LBL;
1114 bid = (struct tape_34xx_block_id *) request->cpdata;
1115 bid->format = (*device->modeset_byte & 0x08) ?
1116 TAPE34XX_FMT_3480_XF : TAPE34XX_FMT_3480;
1117 bid->block = mt_count;
1118 tape_34xx_merge_sbid(device, bid);
1120 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
1121 tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
1122 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
1124 /* execute it */
1125 return tape_do_io_free(device, request);
1128 #ifdef CONFIG_S390_TAPE_BLOCK
1130 * Tape block read for 34xx.
1132 static struct tape_request *
1133 tape_34xx_bread(struct tape_device *device, struct request *req)
1135 struct tape_request *request;
1136 struct ccw1 *ccw;
1137 int count = 0;
1138 unsigned off;
1139 char *dst;
1140 struct bio_vec *bv;
1141 struct req_iterator iter;
1142 struct tape_34xx_block_id * start_block;
1144 DBF_EVENT(6, "xBREDid:");
1146 /* Count the number of blocks for the request. */
1147 rq_for_each_segment(bv, req, iter)
1148 count += bv->bv_len >> (TAPEBLOCK_HSEC_S2B + 9);
1150 /* Allocate the ccw request. */
1151 request = tape_alloc_request(3+count+1, 8);
1152 if (IS_ERR(request))
1153 return request;
1155 /* Setup ccws. */
1156 request->op = TO_BLOCK;
1157 start_block = (struct tape_34xx_block_id *) request->cpdata;
1158 start_block->block = req->sector >> TAPEBLOCK_HSEC_S2B;
1159 DBF_EVENT(6, "start_block = %i\n", start_block->block);
1161 ccw = request->cpaddr;
1162 ccw = tape_ccw_cc(ccw, MODE_SET_DB, 1, device->modeset_byte);
1165 * We always setup a nop after the mode set ccw. This slot is
1166 * used in tape_std_check_locate to insert a locate ccw if the
1167 * current tape position doesn't match the start block to be read.
1168 * The second nop will be filled with a read block id which is in
1169 * turn used by tape_34xx_free_bread to populate the segment bid
1170 * table.
1172 ccw = tape_ccw_cc(ccw, NOP, 0, NULL);
1173 ccw = tape_ccw_cc(ccw, NOP, 0, NULL);
1175 rq_for_each_segment(bv, req, iter) {
1176 dst = kmap(bv->bv_page) + bv->bv_offset;
1177 for (off = 0; off < bv->bv_len; off += TAPEBLOCK_HSEC_SIZE) {
1178 ccw->flags = CCW_FLAG_CC;
1179 ccw->cmd_code = READ_FORWARD;
1180 ccw->count = TAPEBLOCK_HSEC_SIZE;
1181 set_normalized_cda(ccw, (void*) __pa(dst));
1182 ccw++;
1183 dst += TAPEBLOCK_HSEC_SIZE;
1187 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
1188 DBF_EVENT(6, "xBREDccwg\n");
1189 return request;
1192 static void
1193 tape_34xx_free_bread (struct tape_request *request)
1195 struct ccw1* ccw;
1197 ccw = request->cpaddr;
1198 if ((ccw + 2)->cmd_code == READ_BLOCK_ID) {
1199 struct {
1200 struct tape_34xx_block_id cbid;
1201 struct tape_34xx_block_id dbid;
1202 } __attribute__ ((packed)) *rbi_data;
1204 rbi_data = request->cpdata;
1206 if (request->device)
1207 tape_34xx_add_sbid(request->device, rbi_data->cbid);
1210 /* Last ccw is a nop and doesn't need clear_normalized_cda */
1211 for (; ccw->flags & CCW_FLAG_CC; ccw++)
1212 if (ccw->cmd_code == READ_FORWARD)
1213 clear_normalized_cda(ccw);
1214 tape_free_request(request);
1218 * check_locate is called just before the tape request is passed to
1219 * the common io layer for execution. It has to check the current
1220 * tape position and insert a locate ccw if it doesn't match the
1221 * start block for the request.
1223 static void
1224 tape_34xx_check_locate(struct tape_device *device, struct tape_request *request)
1226 struct tape_34xx_block_id * start_block;
1228 start_block = (struct tape_34xx_block_id *) request->cpdata;
1229 if (start_block->block == device->blk_data.block_position)
1230 return;
1232 DBF_LH(4, "Block seek(%06d+%06d)\n", start_block->block, device->bof);
1233 start_block->wrap = 0;
1234 start_block->segment = 1;
1235 start_block->format = (*device->modeset_byte & 0x08) ?
1236 TAPE34XX_FMT_3480_XF :
1237 TAPE34XX_FMT_3480;
1238 start_block->block = start_block->block + device->bof;
1239 tape_34xx_merge_sbid(device, start_block);
1240 tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
1241 tape_ccw_cc(request->cpaddr + 2, READ_BLOCK_ID, 8, request->cpdata);
1243 #endif
1246 * List of 3480/3490 magnetic tape commands.
1248 static tape_mtop_fn tape_34xx_mtop[TAPE_NR_MTOPS] = {
1249 [MTRESET] = tape_std_mtreset,
1250 [MTFSF] = tape_std_mtfsf,
1251 [MTBSF] = tape_std_mtbsf,
1252 [MTFSR] = tape_std_mtfsr,
1253 [MTBSR] = tape_std_mtbsr,
1254 [MTWEOF] = tape_std_mtweof,
1255 [MTREW] = tape_std_mtrew,
1256 [MTOFFL] = tape_std_mtoffl,
1257 [MTNOP] = tape_std_mtnop,
1258 [MTRETEN] = tape_std_mtreten,
1259 [MTBSFM] = tape_std_mtbsfm,
1260 [MTFSFM] = tape_std_mtfsfm,
1261 [MTEOM] = tape_std_mteom,
1262 [MTERASE] = tape_std_mterase,
1263 [MTRAS1] = NULL,
1264 [MTRAS2] = NULL,
1265 [MTRAS3] = NULL,
1266 [MTSETBLK] = tape_std_mtsetblk,
1267 [MTSETDENSITY] = NULL,
1268 [MTSEEK] = tape_34xx_mtseek,
1269 [MTTELL] = tape_34xx_mttell,
1270 [MTSETDRVBUFFER] = NULL,
1271 [MTFSS] = NULL,
1272 [MTBSS] = NULL,
1273 [MTWSM] = NULL,
1274 [MTLOCK] = NULL,
1275 [MTUNLOCK] = NULL,
1276 [MTLOAD] = tape_std_mtload,
1277 [MTUNLOAD] = tape_std_mtunload,
1278 [MTCOMPRESSION] = tape_std_mtcompression,
1279 [MTSETPART] = NULL,
1280 [MTMKPART] = NULL
1284 * Tape discipline structure for 3480 and 3490.
1286 static struct tape_discipline tape_discipline_34xx = {
1287 .owner = THIS_MODULE,
1288 .setup_device = tape_34xx_setup_device,
1289 .cleanup_device = tape_34xx_cleanup_device,
1290 .process_eov = tape_std_process_eov,
1291 .irq = tape_34xx_irq,
1292 .read_block = tape_std_read_block,
1293 .write_block = tape_std_write_block,
1294 #ifdef CONFIG_S390_TAPE_BLOCK
1295 .bread = tape_34xx_bread,
1296 .free_bread = tape_34xx_free_bread,
1297 .check_locate = tape_34xx_check_locate,
1298 #endif
1299 .ioctl_fn = tape_34xx_ioctl,
1300 .mtop_array = tape_34xx_mtop
1303 static struct ccw_device_id tape_34xx_ids[] = {
1304 { CCW_DEVICE_DEVTYPE(0x3480, 0, 0x3480, 0), .driver_info = tape_3480},
1305 { CCW_DEVICE_DEVTYPE(0x3490, 0, 0x3490, 0), .driver_info = tape_3490},
1306 { /* end of list */ },
1309 static int
1310 tape_34xx_online(struct ccw_device *cdev)
1312 return tape_generic_online(
1313 cdev->dev.driver_data,
1314 &tape_discipline_34xx
1318 static int
1319 tape_34xx_offline(struct ccw_device *cdev)
1321 return tape_generic_offline(cdev->dev.driver_data);
1324 static struct ccw_driver tape_34xx_driver = {
1325 .name = "tape_34xx",
1326 .owner = THIS_MODULE,
1327 .ids = tape_34xx_ids,
1328 .probe = tape_generic_probe,
1329 .remove = tape_generic_remove,
1330 .set_online = tape_34xx_online,
1331 .set_offline = tape_34xx_offline,
1334 static int
1335 tape_34xx_init (void)
1337 int rc;
1339 TAPE_DBF_AREA = debug_register ( "tape_34xx", 2, 2, 4*sizeof(long));
1340 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1341 #ifdef DBF_LIKE_HELL
1342 debug_set_level(TAPE_DBF_AREA, 6);
1343 #endif
1345 DBF_EVENT(3, "34xx init\n");
1346 /* Register driver for 3480/3490 tapes. */
1347 rc = ccw_driver_register(&tape_34xx_driver);
1348 if (rc)
1349 DBF_EVENT(3, "34xx init failed\n");
1350 else
1351 DBF_EVENT(3, "34xx registered\n");
1352 return rc;
1355 static void
1356 tape_34xx_exit(void)
1358 ccw_driver_unregister(&tape_34xx_driver);
1360 debug_unregister(TAPE_DBF_AREA);
1363 MODULE_DEVICE_TABLE(ccw, tape_34xx_ids);
1364 MODULE_AUTHOR("(C) 2001-2002 IBM Deutschland Entwicklung GmbH");
1365 MODULE_DESCRIPTION("Linux on zSeries channel attached 3480 tape device driver");
1366 MODULE_LICENSE("GPL");
1368 module_init(tape_34xx_init);
1369 module_exit(tape_34xx_exit);