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