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