1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * tape device discipline for 3480/3490 tapes.
4 *
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 */
10
11 #define KMSG_COMPONENT "tape_34xx"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
14 #include <linux/export.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/bio.h>
18 #include <linux/workqueue.h>
19 #include <linux/slab.h>
20
21 #define TAPE_DBF_AREA tape_34xx_dbf
22
23 #include "tape.h"
24 #include "tape_std.h"
25
26 /*
27 * Pointer to debug area.
28 */
29 debug_info_t *TAPE_DBF_AREA = NULL;
30 EXPORT_SYMBOL(TAPE_DBF_AREA);
31
32 #define TAPE34XX_FMT_3480 0
33 #define TAPE34XX_FMT_3480_2_XF 1
34 #define TAPE34XX_FMT_3480_XF 2
35
36 struct tape_34xx_block_id {
37 unsigned int wrap : 1;
38 unsigned int segment : 7;
39 unsigned int format : 2;
40 unsigned int block : 22;
41 };
42
43 /*
44 * A list of block ID's is used to faster seek blocks.
45 */
46 struct tape_34xx_sbid {
47 struct list_head list;
48 struct tape_34xx_block_id bid;
49 };
50
51 static void tape_34xx_delete_sbid_from(struct tape_device *, int);
52
53 /*
54 * Medium sense for 34xx tapes. There is no 'real' medium sense call.
55 * So we just do a normal sense.
56 */
__tape_34xx_medium_sense(struct tape_request * request)57 static void __tape_34xx_medium_sense(struct tape_request *request)
58 {
59 struct tape_device *device = request->device;
60 unsigned char *sense;
61
62 if (request->rc == 0) {
63 sense = request->cpdata;
64
65 /*
66 * This isn't quite correct. But since INTERVENTION_REQUIRED
67 * means that the drive is 'neither ready nor on-line' it is
68 * only slightly inaccurate to say there is no tape loaded if
69 * the drive isn't online...
70 */
71 if (sense[0] & SENSE_INTERVENTION_REQUIRED)
72 tape_med_state_set(device, MS_UNLOADED);
73 else
74 tape_med_state_set(device, MS_LOADED);
75
76 if (sense[1] & SENSE_WRITE_PROTECT)
77 device->tape_generic_status |= GMT_WR_PROT(~0);
78 else
79 device->tape_generic_status &= ~GMT_WR_PROT(~0);
80 } else
81 DBF_EVENT(4, "tape_34xx: medium sense failed with rc=%d\n",
82 request->rc);
83 tape_free_request(request);
84 }
85
tape_34xx_medium_sense(struct tape_device * device)86 static int tape_34xx_medium_sense(struct tape_device *device)
87 {
88 struct tape_request *request;
89 int rc;
90
91 request = tape_alloc_request(1, 32);
92 if (IS_ERR(request)) {
93 DBF_EXCEPTION(6, "MSEN fail\n");
94 return PTR_ERR(request);
95 }
96
97 request->op = TO_MSEN;
98 tape_ccw_end(request->cpaddr, SENSE, 32, request->cpdata);
99 rc = tape_do_io_interruptible(device, request);
100 __tape_34xx_medium_sense(request);
101 return rc;
102 }
103
tape_34xx_medium_sense_async(struct tape_device * device)104 static void tape_34xx_medium_sense_async(struct tape_device *device)
105 {
106 struct tape_request *request;
107
108 request = tape_alloc_request(1, 32);
109 if (IS_ERR(request)) {
110 DBF_EXCEPTION(6, "MSEN fail\n");
111 return;
112 }
113
114 request->op = TO_MSEN;
115 tape_ccw_end(request->cpaddr, SENSE, 32, request->cpdata);
116 request->callback = (void *) __tape_34xx_medium_sense;
117 request->callback_data = NULL;
118 tape_do_io_async(device, request);
119 }
120
121 struct tape_34xx_work {
122 struct tape_device *device;
123 enum tape_op op;
124 struct work_struct work;
125 };
126
127 /*
128 * These functions are currently used only to schedule a medium_sense for
129 * later execution. This is because we get an interrupt whenever a medium
130 * is inserted but cannot call tape_do_io* from an interrupt context.
131 * Maybe that's useful for other actions we want to start from the
132 * interrupt handler.
133 * Note: the work handler is called by the system work queue. The tape
134 * commands started by the handler need to be asynchrounous, otherwise
135 * a deadlock can occur e.g. in case of a deferred cc=1 (see __tape_do_irq).
136 */
137 static void
tape_34xx_work_handler(struct work_struct * work)138 tape_34xx_work_handler(struct work_struct *work)
139 {
140 struct tape_34xx_work *p =
141 container_of(work, struct tape_34xx_work, work);
142 struct tape_device *device = p->device;
143
144 switch(p->op) {
145 case TO_MSEN:
146 tape_34xx_medium_sense_async(device);
147 break;
148 default:
149 DBF_EVENT(3, "T34XX: internal error: unknown work\n");
150 }
151 tape_put_device(device);
152 kfree(p);
153 }
154
155 static int
tape_34xx_schedule_work(struct tape_device * device,enum tape_op op)156 tape_34xx_schedule_work(struct tape_device *device, enum tape_op op)
157 {
158 struct tape_34xx_work *p;
159
160 if ((p = kzalloc(sizeof(*p), GFP_ATOMIC)) == NULL)
161 return -ENOMEM;
162
163 INIT_WORK(&p->work, tape_34xx_work_handler);
164
165 p->device = tape_get_device(device);
166 p->op = op;
167
168 schedule_work(&p->work);
169 return 0;
170 }
171
172 /*
173 * Done Handler is called when dev stat = DEVICE-END (successful operation)
174 */
175 static inline int
tape_34xx_done(struct tape_request * request)176 tape_34xx_done(struct tape_request *request)
177 {
178 DBF_EVENT(6, "%s done\n", tape_op_verbose[request->op]);
179
180 switch (request->op) {
181 case TO_DSE:
182 case TO_RUN:
183 case TO_WRI:
184 case TO_WTM:
185 case TO_ASSIGN:
186 case TO_UNASSIGN:
187 tape_34xx_delete_sbid_from(request->device, 0);
188 break;
189 default:
190 ;
191 }
192 return TAPE_IO_SUCCESS;
193 }
194
195 static inline int
tape_34xx_erp_failed(struct tape_request * request,int rc)196 tape_34xx_erp_failed(struct tape_request *request, int rc)
197 {
198 DBF_EVENT(3, "Error recovery failed for %s (RC=%d)\n",
199 tape_op_verbose[request->op], rc);
200 return rc;
201 }
202
203 static inline int
tape_34xx_erp_succeeded(struct tape_request * request)204 tape_34xx_erp_succeeded(struct tape_request *request)
205 {
206 DBF_EVENT(3, "Error Recovery successful for %s\n",
207 tape_op_verbose[request->op]);
208 return tape_34xx_done(request);
209 }
210
211 static inline int
tape_34xx_erp_retry(struct tape_request * request)212 tape_34xx_erp_retry(struct tape_request *request)
213 {
214 DBF_EVENT(3, "xerp retr %s\n", tape_op_verbose[request->op]);
215 return TAPE_IO_RETRY;
216 }
217
218 /*
219 * This function is called, when no request is outstanding and we get an
220 * interrupt
221 */
222 static int
tape_34xx_unsolicited_irq(struct tape_device * device,struct irb * irb)223 tape_34xx_unsolicited_irq(struct tape_device *device, struct irb *irb)
224 {
225 if (irb->scsw.cmd.dstat == 0x85) { /* READY */
226 /* A medium was inserted in the drive. */
227 DBF_EVENT(6, "xuud med\n");
228 tape_34xx_delete_sbid_from(device, 0);
229 tape_34xx_schedule_work(device, TO_MSEN);
230 } else {
231 DBF_EVENT(3, "unsol.irq! dev end: %08x\n", device->cdev_id);
232 tape_dump_sense_dbf(device, NULL, irb);
233 }
234 return TAPE_IO_SUCCESS;
235 }
236
237 /*
238 * Read Opposite Error Recovery Function:
239 * Used, when Read Forward does not work
240 */
241 static int
tape_34xx_erp_read_opposite(struct tape_device * device,struct tape_request * request)242 tape_34xx_erp_read_opposite(struct tape_device *device,
243 struct tape_request *request)
244 {
245 if (request->op == TO_RFO) {
246 /*
247 * We did read forward, but the data could not be read
248 * *correctly*. We transform the request to a read backward
249 * and try again.
250 */
251 tape_std_read_backward(device, request);
252 return tape_34xx_erp_retry(request);
253 }
254
255 /*
256 * We tried to read forward and backward, but hat no
257 * success -> failed.
258 */
259 return tape_34xx_erp_failed(request, -EIO);
260 }
261
262 static int
tape_34xx_erp_bug(struct tape_device * device,struct tape_request * request,struct irb * irb,int no)263 tape_34xx_erp_bug(struct tape_device *device, struct tape_request *request,
264 struct irb *irb, int no)
265 {
266 if (request->op != TO_ASSIGN) {
267 dev_err(&device->cdev->dev, "An unexpected condition %d "
268 "occurred in tape error recovery\n", no);
269 tape_dump_sense_dbf(device, request, irb);
270 }
271 return tape_34xx_erp_failed(request, -EIO);
272 }
273
274 /*
275 * Handle data overrun between cu and drive. The channel speed might
276 * be too slow.
277 */
278 static int
tape_34xx_erp_overrun(struct tape_device * device,struct tape_request * request,struct irb * irb)279 tape_34xx_erp_overrun(struct tape_device *device, struct tape_request *request,
280 struct irb *irb)
281 {
282 if (irb->ecw[3] == 0x40) {
283 dev_warn (&device->cdev->dev, "A data overrun occurred between"
284 " the control unit and tape unit\n");
285 return tape_34xx_erp_failed(request, -EIO);
286 }
287 return tape_34xx_erp_bug(device, request, irb, -1);
288 }
289
290 /*
291 * Handle record sequence error.
292 */
293 static int
tape_34xx_erp_sequence(struct tape_device * device,struct tape_request * request,struct irb * irb)294 tape_34xx_erp_sequence(struct tape_device *device,
295 struct tape_request *request, struct irb *irb)
296 {
297 if (irb->ecw[3] == 0x41) {
298 /*
299 * cu detected incorrect block-id sequence on tape.
300 */
301 dev_warn (&device->cdev->dev, "The block ID sequence on the "
302 "tape is incorrect\n");
303 return tape_34xx_erp_failed(request, -EIO);
304 }
305 /*
306 * Record sequence error bit is set, but erpa does not
307 * show record sequence error.
308 */
309 return tape_34xx_erp_bug(device, request, irb, -2);
310 }
311
312 /*
313 * This function analyses the tape's sense-data in case of a unit-check.
314 * If possible, it tries to recover from the error. Else the user is
315 * informed about the problem.
316 */
317 static int
tape_34xx_unit_check(struct tape_device * device,struct tape_request * request,struct irb * irb)318 tape_34xx_unit_check(struct tape_device *device, struct tape_request *request,
319 struct irb *irb)
320 {
321 int inhibit_cu_recovery;
322 __u8* sense;
323
324 inhibit_cu_recovery = (*device->modeset_byte & 0x80) ? 1 : 0;
325 sense = irb->ecw;
326
327 if (
328 sense[0] & SENSE_COMMAND_REJECT &&
329 sense[1] & SENSE_WRITE_PROTECT
330 ) {
331 if (
332 request->op == TO_DSE ||
333 request->op == TO_WRI ||
334 request->op == TO_WTM
335 ) {
336 /* medium is write protected */
337 return tape_34xx_erp_failed(request, -EACCES);
338 } else {
339 return tape_34xx_erp_bug(device, request, irb, -3);
340 }
341 }
342
343 /*
344 * Special cases for various tape-states when reaching
345 * end of recorded area
346 *
347 * FIXME: Maybe a special case of the special case:
348 * sense[0] == SENSE_EQUIPMENT_CHECK &&
349 * sense[1] == SENSE_DRIVE_ONLINE &&
350 * sense[3] == 0x47 (Volume Fenced)
351 *
352 * This was caused by continued FSF or FSR after an
353 * 'End Of Data'.
354 */
355 if ((
356 sense[0] == SENSE_DATA_CHECK ||
357 sense[0] == SENSE_EQUIPMENT_CHECK ||
358 sense[0] == (SENSE_EQUIPMENT_CHECK | SENSE_DEFERRED_UNIT_CHECK)
359 ) && (
360 sense[1] == SENSE_DRIVE_ONLINE ||
361 sense[1] == (SENSE_BEGINNING_OF_TAPE | SENSE_WRITE_MODE)
362 )) {
363 switch (request->op) {
364 /*
365 * sense[0] == SENSE_DATA_CHECK &&
366 * sense[1] == SENSE_DRIVE_ONLINE
367 * sense[3] == 0x36 (End Of Data)
368 *
369 * Further seeks might return a 'Volume Fenced'.
370 */
371 case TO_FSF:
372 case TO_FSB:
373 /* Trying to seek beyond end of recorded area */
374 return tape_34xx_erp_failed(request, -ENOSPC);
375 case TO_BSB:
376 return tape_34xx_erp_retry(request);
377
378 /*
379 * sense[0] == SENSE_DATA_CHECK &&
380 * sense[1] == SENSE_DRIVE_ONLINE &&
381 * sense[3] == 0x36 (End Of Data)
382 */
383 case TO_LBL:
384 /* Block could not be located. */
385 tape_34xx_delete_sbid_from(device, 0);
386 return tape_34xx_erp_failed(request, -EIO);
387
388 case TO_RFO:
389 /* Read beyond end of recorded area -> 0 bytes read */
390 return tape_34xx_erp_failed(request, 0);
391
392 /*
393 * sense[0] == SENSE_EQUIPMENT_CHECK &&
394 * sense[1] == SENSE_DRIVE_ONLINE &&
395 * sense[3] == 0x38 (Physical End Of Volume)
396 */
397 case TO_WRI:
398 /* Writing at physical end of volume */
399 return tape_34xx_erp_failed(request, -ENOSPC);
400 default:
401 return tape_34xx_erp_failed(request, 0);
402 }
403 }
404
405 /* Sensing special bits */
406 if (sense[0] & SENSE_BUS_OUT_CHECK)
407 return tape_34xx_erp_retry(request);
408
409 if (sense[0] & SENSE_DATA_CHECK) {
410 /*
411 * hardware failure, damaged tape or improper
412 * operating conditions
413 */
414 switch (sense[3]) {
415 case 0x23:
416 /* a read data check occurred */
417 if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
418 inhibit_cu_recovery)
419 // data check is not permanent, may be
420 // recovered. We always use async-mode with
421 // cu-recovery, so this should *never* happen.
422 return tape_34xx_erp_bug(device, request,
423 irb, -4);
424
425 /* data check is permanent, CU recovery has failed */
426 dev_warn (&device->cdev->dev, "A read error occurred "
427 "that cannot be recovered\n");
428 return tape_34xx_erp_failed(request, -EIO);
429 case 0x25:
430 // a write data check occurred
431 if ((sense[2] & SENSE_TAPE_SYNC_MODE) ||
432 inhibit_cu_recovery)
433 // data check is not permanent, may be
434 // recovered. We always use async-mode with
435 // cu-recovery, so this should *never* happen.
436 return tape_34xx_erp_bug(device, request,
437 irb, -5);
438
439 // data check is permanent, cu-recovery has failed
440 dev_warn (&device->cdev->dev, "A write error on the "
441 "tape cannot be recovered\n");
442 return tape_34xx_erp_failed(request, -EIO);
443 case 0x26:
444 /* Data Check (read opposite) occurred. */
445 return tape_34xx_erp_read_opposite(device, request);
446 case 0x28:
447 /* ID-Mark at tape start couldn't be written */
448 dev_warn (&device->cdev->dev, "Writing the ID-mark "
449 "failed\n");
450 return tape_34xx_erp_failed(request, -EIO);
451 case 0x31:
452 /* Tape void. Tried to read beyond end of device. */
453 dev_warn (&device->cdev->dev, "Reading the tape beyond"
454 " the end of the recorded area failed\n");
455 return tape_34xx_erp_failed(request, -ENOSPC);
456 case 0x41:
457 /* Record sequence error. */
458 dev_warn (&device->cdev->dev, "The tape contains an "
459 "incorrect block ID sequence\n");
460 return tape_34xx_erp_failed(request, -EIO);
461 default:
462 /* all data checks for 3480 should result in one of
463 * the above erpa-codes. For 3490, other data-check
464 * conditions do exist. */
465 if (device->cdev->id.driver_info == tape_3480)
466 return tape_34xx_erp_bug(device, request,
467 irb, -6);
468 }
469 }
470
471 if (sense[0] & SENSE_OVERRUN)
472 return tape_34xx_erp_overrun(device, request, irb);
473
474 if (sense[1] & SENSE_RECORD_SEQUENCE_ERR)
475 return tape_34xx_erp_sequence(device, request, irb);
476
477 /* Sensing erpa codes */
478 switch (sense[3]) {
479 case 0x00:
480 /* Unit check with erpa code 0. Report and ignore. */
481 return TAPE_IO_SUCCESS;
482 case 0x21:
483 /*
484 * Data streaming not operational. CU will switch to
485 * interlock mode. Reissue the command.
486 */
487 return tape_34xx_erp_retry(request);
488 case 0x22:
489 /*
490 * Path equipment check. Might be drive adapter error, buffer
491 * error on the lower interface, internal path not usable,
492 * or error during cartridge load.
493 */
494 dev_warn (&device->cdev->dev, "A path equipment check occurred"
495 " for the tape device\n");
496 return tape_34xx_erp_failed(request, -EIO);
497 case 0x24:
498 /*
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".
502 */
503 return tape_34xx_erp_succeeded(request);
504 case 0x27:
505 /*
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.
510 */
511 return tape_34xx_erp_retry(request);
512 case 0x29:
513 /*
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.
517 */
518 return tape_34xx_erp_failed(request, -EIO);
519 case 0x2a:
520 /*
521 * Unsolicited environmental data. An internal counter
522 * overflows, we can ignore this and reissue the cmd.
523 */
524 return tape_34xx_erp_retry(request);
525 case 0x2b:
526 /*
527 * Environmental data present. Indicates either unload
528 * completed ok or read buffered log command completed ok.
529 */
530 if (request->op == TO_RUN) {
531 /* Rewind unload completed ok. */
532 tape_med_state_set(device, MS_UNLOADED);
533 return tape_34xx_erp_succeeded(request);
534 }
535 /* tape_34xx doesn't use read buffered log commands. */
536 return tape_34xx_erp_bug(device, request, irb, sense[3]);
537 case 0x2c:
538 /*
539 * Permanent equipment check. CU has tried recovery, but
540 * did not succeed.
541 */
542 return tape_34xx_erp_failed(request, -EIO);
543 case 0x2d:
544 /* Data security erase failure. */
545 if (request->op == TO_DSE)
546 return tape_34xx_erp_failed(request, -EIO);
547 /* Data security erase failure, but no such command issued. */
548 return tape_34xx_erp_bug(device, request, irb, sense[3]);
549 case 0x2e:
550 /*
551 * Not capable. This indicates either that the drive fails
552 * reading the format id mark or that format specified
553 * is not supported by the drive.
554 */
555 dev_warn (&device->cdev->dev, "The tape unit cannot process "
556 "the tape format\n");
557 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
558 case 0x30:
559 /* The medium is write protected. */
560 dev_warn (&device->cdev->dev, "The tape medium is write-"
561 "protected\n");
562 return tape_34xx_erp_failed(request, -EACCES);
563 case 0x32:
564 // Tension loss. We cannot recover this, it's an I/O error.
565 dev_warn (&device->cdev->dev, "The tape does not have the "
566 "required tape tension\n");
567 return tape_34xx_erp_failed(request, -EIO);
568 case 0x33:
569 /*
570 * Load Failure. The cartridge was not inserted correctly or
571 * the tape is not threaded correctly.
572 */
573 dev_warn (&device->cdev->dev, "The tape unit failed to load"
574 " the cartridge\n");
575 tape_34xx_delete_sbid_from(device, 0);
576 return tape_34xx_erp_failed(request, -EIO);
577 case 0x34:
578 /*
579 * Unload failure. The drive cannot maintain tape tension
580 * and control tape movement during an unload operation.
581 */
582 dev_warn (&device->cdev->dev, "Automatic unloading of the tape"
583 " cartridge failed\n");
584 if (request->op == TO_RUN)
585 return tape_34xx_erp_failed(request, -EIO);
586 return tape_34xx_erp_bug(device, request, irb, sense[3]);
587 case 0x35:
588 /*
589 * Drive equipment check. One of the following:
590 * - cu cannot recover from a drive detected error
591 * - a check code message is shown on drive display
592 * - the cartridge loader does not respond correctly
593 * - a failure occurs during an index, load, or unload cycle
594 */
595 dev_warn (&device->cdev->dev, "An equipment check has occurred"
596 " on the tape unit\n");
597 return tape_34xx_erp_failed(request, -EIO);
598 case 0x36:
599 if (device->cdev->id.driver_info == tape_3490)
600 /* End of data. */
601 return tape_34xx_erp_failed(request, -EIO);
602 /* This erpa is reserved for 3480 */
603 return tape_34xx_erp_bug(device, request, irb, sense[3]);
604 case 0x37:
605 /*
606 * Tape length error. The tape is shorter than reported in
607 * the beginning-of-tape data.
608 */
609 dev_warn (&device->cdev->dev, "The tape information states an"
610 " incorrect length\n");
611 return tape_34xx_erp_failed(request, -EIO);
612 case 0x38:
613 /*
614 * Physical end of tape. A read/write operation reached
615 * the physical end of tape.
616 */
617 if (request->op==TO_WRI ||
618 request->op==TO_DSE ||
619 request->op==TO_WTM)
620 return tape_34xx_erp_failed(request, -ENOSPC);
621 return tape_34xx_erp_failed(request, -EIO);
622 case 0x39:
623 /* Backward at Beginning of tape. */
624 return tape_34xx_erp_failed(request, -EIO);
625 case 0x3a:
626 /* Drive switched to not ready. */
627 dev_warn (&device->cdev->dev, "The tape unit is not ready\n");
628 return tape_34xx_erp_failed(request, -EIO);
629 case 0x3b:
630 /* Manual rewind or unload. This causes an I/O error. */
631 dev_warn (&device->cdev->dev, "The tape medium has been "
632 "rewound or unloaded manually\n");
633 tape_34xx_delete_sbid_from(device, 0);
634 return tape_34xx_erp_failed(request, -EIO);
635 case 0x42:
636 /*
637 * Degraded mode. A condition that can cause degraded
638 * performance is detected.
639 */
640 dev_warn (&device->cdev->dev, "The tape subsystem is running "
641 "in degraded mode\n");
642 return tape_34xx_erp_retry(request);
643 case 0x43:
644 /* Drive not ready. */
645 tape_34xx_delete_sbid_from(device, 0);
646 tape_med_state_set(device, MS_UNLOADED);
647 /* Some commands commands are successful even in this case */
648 if (sense[1] & SENSE_DRIVE_ONLINE) {
649 switch(request->op) {
650 case TO_ASSIGN:
651 case TO_UNASSIGN:
652 case TO_DIS:
653 case TO_NOP:
654 return tape_34xx_done(request);
655 break;
656 default:
657 break;
658 }
659 }
660 return tape_34xx_erp_failed(request, -ENOMEDIUM);
661 case 0x44:
662 /* Locate Block unsuccessful. */
663 if (request->op != TO_BLOCK && request->op != TO_LBL)
664 /* No locate block was issued. */
665 return tape_34xx_erp_bug(device, request,
666 irb, sense[3]);
667 return tape_34xx_erp_failed(request, -EIO);
668 case 0x45:
669 /* The drive is assigned to a different channel path. */
670 dev_warn (&device->cdev->dev, "The tape unit is already "
671 "assigned\n");
672 return tape_34xx_erp_failed(request, -EIO);
673 case 0x46:
674 /*
675 * Drive not on-line. Drive may be switched offline,
676 * the power supply may be switched off or
677 * the drive address may not be set correctly.
678 */
679 dev_warn (&device->cdev->dev, "The tape unit is not online\n");
680 return tape_34xx_erp_failed(request, -EIO);
681 case 0x47:
682 /* Volume fenced. CU reports volume integrity is lost. */
683 dev_warn (&device->cdev->dev, "The control unit has fenced "
684 "access to the tape volume\n");
685 tape_34xx_delete_sbid_from(device, 0);
686 return tape_34xx_erp_failed(request, -EIO);
687 case 0x48:
688 /* Log sense data and retry request. */
689 return tape_34xx_erp_retry(request);
690 case 0x49:
691 /* Bus out check. A parity check error on the bus was found. */
692 dev_warn (&device->cdev->dev, "A parity error occurred on the "
693 "tape bus\n");
694 return tape_34xx_erp_failed(request, -EIO);
695 case 0x4a:
696 /* Control unit erp failed. */
697 dev_warn (&device->cdev->dev, "I/O error recovery failed on "
698 "the tape control unit\n");
699 return tape_34xx_erp_failed(request, -EIO);
700 case 0x4b:
701 /*
702 * CU and drive incompatible. The drive requests micro-program
703 * patches, which are not available on the CU.
704 */
705 dev_warn (&device->cdev->dev, "The tape unit requires a "
706 "firmware update\n");
707 return tape_34xx_erp_failed(request, -EIO);
708 case 0x4c:
709 /*
710 * Recovered Check-One failure. Cu develops a hardware error,
711 * but is able to recover.
712 */
713 return tape_34xx_erp_retry(request);
714 case 0x4d:
715 if (device->cdev->id.driver_info == tape_3490)
716 /*
717 * Resetting event received. Since the driver does
718 * not support resetting event recovery (which has to
719 * be handled by the I/O Layer), retry our command.
720 */
721 return tape_34xx_erp_retry(request);
722 /* This erpa is reserved for 3480. */
723 return tape_34xx_erp_bug(device, request, irb, sense[3]);
724 case 0x4e:
725 if (device->cdev->id.driver_info == tape_3490) {
726 /*
727 * Maximum block size exceeded. This indicates, that
728 * the block to be written is larger than allowed for
729 * buffered mode.
730 */
731 dev_warn (&device->cdev->dev, "The maximum block size"
732 " for buffered mode is exceeded\n");
733 return tape_34xx_erp_failed(request, -ENOBUFS);
734 }
735 /* This erpa is reserved for 3480. */
736 return tape_34xx_erp_bug(device, request, irb, sense[3]);
737 case 0x50:
738 /*
739 * Read buffered log (Overflow). CU is running in extended
740 * buffered log mode, and a counter overflows. This should
741 * never happen, since we're never running in extended
742 * buffered log mode.
743 */
744 return tape_34xx_erp_retry(request);
745 case 0x51:
746 /*
747 * Read buffered log (EOV). EOF processing occurs while the
748 * CU is in extended buffered log mode. This should never
749 * happen, since we're never running in extended buffered
750 * log mode.
751 */
752 return tape_34xx_erp_retry(request);
753 case 0x52:
754 /* End of Volume complete. Rewind unload completed ok. */
755 if (request->op == TO_RUN) {
756 tape_med_state_set(device, MS_UNLOADED);
757 tape_34xx_delete_sbid_from(device, 0);
758 return tape_34xx_erp_succeeded(request);
759 }
760 return tape_34xx_erp_bug(device, request, irb, sense[3]);
761 case 0x53:
762 /* Global command intercept. */
763 return tape_34xx_erp_retry(request);
764 case 0x54:
765 /* Channel interface recovery (temporary). */
766 return tape_34xx_erp_retry(request);
767 case 0x55:
768 /* Channel interface recovery (permanent). */
769 dev_warn (&device->cdev->dev, "A channel interface error cannot be"
770 " recovered\n");
771 return tape_34xx_erp_failed(request, -EIO);
772 case 0x56:
773 /* Channel protocol error. */
774 dev_warn (&device->cdev->dev, "A channel protocol error "
775 "occurred\n");
776 return tape_34xx_erp_failed(request, -EIO);
777 case 0x57:
778 /*
779 * 3480: Attention intercept.
780 * 3490: Global status intercept.
781 */
782 return tape_34xx_erp_retry(request);
783 case 0x5a:
784 /*
785 * Tape length incompatible. The tape inserted is too long,
786 * which could cause damage to the tape or the drive.
787 */
788 dev_warn (&device->cdev->dev, "The tape unit does not support "
789 "the tape length\n");
790 return tape_34xx_erp_failed(request, -EIO);
791 case 0x5b:
792 /* Format 3480 XF incompatible */
793 if (sense[1] & SENSE_BEGINNING_OF_TAPE)
794 /* The tape will get overwritten. */
795 return tape_34xx_erp_retry(request);
796 dev_warn (&device->cdev->dev, "The tape unit does not support"
797 " format 3480 XF\n");
798 return tape_34xx_erp_failed(request, -EIO);
799 case 0x5c:
800 /* Format 3480-2 XF incompatible */
801 dev_warn (&device->cdev->dev, "The tape unit does not support tape "
802 "format 3480-2 XF\n");
803 return tape_34xx_erp_failed(request, -EIO);
804 case 0x5d:
805 /* Tape length violation. */
806 dev_warn (&device->cdev->dev, "The tape unit does not support"
807 " the current tape length\n");
808 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
809 case 0x5e:
810 /* Compaction algorithm incompatible. */
811 dev_warn (&device->cdev->dev, "The tape unit does not support"
812 " the compaction algorithm\n");
813 return tape_34xx_erp_failed(request, -EMEDIUMTYPE);
814
815 /* The following erpas should have been covered earlier. */
816 case 0x23: /* Read data check. */
817 case 0x25: /* Write data check. */
818 case 0x26: /* Data check (read opposite). */
819 case 0x28: /* Write id mark check. */
820 case 0x31: /* Tape void. */
821 case 0x40: /* Overrun error. */
822 case 0x41: /* Record sequence error. */
823 /* All other erpas are reserved for future use. */
824 default:
825 return tape_34xx_erp_bug(device, request, irb, sense[3]);
826 }
827 }
828
829 /*
830 * 3480/3490 interrupt handler
831 */
832 static int
tape_34xx_irq(struct tape_device * device,struct tape_request * request,struct irb * irb)833 tape_34xx_irq(struct tape_device *device, struct tape_request *request,
834 struct irb *irb)
835 {
836 if (request == NULL)
837 return tape_34xx_unsolicited_irq(device, irb);
838
839 if ((irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) &&
840 (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) &&
841 (request->op == TO_WRI)) {
842 /* Write at end of volume */
843 return tape_34xx_erp_failed(request, -ENOSPC);
844 }
845
846 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
847 return tape_34xx_unit_check(device, request, irb);
848
849 if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
850 /*
851 * A unit exception occurs on skipping over a tapemark block.
852 */
853 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) {
854 if (request->op == TO_BSB || request->op == TO_FSB)
855 request->rescnt++;
856 else
857 DBF_EVENT(5, "Unit Exception!\n");
858 }
859 return tape_34xx_done(request);
860 }
861
862 DBF_EVENT(6, "xunknownirq\n");
863 tape_dump_sense_dbf(device, request, irb);
864 return TAPE_IO_STOP;
865 }
866
867 /*
868 * ioctl_overload
869 */
870 static int
tape_34xx_ioctl(struct tape_device * device,unsigned int cmd,unsigned long arg)871 tape_34xx_ioctl(struct tape_device *device, unsigned int cmd, unsigned long arg)
872 {
873 if (cmd == TAPE390_DISPLAY) {
874 struct display_struct disp;
875
876 if (copy_from_user(&disp, (char __user *) arg, sizeof(disp)) != 0)
877 return -EFAULT;
878
879 return tape_std_display(device, &disp);
880 } else
881 return -EINVAL;
882 }
883
884 static inline void
tape_34xx_append_new_sbid(struct tape_34xx_block_id bid,struct list_head * l)885 tape_34xx_append_new_sbid(struct tape_34xx_block_id bid, struct list_head *l)
886 {
887 struct tape_34xx_sbid * new_sbid;
888
889 new_sbid = kmalloc(sizeof(*new_sbid), GFP_ATOMIC);
890 if (!new_sbid)
891 return;
892
893 new_sbid->bid = bid;
894 list_add(&new_sbid->list, l);
895 }
896
897 /*
898 * Build up the search block ID list. The block ID consists of a logical
899 * block number and a hardware specific part. The hardware specific part
900 * helps the tape drive to speed up searching for a specific block.
901 */
902 static void
tape_34xx_add_sbid(struct tape_device * device,struct tape_34xx_block_id bid)903 tape_34xx_add_sbid(struct tape_device *device, struct tape_34xx_block_id bid)
904 {
905 struct list_head * sbid_list;
906 struct tape_34xx_sbid * sbid;
907 struct list_head * l;
908
909 /*
910 * immediately return if there is no list at all or the block to add
911 * is located in segment 1 of wrap 0 because this position is used
912 * if no hardware position data is supplied.
913 */
914 sbid_list = (struct list_head *) device->discdata;
915 if (!sbid_list || (bid.segment < 2 && bid.wrap == 0))
916 return;
917
918 /*
919 * Search the position where to insert the new entry. Hardware
920 * acceleration uses only the segment and wrap number. So we
921 * need only one entry for a specific wrap/segment combination.
922 * If there is a block with a lower number but the same hard-
923 * ware position data we just update the block number in the
924 * existing entry.
925 */
926 list_for_each(l, sbid_list) {
927 sbid = list_entry(l, struct tape_34xx_sbid, list);
928
929 if (
930 (sbid->bid.segment == bid.segment) &&
931 (sbid->bid.wrap == bid.wrap)
932 ) {
933 if (bid.block < sbid->bid.block)
934 sbid->bid = bid;
935 else return;
936 break;
937 }
938
939 /* Sort in according to logical block number. */
940 if (bid.block < sbid->bid.block) {
941 tape_34xx_append_new_sbid(bid, l->prev);
942 break;
943 }
944 }
945 /* List empty or new block bigger than last entry. */
946 if (l == sbid_list)
947 tape_34xx_append_new_sbid(bid, l->prev);
948
949 DBF_LH(4, "Current list is:\n");
950 list_for_each(l, sbid_list) {
951 sbid = list_entry(l, struct tape_34xx_sbid, list);
952 DBF_LH(4, "%d:%03d@%05d\n",
953 sbid->bid.wrap,
954 sbid->bid.segment,
955 sbid->bid.block
956 );
957 }
958 }
959
960 /*
961 * Delete all entries from the search block ID list that belong to tape blocks
962 * equal or higher than the given number.
963 */
964 static void
tape_34xx_delete_sbid_from(struct tape_device * device,int from)965 tape_34xx_delete_sbid_from(struct tape_device *device, int from)
966 {
967 struct list_head * sbid_list;
968 struct tape_34xx_sbid * sbid;
969 struct list_head * l;
970 struct list_head * n;
971
972 sbid_list = (struct list_head *) device->discdata;
973 if (!sbid_list)
974 return;
975
976 list_for_each_safe(l, n, sbid_list) {
977 sbid = list_entry(l, struct tape_34xx_sbid, list);
978 if (sbid->bid.block >= from) {
979 DBF_LH(4, "Delete sbid %d:%03d@%05d\n",
980 sbid->bid.wrap,
981 sbid->bid.segment,
982 sbid->bid.block
983 );
984 list_del(l);
985 kfree(sbid);
986 }
987 }
988 }
989
990 /*
991 * Merge hardware position data into a block id.
992 */
993 static void
tape_34xx_merge_sbid(struct tape_device * device,struct tape_34xx_block_id * bid)994 tape_34xx_merge_sbid(
995 struct tape_device * device,
996 struct tape_34xx_block_id * bid
997 ) {
998 struct tape_34xx_sbid * sbid;
999 struct tape_34xx_sbid * sbid_to_use;
1000 struct list_head * sbid_list;
1001 struct list_head * l;
1002
1003 sbid_list = (struct list_head *) device->discdata;
1004 bid->wrap = 0;
1005 bid->segment = 1;
1006
1007 if (!sbid_list || list_empty(sbid_list))
1008 return;
1009
1010 sbid_to_use = NULL;
1011 list_for_each(l, sbid_list) {
1012 sbid = list_entry(l, struct tape_34xx_sbid, list);
1013
1014 if (sbid->bid.block >= bid->block)
1015 break;
1016 sbid_to_use = sbid;
1017 }
1018 if (sbid_to_use) {
1019 bid->wrap = sbid_to_use->bid.wrap;
1020 bid->segment = sbid_to_use->bid.segment;
1021 DBF_LH(4, "Use %d:%03d@%05d for %05d\n",
1022 sbid_to_use->bid.wrap,
1023 sbid_to_use->bid.segment,
1024 sbid_to_use->bid.block,
1025 bid->block
1026 );
1027 }
1028 }
1029
1030 static int
tape_34xx_setup_device(struct tape_device * device)1031 tape_34xx_setup_device(struct tape_device * device)
1032 {
1033 int rc;
1034 struct list_head * discdata;
1035
1036 DBF_EVENT(6, "34xx device setup\n");
1037 if ((rc = tape_std_assign(device)) == 0) {
1038 if ((rc = tape_34xx_medium_sense(device)) != 0) {
1039 DBF_LH(3, "34xx medium sense returned %d\n", rc);
1040 }
1041 }
1042 discdata = kmalloc(sizeof(struct list_head), GFP_KERNEL);
1043 if (discdata) {
1044 INIT_LIST_HEAD(discdata);
1045 device->discdata = discdata;
1046 }
1047
1048 return rc;
1049 }
1050
1051 static void
tape_34xx_cleanup_device(struct tape_device * device)1052 tape_34xx_cleanup_device(struct tape_device *device)
1053 {
1054 tape_std_unassign(device);
1055
1056 if (device->discdata) {
1057 tape_34xx_delete_sbid_from(device, 0);
1058 kfree(device->discdata);
1059 device->discdata = NULL;
1060 }
1061 }
1062
1063
1064 /*
1065 * MTTELL: Tell block. Return the number of block relative to current file.
1066 */
1067 static int
tape_34xx_mttell(struct tape_device * device,int mt_count)1068 tape_34xx_mttell(struct tape_device *device, int mt_count)
1069 {
1070 struct {
1071 struct tape_34xx_block_id cbid;
1072 struct tape_34xx_block_id dbid;
1073 } __attribute__ ((packed)) block_id;
1074 int rc;
1075
1076 rc = tape_std_read_block_id(device, (__u64 *) &block_id);
1077 if (rc)
1078 return rc;
1079
1080 tape_34xx_add_sbid(device, block_id.cbid);
1081 return block_id.cbid.block;
1082 }
1083
1084 /*
1085 * MTSEEK: seek to the specified block.
1086 */
1087 static int
tape_34xx_mtseek(struct tape_device * device,int mt_count)1088 tape_34xx_mtseek(struct tape_device *device, int mt_count)
1089 {
1090 struct tape_request *request;
1091 struct tape_34xx_block_id * bid;
1092
1093 if (mt_count > 0x3fffff) {
1094 DBF_EXCEPTION(6, "xsee parm\n");
1095 return -EINVAL;
1096 }
1097 request = tape_alloc_request(3, 4);
1098 if (IS_ERR(request))
1099 return PTR_ERR(request);
1100
1101 /* setup ccws */
1102 request->op = TO_LBL;
1103 bid = (struct tape_34xx_block_id *) request->cpdata;
1104 bid->format = (*device->modeset_byte & 0x08) ?
1105 TAPE34XX_FMT_3480_XF : TAPE34XX_FMT_3480;
1106 bid->block = mt_count;
1107 tape_34xx_merge_sbid(device, bid);
1108
1109 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
1110 tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
1111 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
1112
1113 /* execute it */
1114 return tape_do_io_free(device, request);
1115 }
1116
1117 /*
1118 * List of 3480/3490 magnetic tape commands.
1119 */
1120 static tape_mtop_fn tape_34xx_mtop[TAPE_NR_MTOPS] = {
1121 [MTRESET] = tape_std_mtreset,
1122 [MTFSF] = tape_std_mtfsf,
1123 [MTBSF] = tape_std_mtbsf,
1124 [MTFSR] = tape_std_mtfsr,
1125 [MTBSR] = tape_std_mtbsr,
1126 [MTWEOF] = tape_std_mtweof,
1127 [MTREW] = tape_std_mtrew,
1128 [MTOFFL] = tape_std_mtoffl,
1129 [MTNOP] = tape_std_mtnop,
1130 [MTRETEN] = tape_std_mtreten,
1131 [MTBSFM] = tape_std_mtbsfm,
1132 [MTFSFM] = tape_std_mtfsfm,
1133 [MTEOM] = tape_std_mteom,
1134 [MTERASE] = tape_std_mterase,
1135 [MTRAS1] = NULL,
1136 [MTRAS2] = NULL,
1137 [MTRAS3] = NULL,
1138 [MTSETBLK] = tape_std_mtsetblk,
1139 [MTSETDENSITY] = NULL,
1140 [MTSEEK] = tape_34xx_mtseek,
1141 [MTTELL] = tape_34xx_mttell,
1142 [MTSETDRVBUFFER] = NULL,
1143 [MTFSS] = NULL,
1144 [MTBSS] = NULL,
1145 [MTWSM] = NULL,
1146 [MTLOCK] = NULL,
1147 [MTUNLOCK] = NULL,
1148 [MTLOAD] = tape_std_mtload,
1149 [MTUNLOAD] = tape_std_mtunload,
1150 [MTCOMPRESSION] = tape_std_mtcompression,
1151 [MTSETPART] = NULL,
1152 [MTMKPART] = NULL
1153 };
1154
1155 /*
1156 * Tape discipline structure for 3480 and 3490.
1157 */
1158 static struct tape_discipline tape_discipline_34xx = {
1159 .owner = THIS_MODULE,
1160 .setup_device = tape_34xx_setup_device,
1161 .cleanup_device = tape_34xx_cleanup_device,
1162 .process_eov = tape_std_process_eov,
1163 .irq = tape_34xx_irq,
1164 .read_block = tape_std_read_block,
1165 .write_block = tape_std_write_block,
1166 .ioctl_fn = tape_34xx_ioctl,
1167 .mtop_array = tape_34xx_mtop
1168 };
1169
1170 static struct ccw_device_id tape_34xx_ids[] = {
1171 { CCW_DEVICE_DEVTYPE(0x3480, 0, 0x3480, 0), .driver_info = tape_3480},
1172 { CCW_DEVICE_DEVTYPE(0x3490, 0, 0x3490, 0), .driver_info = tape_3490},
1173 { /* end of list */ },
1174 };
1175
1176 static int
tape_34xx_online(struct ccw_device * cdev)1177 tape_34xx_online(struct ccw_device *cdev)
1178 {
1179 return tape_generic_online(
1180 dev_get_drvdata(&cdev->dev),
1181 &tape_discipline_34xx
1182 );
1183 }
1184
1185 static struct ccw_driver tape_34xx_driver = {
1186 .driver = {
1187 .name = "tape_34xx",
1188 .owner = THIS_MODULE,
1189 },
1190 .ids = tape_34xx_ids,
1191 .probe = tape_generic_probe,
1192 .remove = tape_generic_remove,
1193 .set_online = tape_34xx_online,
1194 .set_offline = tape_generic_offline,
1195 .int_class = IRQIO_TAP,
1196 };
1197
1198 static int
tape_34xx_init(void)1199 tape_34xx_init (void)
1200 {
1201 int rc;
1202
1203 TAPE_DBF_AREA = debug_register ( "tape_34xx", 2, 2, 4*sizeof(long));
1204 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1205 #ifdef DBF_LIKE_HELL
1206 debug_set_level(TAPE_DBF_AREA, 6);
1207 #endif
1208
1209 DBF_EVENT(3, "34xx init\n");
1210 /* Register driver for 3480/3490 tapes. */
1211 rc = ccw_driver_register(&tape_34xx_driver);
1212 if (rc)
1213 DBF_EVENT(3, "34xx init failed\n");
1214 else
1215 DBF_EVENT(3, "34xx registered\n");
1216 return rc;
1217 }
1218
1219 static void
tape_34xx_exit(void)1220 tape_34xx_exit(void)
1221 {
1222 ccw_driver_unregister(&tape_34xx_driver);
1223
1224 debug_unregister(TAPE_DBF_AREA);
1225 }
1226
1227 MODULE_DEVICE_TABLE(ccw, tape_34xx_ids);
1228 MODULE_AUTHOR("(C) 2001-2002 IBM Deutschland Entwicklung GmbH");
1229 MODULE_DESCRIPTION("Linux on zSeries channel attached 3480 tape device driver");
1230 MODULE_LICENSE("GPL");
1231
1232 module_init(tape_34xx_init);
1233 module_exit(tape_34xx_exit);
1234