[S390] Remove error checking from copy_oldmem_page()
[linux/fpc-iii.git] / drivers / s390 / char / tape_34xx.c
blob934ef33eb9a478bf357341100468e89183e88429
1 /*
2 * drivers/s390/char/tape_34xx.c
3 * tape device discipline for 3480/3490 tapes.
5 * Copyright IBM Corp. 2001, 2009
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 #define KMSG_COMPONENT "tape_34xx"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/bio.h>
17 #include <linux/workqueue.h>
18 #include <linux/slab.h>
20 #define TAPE_DBF_AREA tape_34xx_dbf
22 #include "tape.h"
23 #include "tape_std.h"
26 * Pointer to debug area.
28 debug_info_t *TAPE_DBF_AREA = NULL;
29 EXPORT_SYMBOL(TAPE_DBF_AREA);
31 #define TAPE34XX_FMT_3480 0
32 #define TAPE34XX_FMT_3480_2_XF 1
33 #define TAPE34XX_FMT_3480_XF 2
35 struct tape_34xx_block_id {
36 unsigned int wrap : 1;
37 unsigned int segment : 7;
38 unsigned int format : 2;
39 unsigned int block : 22;
43 * A list of block ID's is used to faster seek blocks.
45 struct tape_34xx_sbid {
46 struct list_head list;
47 struct tape_34xx_block_id bid;
50 static void tape_34xx_delete_sbid_from(struct tape_device *, int);
53 * Medium sense for 34xx tapes. There is no 'real' medium sense call.
54 * So we just do a normal sense.
56 static void __tape_34xx_medium_sense(struct tape_request *request)
58 struct tape_device *device = request->device;
59 unsigned char *sense;
61 if (request->rc == 0) {
62 sense = request->cpdata;
65 * This isn't quite correct. But since INTERVENTION_REQUIRED
66 * means that the drive is 'neither ready nor on-line' it is
67 * only slightly inaccurate to say there is no tape loaded if
68 * the drive isn't online...
70 if (sense[0] & SENSE_INTERVENTION_REQUIRED)
71 tape_med_state_set(device, MS_UNLOADED);
72 else
73 tape_med_state_set(device, MS_LOADED);
75 if (sense[1] & SENSE_WRITE_PROTECT)
76 device->tape_generic_status |= GMT_WR_PROT(~0);
77 else
78 device->tape_generic_status &= ~GMT_WR_PROT(~0);
79 } else
80 DBF_EVENT(4, "tape_34xx: medium sense failed with rc=%d\n",
81 request->rc);
82 tape_free_request(request);
85 static int tape_34xx_medium_sense(struct tape_device *device)
87 struct tape_request *request;
88 int rc;
90 request = tape_alloc_request(1, 32);
91 if (IS_ERR(request)) {
92 DBF_EXCEPTION(6, "MSEN fail\n");
93 return PTR_ERR(request);
96 request->op = TO_MSEN;
97 tape_ccw_end(request->cpaddr, SENSE, 32, request->cpdata);
98 rc = tape_do_io_interruptible(device, request);
99 __tape_34xx_medium_sense(request);
100 return rc;
103 static void tape_34xx_medium_sense_async(struct tape_device *device)
105 struct tape_request *request;
107 request = tape_alloc_request(1, 32);
108 if (IS_ERR(request)) {
109 DBF_EXCEPTION(6, "MSEN fail\n");
110 return;
113 request->op = TO_MSEN;
114 tape_ccw_end(request->cpaddr, SENSE, 32, request->cpdata);
115 request->callback = (void *) __tape_34xx_medium_sense;
116 request->callback_data = NULL;
117 tape_do_io_async(device, request);
120 struct tape_34xx_work {
121 struct tape_device *device;
122 enum tape_op op;
123 struct work_struct work;
127 * These functions are currently used only to schedule a medium_sense for
128 * later execution. This is because we get an interrupt whenever a medium
129 * is inserted but cannot call tape_do_io* from an interrupt context.
130 * Maybe that's useful for other actions we want to start from the
131 * interrupt handler.
132 * Note: the work handler is called by the system work queue. The tape
133 * commands started by the handler need to be asynchrounous, otherwise
134 * a deadlock can occur e.g. in case of a deferred cc=1 (see __tape_do_irq).
136 static void
137 tape_34xx_work_handler(struct work_struct *work)
139 struct tape_34xx_work *p =
140 container_of(work, struct tape_34xx_work, work);
141 struct tape_device *device = p->device;
143 switch(p->op) {
144 case TO_MSEN:
145 tape_34xx_medium_sense_async(device);
146 break;
147 default:
148 DBF_EVENT(3, "T34XX: internal error: unknown work\n");
150 tape_put_device(device);
151 kfree(p);
154 static int
155 tape_34xx_schedule_work(struct tape_device *device, enum tape_op op)
157 struct tape_34xx_work *p;
159 if ((p = kzalloc(sizeof(*p), GFP_ATOMIC)) == NULL)
160 return -ENOMEM;
162 INIT_WORK(&p->work, tape_34xx_work_handler);
164 p->device = tape_get_device(device);
165 p->op = op;
167 schedule_work(&p->work);
168 return 0;
172 * Done Handler is called when dev stat = DEVICE-END (successful operation)
174 static inline int
175 tape_34xx_done(struct tape_request *request)
177 DBF_EVENT(6, "%s done\n", tape_op_verbose[request->op]);
179 switch (request->op) {
180 case TO_DSE:
181 case TO_RUN:
182 case TO_WRI:
183 case TO_WTM:
184 case TO_ASSIGN:
185 case TO_UNASSIGN:
186 tape_34xx_delete_sbid_from(request->device, 0);
187 break;
188 default:
191 return TAPE_IO_SUCCESS;
194 static inline int
195 tape_34xx_erp_failed(struct tape_request *request, int rc)
197 DBF_EVENT(3, "Error recovery failed for %s (RC=%d)\n",
198 tape_op_verbose[request->op], rc);
199 return rc;
202 static inline int
203 tape_34xx_erp_succeeded(struct tape_request *request)
205 DBF_EVENT(3, "Error Recovery successful for %s\n",
206 tape_op_verbose[request->op]);
207 return tape_34xx_done(request);
210 static inline int
211 tape_34xx_erp_retry(struct tape_request *request)
213 DBF_EVENT(3, "xerp retr %s\n", tape_op_verbose[request->op]);
214 return TAPE_IO_RETRY;
218 * This function is called, when no request is outstanding and we get an
219 * interrupt
221 static int
222 tape_34xx_unsolicited_irq(struct tape_device *device, struct irb *irb)
224 if (irb->scsw.cmd.dstat == 0x85) { /* READY */
225 /* A medium was inserted in the drive. */
226 DBF_EVENT(6, "xuud med\n");
227 tape_34xx_delete_sbid_from(device, 0);
228 tape_34xx_schedule_work(device, TO_MSEN);
229 } else {
230 DBF_EVENT(3, "unsol.irq! dev end: %08x\n", device->cdev_id);
231 tape_dump_sense_dbf(device, NULL, irb);
233 return TAPE_IO_SUCCESS;
237 * Read Opposite Error Recovery Function:
238 * Used, when Read Forward does not work
240 static int
241 tape_34xx_erp_read_opposite(struct tape_device *device,
242 struct tape_request *request)
244 if (request->op == TO_RFO) {
246 * We did read forward, but the data could not be read
247 * *correctly*. We transform the request to a read backward
248 * and try again.
250 tape_std_read_backward(device, request);
251 return tape_34xx_erp_retry(request);
255 * We tried to read forward and backward, but hat no
256 * success -> failed.
258 return tape_34xx_erp_failed(request, -EIO);
261 static int
262 tape_34xx_erp_bug(struct tape_device *device, struct tape_request *request,
263 struct irb *irb, int no)
265 if (request->op != TO_ASSIGN) {
266 dev_err(&device->cdev->dev, "An unexpected condition %d "
267 "occurred in tape error recovery\n", no);
268 tape_dump_sense_dbf(device, request, irb);
270 return tape_34xx_erp_failed(request, -EIO);
274 * Handle data overrun between cu and drive. The channel speed might
275 * be too slow.
277 static int
278 tape_34xx_erp_overrun(struct tape_device *device, struct tape_request *request,
279 struct irb *irb)
281 if (irb->ecw[3] == 0x40) {
282 dev_warn (&device->cdev->dev, "A data overrun occurred between"
283 " the control unit and tape unit\n");
284 return tape_34xx_erp_failed(request, -EIO);
286 return tape_34xx_erp_bug(device, request, irb, -1);
290 * Handle record sequence error.
292 static int
293 tape_34xx_erp_sequence(struct tape_device *device,
294 struct tape_request *request, struct irb *irb)
296 if (irb->ecw[3] == 0x41) {
298 * cu detected incorrect block-id sequence on tape.
300 dev_warn (&device->cdev->dev, "The block ID sequence on the "
301 "tape is incorrect\n");
302 return tape_34xx_erp_failed(request, -EIO);
305 * Record sequence error bit is set, but erpa does not
306 * show record sequence error.
308 return tape_34xx_erp_bug(device, request, irb, -2);
312 * This function analyses the tape's sense-data in case of a unit-check.
313 * If possible, it tries to recover from the error. Else the user is
314 * informed about the problem.
316 static int
317 tape_34xx_unit_check(struct tape_device *device, struct tape_request *request,
318 struct irb *irb)
320 int inhibit_cu_recovery;
321 __u8* sense;
323 inhibit_cu_recovery = (*device->modeset_byte & 0x80) ? 1 : 0;
324 sense = irb->ecw;
326 #ifdef CONFIG_S390_TAPE_BLOCK
327 if (request->op == TO_BLOCK) {
329 * Recovery for block device requests. Set the block_position
330 * to something invalid and retry.
332 device->blk_data.block_position = -1;
333 if (request->retries-- <= 0)
334 return tape_34xx_erp_failed(request, -EIO);
335 else
336 return tape_34xx_erp_retry(request);
338 #endif
340 if (
341 sense[0] & SENSE_COMMAND_REJECT &&
342 sense[1] & SENSE_WRITE_PROTECT
344 if (
345 request->op == TO_DSE ||
346 request->op == TO_WRI ||
347 request->op == TO_WTM
349 /* medium is write protected */
350 return tape_34xx_erp_failed(request, -EACCES);
351 } else {
352 return tape_34xx_erp_bug(device, request, irb, -3);
357 * Special cases for various tape-states when reaching
358 * end of recorded area
360 * FIXME: Maybe a special case of the special case:
361 * sense[0] == SENSE_EQUIPMENT_CHECK &&
362 * sense[1] == SENSE_DRIVE_ONLINE &&
363 * sense[3] == 0x47 (Volume Fenced)
365 * This was caused by continued FSF or FSR after an
366 * 'End Of Data'.
368 if ((
369 sense[0] == SENSE_DATA_CHECK ||
370 sense[0] == SENSE_EQUIPMENT_CHECK ||
371 sense[0] == SENSE_EQUIPMENT_CHECK + SENSE_DEFERRED_UNIT_CHECK
372 ) && (
373 sense[1] == SENSE_DRIVE_ONLINE ||
374 sense[1] == SENSE_BEGINNING_OF_TAPE + SENSE_WRITE_MODE
375 )) {
376 switch (request->op) {
378 * sense[0] == SENSE_DATA_CHECK &&
379 * sense[1] == SENSE_DRIVE_ONLINE
380 * sense[3] == 0x36 (End Of Data)
382 * Further seeks might return a 'Volume Fenced'.
384 case TO_FSF:
385 case TO_FSB:
386 /* Trying to seek beyond end of recorded area */
387 return tape_34xx_erp_failed(request, -ENOSPC);
388 case TO_BSB:
389 return tape_34xx_erp_retry(request);
392 * sense[0] == SENSE_DATA_CHECK &&
393 * sense[1] == SENSE_DRIVE_ONLINE &&
394 * sense[3] == 0x36 (End Of Data)
396 case TO_LBL:
397 /* Block could not be located. */
398 tape_34xx_delete_sbid_from(device, 0);
399 return tape_34xx_erp_failed(request, -EIO);
401 case TO_RFO:
402 /* Read beyond end of recorded area -> 0 bytes read */
403 return tape_34xx_erp_failed(request, 0);
406 * sense[0] == SENSE_EQUIPMENT_CHECK &&
407 * sense[1] == SENSE_DRIVE_ONLINE &&
408 * sense[3] == 0x38 (Physical End Of Volume)
410 case TO_WRI:
411 /* Writing at physical end of volume */
412 return tape_34xx_erp_failed(request, -ENOSPC);
413 default:
414 return tape_34xx_erp_failed(request, 0);
418 /* Sensing special bits */
419 if (sense[0] & SENSE_BUS_OUT_CHECK)
420 return tape_34xx_erp_retry(request);
422 if (sense[0] & SENSE_DATA_CHECK) {
424 * hardware failure, damaged tape or improper
425 * operating conditions
427 switch (sense[3]) {
428 case 0x23:
429 /* a read 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, -4);
438 /* data check is permanent, CU recovery has failed */
439 dev_warn (&device->cdev->dev, "A read error occurred "
440 "that cannot be recovered\n");
441 return tape_34xx_erp_failed(request, -EIO);
442 case 0x25:
443 // a write data check occurred
444 if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
445 inhibit_cu_recovery)
446 // data check is not permanent, may be
447 // recovered. We always use async-mode with
448 // cu-recovery, so this should *never* happen.
449 return tape_34xx_erp_bug(device, request,
450 irb, -5);
452 // data check is permanent, cu-recovery has failed
453 dev_warn (&device->cdev->dev, "A write error on the "
454 "tape cannot be recovered\n");
455 return tape_34xx_erp_failed(request, -EIO);
456 case 0x26:
457 /* Data Check (read opposite) occurred. */
458 return tape_34xx_erp_read_opposite(device, request);
459 case 0x28:
460 /* ID-Mark at tape start couldn't be written */
461 dev_warn (&device->cdev->dev, "Writing the ID-mark "
462 "failed\n");
463 return tape_34xx_erp_failed(request, -EIO);
464 case 0x31:
465 /* Tape void. Tried to read beyond end of device. */
466 dev_warn (&device->cdev->dev, "Reading the tape beyond"
467 " the end of the recorded area failed\n");
468 return tape_34xx_erp_failed(request, -ENOSPC);
469 case 0x41:
470 /* Record sequence error. */
471 dev_warn (&device->cdev->dev, "The tape contains an "
472 "incorrect block ID sequence\n");
473 return tape_34xx_erp_failed(request, -EIO);
474 default:
475 /* all data checks for 3480 should result in one of
476 * the above erpa-codes. For 3490, other data-check
477 * conditions do exist. */
478 if (device->cdev->id.driver_info == tape_3480)
479 return tape_34xx_erp_bug(device, request,
480 irb, -6);
484 if (sense[0] & SENSE_OVERRUN)
485 return tape_34xx_erp_overrun(device, request, irb);
487 if (sense[1] & SENSE_RECORD_SEQUENCE_ERR)
488 return tape_34xx_erp_sequence(device, request, irb);
490 /* Sensing erpa codes */
491 switch (sense[3]) {
492 case 0x00:
493 /* Unit check with erpa code 0. Report and ignore. */
494 return TAPE_IO_SUCCESS;
495 case 0x21:
497 * Data streaming not operational. CU will switch to
498 * interlock mode. Reissue the command.
500 return tape_34xx_erp_retry(request);
501 case 0x22:
503 * Path equipment check. Might be drive adapter error, buffer
504 * error on the lower interface, internal path not usable,
505 * or error during cartridge load.
507 dev_warn (&device->cdev->dev, "A path equipment check occurred"
508 " for the tape device\n");
509 return tape_34xx_erp_failed(request, -EIO);
510 case 0x24:
512 * Load display check. Load display was command was issued,
513 * but the drive is displaying a drive check message. Can
514 * be threated as "device end".
516 return tape_34xx_erp_succeeded(request);
517 case 0x27:
519 * Command reject. May indicate illegal channel program or
520 * buffer over/underrun. Since all channel programs are
521 * issued by this driver and ought be correct, we assume a
522 * over/underrun situation and retry the channel program.
524 return tape_34xx_erp_retry(request);
525 case 0x29:
527 * Function incompatible. Either the tape is idrc compressed
528 * but the hardware isn't capable to do idrc, or a perform
529 * subsystem func is issued and the CU is not on-line.
531 return tape_34xx_erp_failed(request, -EIO);
532 case 0x2a:
534 * Unsolicited environmental data. An internal counter
535 * overflows, we can ignore this and reissue the cmd.
537 return tape_34xx_erp_retry(request);
538 case 0x2b:
540 * Environmental data present. Indicates either unload
541 * completed ok or read buffered log command completed ok.
543 if (request->op == TO_RUN) {
544 /* Rewind unload completed ok. */
545 tape_med_state_set(device, MS_UNLOADED);
546 return tape_34xx_erp_succeeded(request);
548 /* tape_34xx doesn't use read buffered log commands. */
549 return tape_34xx_erp_bug(device, request, irb, sense[3]);
550 case 0x2c:
552 * Permanent equipment check. CU has tried recovery, but
553 * did not succeed.
555 return tape_34xx_erp_failed(request, -EIO);
556 case 0x2d:
557 /* Data security erase failure. */
558 if (request->op == TO_DSE)
559 return tape_34xx_erp_failed(request, -EIO);
560 /* Data security erase failure, but no such command issued. */
561 return tape_34xx_erp_bug(device, request, irb, sense[3]);
562 case 0x2e:
564 * Not capable. This indicates either that the drive fails
565 * reading the format id mark or that that format specified
566 * is not supported by the drive.
568 dev_warn (&device->cdev->dev, "The tape unit cannot process "
569 "the tape format\n");
570 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
571 case 0x30:
572 /* The medium is write protected. */
573 dev_warn (&device->cdev->dev, "The tape medium is write-"
574 "protected\n");
575 return tape_34xx_erp_failed(request, -EACCES);
576 case 0x32:
577 // Tension loss. We cannot recover this, it's an I/O error.
578 dev_warn (&device->cdev->dev, "The tape does not have the "
579 "required tape tension\n");
580 return tape_34xx_erp_failed(request, -EIO);
581 case 0x33:
583 * Load Failure. The cartridge was not inserted correctly or
584 * the tape is not threaded correctly.
586 dev_warn (&device->cdev->dev, "The tape unit failed to load"
587 " the cartridge\n");
588 tape_34xx_delete_sbid_from(device, 0);
589 return tape_34xx_erp_failed(request, -EIO);
590 case 0x34:
592 * Unload failure. The drive cannot maintain tape tension
593 * and control tape movement during an unload operation.
595 dev_warn (&device->cdev->dev, "Automatic unloading of the tape"
596 " cartridge failed\n");
597 if (request->op == TO_RUN)
598 return tape_34xx_erp_failed(request, -EIO);
599 return tape_34xx_erp_bug(device, request, irb, sense[3]);
600 case 0x35:
602 * Drive equipment check. One of the following:
603 * - cu cannot recover from a drive detected error
604 * - a check code message is shown on drive display
605 * - the cartridge loader does not respond correctly
606 * - a failure occurs during an index, load, or unload cycle
608 dev_warn (&device->cdev->dev, "An equipment check has occurred"
609 " on the tape unit\n");
610 return tape_34xx_erp_failed(request, -EIO);
611 case 0x36:
612 if (device->cdev->id.driver_info == tape_3490)
613 /* End of data. */
614 return tape_34xx_erp_failed(request, -EIO);
615 /* This erpa is reserved for 3480 */
616 return tape_34xx_erp_bug(device, request, irb, sense[3]);
617 case 0x37:
619 * Tape length error. The tape is shorter than reported in
620 * the beginning-of-tape data.
622 dev_warn (&device->cdev->dev, "The tape information states an"
623 " incorrect length\n");
624 return tape_34xx_erp_failed(request, -EIO);
625 case 0x38:
627 * Physical end of tape. A read/write operation reached
628 * the physical end of tape.
630 if (request->op==TO_WRI ||
631 request->op==TO_DSE ||
632 request->op==TO_WTM)
633 return tape_34xx_erp_failed(request, -ENOSPC);
634 return tape_34xx_erp_failed(request, -EIO);
635 case 0x39:
636 /* Backward at Beginning of tape. */
637 return tape_34xx_erp_failed(request, -EIO);
638 case 0x3a:
639 /* Drive switched to not ready. */
640 dev_warn (&device->cdev->dev, "The tape unit is not ready\n");
641 return tape_34xx_erp_failed(request, -EIO);
642 case 0x3b:
643 /* Manual rewind or unload. This causes an I/O error. */
644 dev_warn (&device->cdev->dev, "The tape medium has been "
645 "rewound or unloaded manually\n");
646 tape_34xx_delete_sbid_from(device, 0);
647 return tape_34xx_erp_failed(request, -EIO);
648 case 0x42:
650 * Degraded mode. A condition that can cause degraded
651 * performance is detected.
653 dev_warn (&device->cdev->dev, "The tape subsystem is running "
654 "in degraded mode\n");
655 return tape_34xx_erp_retry(request);
656 case 0x43:
657 /* Drive not ready. */
658 tape_34xx_delete_sbid_from(device, 0);
659 tape_med_state_set(device, MS_UNLOADED);
660 /* Some commands commands are successful even in this case */
661 if (sense[1] & SENSE_DRIVE_ONLINE) {
662 switch(request->op) {
663 case TO_ASSIGN:
664 case TO_UNASSIGN:
665 case TO_DIS:
666 case TO_NOP:
667 return tape_34xx_done(request);
668 break;
669 default:
670 break;
673 return tape_34xx_erp_failed(request, -ENOMEDIUM);
674 case 0x44:
675 /* Locate Block unsuccessful. */
676 if (request->op != TO_BLOCK && request->op != TO_LBL)
677 /* No locate block was issued. */
678 return tape_34xx_erp_bug(device, request,
679 irb, sense[3]);
680 return tape_34xx_erp_failed(request, -EIO);
681 case 0x45:
682 /* The drive is assigned to a different channel path. */
683 dev_warn (&device->cdev->dev, "The tape unit is already "
684 "assigned\n");
685 return tape_34xx_erp_failed(request, -EIO);
686 case 0x46:
688 * Drive not on-line. Drive may be switched offline,
689 * the power supply may be switched off or
690 * the drive address may not be set correctly.
692 dev_warn (&device->cdev->dev, "The tape unit is not online\n");
693 return tape_34xx_erp_failed(request, -EIO);
694 case 0x47:
695 /* Volume fenced. CU reports volume integrity is lost. */
696 dev_warn (&device->cdev->dev, "The control unit has fenced "
697 "access to the tape volume\n");
698 tape_34xx_delete_sbid_from(device, 0);
699 return tape_34xx_erp_failed(request, -EIO);
700 case 0x48:
701 /* Log sense data and retry request. */
702 return tape_34xx_erp_retry(request);
703 case 0x49:
704 /* Bus out check. A parity check error on the bus was found. */
705 dev_warn (&device->cdev->dev, "A parity error occurred on the "
706 "tape bus\n");
707 return tape_34xx_erp_failed(request, -EIO);
708 case 0x4a:
709 /* Control unit erp failed. */
710 dev_warn (&device->cdev->dev, "I/O error recovery failed on "
711 "the tape control unit\n");
712 return tape_34xx_erp_failed(request, -EIO);
713 case 0x4b:
715 * CU and drive incompatible. The drive requests micro-program
716 * patches, which are not available on the CU.
718 dev_warn (&device->cdev->dev, "The tape unit requires a "
719 "firmware update\n");
720 return tape_34xx_erp_failed(request, -EIO);
721 case 0x4c:
723 * Recovered Check-One failure. Cu develops a hardware error,
724 * but is able to recover.
726 return tape_34xx_erp_retry(request);
727 case 0x4d:
728 if (device->cdev->id.driver_info == tape_3490)
730 * Resetting event received. Since the driver does
731 * not support resetting event recovery (which has to
732 * be handled by the I/O Layer), retry our command.
734 return tape_34xx_erp_retry(request);
735 /* This erpa is reserved for 3480. */
736 return tape_34xx_erp_bug(device, request, irb, sense[3]);
737 case 0x4e:
738 if (device->cdev->id.driver_info == tape_3490) {
740 * Maximum block size exceeded. This indicates, that
741 * the block to be written is larger than allowed for
742 * buffered mode.
744 dev_warn (&device->cdev->dev, "The maximum block size"
745 " for buffered mode is exceeded\n");
746 return tape_34xx_erp_failed(request, -ENOBUFS);
748 /* This erpa is reserved for 3480. */
749 return tape_34xx_erp_bug(device, request, irb, sense[3]);
750 case 0x50:
752 * Read buffered log (Overflow). CU is running in extended
753 * buffered log mode, and a counter overflows. This should
754 * never happen, since we're never running in extended
755 * buffered log mode.
757 return tape_34xx_erp_retry(request);
758 case 0x51:
760 * Read buffered log (EOV). EOF processing occurs while the
761 * CU is in extended buffered log mode. This should never
762 * happen, since we're never running in extended buffered
763 * log mode.
765 return tape_34xx_erp_retry(request);
766 case 0x52:
767 /* End of Volume complete. Rewind unload completed ok. */
768 if (request->op == TO_RUN) {
769 tape_med_state_set(device, MS_UNLOADED);
770 tape_34xx_delete_sbid_from(device, 0);
771 return tape_34xx_erp_succeeded(request);
773 return tape_34xx_erp_bug(device, request, irb, sense[3]);
774 case 0x53:
775 /* Global command intercept. */
776 return tape_34xx_erp_retry(request);
777 case 0x54:
778 /* Channel interface recovery (temporary). */
779 return tape_34xx_erp_retry(request);
780 case 0x55:
781 /* Channel interface recovery (permanent). */
782 dev_warn (&device->cdev->dev, "A channel interface error cannot be"
783 " recovered\n");
784 return tape_34xx_erp_failed(request, -EIO);
785 case 0x56:
786 /* Channel protocol error. */
787 dev_warn (&device->cdev->dev, "A channel protocol error "
788 "occurred\n");
789 return tape_34xx_erp_failed(request, -EIO);
790 case 0x57:
791 if (device->cdev->id.driver_info == tape_3480) {
792 /* Attention intercept. */
793 return tape_34xx_erp_retry(request);
794 } else {
795 /* Global status intercept. */
796 return tape_34xx_erp_retry(request);
798 case 0x5a:
800 * Tape length incompatible. The tape inserted is too long,
801 * which could cause damage to the tape or the drive.
803 dev_warn (&device->cdev->dev, "The tape unit does not support "
804 "the tape length\n");
805 return tape_34xx_erp_failed(request, -EIO);
806 case 0x5b:
807 /* Format 3480 XF incompatible */
808 if (sense[1] & SENSE_BEGINNING_OF_TAPE)
809 /* The tape will get overwritten. */
810 return tape_34xx_erp_retry(request);
811 dev_warn (&device->cdev->dev, "The tape unit does not support"
812 " format 3480 XF\n");
813 return tape_34xx_erp_failed(request, -EIO);
814 case 0x5c:
815 /* Format 3480-2 XF incompatible */
816 dev_warn (&device->cdev->dev, "The tape unit does not support tape "
817 "format 3480-2 XF\n");
818 return tape_34xx_erp_failed(request, -EIO);
819 case 0x5d:
820 /* Tape length violation. */
821 dev_warn (&device->cdev->dev, "The tape unit does not support"
822 " the current tape length\n");
823 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
824 case 0x5e:
825 /* Compaction algorithm incompatible. */
826 dev_warn (&device->cdev->dev, "The tape unit does not support"
827 " the compaction algorithm\n");
828 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
830 /* The following erpas should have been covered earlier. */
831 case 0x23: /* Read data check. */
832 case 0x25: /* Write data check. */
833 case 0x26: /* Data check (read opposite). */
834 case 0x28: /* Write id mark check. */
835 case 0x31: /* Tape void. */
836 case 0x40: /* Overrun error. */
837 case 0x41: /* Record sequence error. */
838 /* All other erpas are reserved for future use. */
839 default:
840 return tape_34xx_erp_bug(device, request, irb, sense[3]);
845 * 3480/3490 interrupt handler
847 static int
848 tape_34xx_irq(struct tape_device *device, struct tape_request *request,
849 struct irb *irb)
851 if (request == NULL)
852 return tape_34xx_unsolicited_irq(device, irb);
854 if ((irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) &&
855 (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) &&
856 (request->op == TO_WRI)) {
857 /* Write at end of volume */
858 return tape_34xx_erp_failed(request, -ENOSPC);
861 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
862 return tape_34xx_unit_check(device, request, irb);
864 if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
866 * A unit exception occurs on skipping over a tapemark block.
868 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) {
869 if (request->op == TO_BSB || request->op == TO_FSB)
870 request->rescnt++;
871 else
872 DBF_EVENT(5, "Unit Exception!\n");
874 return tape_34xx_done(request);
877 DBF_EVENT(6, "xunknownirq\n");
878 tape_dump_sense_dbf(device, request, irb);
879 return TAPE_IO_STOP;
883 * ioctl_overload
885 static int
886 tape_34xx_ioctl(struct tape_device *device, unsigned int cmd, unsigned long arg)
888 if (cmd == TAPE390_DISPLAY) {
889 struct display_struct disp;
891 if (copy_from_user(&disp, (char __user *) arg, sizeof(disp)) != 0)
892 return -EFAULT;
894 return tape_std_display(device, &disp);
895 } else
896 return -EINVAL;
899 static inline void
900 tape_34xx_append_new_sbid(struct tape_34xx_block_id bid, struct list_head *l)
902 struct tape_34xx_sbid * new_sbid;
904 new_sbid = kmalloc(sizeof(*new_sbid), GFP_ATOMIC);
905 if (!new_sbid)
906 return;
908 new_sbid->bid = bid;
909 list_add(&new_sbid->list, l);
913 * Build up the search block ID list. The block ID consists of a logical
914 * block number and a hardware specific part. The hardware specific part
915 * helps the tape drive to speed up searching for a specific block.
917 static void
918 tape_34xx_add_sbid(struct tape_device *device, struct tape_34xx_block_id bid)
920 struct list_head * sbid_list;
921 struct tape_34xx_sbid * sbid;
922 struct list_head * l;
925 * immediately return if there is no list at all or the block to add
926 * is located in segment 1 of wrap 0 because this position is used
927 * if no hardware position data is supplied.
929 sbid_list = (struct list_head *) device->discdata;
930 if (!sbid_list || (bid.segment < 2 && bid.wrap == 0))
931 return;
934 * Search the position where to insert the new entry. Hardware
935 * acceleration uses only the segment and wrap number. So we
936 * need only one entry for a specific wrap/segment combination.
937 * If there is a block with a lower number but the same hard-
938 * ware position data we just update the block number in the
939 * existing entry.
941 list_for_each(l, sbid_list) {
942 sbid = list_entry(l, struct tape_34xx_sbid, list);
944 if (
945 (sbid->bid.segment == bid.segment) &&
946 (sbid->bid.wrap == bid.wrap)
948 if (bid.block < sbid->bid.block)
949 sbid->bid = bid;
950 else return;
951 break;
954 /* Sort in according to logical block number. */
955 if (bid.block < sbid->bid.block) {
956 tape_34xx_append_new_sbid(bid, l->prev);
957 break;
960 /* List empty or new block bigger than last entry. */
961 if (l == sbid_list)
962 tape_34xx_append_new_sbid(bid, l->prev);
964 DBF_LH(4, "Current list is:\n");
965 list_for_each(l, sbid_list) {
966 sbid = list_entry(l, struct tape_34xx_sbid, list);
967 DBF_LH(4, "%d:%03d@%05d\n",
968 sbid->bid.wrap,
969 sbid->bid.segment,
970 sbid->bid.block
976 * Delete all entries from the search block ID list that belong to tape blocks
977 * equal or higher than the given number.
979 static void
980 tape_34xx_delete_sbid_from(struct tape_device *device, int from)
982 struct list_head * sbid_list;
983 struct tape_34xx_sbid * sbid;
984 struct list_head * l;
985 struct list_head * n;
987 sbid_list = (struct list_head *) device->discdata;
988 if (!sbid_list)
989 return;
991 list_for_each_safe(l, n, sbid_list) {
992 sbid = list_entry(l, struct tape_34xx_sbid, list);
993 if (sbid->bid.block >= from) {
994 DBF_LH(4, "Delete sbid %d:%03d@%05d\n",
995 sbid->bid.wrap,
996 sbid->bid.segment,
997 sbid->bid.block
999 list_del(l);
1000 kfree(sbid);
1006 * Merge hardware position data into a block id.
1008 static void
1009 tape_34xx_merge_sbid(
1010 struct tape_device * device,
1011 struct tape_34xx_block_id * bid
1013 struct tape_34xx_sbid * sbid;
1014 struct tape_34xx_sbid * sbid_to_use;
1015 struct list_head * sbid_list;
1016 struct list_head * l;
1018 sbid_list = (struct list_head *) device->discdata;
1019 bid->wrap = 0;
1020 bid->segment = 1;
1022 if (!sbid_list || list_empty(sbid_list))
1023 return;
1025 sbid_to_use = NULL;
1026 list_for_each(l, sbid_list) {
1027 sbid = list_entry(l, struct tape_34xx_sbid, list);
1029 if (sbid->bid.block >= bid->block)
1030 break;
1031 sbid_to_use = sbid;
1033 if (sbid_to_use) {
1034 bid->wrap = sbid_to_use->bid.wrap;
1035 bid->segment = sbid_to_use->bid.segment;
1036 DBF_LH(4, "Use %d:%03d@%05d for %05d\n",
1037 sbid_to_use->bid.wrap,
1038 sbid_to_use->bid.segment,
1039 sbid_to_use->bid.block,
1040 bid->block
1045 static int
1046 tape_34xx_setup_device(struct tape_device * device)
1048 int rc;
1049 struct list_head * discdata;
1051 DBF_EVENT(6, "34xx device setup\n");
1052 if ((rc = tape_std_assign(device)) == 0) {
1053 if ((rc = tape_34xx_medium_sense(device)) != 0) {
1054 DBF_LH(3, "34xx medium sense returned %d\n", rc);
1057 discdata = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1058 if (discdata) {
1059 INIT_LIST_HEAD(discdata);
1060 device->discdata = discdata;
1063 return rc;
1066 static void
1067 tape_34xx_cleanup_device(struct tape_device *device)
1069 tape_std_unassign(device);
1071 if (device->discdata) {
1072 tape_34xx_delete_sbid_from(device, 0);
1073 kfree(device->discdata);
1074 device->discdata = NULL;
1080 * MTTELL: Tell block. Return the number of block relative to current file.
1082 static int
1083 tape_34xx_mttell(struct tape_device *device, int mt_count)
1085 struct {
1086 struct tape_34xx_block_id cbid;
1087 struct tape_34xx_block_id dbid;
1088 } __attribute__ ((packed)) block_id;
1089 int rc;
1091 rc = tape_std_read_block_id(device, (__u64 *) &block_id);
1092 if (rc)
1093 return rc;
1095 tape_34xx_add_sbid(device, block_id.cbid);
1096 return block_id.cbid.block;
1100 * MTSEEK: seek to the specified block.
1102 static int
1103 tape_34xx_mtseek(struct tape_device *device, int mt_count)
1105 struct tape_request *request;
1106 struct tape_34xx_block_id * bid;
1108 if (mt_count > 0x3fffff) {
1109 DBF_EXCEPTION(6, "xsee parm\n");
1110 return -EINVAL;
1112 request = tape_alloc_request(3, 4);
1113 if (IS_ERR(request))
1114 return PTR_ERR(request);
1116 /* setup ccws */
1117 request->op = TO_LBL;
1118 bid = (struct tape_34xx_block_id *) request->cpdata;
1119 bid->format = (*device->modeset_byte & 0x08) ?
1120 TAPE34XX_FMT_3480_XF : TAPE34XX_FMT_3480;
1121 bid->block = mt_count;
1122 tape_34xx_merge_sbid(device, bid);
1124 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
1125 tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
1126 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
1128 /* execute it */
1129 return tape_do_io_free(device, request);
1132 #ifdef CONFIG_S390_TAPE_BLOCK
1134 * Tape block read for 34xx.
1136 static struct tape_request *
1137 tape_34xx_bread(struct tape_device *device, struct request *req)
1139 struct tape_request *request;
1140 struct ccw1 *ccw;
1141 int count = 0;
1142 unsigned off;
1143 char *dst;
1144 struct bio_vec *bv;
1145 struct req_iterator iter;
1146 struct tape_34xx_block_id * start_block;
1148 DBF_EVENT(6, "xBREDid:");
1150 /* Count the number of blocks for the request. */
1151 rq_for_each_segment(bv, req, iter)
1152 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 = blk_rq_pos(req) >> 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_segment(bv, req, iter) {
1180 dst = kmap(bv->bv_page) + bv->bv_offset;
1181 for (off = 0; off < bv->bv_len; off += TAPEBLOCK_HSEC_SIZE) {
1182 ccw->flags = CCW_FLAG_CC;
1183 ccw->cmd_code = READ_FORWARD;
1184 ccw->count = TAPEBLOCK_HSEC_SIZE;
1185 set_normalized_cda(ccw, (void*) __pa(dst));
1186 ccw++;
1187 dst += TAPEBLOCK_HSEC_SIZE;
1191 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
1192 DBF_EVENT(6, "xBREDccwg\n");
1193 return request;
1196 static void
1197 tape_34xx_free_bread (struct tape_request *request)
1199 struct ccw1* ccw;
1201 ccw = request->cpaddr;
1202 if ((ccw + 2)->cmd_code == READ_BLOCK_ID) {
1203 struct {
1204 struct tape_34xx_block_id cbid;
1205 struct tape_34xx_block_id dbid;
1206 } __attribute__ ((packed)) *rbi_data;
1208 rbi_data = request->cpdata;
1210 if (request->device)
1211 tape_34xx_add_sbid(request->device, rbi_data->cbid);
1214 /* Last ccw is a nop and doesn't need clear_normalized_cda */
1215 for (; ccw->flags & CCW_FLAG_CC; ccw++)
1216 if (ccw->cmd_code == READ_FORWARD)
1217 clear_normalized_cda(ccw);
1218 tape_free_request(request);
1222 * check_locate is called just before the tape request is passed to
1223 * the common io layer for execution. It has to check the current
1224 * tape position and insert a locate ccw if it doesn't match the
1225 * start block for the request.
1227 static void
1228 tape_34xx_check_locate(struct tape_device *device, struct tape_request *request)
1230 struct tape_34xx_block_id * start_block;
1232 start_block = (struct tape_34xx_block_id *) request->cpdata;
1233 if (start_block->block == device->blk_data.block_position)
1234 return;
1236 DBF_LH(4, "Block seek(%06d+%06d)\n", start_block->block, device->bof);
1237 start_block->wrap = 0;
1238 start_block->segment = 1;
1239 start_block->format = (*device->modeset_byte & 0x08) ?
1240 TAPE34XX_FMT_3480_XF :
1241 TAPE34XX_FMT_3480;
1242 start_block->block = start_block->block + device->bof;
1243 tape_34xx_merge_sbid(device, start_block);
1244 tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
1245 tape_ccw_cc(request->cpaddr + 2, READ_BLOCK_ID, 8, request->cpdata);
1247 #endif
1250 * List of 3480/3490 magnetic tape commands.
1252 static tape_mtop_fn tape_34xx_mtop[TAPE_NR_MTOPS] = {
1253 [MTRESET] = tape_std_mtreset,
1254 [MTFSF] = tape_std_mtfsf,
1255 [MTBSF] = tape_std_mtbsf,
1256 [MTFSR] = tape_std_mtfsr,
1257 [MTBSR] = tape_std_mtbsr,
1258 [MTWEOF] = tape_std_mtweof,
1259 [MTREW] = tape_std_mtrew,
1260 [MTOFFL] = tape_std_mtoffl,
1261 [MTNOP] = tape_std_mtnop,
1262 [MTRETEN] = tape_std_mtreten,
1263 [MTBSFM] = tape_std_mtbsfm,
1264 [MTFSFM] = tape_std_mtfsfm,
1265 [MTEOM] = tape_std_mteom,
1266 [MTERASE] = tape_std_mterase,
1267 [MTRAS1] = NULL,
1268 [MTRAS2] = NULL,
1269 [MTRAS3] = NULL,
1270 [MTSETBLK] = tape_std_mtsetblk,
1271 [MTSETDENSITY] = NULL,
1272 [MTSEEK] = tape_34xx_mtseek,
1273 [MTTELL] = tape_34xx_mttell,
1274 [MTSETDRVBUFFER] = NULL,
1275 [MTFSS] = NULL,
1276 [MTBSS] = NULL,
1277 [MTWSM] = NULL,
1278 [MTLOCK] = NULL,
1279 [MTUNLOCK] = NULL,
1280 [MTLOAD] = tape_std_mtload,
1281 [MTUNLOAD] = tape_std_mtunload,
1282 [MTCOMPRESSION] = tape_std_mtcompression,
1283 [MTSETPART] = NULL,
1284 [MTMKPART] = NULL
1288 * Tape discipline structure for 3480 and 3490.
1290 static struct tape_discipline tape_discipline_34xx = {
1291 .owner = THIS_MODULE,
1292 .setup_device = tape_34xx_setup_device,
1293 .cleanup_device = tape_34xx_cleanup_device,
1294 .process_eov = tape_std_process_eov,
1295 .irq = tape_34xx_irq,
1296 .read_block = tape_std_read_block,
1297 .write_block = tape_std_write_block,
1298 #ifdef CONFIG_S390_TAPE_BLOCK
1299 .bread = tape_34xx_bread,
1300 .free_bread = tape_34xx_free_bread,
1301 .check_locate = tape_34xx_check_locate,
1302 #endif
1303 .ioctl_fn = tape_34xx_ioctl,
1304 .mtop_array = tape_34xx_mtop
1307 static struct ccw_device_id tape_34xx_ids[] = {
1308 { CCW_DEVICE_DEVTYPE(0x3480, 0, 0x3480, 0), .driver_info = tape_3480},
1309 { CCW_DEVICE_DEVTYPE(0x3490, 0, 0x3490, 0), .driver_info = tape_3490},
1310 { /* end of list */ },
1313 static int
1314 tape_34xx_online(struct ccw_device *cdev)
1316 return tape_generic_online(
1317 dev_get_drvdata(&cdev->dev),
1318 &tape_discipline_34xx
1322 static struct ccw_driver tape_34xx_driver = {
1323 .driver = {
1324 .name = "tape_34xx",
1325 .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_generic_offline,
1332 .freeze = tape_generic_pm_suspend,
1333 .int_class = IOINT_TAP,
1336 static int
1337 tape_34xx_init (void)
1339 int rc;
1341 TAPE_DBF_AREA = debug_register ( "tape_34xx", 2, 2, 4*sizeof(long));
1342 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1343 #ifdef DBF_LIKE_HELL
1344 debug_set_level(TAPE_DBF_AREA, 6);
1345 #endif
1347 DBF_EVENT(3, "34xx init\n");
1348 /* Register driver for 3480/3490 tapes. */
1349 rc = ccw_driver_register(&tape_34xx_driver);
1350 if (rc)
1351 DBF_EVENT(3, "34xx init failed\n");
1352 else
1353 DBF_EVENT(3, "34xx registered\n");
1354 return rc;
1357 static void
1358 tape_34xx_exit(void)
1360 ccw_driver_unregister(&tape_34xx_driver);
1362 debug_unregister(TAPE_DBF_AREA);
1365 MODULE_DEVICE_TABLE(ccw, tape_34xx_ids);
1366 MODULE_AUTHOR("(C) 2001-2002 IBM Deutschland Entwicklung GmbH");
1367 MODULE_DESCRIPTION("Linux on zSeries channel attached 3480 tape device driver");
1368 MODULE_LICENSE("GPL");
1370 module_init(tape_34xx_init);
1371 module_exit(tape_34xx_exit);