update checkpatch.pl to version 0.05
[pv_ops_mirror.git] / drivers / s390 / char / tape_34xx.c
blobe765875e8db2eb4fb4ae3c0a4b6a03d8215bcaa4
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 = kmalloc(sizeof(*p), GFP_ATOMIC)) == NULL)
135 return -ENOMEM;
137 memset(p, 0, sizeof(*p));
138 INIT_WORK(&p->work, tape_34xx_work_handler);
140 p->device = tape_get_device_reference(device);
141 p->op = op;
143 schedule_work(&p->work);
144 return 0;
148 * Done Handler is called when dev stat = DEVICE-END (successful operation)
150 static inline int
151 tape_34xx_done(struct tape_request *request)
153 DBF_EVENT(6, "%s done\n", tape_op_verbose[request->op]);
155 switch (request->op) {
156 case TO_DSE:
157 case TO_RUN:
158 case TO_WRI:
159 case TO_WTM:
160 case TO_ASSIGN:
161 case TO_UNASSIGN:
162 tape_34xx_delete_sbid_from(request->device, 0);
163 break;
164 default:
167 return TAPE_IO_SUCCESS;
170 static inline int
171 tape_34xx_erp_failed(struct tape_request *request, int rc)
173 DBF_EVENT(3, "Error recovery failed for %s (RC=%d)\n",
174 tape_op_verbose[request->op], rc);
175 return rc;
178 static inline int
179 tape_34xx_erp_succeeded(struct tape_request *request)
181 DBF_EVENT(3, "Error Recovery successful for %s\n",
182 tape_op_verbose[request->op]);
183 return tape_34xx_done(request);
186 static inline int
187 tape_34xx_erp_retry(struct tape_request *request)
189 DBF_EVENT(3, "xerp retr %s\n", tape_op_verbose[request->op]);
190 return TAPE_IO_RETRY;
194 * This function is called, when no request is outstanding and we get an
195 * interrupt
197 static int
198 tape_34xx_unsolicited_irq(struct tape_device *device, struct irb *irb)
200 if (irb->scsw.dstat == 0x85 /* READY */) {
201 /* A medium was inserted in the drive. */
202 DBF_EVENT(6, "xuud med\n");
203 tape_34xx_delete_sbid_from(device, 0);
204 tape_34xx_schedule_work(device, TO_MSEN);
205 } else {
206 DBF_EVENT(3, "unsol.irq! dev end: %08x\n", device->cdev_id);
207 PRINT_WARN("Unsolicited IRQ (Device End) caught.\n");
208 tape_dump_sense(device, NULL, irb);
210 return TAPE_IO_SUCCESS;
214 * Read Opposite Error Recovery Function:
215 * Used, when Read Forward does not work
217 static int
218 tape_34xx_erp_read_opposite(struct tape_device *device,
219 struct tape_request *request)
221 if (request->op == TO_RFO) {
223 * We did read forward, but the data could not be read
224 * *correctly*. We transform the request to a read backward
225 * and try again.
227 tape_std_read_backward(device, request);
228 return tape_34xx_erp_retry(request);
230 if (request->op != TO_RBA)
231 PRINT_ERR("read_opposite called with state:%s\n",
232 tape_op_verbose[request->op]);
234 * We tried to read forward and backward, but hat no
235 * success -> failed.
237 return tape_34xx_erp_failed(request, -EIO);
240 static int
241 tape_34xx_erp_bug(struct tape_device *device, struct tape_request *request,
242 struct irb *irb, int no)
244 if (request->op != TO_ASSIGN) {
245 PRINT_WARN("An unexpected condition #%d was caught in "
246 "tape error recovery.\n", no);
247 PRINT_WARN("Please report this incident.\n");
248 if (request)
249 PRINT_WARN("Operation of tape:%s\n",
250 tape_op_verbose[request->op]);
251 tape_dump_sense(device, request, irb);
253 return tape_34xx_erp_failed(request, -EIO);
257 * Handle data overrun between cu and drive. The channel speed might
258 * be too slow.
260 static int
261 tape_34xx_erp_overrun(struct tape_device *device, struct tape_request *request,
262 struct irb *irb)
264 if (irb->ecw[3] == 0x40) {
265 PRINT_WARN ("Data overrun error between control-unit "
266 "and drive. Use a faster channel connection, "
267 "if possible! \n");
268 return tape_34xx_erp_failed(request, -EIO);
270 return tape_34xx_erp_bug(device, request, irb, -1);
274 * Handle record sequence error.
276 static int
277 tape_34xx_erp_sequence(struct tape_device *device,
278 struct tape_request *request, struct irb *irb)
280 if (irb->ecw[3] == 0x41) {
282 * cu detected incorrect block-id sequence on tape.
284 PRINT_WARN("Illegal block-id sequence found!\n");
285 return tape_34xx_erp_failed(request, -EIO);
288 * Record sequence error bit is set, but erpa does not
289 * show record sequence error.
291 return tape_34xx_erp_bug(device, request, irb, -2);
295 * This function analyses the tape's sense-data in case of a unit-check.
296 * If possible, it tries to recover from the error. Else the user is
297 * informed about the problem.
299 static int
300 tape_34xx_unit_check(struct tape_device *device, struct tape_request *request,
301 struct irb *irb)
303 int inhibit_cu_recovery;
304 __u8* sense;
306 inhibit_cu_recovery = (*device->modeset_byte & 0x80) ? 1 : 0;
307 sense = irb->ecw;
309 #ifdef CONFIG_S390_TAPE_BLOCK
310 if (request->op == TO_BLOCK) {
312 * Recovery for block device requests. Set the block_position
313 * to something invalid and retry.
315 device->blk_data.block_position = -1;
316 if (request->retries-- <= 0)
317 return tape_34xx_erp_failed(request, -EIO);
318 else
319 return tape_34xx_erp_retry(request);
321 #endif
323 if (
324 sense[0] & SENSE_COMMAND_REJECT &&
325 sense[1] & SENSE_WRITE_PROTECT
327 if (
328 request->op == TO_DSE ||
329 request->op == TO_WRI ||
330 request->op == TO_WTM
332 /* medium is write protected */
333 return tape_34xx_erp_failed(request, -EACCES);
334 } else {
335 return tape_34xx_erp_bug(device, request, irb, -3);
340 * Special cases for various tape-states when reaching
341 * end of recorded area
343 * FIXME: Maybe a special case of the special case:
344 * sense[0] == SENSE_EQUIPMENT_CHECK &&
345 * sense[1] == SENSE_DRIVE_ONLINE &&
346 * sense[3] == 0x47 (Volume Fenced)
348 * This was caused by continued FSF or FSR after an
349 * 'End Of Data'.
351 if ((
352 sense[0] == SENSE_DATA_CHECK ||
353 sense[0] == SENSE_EQUIPMENT_CHECK ||
354 sense[0] == SENSE_EQUIPMENT_CHECK + SENSE_DEFERRED_UNIT_CHECK
355 ) && (
356 sense[1] == SENSE_DRIVE_ONLINE ||
357 sense[1] == SENSE_BEGINNING_OF_TAPE + SENSE_WRITE_MODE
358 )) {
359 switch (request->op) {
361 * sense[0] == SENSE_DATA_CHECK &&
362 * sense[1] == SENSE_DRIVE_ONLINE
363 * sense[3] == 0x36 (End Of Data)
365 * Further seeks might return a 'Volume Fenced'.
367 case TO_FSF:
368 case TO_FSB:
369 /* Trying to seek beyond end of recorded area */
370 return tape_34xx_erp_failed(request, -ENOSPC);
371 case TO_BSB:
372 return tape_34xx_erp_retry(request);
375 * sense[0] == SENSE_DATA_CHECK &&
376 * sense[1] == SENSE_DRIVE_ONLINE &&
377 * sense[3] == 0x36 (End Of Data)
379 case TO_LBL:
380 /* Block could not be located. */
381 tape_34xx_delete_sbid_from(device, 0);
382 return tape_34xx_erp_failed(request, -EIO);
384 case TO_RFO:
385 /* Read beyond end of recorded area -> 0 bytes read */
386 return tape_34xx_erp_failed(request, 0);
389 * sense[0] == SENSE_EQUIPMENT_CHECK &&
390 * sense[1] == SENSE_DRIVE_ONLINE &&
391 * sense[3] == 0x38 (Physical End Of Volume)
393 case TO_WRI:
394 /* Writing at physical end of volume */
395 return tape_34xx_erp_failed(request, -ENOSPC);
396 default:
397 PRINT_ERR("Invalid op in %s:%i\n",
398 __FUNCTION__, __LINE__);
399 return tape_34xx_erp_failed(request, 0);
403 /* Sensing special bits */
404 if (sense[0] & SENSE_BUS_OUT_CHECK)
405 return tape_34xx_erp_retry(request);
407 if (sense[0] & SENSE_DATA_CHECK) {
409 * hardware failure, damaged tape or improper
410 * operating conditions
412 switch (sense[3]) {
413 case 0x23:
414 /* a read data check occurred */
415 if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
416 inhibit_cu_recovery)
417 // data check is not permanent, may be
418 // recovered. We always use async-mode with
419 // cu-recovery, so this should *never* happen.
420 return tape_34xx_erp_bug(device, request,
421 irb, -4);
423 /* data check is permanent, CU recovery has failed */
424 PRINT_WARN("Permanent read error\n");
425 return tape_34xx_erp_failed(request, -EIO);
426 case 0x25:
427 // a write data check occurred
428 if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
429 inhibit_cu_recovery)
430 // data check is not permanent, may be
431 // recovered. We always use async-mode with
432 // cu-recovery, so this should *never* happen.
433 return tape_34xx_erp_bug(device, request,
434 irb, -5);
436 // data check is permanent, cu-recovery has failed
437 PRINT_WARN("Permanent write error\n");
438 return tape_34xx_erp_failed(request, -EIO);
439 case 0x26:
440 /* Data Check (read opposite) occurred. */
441 return tape_34xx_erp_read_opposite(device, request);
442 case 0x28:
443 /* ID-Mark at tape start couldn't be written */
444 PRINT_WARN("ID-Mark could not be written.\n");
445 return tape_34xx_erp_failed(request, -EIO);
446 case 0x31:
447 /* Tape void. Tried to read beyond end of device. */
448 PRINT_WARN("Read beyond end of recorded area.\n");
449 return tape_34xx_erp_failed(request, -ENOSPC);
450 case 0x41:
451 /* Record sequence error. */
452 PRINT_WARN("Invalid block-id sequence found.\n");
453 return tape_34xx_erp_failed(request, -EIO);
454 default:
455 /* all data checks for 3480 should result in one of
456 * the above erpa-codes. For 3490, other data-check
457 * conditions do exist. */
458 if (device->cdev->id.driver_info == tape_3480)
459 return tape_34xx_erp_bug(device, request,
460 irb, -6);
464 if (sense[0] & SENSE_OVERRUN)
465 return tape_34xx_erp_overrun(device, request, irb);
467 if (sense[1] & SENSE_RECORD_SEQUENCE_ERR)
468 return tape_34xx_erp_sequence(device, request, irb);
470 /* Sensing erpa codes */
471 switch (sense[3]) {
472 case 0x00:
473 /* Unit check with erpa code 0. Report and ignore. */
474 PRINT_WARN("Non-error sense was found. "
475 "Unit-check will be ignored.\n");
476 return TAPE_IO_SUCCESS;
477 case 0x21:
479 * Data streaming not operational. CU will switch to
480 * interlock mode. Reissue the command.
482 PRINT_WARN("Data streaming not operational. "
483 "Switching to interlock-mode.\n");
484 return tape_34xx_erp_retry(request);
485 case 0x22:
487 * Path equipment check. Might be drive adapter error, buffer
488 * error on the lower interface, internal path not usable,
489 * or error during cartridge load.
491 PRINT_WARN("A path equipment check occurred. One of the "
492 "following conditions occurred:\n");
493 PRINT_WARN("drive adapter error, buffer error on the lower "
494 "interface, internal path not usable, error "
495 "during cartridge load.\n");
496 return tape_34xx_erp_failed(request, -EIO);
497 case 0x24:
499 * Load display check. Load display was command was issued,
500 * but the drive is displaying a drive check message. Can
501 * be threated as "device end".
503 return tape_34xx_erp_succeeded(request);
504 case 0x27:
506 * Command reject. May indicate illegal channel program or
507 * buffer over/underrun. Since all channel programs are
508 * issued by this driver and ought be correct, we assume a
509 * over/underrun situation and retry the channel program.
511 return tape_34xx_erp_retry(request);
512 case 0x29:
514 * Function incompatible. Either the tape is idrc compressed
515 * but the hardware isn't capable to do idrc, or a perform
516 * subsystem func is issued and the CU is not on-line.
518 PRINT_WARN ("Function incompatible. Try to switch off idrc\n");
519 return tape_34xx_erp_failed(request, -EIO);
520 case 0x2a:
522 * Unsolicited environmental data. An internal counter
523 * overflows, we can ignore this and reissue the cmd.
525 return tape_34xx_erp_retry(request);
526 case 0x2b:
528 * Environmental data present. Indicates either unload
529 * completed ok or read buffered log command completed ok.
531 if (request->op == TO_RUN) {
532 /* Rewind unload completed ok. */
533 tape_med_state_set(device, MS_UNLOADED);
534 return tape_34xx_erp_succeeded(request);
536 /* tape_34xx doesn't use read buffered log commands. */
537 return tape_34xx_erp_bug(device, request, irb, sense[3]);
538 case 0x2c:
540 * Permanent equipment check. CU has tried recovery, but
541 * did not succeed.
543 return tape_34xx_erp_failed(request, -EIO);
544 case 0x2d:
545 /* Data security erase failure. */
546 if (request->op == TO_DSE)
547 return tape_34xx_erp_failed(request, -EIO);
548 /* Data security erase failure, but no such command issued. */
549 return tape_34xx_erp_bug(device, request, irb, sense[3]);
550 case 0x2e:
552 * Not capable. This indicates either that the drive fails
553 * reading the format id mark or that that format specified
554 * is not supported by the drive.
556 PRINT_WARN("Drive not capable processing the tape format!\n");
557 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
558 case 0x30:
559 /* The medium is write protected. */
560 PRINT_WARN("Medium is write protected!\n");
561 return tape_34xx_erp_failed(request, -EACCES);
562 case 0x32:
563 // Tension loss. We cannot recover this, it's an I/O error.
564 PRINT_WARN("The drive lost tape tension.\n");
565 return tape_34xx_erp_failed(request, -EIO);
566 case 0x33:
568 * Load Failure. The cartridge was not inserted correctly or
569 * the tape is not threaded correctly.
571 PRINT_WARN("Cartridge load failure. Reload the cartridge "
572 "and try again.\n");
573 tape_34xx_delete_sbid_from(device, 0);
574 return tape_34xx_erp_failed(request, -EIO);
575 case 0x34:
577 * Unload failure. The drive cannot maintain tape tension
578 * and control tape movement during an unload operation.
580 PRINT_WARN("Failure during cartridge unload. "
581 "Please try manually.\n");
582 if (request->op == TO_RUN)
583 return tape_34xx_erp_failed(request, -EIO);
584 return tape_34xx_erp_bug(device, request, irb, sense[3]);
585 case 0x35:
587 * Drive equipment check. One of the following:
588 * - cu cannot recover from a drive detected error
589 * - a check code message is shown on drive display
590 * - the cartridge loader does not respond correctly
591 * - a failure occurs during an index, load, or unload cycle
593 PRINT_WARN("Equipment check! Please check the drive and "
594 "the cartridge loader.\n");
595 return tape_34xx_erp_failed(request, -EIO);
596 case 0x36:
597 if (device->cdev->id.driver_info == tape_3490)
598 /* End of data. */
599 return tape_34xx_erp_failed(request, -EIO);
600 /* This erpa is reserved for 3480 */
601 return tape_34xx_erp_bug(device, request, irb, sense[3]);
602 case 0x37:
604 * Tape length error. The tape is shorter than reported in
605 * the beginning-of-tape data.
607 PRINT_WARN("Tape length error.\n");
608 return tape_34xx_erp_failed(request, -EIO);
609 case 0x38:
611 * Physical end of tape. A read/write operation reached
612 * the physical end of tape.
614 if (request->op==TO_WRI ||
615 request->op==TO_DSE ||
616 request->op==TO_WTM)
617 return tape_34xx_erp_failed(request, -ENOSPC);
618 return tape_34xx_erp_failed(request, -EIO);
619 case 0x39:
620 /* Backward at Beginning of tape. */
621 return tape_34xx_erp_failed(request, -EIO);
622 case 0x3a:
623 /* Drive switched to not ready. */
624 PRINT_WARN("Drive not ready. Turn the ready/not ready switch "
625 "to ready position and try again.\n");
626 return tape_34xx_erp_failed(request, -EIO);
627 case 0x3b:
628 /* Manual rewind or unload. This causes an I/O error. */
629 PRINT_WARN("Medium was rewound or unloaded manually.\n");
630 tape_34xx_delete_sbid_from(device, 0);
631 return tape_34xx_erp_failed(request, -EIO);
632 case 0x42:
634 * Degraded mode. A condition that can cause degraded
635 * performance is detected.
637 PRINT_WARN("Subsystem is running in degraded mode.\n");
638 return tape_34xx_erp_retry(request);
639 case 0x43:
640 /* Drive not ready. */
641 tape_34xx_delete_sbid_from(device, 0);
642 tape_med_state_set(device, MS_UNLOADED);
643 /* Some commands commands are successful even in this case */
644 if (sense[1] & SENSE_DRIVE_ONLINE) {
645 switch(request->op) {
646 case TO_ASSIGN:
647 case TO_UNASSIGN:
648 case TO_DIS:
649 case TO_NOP:
650 return tape_34xx_done(request);
651 break;
652 default:
653 break;
656 PRINT_WARN("The drive is not ready.\n");
657 return tape_34xx_erp_failed(request, -ENOMEDIUM);
658 case 0x44:
659 /* Locate Block unsuccessful. */
660 if (request->op != TO_BLOCK && request->op != TO_LBL)
661 /* No locate block was issued. */
662 return tape_34xx_erp_bug(device, request,
663 irb, sense[3]);
664 return tape_34xx_erp_failed(request, -EIO);
665 case 0x45:
666 /* The drive is assigned to a different channel path. */
667 PRINT_WARN("The drive is assigned elsewhere.\n");
668 return tape_34xx_erp_failed(request, -EIO);
669 case 0x46:
671 * Drive not on-line. Drive may be switched offline,
672 * the power supply may be switched off or
673 * the drive address may not be set correctly.
675 PRINT_WARN("The drive is not on-line.");
676 return tape_34xx_erp_failed(request, -EIO);
677 case 0x47:
678 /* Volume fenced. CU reports volume integrity is lost. */
679 PRINT_WARN("Volume fenced. The volume integrity is lost.\n");
680 tape_34xx_delete_sbid_from(device, 0);
681 return tape_34xx_erp_failed(request, -EIO);
682 case 0x48:
683 /* Log sense data and retry request. */
684 return tape_34xx_erp_retry(request);
685 case 0x49:
686 /* Bus out check. A parity check error on the bus was found. */
687 PRINT_WARN("Bus out check. A data transfer over the bus "
688 "has been corrupted.\n");
689 return tape_34xx_erp_failed(request, -EIO);
690 case 0x4a:
691 /* Control unit erp failed. */
692 PRINT_WARN("The control unit I/O error recovery failed.\n");
693 return tape_34xx_erp_failed(request, -EIO);
694 case 0x4b:
696 * CU and drive incompatible. The drive requests micro-program
697 * patches, which are not available on the CU.
699 PRINT_WARN("The drive needs microprogram patches from the "
700 "control unit, which are not available.\n");
701 return tape_34xx_erp_failed(request, -EIO);
702 case 0x4c:
704 * Recovered Check-One failure. Cu develops a hardware error,
705 * but is able to recover.
707 return tape_34xx_erp_retry(request);
708 case 0x4d:
709 if (device->cdev->id.driver_info == tape_3490)
711 * Resetting event received. Since the driver does
712 * not support resetting event recovery (which has to
713 * be handled by the I/O Layer), retry our command.
715 return tape_34xx_erp_retry(request);
716 /* This erpa is reserved for 3480. */
717 return tape_34xx_erp_bug(device, request, irb, sense[3]);
718 case 0x4e:
719 if (device->cdev->id.driver_info == tape_3490) {
721 * Maximum block size exceeded. This indicates, that
722 * the block to be written is larger than allowed for
723 * buffered mode.
725 PRINT_WARN("Maximum block size for buffered "
726 "mode exceeded.\n");
727 return tape_34xx_erp_failed(request, -ENOBUFS);
729 /* This erpa is reserved for 3480. */
730 return tape_34xx_erp_bug(device, request, irb, sense[3]);
731 case 0x50:
733 * Read buffered log (Overflow). CU is running in extended
734 * buffered log mode, and a counter overflows. This should
735 * never happen, since we're never running in extended
736 * buffered log mode.
738 return tape_34xx_erp_retry(request);
739 case 0x51:
741 * Read buffered log (EOV). EOF processing occurs while the
742 * CU is in extended buffered log mode. This should never
743 * happen, since we're never running in extended buffered
744 * log mode.
746 return tape_34xx_erp_retry(request);
747 case 0x52:
748 /* End of Volume complete. Rewind unload completed ok. */
749 if (request->op == TO_RUN) {
750 tape_med_state_set(device, MS_UNLOADED);
751 tape_34xx_delete_sbid_from(device, 0);
752 return tape_34xx_erp_succeeded(request);
754 return tape_34xx_erp_bug(device, request, irb, sense[3]);
755 case 0x53:
756 /* Global command intercept. */
757 return tape_34xx_erp_retry(request);
758 case 0x54:
759 /* Channel interface recovery (temporary). */
760 return tape_34xx_erp_retry(request);
761 case 0x55:
762 /* Channel interface recovery (permanent). */
763 PRINT_WARN("A permanent channel interface error occurred.\n");
764 return tape_34xx_erp_failed(request, -EIO);
765 case 0x56:
766 /* Channel protocol error. */
767 PRINT_WARN("A channel protocol error occurred.\n");
768 return tape_34xx_erp_failed(request, -EIO);
769 case 0x57:
770 if (device->cdev->id.driver_info == tape_3480) {
771 /* Attention intercept. */
772 PRINT_WARN("An attention intercept occurred, "
773 "which will be recovered.\n");
774 return tape_34xx_erp_retry(request);
775 } else {
776 /* Global status intercept. */
777 PRINT_WARN("An global status intercept was received, "
778 "which will be recovered.\n");
779 return tape_34xx_erp_retry(request);
781 case 0x5a:
783 * Tape length incompatible. The tape inserted is too long,
784 * which could cause damage to the tape or the drive.
786 PRINT_WARN("Tape Length Incompatible\n");
787 PRINT_WARN("Tape length exceeds IBM enhanced capacity "
788 "cartdridge length or a medium\n");
789 PRINT_WARN("with EC-CST identification mark has been mounted "
790 "in a device that writes\n");
791 PRINT_WARN("3480 or 3480 XF format.\n");
792 return tape_34xx_erp_failed(request, -EIO);
793 case 0x5b:
794 /* Format 3480 XF incompatible */
795 if (sense[1] & SENSE_BEGINNING_OF_TAPE)
796 /* The tape will get overwritten. */
797 return tape_34xx_erp_retry(request);
798 PRINT_WARN("Format 3480 XF Incompatible\n");
799 PRINT_WARN("Medium has been created in 3480 format. "
800 "To change the format writes\n");
801 PRINT_WARN("must be issued at BOT.\n");
802 return tape_34xx_erp_failed(request, -EIO);
803 case 0x5c:
804 /* Format 3480-2 XF incompatible */
805 PRINT_WARN("Format 3480-2 XF Incompatible\n");
806 PRINT_WARN("Device can only read 3480 or 3480 XF format.\n");
807 return tape_34xx_erp_failed(request, -EIO);
808 case 0x5d:
809 /* Tape length violation. */
810 PRINT_WARN("Tape Length Violation\n");
811 PRINT_WARN("The mounted tape exceeds IBM Enhanced Capacity "
812 "Cartdridge System Tape length.\n");
813 PRINT_WARN("This may cause damage to the drive or tape when "
814 "processing to the EOV\n");
815 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
816 case 0x5e:
817 /* Compaction algorithm incompatible. */
818 PRINT_WARN("Compaction Algorithm Incompatible\n");
819 PRINT_WARN("The volume is recorded using an incompatible "
820 "compaction algorithm,\n");
821 PRINT_WARN("which is not supported by the device.\n");
822 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
824 /* The following erpas should have been covered earlier. */
825 case 0x23: /* Read data check. */
826 case 0x25: /* Write data check. */
827 case 0x26: /* Data check (read opposite). */
828 case 0x28: /* Write id mark check. */
829 case 0x31: /* Tape void. */
830 case 0x40: /* Overrun error. */
831 case 0x41: /* Record sequence error. */
832 /* All other erpas are reserved for future use. */
833 default:
834 return tape_34xx_erp_bug(device, request, irb, sense[3]);
839 * 3480/3490 interrupt handler
841 static int
842 tape_34xx_irq(struct tape_device *device, struct tape_request *request,
843 struct irb *irb)
845 if (request == NULL)
846 return tape_34xx_unsolicited_irq(device, irb);
848 if ((irb->scsw.dstat & DEV_STAT_UNIT_EXCEP) &&
849 (irb->scsw.dstat & DEV_STAT_DEV_END) &&
850 (request->op == TO_WRI)) {
851 /* Write at end of volume */
852 PRINT_INFO("End of volume\n"); /* XXX */
853 return tape_34xx_erp_failed(request, -ENOSPC);
856 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
857 return tape_34xx_unit_check(device, request, irb);
859 if (irb->scsw.dstat & DEV_STAT_DEV_END) {
861 * A unit exception occurs on skipping over a tapemark block.
863 if (irb->scsw.dstat & DEV_STAT_UNIT_EXCEP) {
864 if (request->op == TO_BSB || request->op == TO_FSB)
865 request->rescnt++;
866 else
867 DBF_EVENT(5, "Unit Exception!\n");
869 return tape_34xx_done(request);
872 DBF_EVENT(6, "xunknownirq\n");
873 PRINT_ERR("Unexpected interrupt.\n");
874 PRINT_ERR("Current op is: %s", tape_op_verbose[request->op]);
875 tape_dump_sense(device, request, irb);
876 return TAPE_IO_STOP;
880 * ioctl_overload
882 static int
883 tape_34xx_ioctl(struct tape_device *device, unsigned int cmd, unsigned long arg)
885 if (cmd == TAPE390_DISPLAY) {
886 struct display_struct disp;
888 if (copy_from_user(&disp, (char __user *) arg, sizeof(disp)) != 0)
889 return -EFAULT;
891 return tape_std_display(device, &disp);
892 } else
893 return -EINVAL;
896 static inline void
897 tape_34xx_append_new_sbid(struct tape_34xx_block_id bid, struct list_head *l)
899 struct tape_34xx_sbid * new_sbid;
901 new_sbid = kmalloc(sizeof(*new_sbid), GFP_ATOMIC);
902 if (!new_sbid)
903 return;
905 new_sbid->bid = bid;
906 list_add(&new_sbid->list, l);
910 * Build up the search block ID list. The block ID consists of a logical
911 * block number and a hardware specific part. The hardware specific part
912 * helps the tape drive to speed up searching for a specific block.
914 static void
915 tape_34xx_add_sbid(struct tape_device *device, struct tape_34xx_block_id bid)
917 struct list_head * sbid_list;
918 struct tape_34xx_sbid * sbid;
919 struct list_head * l;
922 * immediately return if there is no list at all or the block to add
923 * is located in segment 1 of wrap 0 because this position is used
924 * if no hardware position data is supplied.
926 sbid_list = (struct list_head *) device->discdata;
927 if (!sbid_list || (bid.segment < 2 && bid.wrap == 0))
928 return;
931 * Search the position where to insert the new entry. Hardware
932 * acceleration uses only the segment and wrap number. So we
933 * need only one entry for a specific wrap/segment combination.
934 * If there is a block with a lower number but the same hard-
935 * ware position data we just update the block number in the
936 * existing entry.
938 list_for_each(l, sbid_list) {
939 sbid = list_entry(l, struct tape_34xx_sbid, list);
941 if (
942 (sbid->bid.segment == bid.segment) &&
943 (sbid->bid.wrap == bid.wrap)
945 if (bid.block < sbid->bid.block)
946 sbid->bid = bid;
947 else return;
948 break;
951 /* Sort in according to logical block number. */
952 if (bid.block < sbid->bid.block) {
953 tape_34xx_append_new_sbid(bid, l->prev);
954 break;
957 /* List empty or new block bigger than last entry. */
958 if (l == sbid_list)
959 tape_34xx_append_new_sbid(bid, l->prev);
961 DBF_LH(4, "Current list is:\n");
962 list_for_each(l, sbid_list) {
963 sbid = list_entry(l, struct tape_34xx_sbid, list);
964 DBF_LH(4, "%d:%03d@%05d\n",
965 sbid->bid.wrap,
966 sbid->bid.segment,
967 sbid->bid.block
973 * Delete all entries from the search block ID list that belong to tape blocks
974 * equal or higher than the given number.
976 static void
977 tape_34xx_delete_sbid_from(struct tape_device *device, int from)
979 struct list_head * sbid_list;
980 struct tape_34xx_sbid * sbid;
981 struct list_head * l;
982 struct list_head * n;
984 sbid_list = (struct list_head *) device->discdata;
985 if (!sbid_list)
986 return;
988 list_for_each_safe(l, n, sbid_list) {
989 sbid = list_entry(l, struct tape_34xx_sbid, list);
990 if (sbid->bid.block >= from) {
991 DBF_LH(4, "Delete sbid %d:%03d@%05d\n",
992 sbid->bid.wrap,
993 sbid->bid.segment,
994 sbid->bid.block
996 list_del(l);
997 kfree(sbid);
1003 * Merge hardware position data into a block id.
1005 static void
1006 tape_34xx_merge_sbid(
1007 struct tape_device * device,
1008 struct tape_34xx_block_id * bid
1010 struct tape_34xx_sbid * sbid;
1011 struct tape_34xx_sbid * sbid_to_use;
1012 struct list_head * sbid_list;
1013 struct list_head * l;
1015 sbid_list = (struct list_head *) device->discdata;
1016 bid->wrap = 0;
1017 bid->segment = 1;
1019 if (!sbid_list || list_empty(sbid_list))
1020 return;
1022 sbid_to_use = NULL;
1023 list_for_each(l, sbid_list) {
1024 sbid = list_entry(l, struct tape_34xx_sbid, list);
1026 if (sbid->bid.block >= bid->block)
1027 break;
1028 sbid_to_use = sbid;
1030 if (sbid_to_use) {
1031 bid->wrap = sbid_to_use->bid.wrap;
1032 bid->segment = sbid_to_use->bid.segment;
1033 DBF_LH(4, "Use %d:%03d@%05d for %05d\n",
1034 sbid_to_use->bid.wrap,
1035 sbid_to_use->bid.segment,
1036 sbid_to_use->bid.block,
1037 bid->block
1042 static int
1043 tape_34xx_setup_device(struct tape_device * device)
1045 int rc;
1046 struct list_head * discdata;
1048 DBF_EVENT(6, "34xx device setup\n");
1049 if ((rc = tape_std_assign(device)) == 0) {
1050 if ((rc = tape_34xx_medium_sense(device)) != 0) {
1051 DBF_LH(3, "34xx medium sense returned %d\n", rc);
1054 discdata = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1055 if (discdata) {
1056 INIT_LIST_HEAD(discdata);
1057 device->discdata = discdata;
1060 return rc;
1063 static void
1064 tape_34xx_cleanup_device(struct tape_device *device)
1066 tape_std_unassign(device);
1068 if (device->discdata) {
1069 tape_34xx_delete_sbid_from(device, 0);
1070 kfree(device->discdata);
1071 device->discdata = NULL;
1077 * MTTELL: Tell block. Return the number of block relative to current file.
1079 static int
1080 tape_34xx_mttell(struct tape_device *device, int mt_count)
1082 struct {
1083 struct tape_34xx_block_id cbid;
1084 struct tape_34xx_block_id dbid;
1085 } __attribute__ ((packed)) block_id;
1086 int rc;
1088 rc = tape_std_read_block_id(device, (__u64 *) &block_id);
1089 if (rc)
1090 return rc;
1092 tape_34xx_add_sbid(device, block_id.cbid);
1093 return block_id.cbid.block;
1097 * MTSEEK: seek to the specified block.
1099 static int
1100 tape_34xx_mtseek(struct tape_device *device, int mt_count)
1102 struct tape_request *request;
1103 struct tape_34xx_block_id * bid;
1105 if (mt_count > 0x3fffff) {
1106 DBF_EXCEPTION(6, "xsee parm\n");
1107 return -EINVAL;
1109 request = tape_alloc_request(3, 4);
1110 if (IS_ERR(request))
1111 return PTR_ERR(request);
1113 /* setup ccws */
1114 request->op = TO_LBL;
1115 bid = (struct tape_34xx_block_id *) request->cpdata;
1116 bid->format = (*device->modeset_byte & 0x08) ?
1117 TAPE34XX_FMT_3480_XF : TAPE34XX_FMT_3480;
1118 bid->block = mt_count;
1119 tape_34xx_merge_sbid(device, bid);
1121 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
1122 tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
1123 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
1125 /* execute it */
1126 return tape_do_io_free(device, request);
1129 #ifdef CONFIG_S390_TAPE_BLOCK
1131 * Tape block read for 34xx.
1133 static struct tape_request *
1134 tape_34xx_bread(struct tape_device *device, struct request *req)
1136 struct tape_request *request;
1137 struct ccw1 *ccw;
1138 int count = 0, i;
1139 unsigned off;
1140 char *dst;
1141 struct bio_vec *bv;
1142 struct bio *bio;
1143 struct tape_34xx_block_id * start_block;
1145 DBF_EVENT(6, "xBREDid:");
1147 /* Count the number of blocks for the request. */
1148 rq_for_each_bio(bio, req) {
1149 bio_for_each_segment(bv, bio, i) {
1150 count += bv->bv_len >> (TAPEBLOCK_HSEC_S2B + 9);
1154 /* Allocate the ccw request. */
1155 request = tape_alloc_request(3+count+1, 8);
1156 if (IS_ERR(request))
1157 return request;
1159 /* Setup ccws. */
1160 request->op = TO_BLOCK;
1161 start_block = (struct tape_34xx_block_id *) request->cpdata;
1162 start_block->block = req->sector >> TAPEBLOCK_HSEC_S2B;
1163 DBF_EVENT(6, "start_block = %i\n", start_block->block);
1165 ccw = request->cpaddr;
1166 ccw = tape_ccw_cc(ccw, MODE_SET_DB, 1, device->modeset_byte);
1169 * We always setup a nop after the mode set ccw. This slot is
1170 * used in tape_std_check_locate to insert a locate ccw if the
1171 * current tape position doesn't match the start block to be read.
1172 * The second nop will be filled with a read block id which is in
1173 * turn used by tape_34xx_free_bread to populate the segment bid
1174 * table.
1176 ccw = tape_ccw_cc(ccw, NOP, 0, NULL);
1177 ccw = tape_ccw_cc(ccw, NOP, 0, NULL);
1179 rq_for_each_bio(bio, req) {
1180 bio_for_each_segment(bv, bio, i) {
1181 dst = kmap(bv->bv_page) + bv->bv_offset;
1182 for (off = 0; off < bv->bv_len;
1183 off += TAPEBLOCK_HSEC_SIZE) {
1184 ccw->flags = CCW_FLAG_CC;
1185 ccw->cmd_code = READ_FORWARD;
1186 ccw->count = TAPEBLOCK_HSEC_SIZE;
1187 set_normalized_cda(ccw, (void*) __pa(dst));
1188 ccw++;
1189 dst += TAPEBLOCK_HSEC_SIZE;
1194 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
1195 DBF_EVENT(6, "xBREDccwg\n");
1196 return request;
1199 static void
1200 tape_34xx_free_bread (struct tape_request *request)
1202 struct ccw1* ccw;
1204 ccw = request->cpaddr;
1205 if ((ccw + 2)->cmd_code == READ_BLOCK_ID) {
1206 struct {
1207 struct tape_34xx_block_id cbid;
1208 struct tape_34xx_block_id dbid;
1209 } __attribute__ ((packed)) *rbi_data;
1211 rbi_data = request->cpdata;
1213 if (request->device)
1214 tape_34xx_add_sbid(request->device, rbi_data->cbid);
1217 /* Last ccw is a nop and doesn't need clear_normalized_cda */
1218 for (; ccw->flags & CCW_FLAG_CC; ccw++)
1219 if (ccw->cmd_code == READ_FORWARD)
1220 clear_normalized_cda(ccw);
1221 tape_free_request(request);
1225 * check_locate is called just before the tape request is passed to
1226 * the common io layer for execution. It has to check the current
1227 * tape position and insert a locate ccw if it doesn't match the
1228 * start block for the request.
1230 static void
1231 tape_34xx_check_locate(struct tape_device *device, struct tape_request *request)
1233 struct tape_34xx_block_id * start_block;
1235 start_block = (struct tape_34xx_block_id *) request->cpdata;
1236 if (start_block->block == device->blk_data.block_position)
1237 return;
1239 DBF_LH(4, "Block seek(%06d+%06d)\n", start_block->block, device->bof);
1240 start_block->wrap = 0;
1241 start_block->segment = 1;
1242 start_block->format = (*device->modeset_byte & 0x08) ?
1243 TAPE34XX_FMT_3480_XF :
1244 TAPE34XX_FMT_3480;
1245 start_block->block = start_block->block + device->bof;
1246 tape_34xx_merge_sbid(device, start_block);
1247 tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
1248 tape_ccw_cc(request->cpaddr + 2, READ_BLOCK_ID, 8, request->cpdata);
1250 #endif
1253 * List of 3480/3490 magnetic tape commands.
1255 static tape_mtop_fn tape_34xx_mtop[TAPE_NR_MTOPS] = {
1256 [MTRESET] = tape_std_mtreset,
1257 [MTFSF] = tape_std_mtfsf,
1258 [MTBSF] = tape_std_mtbsf,
1259 [MTFSR] = tape_std_mtfsr,
1260 [MTBSR] = tape_std_mtbsr,
1261 [MTWEOF] = tape_std_mtweof,
1262 [MTREW] = tape_std_mtrew,
1263 [MTOFFL] = tape_std_mtoffl,
1264 [MTNOP] = tape_std_mtnop,
1265 [MTRETEN] = tape_std_mtreten,
1266 [MTBSFM] = tape_std_mtbsfm,
1267 [MTFSFM] = tape_std_mtfsfm,
1268 [MTEOM] = tape_std_mteom,
1269 [MTERASE] = tape_std_mterase,
1270 [MTRAS1] = NULL,
1271 [MTRAS2] = NULL,
1272 [MTRAS3] = NULL,
1273 [MTSETBLK] = tape_std_mtsetblk,
1274 [MTSETDENSITY] = NULL,
1275 [MTSEEK] = tape_34xx_mtseek,
1276 [MTTELL] = tape_34xx_mttell,
1277 [MTSETDRVBUFFER] = NULL,
1278 [MTFSS] = NULL,
1279 [MTBSS] = NULL,
1280 [MTWSM] = NULL,
1281 [MTLOCK] = NULL,
1282 [MTUNLOCK] = NULL,
1283 [MTLOAD] = tape_std_mtload,
1284 [MTUNLOAD] = tape_std_mtunload,
1285 [MTCOMPRESSION] = tape_std_mtcompression,
1286 [MTSETPART] = NULL,
1287 [MTMKPART] = NULL
1291 * Tape discipline structure for 3480 and 3490.
1293 static struct tape_discipline tape_discipline_34xx = {
1294 .owner = THIS_MODULE,
1295 .setup_device = tape_34xx_setup_device,
1296 .cleanup_device = tape_34xx_cleanup_device,
1297 .process_eov = tape_std_process_eov,
1298 .irq = tape_34xx_irq,
1299 .read_block = tape_std_read_block,
1300 .write_block = tape_std_write_block,
1301 #ifdef CONFIG_S390_TAPE_BLOCK
1302 .bread = tape_34xx_bread,
1303 .free_bread = tape_34xx_free_bread,
1304 .check_locate = tape_34xx_check_locate,
1305 #endif
1306 .ioctl_fn = tape_34xx_ioctl,
1307 .mtop_array = tape_34xx_mtop
1310 static struct ccw_device_id tape_34xx_ids[] = {
1311 { CCW_DEVICE_DEVTYPE(0x3480, 0, 0x3480, 0), .driver_info = tape_3480},
1312 { CCW_DEVICE_DEVTYPE(0x3490, 0, 0x3490, 0), .driver_info = tape_3490},
1313 { /* end of list */ },
1316 static int
1317 tape_34xx_online(struct ccw_device *cdev)
1319 return tape_generic_online(
1320 cdev->dev.driver_data,
1321 &tape_discipline_34xx
1325 static int
1326 tape_34xx_offline(struct ccw_device *cdev)
1328 return tape_generic_offline(cdev->dev.driver_data);
1331 static struct ccw_driver tape_34xx_driver = {
1332 .name = "tape_34xx",
1333 .owner = THIS_MODULE,
1334 .ids = tape_34xx_ids,
1335 .probe = tape_generic_probe,
1336 .remove = tape_generic_remove,
1337 .set_online = tape_34xx_online,
1338 .set_offline = tape_34xx_offline,
1341 static int
1342 tape_34xx_init (void)
1344 int rc;
1346 TAPE_DBF_AREA = debug_register ( "tape_34xx", 2, 2, 4*sizeof(long));
1347 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1348 #ifdef DBF_LIKE_HELL
1349 debug_set_level(TAPE_DBF_AREA, 6);
1350 #endif
1352 DBF_EVENT(3, "34xx init\n");
1353 /* Register driver for 3480/3490 tapes. */
1354 rc = ccw_driver_register(&tape_34xx_driver);
1355 if (rc)
1356 DBF_EVENT(3, "34xx init failed\n");
1357 else
1358 DBF_EVENT(3, "34xx registered\n");
1359 return rc;
1362 static void
1363 tape_34xx_exit(void)
1365 ccw_driver_unregister(&tape_34xx_driver);
1367 debug_unregister(TAPE_DBF_AREA);
1370 MODULE_DEVICE_TABLE(ccw, tape_34xx_ids);
1371 MODULE_AUTHOR("(C) 2001-2002 IBM Deutschland Entwicklung GmbH");
1372 MODULE_DESCRIPTION("Linux on zSeries channel attached 3480 tape device driver");
1373 MODULE_LICENSE("GPL");
1375 module_init(tape_34xx_init);
1376 module_exit(tape_34xx_exit);