xref: /openbmc/linux/drivers/s390/char/tape_3590.c (revision a09d2831)
1 /*
2  *  drivers/s390/char/tape_3590.c
3  *    tape device discipline for 3590 tapes.
4  *
5  *    Copyright IBM Corp. 2001, 2009
6  *    Author(s): Stefan Bader <shbader@de.ibm.com>
7  *		 Michael Holzheu <holzheu@de.ibm.com>
8  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>
9  */
10 
11 #define KMSG_COMPONENT "tape_3590"
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 <asm/ebcdic.h>
18 
19 #define TAPE_DBF_AREA	tape_3590_dbf
20 #define BUFSIZE 512	/* size of buffers for dynamic generated messages */
21 
22 #include "tape.h"
23 #include "tape_std.h"
24 #include "tape_3590.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 /*******************************************************************
33  * Error Recovery fuctions:
34  * - Read Opposite:		 implemented
35  * - Read Device (buffered) log: BRA
36  * - Read Library log:		 BRA
37  * - Swap Devices:		 BRA
38  * - Long Busy:			 implemented
39  * - Special Intercept:		 BRA
40  * - Read Alternate:		 implemented
41  *******************************************************************/
42 
43 static const char *tape_3590_msg[TAPE_3590_MAX_MSG] = {
44 	[0x00] = "",
45 	[0x10] = "Lost Sense",
46 	[0x11] = "Assigned Elsewhere",
47 	[0x12] = "Allegiance Reset",
48 	[0x13] = "Shared Access Violation",
49 	[0x20] = "Command Reject",
50 	[0x21] = "Configuration Error",
51 	[0x22] = "Protection Exception",
52 	[0x23] = "Write Protect",
53 	[0x24] = "Write Length",
54 	[0x25] = "Read-Only Format",
55 	[0x31] = "Beginning of Partition",
56 	[0x33] = "End of Partition",
57 	[0x34] = "End of Data",
58 	[0x35] = "Block not found",
59 	[0x40] = "Device Intervention",
60 	[0x41] = "Loader Intervention",
61 	[0x42] = "Library Intervention",
62 	[0x50] = "Write Error",
63 	[0x51] = "Erase Error",
64 	[0x52] = "Formatting Error",
65 	[0x53] = "Read Error",
66 	[0x54] = "Unsupported Format",
67 	[0x55] = "No Formatting",
68 	[0x56] = "Positioning lost",
69 	[0x57] = "Read Length",
70 	[0x60] = "Unsupported Medium",
71 	[0x61] = "Medium Length Error",
72 	[0x62] = "Medium removed",
73 	[0x64] = "Load Check",
74 	[0x65] = "Unload Check",
75 	[0x70] = "Equipment Check",
76 	[0x71] = "Bus out Check",
77 	[0x72] = "Protocol Error",
78 	[0x73] = "Interface Error",
79 	[0x74] = "Overrun",
80 	[0x75] = "Halt Signal",
81 	[0x90] = "Device fenced",
82 	[0x91] = "Device Path fenced",
83 	[0xa0] = "Volume misplaced",
84 	[0xa1] = "Volume inaccessible",
85 	[0xa2] = "Volume in input",
86 	[0xa3] = "Volume ejected",
87 	[0xa4] = "All categories reserved",
88 	[0xa5] = "Duplicate Volume",
89 	[0xa6] = "Library Manager Offline",
90 	[0xa7] = "Library Output Station full",
91 	[0xa8] = "Vision System non-operational",
92 	[0xa9] = "Library Manager Equipment Check",
93 	[0xaa] = "Library Equipment Check",
94 	[0xab] = "All Library Cells full",
95 	[0xac] = "No Cleaner Volumes in Library",
96 	[0xad] = "I/O Station door open",
97 	[0xae] = "Subsystem environmental alert",
98 };
99 
100 static int crypt_supported(struct tape_device *device)
101 {
102 	return TAPE390_CRYPT_SUPPORTED(TAPE_3590_CRYPT_INFO(device));
103 }
104 
105 static int crypt_enabled(struct tape_device *device)
106 {
107 	return TAPE390_CRYPT_ON(TAPE_3590_CRYPT_INFO(device));
108 }
109 
110 static void ext_to_int_kekl(struct tape390_kekl *in,
111 			    struct tape3592_kekl *out)
112 {
113 	int i;
114 
115 	memset(out, 0, sizeof(*out));
116 	if (in->type == TAPE390_KEKL_TYPE_HASH)
117 		out->flags |= 0x40;
118 	if (in->type_on_tape == TAPE390_KEKL_TYPE_HASH)
119 		out->flags |= 0x80;
120 	strncpy(out->label, in->label, 64);
121 	for (i = strlen(in->label); i < sizeof(out->label); i++)
122 		out->label[i] = ' ';
123 	ASCEBC(out->label, sizeof(out->label));
124 }
125 
126 static void int_to_ext_kekl(struct tape3592_kekl *in,
127 			    struct tape390_kekl *out)
128 {
129 	memset(out, 0, sizeof(*out));
130 	if(in->flags & 0x40)
131 		out->type = TAPE390_KEKL_TYPE_HASH;
132 	else
133 		out->type = TAPE390_KEKL_TYPE_LABEL;
134 	if(in->flags & 0x80)
135 		out->type_on_tape = TAPE390_KEKL_TYPE_HASH;
136 	else
137 		out->type_on_tape = TAPE390_KEKL_TYPE_LABEL;
138 	memcpy(out->label, in->label, sizeof(in->label));
139 	EBCASC(out->label, sizeof(in->label));
140 	strim(out->label);
141 }
142 
143 static void int_to_ext_kekl_pair(struct tape3592_kekl_pair *in,
144 				 struct tape390_kekl_pair *out)
145 {
146 	if (in->count == 0) {
147 		out->kekl[0].type = TAPE390_KEKL_TYPE_NONE;
148 		out->kekl[0].type_on_tape = TAPE390_KEKL_TYPE_NONE;
149 		out->kekl[1].type = TAPE390_KEKL_TYPE_NONE;
150 		out->kekl[1].type_on_tape = TAPE390_KEKL_TYPE_NONE;
151 	} else if (in->count == 1) {
152 		int_to_ext_kekl(&in->kekl[0], &out->kekl[0]);
153 		out->kekl[1].type = TAPE390_KEKL_TYPE_NONE;
154 		out->kekl[1].type_on_tape = TAPE390_KEKL_TYPE_NONE;
155 	} else if (in->count == 2) {
156 		int_to_ext_kekl(&in->kekl[0], &out->kekl[0]);
157 		int_to_ext_kekl(&in->kekl[1], &out->kekl[1]);
158 	} else {
159 		printk("Invalid KEKL number: %d\n", in->count);
160 		BUG();
161 	}
162 }
163 
164 static int check_ext_kekl(struct tape390_kekl *kekl)
165 {
166 	if (kekl->type == TAPE390_KEKL_TYPE_NONE)
167 		goto invalid;
168 	if (kekl->type > TAPE390_KEKL_TYPE_HASH)
169 		goto invalid;
170 	if (kekl->type_on_tape == TAPE390_KEKL_TYPE_NONE)
171 		goto invalid;
172 	if (kekl->type_on_tape > TAPE390_KEKL_TYPE_HASH)
173 		goto invalid;
174 	if ((kekl->type == TAPE390_KEKL_TYPE_HASH) &&
175 	    (kekl->type_on_tape == TAPE390_KEKL_TYPE_LABEL))
176 		goto invalid;
177 
178 	return 0;
179 invalid:
180 	return -EINVAL;
181 }
182 
183 static int check_ext_kekl_pair(struct tape390_kekl_pair *kekls)
184 {
185 	if (check_ext_kekl(&kekls->kekl[0]))
186 		goto invalid;
187 	if (check_ext_kekl(&kekls->kekl[1]))
188 		goto invalid;
189 
190 	return 0;
191 invalid:
192 	return -EINVAL;
193 }
194 
195 /*
196  * Query KEKLs
197  */
198 static int tape_3592_kekl_query(struct tape_device *device,
199 				struct tape390_kekl_pair *ext_kekls)
200 {
201 	struct tape_request *request;
202 	struct tape3592_kekl_query_order *order;
203 	struct tape3592_kekl_query_data *int_kekls;
204 	int rc;
205 
206 	DBF_EVENT(6, "tape3592_kekl_query\n");
207 	int_kekls = kmalloc(sizeof(*int_kekls), GFP_KERNEL|GFP_DMA);
208 	if (!int_kekls)
209 		return -ENOMEM;
210 	request = tape_alloc_request(2, sizeof(*order));
211 	if (IS_ERR(request)) {
212 		rc = PTR_ERR(request);
213 		goto fail_malloc;
214 	}
215 	order = request->cpdata;
216 	memset(order,0,sizeof(*order));
217 	order->code = 0xe2;
218 	order->max_count = 2;
219 	request->op = TO_KEKL_QUERY;
220 	tape_ccw_cc(request->cpaddr, PERF_SUBSYS_FUNC, sizeof(*order), order);
221 	tape_ccw_end(request->cpaddr + 1, READ_SS_DATA, sizeof(*int_kekls),
222 		     int_kekls);
223 	rc = tape_do_io(device, request);
224 	if (rc)
225 		goto fail_request;
226 	int_to_ext_kekl_pair(&int_kekls->kekls, ext_kekls);
227 
228 	rc = 0;
229 fail_request:
230 	tape_free_request(request);
231 fail_malloc:
232 	kfree(int_kekls);
233 	return rc;
234 }
235 
236 /*
237  * IOCTL: Query KEKLs
238  */
239 static int tape_3592_ioctl_kekl_query(struct tape_device *device,
240 				      unsigned long arg)
241 {
242 	int rc;
243 	struct tape390_kekl_pair *ext_kekls;
244 
245 	DBF_EVENT(6, "tape_3592_ioctl_kekl_query\n");
246 	if (!crypt_supported(device))
247 		return -ENOSYS;
248 	if (!crypt_enabled(device))
249 		return -EUNATCH;
250 	ext_kekls = kmalloc(sizeof(*ext_kekls), GFP_KERNEL);
251 	if (!ext_kekls)
252 		return -ENOMEM;
253 	rc = tape_3592_kekl_query(device, ext_kekls);
254 	if (rc != 0)
255 		goto fail;
256 	if (copy_to_user((char __user *) arg, ext_kekls, sizeof(*ext_kekls))) {
257 		rc = -EFAULT;
258 		goto fail;
259 	}
260 	rc = 0;
261 fail:
262 	kfree(ext_kekls);
263 	return rc;
264 }
265 
266 static int tape_3590_mttell(struct tape_device *device, int mt_count);
267 
268 /*
269  * Set KEKLs
270  */
271 static int tape_3592_kekl_set(struct tape_device *device,
272 			      struct tape390_kekl_pair *ext_kekls)
273 {
274 	struct tape_request *request;
275 	struct tape3592_kekl_set_order *order;
276 
277 	DBF_EVENT(6, "tape3592_kekl_set\n");
278 	if (check_ext_kekl_pair(ext_kekls)) {
279 		DBF_EVENT(6, "invalid kekls\n");
280 		return -EINVAL;
281 	}
282 	if (tape_3590_mttell(device, 0) != 0)
283 		return -EBADSLT;
284 	request = tape_alloc_request(1, sizeof(*order));
285 	if (IS_ERR(request))
286 		return PTR_ERR(request);
287 	order = request->cpdata;
288 	memset(order, 0, sizeof(*order));
289 	order->code = 0xe3;
290 	order->kekls.count = 2;
291 	ext_to_int_kekl(&ext_kekls->kekl[0], &order->kekls.kekl[0]);
292 	ext_to_int_kekl(&ext_kekls->kekl[1], &order->kekls.kekl[1]);
293 	request->op = TO_KEKL_SET;
294 	tape_ccw_end(request->cpaddr, PERF_SUBSYS_FUNC, sizeof(*order), order);
295 
296 	return tape_do_io_free(device, request);
297 }
298 
299 /*
300  * IOCTL: Set KEKLs
301  */
302 static int tape_3592_ioctl_kekl_set(struct tape_device *device,
303 				    unsigned long arg)
304 {
305 	int rc;
306 	struct tape390_kekl_pair *ext_kekls;
307 
308 	DBF_EVENT(6, "tape_3592_ioctl_kekl_set\n");
309 	if (!crypt_supported(device))
310 		return -ENOSYS;
311 	if (!crypt_enabled(device))
312 		return -EUNATCH;
313 	ext_kekls = kmalloc(sizeof(*ext_kekls), GFP_KERNEL);
314 	if (!ext_kekls)
315 		return -ENOMEM;
316 	if (copy_from_user(ext_kekls, (char __user *)arg, sizeof(*ext_kekls))) {
317 		rc = -EFAULT;
318 		goto out;
319 	}
320 	rc = tape_3592_kekl_set(device, ext_kekls);
321 out:
322 	kfree(ext_kekls);
323 	return rc;
324 }
325 
326 /*
327  * Enable encryption
328  */
329 static int tape_3592_enable_crypt(struct tape_device *device)
330 {
331 	struct tape_request *request;
332 	char *data;
333 
334 	DBF_EVENT(6, "tape_3592_enable_crypt\n");
335 	if (!crypt_supported(device))
336 		return -ENOSYS;
337 	request = tape_alloc_request(2, 72);
338 	if (IS_ERR(request))
339 		return PTR_ERR(request);
340 	data = request->cpdata;
341 	memset(data,0,72);
342 
343 	data[0]       = 0x05;
344 	data[36 + 0]  = 0x03;
345 	data[36 + 1]  = 0x03;
346 	data[36 + 4]  = 0x40;
347 	data[36 + 6]  = 0x01;
348 	data[36 + 14] = 0x2f;
349 	data[36 + 18] = 0xc3;
350 	data[36 + 35] = 0x72;
351 	request->op = TO_CRYPT_ON;
352 	tape_ccw_cc(request->cpaddr, MODE_SET_CB, 36, data);
353 	tape_ccw_end(request->cpaddr + 1, MODE_SET_CB, 36, data + 36);
354 	return tape_do_io_free(device, request);
355 }
356 
357 /*
358  * Disable encryption
359  */
360 static int tape_3592_disable_crypt(struct tape_device *device)
361 {
362 	struct tape_request *request;
363 	char *data;
364 
365 	DBF_EVENT(6, "tape_3592_disable_crypt\n");
366 	if (!crypt_supported(device))
367 		return -ENOSYS;
368 	request = tape_alloc_request(2, 72);
369 	if (IS_ERR(request))
370 		return PTR_ERR(request);
371 	data = request->cpdata;
372 	memset(data,0,72);
373 
374 	data[0]       = 0x05;
375 	data[36 + 0]  = 0x03;
376 	data[36 + 1]  = 0x03;
377 	data[36 + 35] = 0x32;
378 
379 	request->op = TO_CRYPT_OFF;
380 	tape_ccw_cc(request->cpaddr, MODE_SET_CB, 36, data);
381 	tape_ccw_end(request->cpaddr + 1, MODE_SET_CB, 36, data + 36);
382 
383 	return tape_do_io_free(device, request);
384 }
385 
386 /*
387  * IOCTL: Set encryption status
388  */
389 static int tape_3592_ioctl_crypt_set(struct tape_device *device,
390 				     unsigned long arg)
391 {
392 	struct tape390_crypt_info info;
393 
394 	DBF_EVENT(6, "tape_3592_ioctl_crypt_set\n");
395 	if (!crypt_supported(device))
396 		return -ENOSYS;
397 	if (copy_from_user(&info, (char __user *)arg, sizeof(info)))
398 		return -EFAULT;
399 	if (info.status & ~TAPE390_CRYPT_ON_MASK)
400 		return -EINVAL;
401 	if (info.status & TAPE390_CRYPT_ON_MASK)
402 		return tape_3592_enable_crypt(device);
403 	else
404 		return tape_3592_disable_crypt(device);
405 }
406 
407 static int tape_3590_sense_medium(struct tape_device *device);
408 
409 /*
410  * IOCTL: Query enryption status
411  */
412 static int tape_3592_ioctl_crypt_query(struct tape_device *device,
413 				       unsigned long arg)
414 {
415 	DBF_EVENT(6, "tape_3592_ioctl_crypt_query\n");
416 	if (!crypt_supported(device))
417 		return -ENOSYS;
418 	tape_3590_sense_medium(device);
419 	if (copy_to_user((char __user *) arg, &TAPE_3590_CRYPT_INFO(device),
420 		sizeof(TAPE_3590_CRYPT_INFO(device))))
421 		return -EFAULT;
422 	else
423 		return 0;
424 }
425 
426 /*
427  * 3590 IOCTL Overload
428  */
429 static int
430 tape_3590_ioctl(struct tape_device *device, unsigned int cmd, unsigned long arg)
431 {
432 	switch (cmd) {
433 	case TAPE390_DISPLAY: {
434 		struct display_struct disp;
435 
436 		if (copy_from_user(&disp, (char __user *) arg, sizeof(disp)))
437 			return -EFAULT;
438 
439 		return tape_std_display(device, &disp);
440 	}
441 	case TAPE390_KEKL_SET:
442 		return tape_3592_ioctl_kekl_set(device, arg);
443 	case TAPE390_KEKL_QUERY:
444 		return tape_3592_ioctl_kekl_query(device, arg);
445 	case TAPE390_CRYPT_SET:
446 		return tape_3592_ioctl_crypt_set(device, arg);
447 	case TAPE390_CRYPT_QUERY:
448 		return tape_3592_ioctl_crypt_query(device, arg);
449 	default:
450 		return -EINVAL;	/* no additional ioctls */
451 	}
452 }
453 
454 /*
455  * SENSE Medium: Get Sense data about medium state
456  */
457 static int
458 tape_3590_sense_medium(struct tape_device *device)
459 {
460 	struct tape_request *request;
461 
462 	request = tape_alloc_request(1, 128);
463 	if (IS_ERR(request))
464 		return PTR_ERR(request);
465 	request->op = TO_MSEN;
466 	tape_ccw_end(request->cpaddr, MEDIUM_SENSE, 128, request->cpdata);
467 	return tape_do_io_free(device, request);
468 }
469 
470 /*
471  * MTTELL: Tell block. Return the number of block relative to current file.
472  */
473 static int
474 tape_3590_mttell(struct tape_device *device, int mt_count)
475 {
476 	__u64 block_id;
477 	int rc;
478 
479 	rc = tape_std_read_block_id(device, &block_id);
480 	if (rc)
481 		return rc;
482 	return block_id >> 32;
483 }
484 
485 /*
486  * MTSEEK: seek to the specified block.
487  */
488 static int
489 tape_3590_mtseek(struct tape_device *device, int count)
490 {
491 	struct tape_request *request;
492 
493 	DBF_EVENT(6, "xsee id: %x\n", count);
494 	request = tape_alloc_request(3, 4);
495 	if (IS_ERR(request))
496 		return PTR_ERR(request);
497 	request->op = TO_LBL;
498 	tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
499 	*(__u32 *) request->cpdata = count;
500 	tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
501 	tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
502 	return tape_do_io_free(device, request);
503 }
504 
505 /*
506  * Read Opposite Error Recovery Function:
507  * Used, when Read Forward does not work
508  */
509 static void
510 tape_3590_read_opposite(struct tape_device *device,
511 			struct tape_request *request)
512 {
513 	struct tape_3590_disc_data *data;
514 
515 	/*
516 	 * We have allocated 4 ccws in tape_std_read, so we can now
517 	 * transform the request to a read backward, followed by a
518 	 * forward space block.
519 	 */
520 	request->op = TO_RBA;
521 	tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
522 	data = device->discdata;
523 	tape_ccw_cc_idal(request->cpaddr + 1, data->read_back_op,
524 			 device->char_data.idal_buf);
525 	tape_ccw_cc(request->cpaddr + 2, FORSPACEBLOCK, 0, NULL);
526 	tape_ccw_end(request->cpaddr + 3, NOP, 0, NULL);
527 	DBF_EVENT(6, "xrop ccwg\n");
528 }
529 
530 /*
531  * Read Attention Msg
532  * This should be done after an interrupt with attention bit (0x80)
533  * in device state.
534  *
535  * After a "read attention message" request there are two possible
536  * results:
537  *
538  * 1. A unit check is presented, when attention sense is present (e.g. when
539  * a medium has been unloaded). The attention sense comes then
540  * together with the unit check. The recovery action is either "retry"
541  * (in case there is an attention message pending) or "permanent error".
542  *
543  * 2. The attention msg is written to the "read subsystem data" buffer.
544  * In this case we probably should print it to the console.
545  */
546 static int
547 tape_3590_read_attmsg(struct tape_device *device)
548 {
549 	struct tape_request *request;
550 	char *buf;
551 
552 	request = tape_alloc_request(3, 4096);
553 	if (IS_ERR(request))
554 		return PTR_ERR(request);
555 	request->op = TO_READ_ATTMSG;
556 	buf = request->cpdata;
557 	buf[0] = PREP_RD_SS_DATA;
558 	buf[6] = RD_ATTMSG;	/* read att msg */
559 	tape_ccw_cc(request->cpaddr, PERFORM_SS_FUNC, 12, buf);
560 	tape_ccw_cc(request->cpaddr + 1, READ_SS_DATA, 4096 - 12, buf + 12);
561 	tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
562 	return tape_do_io_free(device, request);
563 }
564 
565 /*
566  * These functions are used to schedule follow-up actions from within an
567  * interrupt context (like unsolicited interrupts).
568  */
569 struct work_handler_data {
570 	struct tape_device *device;
571 	enum tape_op        op;
572 	struct work_struct  work;
573 };
574 
575 static void
576 tape_3590_work_handler(struct work_struct *work)
577 {
578 	struct work_handler_data *p =
579 		container_of(work, struct work_handler_data, work);
580 
581 	switch (p->op) {
582 	case TO_MSEN:
583 		tape_3590_sense_medium(p->device);
584 		break;
585 	case TO_READ_ATTMSG:
586 		tape_3590_read_attmsg(p->device);
587 		break;
588 	case TO_CRYPT_ON:
589 		tape_3592_enable_crypt(p->device);
590 		break;
591 	case TO_CRYPT_OFF:
592 		tape_3592_disable_crypt(p->device);
593 		break;
594 	default:
595 		DBF_EVENT(3, "T3590: work handler undefined for "
596 			  "operation 0x%02x\n", p->op);
597 	}
598 	tape_put_device(p->device);
599 	kfree(p);
600 }
601 
602 static int
603 tape_3590_schedule_work(struct tape_device *device, enum tape_op op)
604 {
605 	struct work_handler_data *p;
606 
607 	if ((p = kzalloc(sizeof(*p), GFP_ATOMIC)) == NULL)
608 		return -ENOMEM;
609 
610 	INIT_WORK(&p->work, tape_3590_work_handler);
611 
612 	p->device = tape_get_device(device);
613 	p->op = op;
614 
615 	schedule_work(&p->work);
616 	return 0;
617 }
618 
619 #ifdef CONFIG_S390_TAPE_BLOCK
620 /*
621  * Tape Block READ
622  */
623 static struct tape_request *
624 tape_3590_bread(struct tape_device *device, struct request *req)
625 {
626 	struct tape_request *request;
627 	struct ccw1 *ccw;
628 	int count = 0, start_block;
629 	unsigned off;
630 	char *dst;
631 	struct bio_vec *bv;
632 	struct req_iterator iter;
633 
634 	DBF_EVENT(6, "xBREDid:");
635 	start_block = blk_rq_pos(req) >> TAPEBLOCK_HSEC_S2B;
636 	DBF_EVENT(6, "start_block = %i\n", start_block);
637 
638 	rq_for_each_segment(bv, req, iter)
639 		count += bv->bv_len >> (TAPEBLOCK_HSEC_S2B + 9);
640 
641 	request = tape_alloc_request(2 + count + 1, 4);
642 	if (IS_ERR(request))
643 		return request;
644 	request->op = TO_BLOCK;
645 	*(__u32 *) request->cpdata = start_block;
646 	ccw = request->cpaddr;
647 	ccw = tape_ccw_cc(ccw, MODE_SET_DB, 1, device->modeset_byte);
648 
649 	/*
650 	 * We always setup a nop after the mode set ccw. This slot is
651 	 * used in tape_std_check_locate to insert a locate ccw if the
652 	 * current tape position doesn't match the start block to be read.
653 	 */
654 	ccw = tape_ccw_cc(ccw, NOP, 0, NULL);
655 
656 	rq_for_each_segment(bv, req, iter) {
657 		dst = page_address(bv->bv_page) + bv->bv_offset;
658 		for (off = 0; off < bv->bv_len; off += TAPEBLOCK_HSEC_SIZE) {
659 			ccw->flags = CCW_FLAG_CC;
660 			ccw->cmd_code = READ_FORWARD;
661 			ccw->count = TAPEBLOCK_HSEC_SIZE;
662 			set_normalized_cda(ccw, (void *) __pa(dst));
663 			ccw++;
664 			dst += TAPEBLOCK_HSEC_SIZE;
665 		}
666 		BUG_ON(off > bv->bv_len);
667 	}
668 	ccw = tape_ccw_end(ccw, NOP, 0, NULL);
669 	DBF_EVENT(6, "xBREDccwg\n");
670 	return request;
671 }
672 
673 static void
674 tape_3590_free_bread(struct tape_request *request)
675 {
676 	struct ccw1 *ccw;
677 
678 	/* Last ccw is a nop and doesn't need clear_normalized_cda */
679 	for (ccw = request->cpaddr; ccw->flags & CCW_FLAG_CC; ccw++)
680 		if (ccw->cmd_code == READ_FORWARD)
681 			clear_normalized_cda(ccw);
682 	tape_free_request(request);
683 }
684 
685 /*
686  * check_locate is called just before the tape request is passed to
687  * the common io layer for execution. It has to check the current
688  * tape position and insert a locate ccw if it doesn't match the
689  * start block for the request.
690  */
691 static void
692 tape_3590_check_locate(struct tape_device *device, struct tape_request *request)
693 {
694 	__u32 *start_block;
695 
696 	start_block = (__u32 *) request->cpdata;
697 	if (*start_block != device->blk_data.block_position) {
698 		/* Add the start offset of the file to get the real block. */
699 		*start_block += device->bof;
700 		tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
701 	}
702 }
703 #endif
704 
705 static void tape_3590_med_state_set(struct tape_device *device,
706 				    struct tape_3590_med_sense *sense)
707 {
708 	struct tape390_crypt_info *c_info;
709 
710 	c_info = &TAPE_3590_CRYPT_INFO(device);
711 
712 	DBF_EVENT(6, "medium state: %x:%x\n", sense->macst, sense->masst);
713 	switch (sense->macst) {
714 	case 0x04:
715 	case 0x05:
716 	case 0x06:
717 		tape_med_state_set(device, MS_UNLOADED);
718 		TAPE_3590_CRYPT_INFO(device).medium_status = 0;
719 		return;
720 	case 0x08:
721 	case 0x09:
722 		tape_med_state_set(device, MS_LOADED);
723 		break;
724 	default:
725 		tape_med_state_set(device, MS_UNKNOWN);
726 		return;
727 	}
728 	c_info->medium_status |= TAPE390_MEDIUM_LOADED_MASK;
729 	if (sense->flags & MSENSE_CRYPT_MASK) {
730 		DBF_EVENT(6, "Medium is encrypted (%04x)\n", sense->flags);
731 		c_info->medium_status |= TAPE390_MEDIUM_ENCRYPTED_MASK;
732 	} else	{
733 		DBF_EVENT(6, "Medium is not encrypted %04x\n", sense->flags);
734 		c_info->medium_status &= ~TAPE390_MEDIUM_ENCRYPTED_MASK;
735 	}
736 }
737 
738 /*
739  * The done handler is called at device/channel end and wakes up the sleeping
740  * process
741  */
742 static int
743 tape_3590_done(struct tape_device *device, struct tape_request *request)
744 {
745 	struct tape_3590_disc_data *disc_data;
746 
747 	DBF_EVENT(6, "%s done\n", tape_op_verbose[request->op]);
748 	disc_data = device->discdata;
749 
750 	switch (request->op) {
751 	case TO_BSB:
752 	case TO_BSF:
753 	case TO_DSE:
754 	case TO_FSB:
755 	case TO_FSF:
756 	case TO_LBL:
757 	case TO_RFO:
758 	case TO_RBA:
759 	case TO_REW:
760 	case TO_WRI:
761 	case TO_WTM:
762 	case TO_BLOCK:
763 	case TO_LOAD:
764 		tape_med_state_set(device, MS_LOADED);
765 		break;
766 	case TO_RUN:
767 		tape_med_state_set(device, MS_UNLOADED);
768 		tape_3590_schedule_work(device, TO_CRYPT_OFF);
769 		break;
770 	case TO_MSEN:
771 		tape_3590_med_state_set(device, request->cpdata);
772 		break;
773 	case TO_CRYPT_ON:
774 		TAPE_3590_CRYPT_INFO(device).status
775 			|= TAPE390_CRYPT_ON_MASK;
776 		*(device->modeset_byte) |= 0x03;
777 		break;
778 	case TO_CRYPT_OFF:
779 		TAPE_3590_CRYPT_INFO(device).status
780 			&= ~TAPE390_CRYPT_ON_MASK;
781 		*(device->modeset_byte) &= ~0x03;
782 		break;
783 	case TO_RBI:	/* RBI seems to succeed even without medium loaded. */
784 	case TO_NOP:	/* Same to NOP. */
785 	case TO_READ_CONFIG:
786 	case TO_READ_ATTMSG:
787 	case TO_DIS:
788 	case TO_ASSIGN:
789 	case TO_UNASSIGN:
790 	case TO_SIZE:
791 	case TO_KEKL_SET:
792 	case TO_KEKL_QUERY:
793 	case TO_RDC:
794 		break;
795 	}
796 	return TAPE_IO_SUCCESS;
797 }
798 
799 /*
800  * This fuction is called, when error recovery was successfull
801  */
802 static inline int
803 tape_3590_erp_succeded(struct tape_device *device, struct tape_request *request)
804 {
805 	DBF_EVENT(3, "Error Recovery successful for %s\n",
806 		  tape_op_verbose[request->op]);
807 	return tape_3590_done(device, request);
808 }
809 
810 /*
811  * This fuction is called, when error recovery was not successfull
812  */
813 static inline int
814 tape_3590_erp_failed(struct tape_device *device, struct tape_request *request,
815 		     struct irb *irb, int rc)
816 {
817 	DBF_EVENT(3, "Error Recovery failed for %s\n",
818 		  tape_op_verbose[request->op]);
819 	tape_dump_sense_dbf(device, request, irb);
820 	return rc;
821 }
822 
823 /*
824  * Error Recovery do retry
825  */
826 static inline int
827 tape_3590_erp_retry(struct tape_device *device, struct tape_request *request,
828 		    struct irb *irb)
829 {
830 	DBF_EVENT(2, "Retry: %s\n", tape_op_verbose[request->op]);
831 	tape_dump_sense_dbf(device, request, irb);
832 	return TAPE_IO_RETRY;
833 }
834 
835 /*
836  * Handle unsolicited interrupts
837  */
838 static int
839 tape_3590_unsolicited_irq(struct tape_device *device, struct irb *irb)
840 {
841 	if (irb->scsw.cmd.dstat == DEV_STAT_CHN_END)
842 		/* Probably result of halt ssch */
843 		return TAPE_IO_PENDING;
844 	else if (irb->scsw.cmd.dstat == 0x85)
845 		/* Device Ready */
846 		DBF_EVENT(3, "unsol.irq! tape ready: %08x\n", device->cdev_id);
847 	else if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
848 		tape_3590_schedule_work(device, TO_READ_ATTMSG);
849 	} else {
850 		DBF_EVENT(3, "unsol.irq! dev end: %08x\n", device->cdev_id);
851 		tape_dump_sense_dbf(device, NULL, irb);
852 	}
853 	/* check medium state */
854 	tape_3590_schedule_work(device, TO_MSEN);
855 	return TAPE_IO_SUCCESS;
856 }
857 
858 /*
859  * Basic Recovery routine
860  */
861 static int
862 tape_3590_erp_basic(struct tape_device *device, struct tape_request *request,
863 		    struct irb *irb, int rc)
864 {
865 	struct tape_3590_sense *sense;
866 
867 	sense = (struct tape_3590_sense *) irb->ecw;
868 
869 	switch (sense->bra) {
870 	case SENSE_BRA_PER:
871 		return tape_3590_erp_failed(device, request, irb, rc);
872 	case SENSE_BRA_CONT:
873 		return tape_3590_erp_succeded(device, request);
874 	case SENSE_BRA_RE:
875 		return tape_3590_erp_retry(device, request, irb);
876 	case SENSE_BRA_DRE:
877 		return tape_3590_erp_failed(device, request, irb, rc);
878 	default:
879 		BUG();
880 		return TAPE_IO_STOP;
881 	}
882 }
883 
884 /*
885  *  RDL: Read Device (buffered) log
886  */
887 static int
888 tape_3590_erp_read_buf_log(struct tape_device *device,
889 			   struct tape_request *request, struct irb *irb)
890 {
891 	/*
892 	 * We just do the basic error recovery at the moment (retry).
893 	 * Perhaps in the future, we read the log and dump it somewhere...
894 	 */
895 	return tape_3590_erp_basic(device, request, irb, -EIO);
896 }
897 
898 /*
899  *  SWAP: Swap Devices
900  */
901 static int
902 tape_3590_erp_swap(struct tape_device *device, struct tape_request *request,
903 		   struct irb *irb)
904 {
905 	/*
906 	 * This error recovery should swap the tapes
907 	 * if the original has a problem. The operation
908 	 * should proceed with the new tape... this
909 	 * should probably be done in user space!
910 	 */
911 	dev_warn (&device->cdev->dev, "The tape medium must be loaded into a "
912 		"different tape unit\n");
913 	return tape_3590_erp_basic(device, request, irb, -EIO);
914 }
915 
916 /*
917  *  LBY: Long Busy
918  */
919 static int
920 tape_3590_erp_long_busy(struct tape_device *device,
921 			struct tape_request *request, struct irb *irb)
922 {
923 	DBF_EVENT(6, "Device is busy\n");
924 	return TAPE_IO_LONG_BUSY;
925 }
926 
927 /*
928  *  SPI: Special Intercept
929  */
930 static int
931 tape_3590_erp_special_interrupt(struct tape_device *device,
932 				struct tape_request *request, struct irb *irb)
933 {
934 	return tape_3590_erp_basic(device, request, irb, -EIO);
935 }
936 
937 /*
938  *  RDA: Read Alternate
939  */
940 static int
941 tape_3590_erp_read_alternate(struct tape_device *device,
942 			     struct tape_request *request, struct irb *irb)
943 {
944 	struct tape_3590_disc_data *data;
945 
946 	/*
947 	 * The issued Read Backward or Read Previous command is not
948 	 * supported by the device
949 	 * The recovery action should be to issue another command:
950 	 * Read Revious: if Read Backward is not supported
951 	 * Read Backward: if Read Previous is not supported
952 	 */
953 	data = device->discdata;
954 	if (data->read_back_op == READ_PREVIOUS) {
955 		DBF_EVENT(2, "(%08x): No support for READ_PREVIOUS command\n",
956 			  device->cdev_id);
957 		data->read_back_op = READ_BACKWARD;
958 	} else {
959 		DBF_EVENT(2, "(%08x): No support for READ_BACKWARD command\n",
960 			  device->cdev_id);
961 		data->read_back_op = READ_PREVIOUS;
962 	}
963 	tape_3590_read_opposite(device, request);
964 	return tape_3590_erp_retry(device, request, irb);
965 }
966 
967 /*
968  * Error Recovery read opposite
969  */
970 static int
971 tape_3590_erp_read_opposite(struct tape_device *device,
972 			    struct tape_request *request, struct irb *irb)
973 {
974 	switch (request->op) {
975 	case TO_RFO:
976 		/*
977 		 * We did read forward, but the data could not be read.
978 		 * We will read backward and then skip forward again.
979 		 */
980 		tape_3590_read_opposite(device, request);
981 		return tape_3590_erp_retry(device, request, irb);
982 	case TO_RBA:
983 		/* We tried to read forward and backward, but hat no success */
984 		return tape_3590_erp_failed(device, request, irb, -EIO);
985 		break;
986 	default:
987 		return tape_3590_erp_failed(device, request, irb, -EIO);
988 	}
989 }
990 
991 /*
992  * Print an MIM (Media Information  Message) (message code f0)
993  */
994 static void
995 tape_3590_print_mim_msg_f0(struct tape_device *device, struct irb *irb)
996 {
997 	struct tape_3590_sense *sense;
998 	char *exception, *service;
999 
1000 	exception = kmalloc(BUFSIZE, GFP_ATOMIC);
1001 	service = kmalloc(BUFSIZE, GFP_ATOMIC);
1002 
1003 	if (!exception || !service)
1004 		goto out_nomem;
1005 
1006 	sense = (struct tape_3590_sense *) irb->ecw;
1007 	/* Exception Message */
1008 	switch (sense->fmt.f70.emc) {
1009 	case 0x02:
1010 		snprintf(exception, BUFSIZE, "Data degraded");
1011 		break;
1012 	case 0x03:
1013 		snprintf(exception, BUFSIZE, "Data degraded in partion %i",
1014 			sense->fmt.f70.mp);
1015 		break;
1016 	case 0x04:
1017 		snprintf(exception, BUFSIZE, "Medium degraded");
1018 		break;
1019 	case 0x05:
1020 		snprintf(exception, BUFSIZE, "Medium degraded in partition %i",
1021 			sense->fmt.f70.mp);
1022 		break;
1023 	case 0x06:
1024 		snprintf(exception, BUFSIZE, "Block 0 Error");
1025 		break;
1026 	case 0x07:
1027 		snprintf(exception, BUFSIZE, "Medium Exception 0x%02x",
1028 			sense->fmt.f70.md);
1029 		break;
1030 	default:
1031 		snprintf(exception, BUFSIZE, "0x%02x",
1032 			sense->fmt.f70.emc);
1033 		break;
1034 	}
1035 	/* Service Message */
1036 	switch (sense->fmt.f70.smc) {
1037 	case 0x02:
1038 		snprintf(service, BUFSIZE, "Reference Media maintenance "
1039 			"procedure %i", sense->fmt.f70.md);
1040 		break;
1041 	default:
1042 		snprintf(service, BUFSIZE, "0x%02x",
1043 			sense->fmt.f70.smc);
1044 		break;
1045 	}
1046 
1047 	dev_warn (&device->cdev->dev, "Tape media information: exception %s, "
1048 		"service %s\n", exception, service);
1049 
1050 out_nomem:
1051 	kfree(exception);
1052 	kfree(service);
1053 }
1054 
1055 /*
1056  * Print an I/O Subsystem Service Information Message (message code f1)
1057  */
1058 static void
1059 tape_3590_print_io_sim_msg_f1(struct tape_device *device, struct irb *irb)
1060 {
1061 	struct tape_3590_sense *sense;
1062 	char *exception, *service;
1063 
1064 	exception = kmalloc(BUFSIZE, GFP_ATOMIC);
1065 	service = kmalloc(BUFSIZE, GFP_ATOMIC);
1066 
1067 	if (!exception || !service)
1068 		goto out_nomem;
1069 
1070 	sense = (struct tape_3590_sense *) irb->ecw;
1071 	/* Exception Message */
1072 	switch (sense->fmt.f71.emc) {
1073 	case 0x01:
1074 		snprintf(exception, BUFSIZE, "Effect of failure is unknown");
1075 		break;
1076 	case 0x02:
1077 		snprintf(exception, BUFSIZE, "CU Exception - no performance "
1078 			"impact");
1079 		break;
1080 	case 0x03:
1081 		snprintf(exception, BUFSIZE, "CU Exception on channel "
1082 			"interface 0x%02x", sense->fmt.f71.md[0]);
1083 		break;
1084 	case 0x04:
1085 		snprintf(exception, BUFSIZE, "CU Exception on device path "
1086 			"0x%02x", sense->fmt.f71.md[0]);
1087 		break;
1088 	case 0x05:
1089 		snprintf(exception, BUFSIZE, "CU Exception on library path "
1090 			"0x%02x", sense->fmt.f71.md[0]);
1091 		break;
1092 	case 0x06:
1093 		snprintf(exception, BUFSIZE, "CU Exception on node 0x%02x",
1094 			sense->fmt.f71.md[0]);
1095 		break;
1096 	case 0x07:
1097 		snprintf(exception, BUFSIZE, "CU Exception on partition "
1098 			"0x%02x", sense->fmt.f71.md[0]);
1099 		break;
1100 	default:
1101 		snprintf(exception, BUFSIZE, "0x%02x",
1102 			sense->fmt.f71.emc);
1103 	}
1104 	/* Service Message */
1105 	switch (sense->fmt.f71.smc) {
1106 	case 0x01:
1107 		snprintf(service, BUFSIZE, "Repair impact is unknown");
1108 		break;
1109 	case 0x02:
1110 		snprintf(service, BUFSIZE, "Repair will not impact cu "
1111 			"performance");
1112 		break;
1113 	case 0x03:
1114 		if (sense->fmt.f71.mdf == 0)
1115 			snprintf(service, BUFSIZE, "Repair will disable node "
1116 				"0x%x on CU", sense->fmt.f71.md[1]);
1117 		else
1118 			snprintf(service, BUFSIZE, "Repair will disable "
1119 				"nodes (0x%x-0x%x) on CU", sense->fmt.f71.md[1],
1120 				sense->fmt.f71.md[2]);
1121 		break;
1122 	case 0x04:
1123 		if (sense->fmt.f71.mdf == 0)
1124 			snprintf(service, BUFSIZE, "Repair will disable "
1125 				"channel path 0x%x on CU",
1126 				sense->fmt.f71.md[1]);
1127 		else
1128 			snprintf(service, BUFSIZE, "Repair will disable cannel"
1129 				" paths (0x%x-0x%x) on CU",
1130 				sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1131 		break;
1132 	case 0x05:
1133 		if (sense->fmt.f71.mdf == 0)
1134 			snprintf(service, BUFSIZE, "Repair will disable device"
1135 				" path 0x%x on CU", sense->fmt.f71.md[1]);
1136 		else
1137 			snprintf(service, BUFSIZE, "Repair will disable device"
1138 				" paths (0x%x-0x%x) on CU",
1139 				sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1140 		break;
1141 	case 0x06:
1142 		if (sense->fmt.f71.mdf == 0)
1143 			snprintf(service, BUFSIZE, "Repair will disable "
1144 				"library path 0x%x on CU",
1145 				sense->fmt.f71.md[1]);
1146 		else
1147 			snprintf(service, BUFSIZE, "Repair will disable "
1148 				"library paths (0x%x-0x%x) on CU",
1149 				sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1150 		break;
1151 	case 0x07:
1152 		snprintf(service, BUFSIZE, "Repair will disable access to CU");
1153 		break;
1154 	default:
1155 		snprintf(service, BUFSIZE, "0x%02x",
1156 			sense->fmt.f71.smc);
1157 	}
1158 
1159 	dev_warn (&device->cdev->dev, "I/O subsystem information: exception"
1160 		" %s, service %s\n", exception, service);
1161 out_nomem:
1162 	kfree(exception);
1163 	kfree(service);
1164 }
1165 
1166 /*
1167  * Print an Device Subsystem Service Information Message (message code f2)
1168  */
1169 static void
1170 tape_3590_print_dev_sim_msg_f2(struct tape_device *device, struct irb *irb)
1171 {
1172 	struct tape_3590_sense *sense;
1173 	char *exception, *service;
1174 
1175 	exception = kmalloc(BUFSIZE, GFP_ATOMIC);
1176 	service = kmalloc(BUFSIZE, GFP_ATOMIC);
1177 
1178 	if (!exception || !service)
1179 		goto out_nomem;
1180 
1181 	sense = (struct tape_3590_sense *) irb->ecw;
1182 	/* Exception Message */
1183 	switch (sense->fmt.f71.emc) {
1184 	case 0x01:
1185 		snprintf(exception, BUFSIZE, "Effect of failure is unknown");
1186 		break;
1187 	case 0x02:
1188 		snprintf(exception, BUFSIZE, "DV Exception - no performance"
1189 			" impact");
1190 		break;
1191 	case 0x03:
1192 		snprintf(exception, BUFSIZE, "DV Exception on channel "
1193 			"interface 0x%02x", sense->fmt.f71.md[0]);
1194 		break;
1195 	case 0x04:
1196 		snprintf(exception, BUFSIZE, "DV Exception on loader 0x%02x",
1197 			sense->fmt.f71.md[0]);
1198 		break;
1199 	case 0x05:
1200 		snprintf(exception, BUFSIZE, "DV Exception on message display"
1201 			" 0x%02x", sense->fmt.f71.md[0]);
1202 		break;
1203 	case 0x06:
1204 		snprintf(exception, BUFSIZE, "DV Exception in tape path");
1205 		break;
1206 	case 0x07:
1207 		snprintf(exception, BUFSIZE, "DV Exception in drive");
1208 		break;
1209 	default:
1210 		snprintf(exception, BUFSIZE, "0x%02x",
1211 			sense->fmt.f71.emc);
1212 	}
1213 	/* Service Message */
1214 	switch (sense->fmt.f71.smc) {
1215 	case 0x01:
1216 		snprintf(service, BUFSIZE, "Repair impact is unknown");
1217 		break;
1218 	case 0x02:
1219 		snprintf(service, BUFSIZE, "Repair will not impact device "
1220 			"performance");
1221 		break;
1222 	case 0x03:
1223 		if (sense->fmt.f71.mdf == 0)
1224 			snprintf(service, BUFSIZE, "Repair will disable "
1225 				"channel path 0x%x on DV",
1226 				sense->fmt.f71.md[1]);
1227 		else
1228 			snprintf(service, BUFSIZE, "Repair will disable "
1229 				"channel path (0x%x-0x%x) on DV",
1230 				sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1231 		break;
1232 	case 0x04:
1233 		if (sense->fmt.f71.mdf == 0)
1234 			snprintf(service, BUFSIZE, "Repair will disable "
1235 				"interface 0x%x on DV", sense->fmt.f71.md[1]);
1236 		else
1237 			snprintf(service, BUFSIZE, "Repair will disable "
1238 				"interfaces (0x%x-0x%x) on DV",
1239 				sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1240 		break;
1241 	case 0x05:
1242 		if (sense->fmt.f71.mdf == 0)
1243 			snprintf(service, BUFSIZE, "Repair will disable loader"
1244 				" 0x%x on DV", sense->fmt.f71.md[1]);
1245 		else
1246 			snprintf(service, BUFSIZE, "Repair will disable loader"
1247 				" (0x%x-0x%x) on DV",
1248 				sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1249 		break;
1250 	case 0x07:
1251 		snprintf(service, BUFSIZE, "Repair will disable access to DV");
1252 		break;
1253 	case 0x08:
1254 		if (sense->fmt.f71.mdf == 0)
1255 			snprintf(service, BUFSIZE, "Repair will disable "
1256 				"message display 0x%x on DV",
1257 				sense->fmt.f71.md[1]);
1258 		else
1259 			snprintf(service, BUFSIZE, "Repair will disable "
1260 				"message displays (0x%x-0x%x) on DV",
1261 				 sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
1262 		break;
1263 	case 0x09:
1264 		snprintf(service, BUFSIZE, "Clean DV");
1265 		break;
1266 	default:
1267 		snprintf(service, BUFSIZE, "0x%02x",
1268 			sense->fmt.f71.smc);
1269 	}
1270 
1271 	dev_warn (&device->cdev->dev, "Device subsystem information: exception"
1272 		" %s, service %s\n", exception, service);
1273 out_nomem:
1274 	kfree(exception);
1275 	kfree(service);
1276 }
1277 
1278 /*
1279  * Print standard ERA Message
1280  */
1281 static void
1282 tape_3590_print_era_msg(struct tape_device *device, struct irb *irb)
1283 {
1284 	struct tape_3590_sense *sense;
1285 
1286 	sense = (struct tape_3590_sense *) irb->ecw;
1287 	if (sense->mc == 0)
1288 		return;
1289 	if ((sense->mc > 0) && (sense->mc < TAPE_3590_MAX_MSG)) {
1290 		if (tape_3590_msg[sense->mc] != NULL)
1291 			dev_warn (&device->cdev->dev, "The tape unit has "
1292 				"issued sense message %s\n",
1293 				tape_3590_msg[sense->mc]);
1294 		else
1295 			dev_warn (&device->cdev->dev, "The tape unit has "
1296 				"issued an unknown sense message code 0x%x\n",
1297 				sense->mc);
1298 		return;
1299 	}
1300 	if (sense->mc == 0xf0) {
1301 		/* Standard Media Information Message */
1302 		dev_warn (&device->cdev->dev, "MIM SEV=%i, MC=%02x, ES=%x/%x, "
1303 			"RC=%02x-%04x-%02x\n", sense->fmt.f70.sev, sense->mc,
1304 			sense->fmt.f70.emc, sense->fmt.f70.smc,
1305 			sense->fmt.f70.refcode, sense->fmt.f70.mid,
1306 			sense->fmt.f70.fid);
1307 		tape_3590_print_mim_msg_f0(device, irb);
1308 		return;
1309 	}
1310 	if (sense->mc == 0xf1) {
1311 		/* Standard I/O Subsystem Service Information Message */
1312 		dev_warn (&device->cdev->dev, "IOSIM SEV=%i, DEVTYPE=3590/%02x,"
1313 			" MC=%02x, ES=%x/%x, REF=0x%04x-0x%04x-0x%04x\n",
1314 			sense->fmt.f71.sev, device->cdev->id.dev_model,
1315 			sense->mc, sense->fmt.f71.emc, sense->fmt.f71.smc,
1316 			sense->fmt.f71.refcode1, sense->fmt.f71.refcode2,
1317 			sense->fmt.f71.refcode3);
1318 		tape_3590_print_io_sim_msg_f1(device, irb);
1319 		return;
1320 	}
1321 	if (sense->mc == 0xf2) {
1322 		/* Standard Device Service Information Message */
1323 		dev_warn (&device->cdev->dev, "DEVSIM SEV=%i, DEVTYPE=3590/%02x"
1324 			", MC=%02x, ES=%x/%x, REF=0x%04x-0x%04x-0x%04x\n",
1325 			sense->fmt.f71.sev, device->cdev->id.dev_model,
1326 			sense->mc, sense->fmt.f71.emc, sense->fmt.f71.smc,
1327 			sense->fmt.f71.refcode1, sense->fmt.f71.refcode2,
1328 			sense->fmt.f71.refcode3);
1329 		tape_3590_print_dev_sim_msg_f2(device, irb);
1330 		return;
1331 	}
1332 	if (sense->mc == 0xf3) {
1333 		/* Standard Library Service Information Message */
1334 		return;
1335 	}
1336 	dev_warn (&device->cdev->dev, "The tape unit has issued an unknown "
1337 		"sense message code %x\n", sense->mc);
1338 }
1339 
1340 static int tape_3590_crypt_error(struct tape_device *device,
1341 				 struct tape_request *request, struct irb *irb)
1342 {
1343 	u8 cu_rc, ekm_rc1;
1344 	u16 ekm_rc2;
1345 	u32 drv_rc;
1346 	const char *bus_id;
1347 	char *sense;
1348 
1349 	sense = ((struct tape_3590_sense *) irb->ecw)->fmt.data;
1350 	bus_id = dev_name(&device->cdev->dev);
1351 	cu_rc = sense[0];
1352 	drv_rc = *((u32*) &sense[5]) & 0xffffff;
1353 	ekm_rc1 = sense[9];
1354 	ekm_rc2 = *((u16*) &sense[10]);
1355 	if ((cu_rc == 0) && (ekm_rc2 == 0xee31))
1356 		/* key not defined on EKM */
1357 		return tape_3590_erp_basic(device, request, irb, -EKEYREJECTED);
1358 	if ((cu_rc == 1) || (cu_rc == 2))
1359 		/* No connection to EKM */
1360 		return tape_3590_erp_basic(device, request, irb, -ENOTCONN);
1361 
1362 	dev_err (&device->cdev->dev, "The tape unit failed to obtain the "
1363 		"encryption key from EKM\n");
1364 
1365 	return tape_3590_erp_basic(device, request, irb, -ENOKEY);
1366 }
1367 
1368 /*
1369  *  3590 error Recovery routine:
1370  *  If possible, it tries to recover from the error. If this is not possible,
1371  *  inform the user about the problem.
1372  */
1373 static int
1374 tape_3590_unit_check(struct tape_device *device, struct tape_request *request,
1375 		     struct irb *irb)
1376 {
1377 	struct tape_3590_sense *sense;
1378 	int rc;
1379 
1380 #ifdef CONFIG_S390_TAPE_BLOCK
1381 	if (request->op == TO_BLOCK) {
1382 		/*
1383 		 * Recovery for block device requests. Set the block_position
1384 		 * to something invalid and retry.
1385 		 */
1386 		device->blk_data.block_position = -1;
1387 		if (request->retries-- <= 0)
1388 			return tape_3590_erp_failed(device, request, irb, -EIO);
1389 		else
1390 			return tape_3590_erp_retry(device, request, irb);
1391 	}
1392 #endif
1393 
1394 	sense = (struct tape_3590_sense *) irb->ecw;
1395 
1396 	DBF_EVENT(6, "Unit Check: RQC = %x\n", sense->rc_rqc);
1397 
1398 	/*
1399 	 * First check all RC-QRCs where we want to do something special
1400 	 *   - "break":     basic error recovery is done
1401 	 *   - "goto out:": just print error message if available
1402 	 */
1403 	rc = -EIO;
1404 	switch (sense->rc_rqc) {
1405 
1406 	case 0x1110:
1407 		tape_3590_print_era_msg(device, irb);
1408 		return tape_3590_erp_read_buf_log(device, request, irb);
1409 
1410 	case 0x2011:
1411 		tape_3590_print_era_msg(device, irb);
1412 		return tape_3590_erp_read_alternate(device, request, irb);
1413 
1414 	case 0x2230:
1415 	case 0x2231:
1416 		tape_3590_print_era_msg(device, irb);
1417 		return tape_3590_erp_special_interrupt(device, request, irb);
1418 	case 0x2240:
1419 		return tape_3590_crypt_error(device, request, irb);
1420 
1421 	case 0x3010:
1422 		DBF_EVENT(2, "(%08x): Backward at Beginning of Partition\n",
1423 			  device->cdev_id);
1424 		return tape_3590_erp_basic(device, request, irb, -ENOSPC);
1425 	case 0x3012:
1426 		DBF_EVENT(2, "(%08x): Forward at End of Partition\n",
1427 			  device->cdev_id);
1428 		return tape_3590_erp_basic(device, request, irb, -ENOSPC);
1429 	case 0x3020:
1430 		DBF_EVENT(2, "(%08x): End of Data Mark\n", device->cdev_id);
1431 		return tape_3590_erp_basic(device, request, irb, -ENOSPC);
1432 
1433 	case 0x3122:
1434 		DBF_EVENT(2, "(%08x): Rewind Unload initiated\n",
1435 			  device->cdev_id);
1436 		return tape_3590_erp_basic(device, request, irb, -EIO);
1437 	case 0x3123:
1438 		DBF_EVENT(2, "(%08x): Rewind Unload complete\n",
1439 			  device->cdev_id);
1440 		tape_med_state_set(device, MS_UNLOADED);
1441 		tape_3590_schedule_work(device, TO_CRYPT_OFF);
1442 		return tape_3590_erp_basic(device, request, irb, 0);
1443 
1444 	case 0x4010:
1445 		/*
1446 		 * print additional msg since default msg
1447 		 * "device intervention" is not very meaningfull
1448 		 */
1449 		tape_med_state_set(device, MS_UNLOADED);
1450 		tape_3590_schedule_work(device, TO_CRYPT_OFF);
1451 		return tape_3590_erp_basic(device, request, irb, -ENOMEDIUM);
1452 	case 0x4012:		/* Device Long Busy */
1453 		/* XXX: Also use long busy handling here? */
1454 		DBF_EVENT(6, "(%08x): LONG BUSY\n", device->cdev_id);
1455 		tape_3590_print_era_msg(device, irb);
1456 		return tape_3590_erp_basic(device, request, irb, -EBUSY);
1457 	case 0x4014:
1458 		DBF_EVENT(6, "(%08x): Crypto LONG BUSY\n", device->cdev_id);
1459 		return tape_3590_erp_long_busy(device, request, irb);
1460 
1461 	case 0x5010:
1462 		if (sense->rac == 0xd0) {
1463 			/* Swap */
1464 			tape_3590_print_era_msg(device, irb);
1465 			return tape_3590_erp_swap(device, request, irb);
1466 		}
1467 		if (sense->rac == 0x26) {
1468 			/* Read Opposite */
1469 			tape_3590_print_era_msg(device, irb);
1470 			return tape_3590_erp_read_opposite(device, request,
1471 							   irb);
1472 		}
1473 		return tape_3590_erp_basic(device, request, irb, -EIO);
1474 	case 0x5020:
1475 	case 0x5021:
1476 	case 0x5022:
1477 	case 0x5040:
1478 	case 0x5041:
1479 	case 0x5042:
1480 		tape_3590_print_era_msg(device, irb);
1481 		return tape_3590_erp_swap(device, request, irb);
1482 
1483 	case 0x5110:
1484 	case 0x5111:
1485 		return tape_3590_erp_basic(device, request, irb, -EMEDIUMTYPE);
1486 
1487 	case 0x5120:
1488 	case 0x1120:
1489 		tape_med_state_set(device, MS_UNLOADED);
1490 		tape_3590_schedule_work(device, TO_CRYPT_OFF);
1491 		return tape_3590_erp_basic(device, request, irb, -ENOMEDIUM);
1492 
1493 	case 0x6020:
1494 		return tape_3590_erp_basic(device, request, irb, -EMEDIUMTYPE);
1495 
1496 	case 0x8011:
1497 		return tape_3590_erp_basic(device, request, irb, -EPERM);
1498 	case 0x8013:
1499 		dev_warn (&device->cdev->dev, "A different host has privileged"
1500 			" access to the tape unit\n");
1501 		return tape_3590_erp_basic(device, request, irb, -EPERM);
1502 	default:
1503 		return tape_3590_erp_basic(device, request, irb, -EIO);
1504 	}
1505 }
1506 
1507 /*
1508  * 3590 interrupt handler:
1509  */
1510 static int
1511 tape_3590_irq(struct tape_device *device, struct tape_request *request,
1512 	      struct irb *irb)
1513 {
1514 	if (request == NULL)
1515 		return tape_3590_unsolicited_irq(device, irb);
1516 
1517 	if ((irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) &&
1518 	    (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) &&
1519 	    (request->op == TO_WRI)) {
1520 		/* Write at end of volume */
1521 		DBF_EVENT(2, "End of volume\n");
1522 		return tape_3590_erp_failed(device, request, irb, -ENOSPC);
1523 	}
1524 
1525 	if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
1526 		return tape_3590_unit_check(device, request, irb);
1527 
1528 	if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
1529 		if (irb->scsw.cmd.dstat == DEV_STAT_UNIT_EXCEP) {
1530 			if (request->op == TO_FSB || request->op == TO_BSB)
1531 				request->rescnt++;
1532 			else
1533 				DBF_EVENT(5, "Unit Exception!\n");
1534 		}
1535 
1536 		return tape_3590_done(device, request);
1537 	}
1538 
1539 	if (irb->scsw.cmd.dstat & DEV_STAT_CHN_END) {
1540 		DBF_EVENT(2, "cannel end\n");
1541 		return TAPE_IO_PENDING;
1542 	}
1543 
1544 	if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
1545 		DBF_EVENT(2, "Unit Attention when busy..\n");
1546 		return TAPE_IO_PENDING;
1547 	}
1548 
1549 	DBF_EVENT(6, "xunknownirq\n");
1550 	tape_dump_sense_dbf(device, request, irb);
1551 	return TAPE_IO_STOP;
1552 }
1553 
1554 
1555 static int tape_3590_read_dev_chars(struct tape_device *device,
1556 				    struct tape_3590_rdc_data *rdc_data)
1557 {
1558 	int rc;
1559 	struct tape_request *request;
1560 
1561 	request = tape_alloc_request(1, sizeof(*rdc_data));
1562 	if (IS_ERR(request))
1563 		return PTR_ERR(request);
1564 	request->op = TO_RDC;
1565 	tape_ccw_end(request->cpaddr, CCW_CMD_RDC, sizeof(*rdc_data),
1566 		     request->cpdata);
1567 	rc = tape_do_io(device, request);
1568 	if (rc == 0)
1569 		memcpy(rdc_data, request->cpdata, sizeof(*rdc_data));
1570 	tape_free_request(request);
1571 	return rc;
1572 }
1573 
1574 /*
1575  * Setup device function
1576  */
1577 static int
1578 tape_3590_setup_device(struct tape_device *device)
1579 {
1580 	int rc;
1581 	struct tape_3590_disc_data *data;
1582 	struct tape_3590_rdc_data *rdc_data;
1583 
1584 	DBF_EVENT(6, "3590 device setup\n");
1585 	data = kzalloc(sizeof(struct tape_3590_disc_data), GFP_KERNEL | GFP_DMA);
1586 	if (data == NULL)
1587 		return -ENOMEM;
1588 	data->read_back_op = READ_PREVIOUS;
1589 	device->discdata = data;
1590 
1591 	rdc_data = kmalloc(sizeof(*rdc_data), GFP_KERNEL | GFP_DMA);
1592 	if (!rdc_data) {
1593 		rc = -ENOMEM;
1594 		goto fail_kmalloc;
1595 	}
1596 	rc = tape_3590_read_dev_chars(device, rdc_data);
1597 	if (rc) {
1598 		DBF_LH(3, "Read device characteristics failed!\n");
1599 		goto fail_rdc_data;
1600 	}
1601 	rc = tape_std_assign(device);
1602 	if (rc)
1603 		goto fail_rdc_data;
1604 	if (rdc_data->data[31] == 0x13) {
1605 		data->crypt_info.capability |= TAPE390_CRYPT_SUPPORTED_MASK;
1606 		tape_3592_disable_crypt(device);
1607 	} else {
1608 		DBF_EVENT(6, "Device has NO crypto support\n");
1609 	}
1610 	/* Try to find out if medium is loaded */
1611 	rc = tape_3590_sense_medium(device);
1612 	if (rc) {
1613 		DBF_LH(3, "3590 medium sense returned %d\n", rc);
1614 		goto fail_rdc_data;
1615 	}
1616 	return 0;
1617 
1618 fail_rdc_data:
1619 	kfree(rdc_data);
1620 fail_kmalloc:
1621 	kfree(data);
1622 	return rc;
1623 }
1624 
1625 /*
1626  * Cleanup device function
1627  */
1628 static void
1629 tape_3590_cleanup_device(struct tape_device *device)
1630 {
1631 	flush_scheduled_work();
1632 	tape_std_unassign(device);
1633 
1634 	kfree(device->discdata);
1635 	device->discdata = NULL;
1636 }
1637 
1638 /*
1639  * List of 3590 magnetic tape commands.
1640  */
1641 static tape_mtop_fn tape_3590_mtop[TAPE_NR_MTOPS] = {
1642 	[MTRESET]	 = tape_std_mtreset,
1643 	[MTFSF]		 = tape_std_mtfsf,
1644 	[MTBSF]		 = tape_std_mtbsf,
1645 	[MTFSR]		 = tape_std_mtfsr,
1646 	[MTBSR]		 = tape_std_mtbsr,
1647 	[MTWEOF]	 = tape_std_mtweof,
1648 	[MTREW]		 = tape_std_mtrew,
1649 	[MTOFFL]	 = tape_std_mtoffl,
1650 	[MTNOP]		 = tape_std_mtnop,
1651 	[MTRETEN]	 = tape_std_mtreten,
1652 	[MTBSFM]	 = tape_std_mtbsfm,
1653 	[MTFSFM]	 = tape_std_mtfsfm,
1654 	[MTEOM]		 = tape_std_mteom,
1655 	[MTERASE]	 = tape_std_mterase,
1656 	[MTRAS1]	 = NULL,
1657 	[MTRAS2]	 = NULL,
1658 	[MTRAS3]	 = NULL,
1659 	[MTSETBLK]	 = tape_std_mtsetblk,
1660 	[MTSETDENSITY]	 = NULL,
1661 	[MTSEEK]	 = tape_3590_mtseek,
1662 	[MTTELL]	 = tape_3590_mttell,
1663 	[MTSETDRVBUFFER] = NULL,
1664 	[MTFSS]		 = NULL,
1665 	[MTBSS]		 = NULL,
1666 	[MTWSM]		 = NULL,
1667 	[MTLOCK]	 = NULL,
1668 	[MTUNLOCK]	 = NULL,
1669 	[MTLOAD]	 = tape_std_mtload,
1670 	[MTUNLOAD]	 = tape_std_mtunload,
1671 	[MTCOMPRESSION]	 = tape_std_mtcompression,
1672 	[MTSETPART]	 = NULL,
1673 	[MTMKPART]	 = NULL
1674 };
1675 
1676 /*
1677  * Tape discipline structure for 3590.
1678  */
1679 static struct tape_discipline tape_discipline_3590 = {
1680 	.owner = THIS_MODULE,
1681 	.setup_device = tape_3590_setup_device,
1682 	.cleanup_device = tape_3590_cleanup_device,
1683 	.process_eov = tape_std_process_eov,
1684 	.irq = tape_3590_irq,
1685 	.read_block = tape_std_read_block,
1686 	.write_block = tape_std_write_block,
1687 #ifdef CONFIG_S390_TAPE_BLOCK
1688 	.bread = tape_3590_bread,
1689 	.free_bread = tape_3590_free_bread,
1690 	.check_locate = tape_3590_check_locate,
1691 #endif
1692 	.ioctl_fn = tape_3590_ioctl,
1693 	.mtop_array = tape_3590_mtop
1694 };
1695 
1696 static struct ccw_device_id tape_3590_ids[] = {
1697 	{CCW_DEVICE_DEVTYPE(0x3590, 0, 0x3590, 0), .driver_info = tape_3590},
1698 	{CCW_DEVICE_DEVTYPE(0x3592, 0, 0x3592, 0), .driver_info = tape_3592},
1699 	{ /* end of list */ }
1700 };
1701 
1702 static int
1703 tape_3590_online(struct ccw_device *cdev)
1704 {
1705 	return tape_generic_online(dev_get_drvdata(&cdev->dev),
1706 				   &tape_discipline_3590);
1707 }
1708 
1709 static struct ccw_driver tape_3590_driver = {
1710 	.name = "tape_3590",
1711 	.owner = THIS_MODULE,
1712 	.ids = tape_3590_ids,
1713 	.probe = tape_generic_probe,
1714 	.remove = tape_generic_remove,
1715 	.set_offline = tape_generic_offline,
1716 	.set_online = tape_3590_online,
1717 	.freeze = tape_generic_pm_suspend,
1718 };
1719 
1720 /*
1721  * Setup discipline structure.
1722  */
1723 static int
1724 tape_3590_init(void)
1725 {
1726 	int rc;
1727 
1728 	TAPE_DBF_AREA = debug_register("tape_3590", 2, 2, 4 * sizeof(long));
1729 	debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1730 #ifdef DBF_LIKE_HELL
1731 	debug_set_level(TAPE_DBF_AREA, 6);
1732 #endif
1733 
1734 	DBF_EVENT(3, "3590 init\n");
1735 	/* Register driver for 3590 tapes. */
1736 	rc = ccw_driver_register(&tape_3590_driver);
1737 	if (rc)
1738 		DBF_EVENT(3, "3590 init failed\n");
1739 	else
1740 		DBF_EVENT(3, "3590 registered\n");
1741 	return rc;
1742 }
1743 
1744 static void
1745 tape_3590_exit(void)
1746 {
1747 	ccw_driver_unregister(&tape_3590_driver);
1748 
1749 	debug_unregister(TAPE_DBF_AREA);
1750 }
1751 
1752 MODULE_DEVICE_TABLE(ccw, tape_3590_ids);
1753 MODULE_AUTHOR("(C) 2001,2006 IBM Corporation");
1754 MODULE_DESCRIPTION("Linux on zSeries channel attached 3590 tape device driver");
1755 MODULE_LICENSE("GPL");
1756 
1757 module_init(tape_3590_init);
1758 module_exit(tape_3590_exit);
1759