1
2
3
4
5
6
7
8
9
10
11#define KMSG_COMPONENT "tape_34xx"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
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>
19
20#define TAPE_DBF_AREA tape_34xx_dbf
21
22#include "tape.h"
23#include "tape_std.h"
24
25
26
27
28debug_info_t *TAPE_DBF_AREA = NULL;
29EXPORT_SYMBOL(TAPE_DBF_AREA);
30
31#define TAPE34XX_FMT_3480 0
32#define TAPE34XX_FMT_3480_2_XF 1
33#define TAPE34XX_FMT_3480_XF 2
34
35struct tape_34xx_block_id {
36 unsigned int wrap : 1;
37 unsigned int segment : 7;
38 unsigned int format : 2;
39 unsigned int block : 22;
40};
41
42
43
44
45struct tape_34xx_sbid {
46 struct list_head list;
47 struct tape_34xx_block_id bid;
48};
49
50static void tape_34xx_delete_sbid_from(struct tape_device *, int);
51
52
53
54
55
56static void __tape_34xx_medium_sense(struct tape_request *request)
57{
58 struct tape_device *device = request->device;
59 unsigned char *sense;
60
61 if (request->rc == 0) {
62 sense = request->cpdata;
63
64
65
66
67
68
69
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);
74
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);
83}
84
85static int tape_34xx_medium_sense(struct tape_device *device)
86{
87 struct tape_request *request;
88 int rc;
89
90 request = tape_alloc_request(1, 32);
91 if (IS_ERR(request)) {
92 DBF_EXCEPTION(6, "MSEN fail\n");
93 return PTR_ERR(request);
94 }
95
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;
101}
102
103static void tape_34xx_medium_sense_async(struct tape_device *device)
104{
105 struct tape_request *request;
106
107 request = tape_alloc_request(1, 32);
108 if (IS_ERR(request)) {
109 DBF_EXCEPTION(6, "MSEN fail\n");
110 return;
111 }
112
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);
118}
119
120struct tape_34xx_work {
121 struct tape_device *device;
122 enum tape_op op;
123 struct work_struct work;
124};
125
126
127
128
129
130
131
132
133
134
135
136static void
137tape_34xx_work_handler(struct work_struct *work)
138{
139 struct tape_34xx_work *p =
140 container_of(work, struct tape_34xx_work, work);
141 struct tape_device *device = p->device;
142
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");
149 }
150 tape_put_device(device);
151 kfree(p);
152}
153
154static int
155tape_34xx_schedule_work(struct tape_device *device, enum tape_op op)
156{
157 struct tape_34xx_work *p;
158
159 if ((p = kzalloc(sizeof(*p), GFP_ATOMIC)) == NULL)
160 return -ENOMEM;
161
162 INIT_WORK(&p->work, tape_34xx_work_handler);
163
164 p->device = tape_get_device(device);
165 p->op = op;
166
167 schedule_work(&p->work);
168 return 0;
169}
170
171
172
173
174static inline int
175tape_34xx_done(struct tape_request *request)
176{
177 DBF_EVENT(6, "%s done\n", tape_op_verbose[request->op]);
178
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:
189 ;
190 }
191 return TAPE_IO_SUCCESS;
192}
193
194static inline int
195tape_34xx_erp_failed(struct tape_request *request, int rc)
196{
197 DBF_EVENT(3, "Error recovery failed for %s (RC=%d)\n",
198 tape_op_verbose[request->op], rc);
199 return rc;
200}
201
202static inline int
203tape_34xx_erp_succeeded(struct tape_request *request)
204{
205 DBF_EVENT(3, "Error Recovery successful for %s\n",
206 tape_op_verbose[request->op]);
207 return tape_34xx_done(request);
208}
209
210static inline int
211tape_34xx_erp_retry(struct tape_request *request)
212{
213 DBF_EVENT(3, "xerp retr %s\n", tape_op_verbose[request->op]);
214 return TAPE_IO_RETRY;
215}
216
217
218
219
220
221static int
222tape_34xx_unsolicited_irq(struct tape_device *device, struct irb *irb)
223{
224 if (irb->scsw.cmd.dstat == 0x85) {
225
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);
232 }
233 return TAPE_IO_SUCCESS;
234}
235
236
237
238
239
240static int
241tape_34xx_erp_read_opposite(struct tape_device *device,
242 struct tape_request *request)
243{
244 if (request->op == TO_RFO) {
245
246
247
248
249
250 tape_std_read_backward(device, request);
251 return tape_34xx_erp_retry(request);
252 }
253
254
255
256
257
258 return tape_34xx_erp_failed(request, -EIO);
259}
260
261static int
262tape_34xx_erp_bug(struct tape_device *device, struct tape_request *request,
263 struct irb *irb, int no)
264{
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);
269 }
270 return tape_34xx_erp_failed(request, -EIO);
271}
272
273
274
275
276
277static int
278tape_34xx_erp_overrun(struct tape_device *device, struct tape_request *request,
279 struct irb *irb)
280{
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);
285 }
286 return tape_34xx_erp_bug(device, request, irb, -1);
287}
288
289
290
291
292static int
293tape_34xx_erp_sequence(struct tape_device *device,
294 struct tape_request *request, struct irb *irb)
295{
296 if (irb->ecw[3] == 0x41) {
297
298
299
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);
303 }
304
305
306
307
308 return tape_34xx_erp_bug(device, request, irb, -2);
309}
310
311
312
313
314
315
316static int
317tape_34xx_unit_check(struct tape_device *device, struct tape_request *request,
318 struct irb *irb)
319{
320 int inhibit_cu_recovery;
321 __u8* sense;
322
323 inhibit_cu_recovery = (*device->modeset_byte & 0x80) ? 1 : 0;
324 sense = irb->ecw;
325
326#ifdef CONFIG_S390_TAPE_BLOCK
327 if (request->op == TO_BLOCK) {
328
329
330
331
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);
337 }
338#endif
339
340 if (
341 sense[0] & SENSE_COMMAND_REJECT &&
342 sense[1] & SENSE_WRITE_PROTECT
343 ) {
344 if (
345 request->op == TO_DSE ||
346 request->op == TO_WRI ||
347 request->op == TO_WTM
348 ) {
349
350 return tape_34xx_erp_failed(request, -EACCES);
351 } else {
352 return tape_34xx_erp_bug(device, request, irb, -3);
353 }
354 }
355
356
357
358
359
360
361
362
363
364
365
366
367
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) {
377
378
379
380
381
382
383
384 case TO_FSF:
385 case TO_FSB:
386
387 return tape_34xx_erp_failed(request, -ENOSPC);
388 case TO_BSB:
389 return tape_34xx_erp_retry(request);
390
391
392
393
394
395
396 case TO_LBL:
397
398 tape_34xx_delete_sbid_from(device, 0);
399 return tape_34xx_erp_failed(request, -EIO);
400
401 case TO_RFO:
402
403 return tape_34xx_erp_failed(request, 0);
404
405
406
407
408
409
410 case TO_WRI:
411
412 return tape_34xx_erp_failed(request, -ENOSPC);
413 default:
414 return tape_34xx_erp_failed(request, 0);
415 }
416 }
417
418
419 if (sense[0] & SENSE_BUS_OUT_CHECK)
420 return tape_34xx_erp_retry(request);
421
422 if (sense[0] & SENSE_DATA_CHECK) {
423
424
425
426
427 switch (sense[3]) {
428 case 0x23:
429
430 if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
431 inhibit_cu_recovery)
432
433
434
435 return tape_34xx_erp_bug(device, request,
436 irb, -4);
437
438
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
444 if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
445 inhibit_cu_recovery)
446
447
448
449 return tape_34xx_erp_bug(device, request,
450 irb, -5);
451
452
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
458 return tape_34xx_erp_read_opposite(device, request);
459 case 0x28:
460
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
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
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
476
477
478 if (device->cdev->id.driver_info == tape_3480)
479 return tape_34xx_erp_bug(device, request,
480 irb, -6);
481 }
482 }
483
484 if (sense[0] & SENSE_OVERRUN)
485 return tape_34xx_erp_overrun(device, request, irb);
486
487 if (sense[1] & SENSE_RECORD_SEQUENCE_ERR)
488 return tape_34xx_erp_sequence(device, request, irb);
489
490
491 switch (sense[3]) {
492 case 0x00:
493
494 return TAPE_IO_SUCCESS;
495 case 0x21:
496
497
498
499
500 return tape_34xx_erp_retry(request);
501 case 0x22:
502
503
504
505
506
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:
511
512
513
514
515
516 return tape_34xx_erp_succeeded(request);
517 case 0x27:
518
519
520
521
522
523
524 return tape_34xx_erp_retry(request);
525 case 0x29:
526
527
528
529
530
531 return tape_34xx_erp_failed(request, -EIO);
532 case 0x2a:
533
534
535
536
537 return tape_34xx_erp_retry(request);
538 case 0x2b:
539
540
541
542
543 if (request->op == TO_RUN) {
544
545 tape_med_state_set(device, MS_UNLOADED);
546 return tape_34xx_erp_succeeded(request);
547 }
548
549 return tape_34xx_erp_bug(device, request, irb, sense[3]);
550 case 0x2c:
551
552
553
554
555 return tape_34xx_erp_failed(request, -EIO);
556 case 0x2d:
557
558 if (request->op == TO_DSE)
559 return tape_34xx_erp_failed(request, -EIO);
560
561 return tape_34xx_erp_bug(device, request, irb, sense[3]);
562 case 0x2e:
563
564
565
566
567
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
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
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:
582
583
584
585
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:
591
592
593
594
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:
601
602
603
604
605
606
607
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
614 return tape_34xx_erp_failed(request, -EIO);
615
616 return tape_34xx_erp_bug(device, request, irb, sense[3]);
617 case 0x37:
618
619
620
621
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:
626
627
628
629
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
637 return tape_34xx_erp_failed(request, -EIO);
638 case 0x3a:
639
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
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:
649
650
651
652
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
658 tape_34xx_delete_sbid_from(device, 0);
659 tape_med_state_set(device, MS_UNLOADED);
660
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;
671 }
672 }
673 return tape_34xx_erp_failed(request, -ENOMEDIUM);
674 case 0x44:
675
676 if (request->op != TO_BLOCK && request->op != TO_LBL)
677
678 return tape_34xx_erp_bug(device, request,
679 irb, sense[3]);
680 return tape_34xx_erp_failed(request, -EIO);
681 case 0x45:
682
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:
687
688
689
690
691
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
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
702 return tape_34xx_erp_retry(request);
703 case 0x49:
704
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
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:
714
715
716
717
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:
722
723
724
725
726 return tape_34xx_erp_retry(request);
727 case 0x4d:
728 if (device->cdev->id.driver_info == tape_3490)
729
730
731
732
733
734 return tape_34xx_erp_retry(request);
735
736 return tape_34xx_erp_bug(device, request, irb, sense[3]);
737 case 0x4e:
738 if (device->cdev->id.driver_info == tape_3490) {
739
740
741
742
743
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);
747 }
748
749 return tape_34xx_erp_bug(device, request, irb, sense[3]);
750 case 0x50:
751
752
753
754
755
756
757 return tape_34xx_erp_retry(request);
758 case 0x51:
759
760
761
762
763
764
765 return tape_34xx_erp_retry(request);
766 case 0x52:
767
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);
772 }
773 return tape_34xx_erp_bug(device, request, irb, sense[3]);
774 case 0x53:
775
776 return tape_34xx_erp_retry(request);
777 case 0x54:
778
779 return tape_34xx_erp_retry(request);
780 case 0x55:
781
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
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
793 return tape_34xx_erp_retry(request);
794 } else {
795
796 return tape_34xx_erp_retry(request);
797 }
798 case 0x5a:
799
800
801
802
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
808 if (sense[1] & SENSE_BEGINNING_OF_TAPE)
809
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
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
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
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);
829
830
831 case 0x23:
832 case 0x25:
833 case 0x26:
834 case 0x28:
835 case 0x31:
836 case 0x40:
837 case 0x41:
838
839 default:
840 return tape_34xx_erp_bug(device, request, irb, sense[3]);
841 }
842}
843
844
845
846
847static int
848tape_34xx_irq(struct tape_device *device, struct tape_request *request,
849 struct irb *irb)
850{
851 if (request == NULL)
852 return tape_34xx_unsolicited_irq(device, irb);
853
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
858 return tape_34xx_erp_failed(request, -ENOSPC);
859 }
860
861 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
862 return tape_34xx_unit_check(device, request, irb);
863
864 if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
865
866
867
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");
873 }
874 return tape_34xx_done(request);
875 }
876
877 DBF_EVENT(6, "xunknownirq\n");
878 tape_dump_sense_dbf(device, request, irb);
879 return TAPE_IO_STOP;
880}
881
882
883
884
885static int
886tape_34xx_ioctl(struct tape_device *device, unsigned int cmd, unsigned long arg)
887{
888 if (cmd == TAPE390_DISPLAY) {
889 struct display_struct disp;
890
891 if (copy_from_user(&disp, (char __user *) arg, sizeof(disp)) != 0)
892 return -EFAULT;
893
894 return tape_std_display(device, &disp);
895 } else
896 return -EINVAL;
897}
898
899static inline void
900tape_34xx_append_new_sbid(struct tape_34xx_block_id bid, struct list_head *l)
901{
902 struct tape_34xx_sbid * new_sbid;
903
904 new_sbid = kmalloc(sizeof(*new_sbid), GFP_ATOMIC);
905 if (!new_sbid)
906 return;
907
908 new_sbid->bid = bid;
909 list_add(&new_sbid->list, l);
910}
911
912
913
914
915
916
917static void
918tape_34xx_add_sbid(struct tape_device *device, struct tape_34xx_block_id bid)
919{
920 struct list_head * sbid_list;
921 struct tape_34xx_sbid * sbid;
922 struct list_head * l;
923
924
925
926
927
928
929 sbid_list = (struct list_head *) device->discdata;
930 if (!sbid_list || (bid.segment < 2 && bid.wrap == 0))
931 return;
932
933
934
935
936
937
938
939
940
941 list_for_each(l, sbid_list) {
942 sbid = list_entry(l, struct tape_34xx_sbid, list);
943
944 if (
945 (sbid->bid.segment == bid.segment) &&
946 (sbid->bid.wrap == bid.wrap)
947 ) {
948 if (bid.block < sbid->bid.block)
949 sbid->bid = bid;
950 else return;
951 break;
952 }
953
954
955 if (bid.block < sbid->bid.block) {
956 tape_34xx_append_new_sbid(bid, l->prev);
957 break;
958 }
959 }
960
961 if (l == sbid_list)
962 tape_34xx_append_new_sbid(bid, l->prev);
963
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
971 );
972 }
973}
974
975
976
977
978
979static void
980tape_34xx_delete_sbid_from(struct tape_device *device, int from)
981{
982 struct list_head * sbid_list;
983 struct tape_34xx_sbid * sbid;
984 struct list_head * l;
985 struct list_head * n;
986
987 sbid_list = (struct list_head *) device->discdata;
988 if (!sbid_list)
989 return;
990
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
998 );
999 list_del(l);
1000 kfree(sbid);
1001 }
1002 }
1003}
1004
1005
1006
1007
1008static void
1009tape_34xx_merge_sbid(
1010 struct tape_device * device,
1011 struct tape_34xx_block_id * bid
1012) {
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;
1017
1018 sbid_list = (struct list_head *) device->discdata;
1019 bid->wrap = 0;
1020 bid->segment = 1;
1021
1022 if (!sbid_list || list_empty(sbid_list))
1023 return;
1024
1025 sbid_to_use = NULL;
1026 list_for_each(l, sbid_list) {
1027 sbid = list_entry(l, struct tape_34xx_sbid, list);
1028
1029 if (sbid->bid.block >= bid->block)
1030 break;
1031 sbid_to_use = sbid;
1032 }
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
1041 );
1042 }
1043}
1044
1045static int
1046tape_34xx_setup_device(struct tape_device * device)
1047{
1048 int rc;
1049 struct list_head * discdata;
1050
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);
1055 }
1056 }
1057 discdata = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1058 if (discdata) {
1059 INIT_LIST_HEAD(discdata);
1060 device->discdata = discdata;
1061 }
1062
1063 return rc;
1064}
1065
1066static void
1067tape_34xx_cleanup_device(struct tape_device *device)
1068{
1069 tape_std_unassign(device);
1070
1071 if (device->discdata) {
1072 tape_34xx_delete_sbid_from(device, 0);
1073 kfree(device->discdata);
1074 device->discdata = NULL;
1075 }
1076}
1077
1078
1079
1080
1081
1082static int
1083tape_34xx_mttell(struct tape_device *device, int mt_count)
1084{
1085 struct {
1086 struct tape_34xx_block_id cbid;
1087 struct tape_34xx_block_id dbid;
1088 } __attribute__ ((packed)) block_id;
1089 int rc;
1090
1091 rc = tape_std_read_block_id(device, (__u64 *) &block_id);
1092 if (rc)
1093 return rc;
1094
1095 tape_34xx_add_sbid(device, block_id.cbid);
1096 return block_id.cbid.block;
1097}
1098
1099
1100
1101
1102static int
1103tape_34xx_mtseek(struct tape_device *device, int mt_count)
1104{
1105 struct tape_request *request;
1106 struct tape_34xx_block_id * bid;
1107
1108 if (mt_count > 0x3fffff) {
1109 DBF_EXCEPTION(6, "xsee parm\n");
1110 return -EINVAL;
1111 }
1112 request = tape_alloc_request(3, 4);
1113 if (IS_ERR(request))
1114 return PTR_ERR(request);
1115
1116
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);
1123
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);
1127
1128
1129 return tape_do_io_free(device, request);
1130}
1131
1132#ifdef CONFIG_S390_TAPE_BLOCK
1133
1134
1135
1136static struct tape_request *
1137tape_34xx_bread(struct tape_device *device, struct request *req)
1138{
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;
1147
1148 DBF_EVENT(6, "xBREDid:");
1149
1150
1151 rq_for_each_segment(bv, req, iter)
1152 count += bv->bv_len >> (TAPEBLOCK_HSEC_S2B + 9);
1153
1154
1155 request = tape_alloc_request(3+count+1, 8);
1156 if (IS_ERR(request))
1157 return request;
1158
1159
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);
1164
1165 ccw = request->cpaddr;
1166 ccw = tape_ccw_cc(ccw, MODE_SET_DB, 1, device->modeset_byte);
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176 ccw = tape_ccw_cc(ccw, NOP, 0, NULL);
1177 ccw = tape_ccw_cc(ccw, NOP, 0, NULL);
1178
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;
1188 }
1189 }
1190
1191 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
1192 DBF_EVENT(6, "xBREDccwg\n");
1193 return request;
1194}
1195
1196static void
1197tape_34xx_free_bread (struct tape_request *request)
1198{
1199 struct ccw1* ccw;
1200
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;
1207
1208 rbi_data = request->cpdata;
1209
1210 if (request->device)
1211 tape_34xx_add_sbid(request->device, rbi_data->cbid);
1212 }
1213
1214
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);
1219}
1220
1221
1222
1223
1224
1225
1226
1227static void
1228tape_34xx_check_locate(struct tape_device *device, struct tape_request *request)
1229{
1230 struct tape_34xx_block_id * start_block;
1231
1232 start_block = (struct tape_34xx_block_id *) request->cpdata;
1233 if (start_block->block == device->blk_data.block_position)
1234 return;
1235
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);
1246}
1247#endif
1248
1249
1250
1251
1252static 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
1285};
1286
1287
1288
1289
1290static 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
1305};
1306
1307static 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 { },
1311};
1312
1313static int
1314tape_34xx_online(struct ccw_device *cdev)
1315{
1316 return tape_generic_online(
1317 dev_get_drvdata(&cdev->dev),
1318 &tape_discipline_34xx
1319 );
1320}
1321
1322static struct ccw_driver tape_34xx_driver = {
1323 .name = "tape_34xx",
1324 .owner = THIS_MODULE,
1325 .ids = tape_34xx_ids,
1326 .probe = tape_generic_probe,
1327 .remove = tape_generic_remove,
1328 .set_online = tape_34xx_online,
1329 .set_offline = tape_generic_offline,
1330 .freeze = tape_generic_pm_suspend,
1331};
1332
1333static int
1334tape_34xx_init (void)
1335{
1336 int rc;
1337
1338 TAPE_DBF_AREA = debug_register ( "tape_34xx", 2, 2, 4*sizeof(long));
1339 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1340#ifdef DBF_LIKE_HELL
1341 debug_set_level(TAPE_DBF_AREA, 6);
1342#endif
1343
1344 DBF_EVENT(3, "34xx init\n");
1345
1346 rc = ccw_driver_register(&tape_34xx_driver);
1347 if (rc)
1348 DBF_EVENT(3, "34xx init failed\n");
1349 else
1350 DBF_EVENT(3, "34xx registered\n");
1351 return rc;
1352}
1353
1354static void
1355tape_34xx_exit(void)
1356{
1357 ccw_driver_unregister(&tape_34xx_driver);
1358
1359 debug_unregister(TAPE_DBF_AREA);
1360}
1361
1362MODULE_DEVICE_TABLE(ccw, tape_34xx_ids);
1363MODULE_AUTHOR("(C) 2001-2002 IBM Deutschland Entwicklung GmbH");
1364MODULE_DESCRIPTION("Linux on zSeries channel attached 3480 tape device driver");
1365MODULE_LICENSE("GPL");
1366
1367module_init(tape_34xx_init);
1368module_exit(tape_34xx_exit);
1369