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