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