blob: b98dcbd16711e7978d8c1fc58394f4797d851fe8 [file] [log] [blame]
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001/*
2 * drivers/s390/char/tape_3590.c
3 * tape device discipline for 3590 tapes.
4 *
Frank Munzert3ef32e622009-06-16 10:30:39 +02005 * Copyright IBM Corp. 2001, 2009
Stefan Baderb6cba4e2006-03-24 03:15:29 -08006 * Author(s): Stefan Bader <shbader@de.ibm.com>
7 * Michael Holzheu <holzheu@de.ibm.com>
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
9 */
10
Michael Holzheu59e36922009-09-11 10:29:07 +020011#define KMSG_COMPONENT "tape_3590"
Michael Holzheubb509912009-12-18 17:43:21 +010012#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
Carsten Otteab640db2009-03-26 15:24:38 +010013
Stefan Baderb6cba4e2006-03-24 03:15:29 -080014#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Stefan Baderb6cba4e2006-03-24 03:15:29 -080016#include <linux/init.h>
17#include <linux/bio.h>
Michael Holzheucced1dd2007-02-05 21:18:26 +010018#include <asm/ebcdic.h>
Stefan Baderb6cba4e2006-03-24 03:15:29 -080019
20#define TAPE_DBF_AREA tape_3590_dbf
Carsten Otteab640db2009-03-26 15:24:38 +010021#define BUFSIZE 512 /* size of buffers for dynamic generated messages */
Stefan Baderb6cba4e2006-03-24 03:15:29 -080022
23#include "tape.h"
24#include "tape_std.h"
25#include "tape_3590.h"
26
Tejun Heo6dca4672010-12-24 16:00:18 +010027static struct workqueue_struct *tape_3590_wq;
28
Stefan Baderb6cba4e2006-03-24 03:15:29 -080029/*
30 * Pointer to debug area.
31 */
32debug_info_t *TAPE_DBF_AREA = NULL;
33EXPORT_SYMBOL(TAPE_DBF_AREA);
34
35/*******************************************************************
Stefan Weileef35c22010-08-06 21:11:15 +020036 * Error Recovery functions:
Stefan Baderb6cba4e2006-03-24 03:15:29 -080037 * - Read Opposite: implemented
38 * - Read Device (buffered) log: BRA
39 * - Read Library log: BRA
40 * - Swap Devices: BRA
Michael Holzheucced1dd2007-02-05 21:18:26 +010041 * - Long Busy: implemented
Stefan Baderb6cba4e2006-03-24 03:15:29 -080042 * - Special Intercept: BRA
43 * - Read Alternate: implemented
44 *******************************************************************/
45
Stefan Baderb6cba4e2006-03-24 03:15:29 -080046static 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
Michael Holzheucced1dd2007-02-05 21:18:26 +0100103static int crypt_supported(struct tape_device *device)
104{
105 return TAPE390_CRYPT_SUPPORTED(TAPE_3590_CRYPT_INFO(device));
106}
107
108static int crypt_enabled(struct tape_device *device)
109{
110 return TAPE390_CRYPT_ON(TAPE_3590_CRYPT_INFO(device));
111}
112
113static void ext_to_int_kekl(struct tape390_kekl *in,
114 struct tape3592_kekl *out)
115{
116 int i;
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 strncpy(out->label, in->label, 64);
124 for (i = strlen(in->label); i < sizeof(out->label); i++)
125 out->label[i] = ' ';
126 ASCEBC(out->label, sizeof(out->label));
127}
128
129static 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));
Heiko Carstens1d802e22009-12-18 17:43:27 +0100143 strim(out->label);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100144}
145
146static 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
167static 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;
182invalid:
183 return -EINVAL;
184}
185
186static 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;
194invalid:
195 return -EINVAL;
196}
197
198/*
199 * Query KEKLs
200 */
201static 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;
232fail_request:
233 tape_free_request(request);
234fail_malloc:
235 kfree(int_kekls);
236 return rc;
237}
238
239/*
240 * IOCTL: Query KEKLs
241 */
242static 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;
264fail:
265 kfree(ext_kekls);
266 return rc;
267}
268
269static int tape_3590_mttell(struct tape_device *device, int mt_count);
270
271/*
272 * Set KEKLs
273 */
274static 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 */
305static 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 = kmalloc(sizeof(*ext_kekls), GFP_KERNEL);
317 if (!ext_kekls)
318 return -ENOMEM;
319 if (copy_from_user(ext_kekls, (char __user *)arg, sizeof(*ext_kekls))) {
320 rc = -EFAULT;
321 goto out;
322 }
323 rc = tape_3592_kekl_set(device, ext_kekls);
324out:
325 kfree(ext_kekls);
326 return rc;
327}
328
329/*
330 * Enable encryption
331 */
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100332static struct tape_request *__tape_3592_enable_crypt(struct tape_device *device)
Michael Holzheucced1dd2007-02-05 21:18:26 +0100333{
334 struct tape_request *request;
335 char *data;
336
337 DBF_EVENT(6, "tape_3592_enable_crypt\n");
338 if (!crypt_supported(device))
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100339 return ERR_PTR(-ENOSYS);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100340 request = tape_alloc_request(2, 72);
341 if (IS_ERR(request))
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100342 return request;
Michael Holzheucced1dd2007-02-05 21:18:26 +0100343 data = request->cpdata;
344 memset(data,0,72);
345
346 data[0] = 0x05;
347 data[36 + 0] = 0x03;
348 data[36 + 1] = 0x03;
349 data[36 + 4] = 0x40;
350 data[36 + 6] = 0x01;
351 data[36 + 14] = 0x2f;
352 data[36 + 18] = 0xc3;
353 data[36 + 35] = 0x72;
354 request->op = TO_CRYPT_ON;
355 tape_ccw_cc(request->cpaddr, MODE_SET_CB, 36, data);
356 tape_ccw_end(request->cpaddr + 1, MODE_SET_CB, 36, data + 36);
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100357 return request;
358}
359
360static int tape_3592_enable_crypt(struct tape_device *device)
361{
362 struct tape_request *request;
363
364 request = __tape_3592_enable_crypt(device);
365 if (IS_ERR(request))
366 return PTR_ERR(request);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100367 return tape_do_io_free(device, request);
368}
369
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100370static void tape_3592_enable_crypt_async(struct tape_device *device)
371{
372 struct tape_request *request;
373
374 request = __tape_3592_enable_crypt(device);
375 if (!IS_ERR(request))
376 tape_do_io_async_free(device, request);
377}
378
Michael Holzheucced1dd2007-02-05 21:18:26 +0100379/*
380 * Disable encryption
381 */
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100382static struct tape_request *__tape_3592_disable_crypt(struct tape_device *device)
Michael Holzheucced1dd2007-02-05 21:18:26 +0100383{
384 struct tape_request *request;
385 char *data;
386
387 DBF_EVENT(6, "tape_3592_disable_crypt\n");
388 if (!crypt_supported(device))
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100389 return ERR_PTR(-ENOSYS);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100390 request = tape_alloc_request(2, 72);
391 if (IS_ERR(request))
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100392 return request;
Michael Holzheucced1dd2007-02-05 21:18:26 +0100393 data = request->cpdata;
394 memset(data,0,72);
395
396 data[0] = 0x05;
397 data[36 + 0] = 0x03;
398 data[36 + 1] = 0x03;
399 data[36 + 35] = 0x32;
400
401 request->op = TO_CRYPT_OFF;
402 tape_ccw_cc(request->cpaddr, MODE_SET_CB, 36, data);
403 tape_ccw_end(request->cpaddr + 1, MODE_SET_CB, 36, data + 36);
404
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100405 return request;
406}
407
408static int tape_3592_disable_crypt(struct tape_device *device)
409{
410 struct tape_request *request;
411
412 request = __tape_3592_disable_crypt(device);
413 if (IS_ERR(request))
414 return PTR_ERR(request);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100415 return tape_do_io_free(device, request);
416}
417
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100418static void tape_3592_disable_crypt_async(struct tape_device *device)
419{
420 struct tape_request *request;
421
422 request = __tape_3592_disable_crypt(device);
423 if (!IS_ERR(request))
424 tape_do_io_async_free(device, request);
425}
426
Michael Holzheucced1dd2007-02-05 21:18:26 +0100427/*
428 * IOCTL: Set encryption status
429 */
430static int tape_3592_ioctl_crypt_set(struct tape_device *device,
431 unsigned long arg)
432{
433 struct tape390_crypt_info info;
434
435 DBF_EVENT(6, "tape_3592_ioctl_crypt_set\n");
436 if (!crypt_supported(device))
437 return -ENOSYS;
438 if (copy_from_user(&info, (char __user *)arg, sizeof(info)))
439 return -EFAULT;
440 if (info.status & ~TAPE390_CRYPT_ON_MASK)
441 return -EINVAL;
442 if (info.status & TAPE390_CRYPT_ON_MASK)
443 return tape_3592_enable_crypt(device);
444 else
445 return tape_3592_disable_crypt(device);
446}
447
448static int tape_3590_sense_medium(struct tape_device *device);
449
450/*
451 * IOCTL: Query enryption status
452 */
453static int tape_3592_ioctl_crypt_query(struct tape_device *device,
454 unsigned long arg)
455{
456 DBF_EVENT(6, "tape_3592_ioctl_crypt_query\n");
457 if (!crypt_supported(device))
458 return -ENOSYS;
459 tape_3590_sense_medium(device);
460 if (copy_to_user((char __user *) arg, &TAPE_3590_CRYPT_INFO(device),
461 sizeof(TAPE_3590_CRYPT_INFO(device))))
462 return -EFAULT;
463 else
464 return 0;
465}
466
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800467/*
468 * 3590 IOCTL Overload
469 */
470static int
471tape_3590_ioctl(struct tape_device *device, unsigned int cmd, unsigned long arg)
472{
473 switch (cmd) {
474 case TAPE390_DISPLAY: {
475 struct display_struct disp;
476
477 if (copy_from_user(&disp, (char __user *) arg, sizeof(disp)))
478 return -EFAULT;
479
480 return tape_std_display(device, &disp);
481 }
Michael Holzheucced1dd2007-02-05 21:18:26 +0100482 case TAPE390_KEKL_SET:
483 return tape_3592_ioctl_kekl_set(device, arg);
484 case TAPE390_KEKL_QUERY:
485 return tape_3592_ioctl_kekl_query(device, arg);
486 case TAPE390_CRYPT_SET:
487 return tape_3592_ioctl_crypt_set(device, arg);
488 case TAPE390_CRYPT_QUERY:
489 return tape_3592_ioctl_crypt_query(device, arg);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800490 default:
491 return -EINVAL; /* no additional ioctls */
492 }
493}
494
495/*
496 * SENSE Medium: Get Sense data about medium state
497 */
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100498static int tape_3590_sense_medium(struct tape_device *device)
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800499{
500 struct tape_request *request;
501
502 request = tape_alloc_request(1, 128);
503 if (IS_ERR(request))
504 return PTR_ERR(request);
505 request->op = TO_MSEN;
506 tape_ccw_end(request->cpaddr, MEDIUM_SENSE, 128, request->cpdata);
507 return tape_do_io_free(device, request);
508}
509
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100510static void tape_3590_sense_medium_async(struct tape_device *device)
511{
512 struct tape_request *request;
513
514 request = tape_alloc_request(1, 128);
515 if (IS_ERR(request))
516 return;
517 request->op = TO_MSEN;
518 tape_ccw_end(request->cpaddr, MEDIUM_SENSE, 128, request->cpdata);
519 tape_do_io_async_free(device, request);
520}
521
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800522/*
523 * MTTELL: Tell block. Return the number of block relative to current file.
524 */
525static int
526tape_3590_mttell(struct tape_device *device, int mt_count)
527{
528 __u64 block_id;
529 int rc;
530
531 rc = tape_std_read_block_id(device, &block_id);
532 if (rc)
533 return rc;
534 return block_id >> 32;
535}
536
537/*
538 * MTSEEK: seek to the specified block.
539 */
540static int
541tape_3590_mtseek(struct tape_device *device, int count)
542{
543 struct tape_request *request;
544
545 DBF_EVENT(6, "xsee id: %x\n", count);
546 request = tape_alloc_request(3, 4);
547 if (IS_ERR(request))
548 return PTR_ERR(request);
549 request->op = TO_LBL;
550 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
551 *(__u32 *) request->cpdata = count;
552 tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
553 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
554 return tape_do_io_free(device, request);
555}
556
557/*
558 * Read Opposite Error Recovery Function:
559 * Used, when Read Forward does not work
560 */
561static void
562tape_3590_read_opposite(struct tape_device *device,
563 struct tape_request *request)
564{
565 struct tape_3590_disc_data *data;
566
567 /*
568 * We have allocated 4 ccws in tape_std_read, so we can now
569 * transform the request to a read backward, followed by a
570 * forward space block.
571 */
572 request->op = TO_RBA;
573 tape_ccw_cc(request->cpaddr, MODE_SET_DB, 1, device->modeset_byte);
574 data = device->discdata;
575 tape_ccw_cc_idal(request->cpaddr + 1, data->read_back_op,
576 device->char_data.idal_buf);
577 tape_ccw_cc(request->cpaddr + 2, FORSPACEBLOCK, 0, NULL);
578 tape_ccw_end(request->cpaddr + 3, NOP, 0, NULL);
579 DBF_EVENT(6, "xrop ccwg\n");
580}
581
582/*
583 * Read Attention Msg
584 * This should be done after an interrupt with attention bit (0x80)
585 * in device state.
586 *
587 * After a "read attention message" request there are two possible
588 * results:
589 *
590 * 1. A unit check is presented, when attention sense is present (e.g. when
591 * a medium has been unloaded). The attention sense comes then
592 * together with the unit check. The recovery action is either "retry"
593 * (in case there is an attention message pending) or "permanent error".
594 *
595 * 2. The attention msg is written to the "read subsystem data" buffer.
596 * In this case we probably should print it to the console.
597 */
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100598static void tape_3590_read_attmsg_async(struct tape_device *device)
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800599{
600 struct tape_request *request;
601 char *buf;
602
603 request = tape_alloc_request(3, 4096);
604 if (IS_ERR(request))
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100605 return;
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800606 request->op = TO_READ_ATTMSG;
607 buf = request->cpdata;
608 buf[0] = PREP_RD_SS_DATA;
609 buf[6] = RD_ATTMSG; /* read att msg */
610 tape_ccw_cc(request->cpaddr, PERFORM_SS_FUNC, 12, buf);
611 tape_ccw_cc(request->cpaddr + 1, READ_SS_DATA, 4096 - 12, buf + 12);
612 tape_ccw_end(request->cpaddr + 2, NOP, 0, NULL);
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100613 tape_do_io_async_free(device, request);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800614}
615
616/*
617 * These functions are used to schedule follow-up actions from within an
618 * interrupt context (like unsolicited interrupts).
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100619 * Note: the work handler is called by the system work queue. The tape
620 * commands started by the handler need to be asynchrounous, otherwise
621 * a deadlock can occur e.g. in case of a deferred cc=1 (see __tape_do_irq).
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800622 */
Stefan Bader2cc924b2006-04-27 18:40:16 -0700623struct work_handler_data {
624 struct tape_device *device;
625 enum tape_op op;
626 struct work_struct work;
627};
628
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800629static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100630tape_3590_work_handler(struct work_struct *work)
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800631{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100632 struct work_handler_data *p =
633 container_of(work, struct work_handler_data, work);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800634
635 switch (p->op) {
636 case TO_MSEN:
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100637 tape_3590_sense_medium_async(p->device);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800638 break;
639 case TO_READ_ATTMSG:
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100640 tape_3590_read_attmsg_async(p->device);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800641 break;
Michael Holzheucced1dd2007-02-05 21:18:26 +0100642 case TO_CRYPT_ON:
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100643 tape_3592_enable_crypt_async(p->device);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100644 break;
645 case TO_CRYPT_OFF:
Martin Schwidefsky0c2bd9b2011-03-03 17:56:07 +0100646 tape_3592_disable_crypt_async(p->device);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100647 break;
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800648 default:
649 DBF_EVENT(3, "T3590: work handler undefined for "
650 "operation 0x%02x\n", p->op);
651 }
652 tape_put_device(p->device);
653 kfree(p);
654}
655
656static int
657tape_3590_schedule_work(struct tape_device *device, enum tape_op op)
658{
Stefan Bader2cc924b2006-04-27 18:40:16 -0700659 struct work_handler_data *p;
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800660
661 if ((p = kzalloc(sizeof(*p), GFP_ATOMIC)) == NULL)
662 return -ENOMEM;
663
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100664 INIT_WORK(&p->work, tape_3590_work_handler);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800665
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100666 p->device = tape_get_device(device);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800667 p->op = op;
668
Tejun Heo6dca4672010-12-24 16:00:18 +0100669 queue_work(tape_3590_wq, &p->work);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800670 return 0;
671}
672
673#ifdef CONFIG_S390_TAPE_BLOCK
674/*
675 * Tape Block READ
676 */
677static struct tape_request *
678tape_3590_bread(struct tape_device *device, struct request *req)
679{
680 struct tape_request *request;
681 struct ccw1 *ccw;
NeilBrown5705f702007-09-25 12:35:59 +0200682 int count = 0, start_block;
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800683 unsigned off;
684 char *dst;
685 struct bio_vec *bv;
NeilBrown5705f702007-09-25 12:35:59 +0200686 struct req_iterator iter;
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800687
688 DBF_EVENT(6, "xBREDid:");
Tejun Heo83096eb2009-05-07 22:24:39 +0900689 start_block = blk_rq_pos(req) >> TAPEBLOCK_HSEC_S2B;
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800690 DBF_EVENT(6, "start_block = %i\n", start_block);
691
NeilBrown5705f702007-09-25 12:35:59 +0200692 rq_for_each_segment(bv, req, iter)
693 count += bv->bv_len >> (TAPEBLOCK_HSEC_S2B + 9);
694
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800695 request = tape_alloc_request(2 + count + 1, 4);
696 if (IS_ERR(request))
697 return request;
698 request->op = TO_BLOCK;
699 *(__u32 *) request->cpdata = start_block;
700 ccw = request->cpaddr;
701 ccw = tape_ccw_cc(ccw, MODE_SET_DB, 1, device->modeset_byte);
702
703 /*
704 * We always setup a nop after the mode set ccw. This slot is
705 * used in tape_std_check_locate to insert a locate ccw if the
706 * current tape position doesn't match the start block to be read.
707 */
708 ccw = tape_ccw_cc(ccw, NOP, 0, NULL);
709
NeilBrown5705f702007-09-25 12:35:59 +0200710 rq_for_each_segment(bv, req, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200711 dst = page_address(bv->bv_page) + bv->bv_offset;
712 for (off = 0; off < bv->bv_len; off += TAPEBLOCK_HSEC_SIZE) {
713 ccw->flags = CCW_FLAG_CC;
714 ccw->cmd_code = READ_FORWARD;
715 ccw->count = TAPEBLOCK_HSEC_SIZE;
716 set_normalized_cda(ccw, (void *) __pa(dst));
717 ccw++;
718 dst += TAPEBLOCK_HSEC_SIZE;
719 }
Stoyan Gaydarov6aa0d3a2009-03-26 15:24:47 +0100720 BUG_ON(off > bv->bv_len);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800721 }
722 ccw = tape_ccw_end(ccw, NOP, 0, NULL);
723 DBF_EVENT(6, "xBREDccwg\n");
724 return request;
725}
726
727static void
728tape_3590_free_bread(struct tape_request *request)
729{
730 struct ccw1 *ccw;
731
732 /* Last ccw is a nop and doesn't need clear_normalized_cda */
733 for (ccw = request->cpaddr; ccw->flags & CCW_FLAG_CC; ccw++)
734 if (ccw->cmd_code == READ_FORWARD)
735 clear_normalized_cda(ccw);
736 tape_free_request(request);
737}
738
739/*
740 * check_locate is called just before the tape request is passed to
741 * the common io layer for execution. It has to check the current
742 * tape position and insert a locate ccw if it doesn't match the
743 * start block for the request.
744 */
745static void
746tape_3590_check_locate(struct tape_device *device, struct tape_request *request)
747{
748 __u32 *start_block;
749
750 start_block = (__u32 *) request->cpdata;
751 if (*start_block != device->blk_data.block_position) {
752 /* Add the start offset of the file to get the real block. */
753 *start_block += device->bof;
754 tape_ccw_cc(request->cpaddr + 1, LOCATE, 4, request->cpdata);
755 }
756}
757#endif
758
Michael Holzheucced1dd2007-02-05 21:18:26 +0100759static void tape_3590_med_state_set(struct tape_device *device,
760 struct tape_3590_med_sense *sense)
761{
762 struct tape390_crypt_info *c_info;
763
764 c_info = &TAPE_3590_CRYPT_INFO(device);
765
Michael Holzheu1b9fd762007-10-12 16:11:47 +0200766 DBF_EVENT(6, "medium state: %x:%x\n", sense->macst, sense->masst);
767 switch (sense->macst) {
768 case 0x04:
769 case 0x05:
770 case 0x06:
Michael Holzheucced1dd2007-02-05 21:18:26 +0100771 tape_med_state_set(device, MS_UNLOADED);
772 TAPE_3590_CRYPT_INFO(device).medium_status = 0;
773 return;
Michael Holzheu1b9fd762007-10-12 16:11:47 +0200774 case 0x08:
775 case 0x09:
776 tape_med_state_set(device, MS_LOADED);
777 break;
778 default:
779 tape_med_state_set(device, MS_UNKNOWN);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100780 return;
781 }
Michael Holzheucced1dd2007-02-05 21:18:26 +0100782 c_info->medium_status |= TAPE390_MEDIUM_LOADED_MASK;
783 if (sense->flags & MSENSE_CRYPT_MASK) {
Carsten Otteab640db2009-03-26 15:24:38 +0100784 DBF_EVENT(6, "Medium is encrypted (%04x)\n", sense->flags);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100785 c_info->medium_status |= TAPE390_MEDIUM_ENCRYPTED_MASK;
786 } else {
787 DBF_EVENT(6, "Medium is not encrypted %04x\n", sense->flags);
788 c_info->medium_status &= ~TAPE390_MEDIUM_ENCRYPTED_MASK;
789 }
790}
791
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800792/*
793 * The done handler is called at device/channel end and wakes up the sleeping
794 * process
795 */
796static int
797tape_3590_done(struct tape_device *device, struct tape_request *request)
798{
Michael Holzheucced1dd2007-02-05 21:18:26 +0100799 struct tape_3590_disc_data *disc_data;
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800800
801 DBF_EVENT(6, "%s done\n", tape_op_verbose[request->op]);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100802 disc_data = device->discdata;
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800803
804 switch (request->op) {
805 case TO_BSB:
806 case TO_BSF:
807 case TO_DSE:
808 case TO_FSB:
809 case TO_FSF:
810 case TO_LBL:
811 case TO_RFO:
812 case TO_RBA:
813 case TO_REW:
814 case TO_WRI:
815 case TO_WTM:
816 case TO_BLOCK:
817 case TO_LOAD:
818 tape_med_state_set(device, MS_LOADED);
819 break;
820 case TO_RUN:
821 tape_med_state_set(device, MS_UNLOADED);
Michael Holzheucced1dd2007-02-05 21:18:26 +0100822 tape_3590_schedule_work(device, TO_CRYPT_OFF);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800823 break;
824 case TO_MSEN:
Michael Holzheucced1dd2007-02-05 21:18:26 +0100825 tape_3590_med_state_set(device, request->cpdata);
826 break;
827 case TO_CRYPT_ON:
828 TAPE_3590_CRYPT_INFO(device).status
829 |= TAPE390_CRYPT_ON_MASK;
830 *(device->modeset_byte) |= 0x03;
831 break;
832 case TO_CRYPT_OFF:
833 TAPE_3590_CRYPT_INFO(device).status
834 &= ~TAPE390_CRYPT_ON_MASK;
835 *(device->modeset_byte) &= ~0x03;
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800836 break;
837 case TO_RBI: /* RBI seems to succeed even without medium loaded. */
838 case TO_NOP: /* Same to NOP. */
839 case TO_READ_CONFIG:
840 case TO_READ_ATTMSG:
841 case TO_DIS:
842 case TO_ASSIGN:
843 case TO_UNASSIGN:
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800844 case TO_SIZE:
Michael Holzheucced1dd2007-02-05 21:18:26 +0100845 case TO_KEKL_SET:
846 case TO_KEKL_QUERY:
Michael Holzheue2963062007-05-04 18:47:53 +0200847 case TO_RDC:
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800848 break;
849 }
850 return TAPE_IO_SUCCESS;
851}
852
853/*
Stefan Weileef35c22010-08-06 21:11:15 +0200854 * This function is called, when error recovery was successful
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800855 */
856static inline int
857tape_3590_erp_succeded(struct tape_device *device, struct tape_request *request)
858{
Coly Li73ac36e2009-01-07 18:09:16 -0800859 DBF_EVENT(3, "Error Recovery successful for %s\n",
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800860 tape_op_verbose[request->op]);
861 return tape_3590_done(device, request);
862}
863
864/*
Stefan Weileef35c22010-08-06 21:11:15 +0200865 * This function is called, when error recovery was not successful
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800866 */
867static inline int
868tape_3590_erp_failed(struct tape_device *device, struct tape_request *request,
869 struct irb *irb, int rc)
870{
871 DBF_EVENT(3, "Error Recovery failed for %s\n",
872 tape_op_verbose[request->op]);
873 tape_dump_sense_dbf(device, request, irb);
874 return rc;
875}
876
877/*
878 * Error Recovery do retry
879 */
880static inline int
881tape_3590_erp_retry(struct tape_device *device, struct tape_request *request,
882 struct irb *irb)
883{
884 DBF_EVENT(2, "Retry: %s\n", tape_op_verbose[request->op]);
885 tape_dump_sense_dbf(device, request, irb);
886 return TAPE_IO_RETRY;
887}
888
889/*
890 * Handle unsolicited interrupts
891 */
892static int
893tape_3590_unsolicited_irq(struct tape_device *device, struct irb *irb)
894{
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200895 if (irb->scsw.cmd.dstat == DEV_STAT_CHN_END)
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800896 /* Probably result of halt ssch */
897 return TAPE_IO_PENDING;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200898 else if (irb->scsw.cmd.dstat == 0x85)
Michael Holzheu1b9fd762007-10-12 16:11:47 +0200899 /* Device Ready */
900 DBF_EVENT(3, "unsol.irq! tape ready: %08x\n", device->cdev_id);
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200901 else if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800902 tape_3590_schedule_work(device, TO_READ_ATTMSG);
Michael Holzheu1b9fd762007-10-12 16:11:47 +0200903 } else {
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800904 DBF_EVENT(3, "unsol.irq! dev end: %08x\n", device->cdev_id);
Carsten Otteab640db2009-03-26 15:24:38 +0100905 tape_dump_sense_dbf(device, NULL, irb);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800906 }
Michael Holzheu1b9fd762007-10-12 16:11:47 +0200907 /* check medium state */
908 tape_3590_schedule_work(device, TO_MSEN);
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800909 return TAPE_IO_SUCCESS;
910}
911
912/*
913 * Basic Recovery routine
914 */
915static int
916tape_3590_erp_basic(struct tape_device *device, struct tape_request *request,
917 struct irb *irb, int rc)
918{
919 struct tape_3590_sense *sense;
920
921 sense = (struct tape_3590_sense *) irb->ecw;
922
923 switch (sense->bra) {
924 case SENSE_BRA_PER:
925 return tape_3590_erp_failed(device, request, irb, rc);
926 case SENSE_BRA_CONT:
927 return tape_3590_erp_succeded(device, request);
928 case SENSE_BRA_RE:
929 return tape_3590_erp_retry(device, request, irb);
930 case SENSE_BRA_DRE:
931 return tape_3590_erp_failed(device, request, irb, rc);
932 default:
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800933 BUG();
934 return TAPE_IO_STOP;
935 }
936}
937
938/*
939 * RDL: Read Device (buffered) log
940 */
941static int
942tape_3590_erp_read_buf_log(struct tape_device *device,
943 struct tape_request *request, struct irb *irb)
944{
945 /*
946 * We just do the basic error recovery at the moment (retry).
947 * Perhaps in the future, we read the log and dump it somewhere...
948 */
949 return tape_3590_erp_basic(device, request, irb, -EIO);
950}
951
952/*
953 * SWAP: Swap Devices
954 */
955static int
956tape_3590_erp_swap(struct tape_device *device, struct tape_request *request,
957 struct irb *irb)
958{
959 /*
960 * This error recovery should swap the tapes
961 * if the original has a problem. The operation
962 * should proceed with the new tape... this
963 * should probably be done in user space!
964 */
Carsten Otteab640db2009-03-26 15:24:38 +0100965 dev_warn (&device->cdev->dev, "The tape medium must be loaded into a "
966 "different tape unit\n");
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800967 return tape_3590_erp_basic(device, request, irb, -EIO);
968}
969
970/*
971 * LBY: Long Busy
972 */
973static int
974tape_3590_erp_long_busy(struct tape_device *device,
975 struct tape_request *request, struct irb *irb)
976{
Michael Holzheucced1dd2007-02-05 21:18:26 +0100977 DBF_EVENT(6, "Device is busy\n");
978 return TAPE_IO_LONG_BUSY;
Stefan Baderb6cba4e2006-03-24 03:15:29 -0800979}
980
981/*
982 * SPI: Special Intercept
983 */
984static int
985tape_3590_erp_special_interrupt(struct tape_device *device,
986 struct tape_request *request, struct irb *irb)
987{
988 return tape_3590_erp_basic(device, request, irb, -EIO);
989}
990
991/*
992 * RDA: Read Alternate
993 */
994static int
995tape_3590_erp_read_alternate(struct tape_device *device,
996 struct tape_request *request, struct irb *irb)
997{
998 struct tape_3590_disc_data *data;
999
1000 /*
1001 * The issued Read Backward or Read Previous command is not
1002 * supported by the device
1003 * The recovery action should be to issue another command:
1004 * Read Revious: if Read Backward is not supported
1005 * Read Backward: if Read Previous is not supported
1006 */
1007 data = device->discdata;
1008 if (data->read_back_op == READ_PREVIOUS) {
1009 DBF_EVENT(2, "(%08x): No support for READ_PREVIOUS command\n",
1010 device->cdev_id);
1011 data->read_back_op = READ_BACKWARD;
1012 } else {
1013 DBF_EVENT(2, "(%08x): No support for READ_BACKWARD command\n",
1014 device->cdev_id);
1015 data->read_back_op = READ_PREVIOUS;
1016 }
1017 tape_3590_read_opposite(device, request);
1018 return tape_3590_erp_retry(device, request, irb);
1019}
1020
1021/*
1022 * Error Recovery read opposite
1023 */
1024static int
1025tape_3590_erp_read_opposite(struct tape_device *device,
1026 struct tape_request *request, struct irb *irb)
1027{
1028 switch (request->op) {
1029 case TO_RFO:
1030 /*
1031 * We did read forward, but the data could not be read.
1032 * We will read backward and then skip forward again.
1033 */
1034 tape_3590_read_opposite(device, request);
1035 return tape_3590_erp_retry(device, request, irb);
1036 case TO_RBA:
1037 /* We tried to read forward and backward, but hat no success */
1038 return tape_3590_erp_failed(device, request, irb, -EIO);
1039 break;
1040 default:
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001041 return tape_3590_erp_failed(device, request, irb, -EIO);
1042 }
1043}
1044
1045/*
1046 * Print an MIM (Media Information Message) (message code f0)
1047 */
1048static void
1049tape_3590_print_mim_msg_f0(struct tape_device *device, struct irb *irb)
1050{
1051 struct tape_3590_sense *sense;
Carsten Otteab640db2009-03-26 15:24:38 +01001052 char *exception, *service;
1053
1054 exception = kmalloc(BUFSIZE, GFP_ATOMIC);
1055 service = kmalloc(BUFSIZE, GFP_ATOMIC);
1056
1057 if (!exception || !service)
1058 goto out_nomem;
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001059
1060 sense = (struct tape_3590_sense *) irb->ecw;
1061 /* Exception Message */
1062 switch (sense->fmt.f70.emc) {
1063 case 0x02:
Carsten Otteab640db2009-03-26 15:24:38 +01001064 snprintf(exception, BUFSIZE, "Data degraded");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001065 break;
1066 case 0x03:
Carsten Otteab640db2009-03-26 15:24:38 +01001067 snprintf(exception, BUFSIZE, "Data degraded in partion %i",
1068 sense->fmt.f70.mp);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001069 break;
1070 case 0x04:
Carsten Otteab640db2009-03-26 15:24:38 +01001071 snprintf(exception, BUFSIZE, "Medium degraded");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001072 break;
1073 case 0x05:
Carsten Otteab640db2009-03-26 15:24:38 +01001074 snprintf(exception, BUFSIZE, "Medium degraded in partition %i",
1075 sense->fmt.f70.mp);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001076 break;
1077 case 0x06:
Carsten Otteab640db2009-03-26 15:24:38 +01001078 snprintf(exception, BUFSIZE, "Block 0 Error");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001079 break;
1080 case 0x07:
Carsten Otteab640db2009-03-26 15:24:38 +01001081 snprintf(exception, BUFSIZE, "Medium Exception 0x%02x",
1082 sense->fmt.f70.md);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001083 break;
1084 default:
Carsten Otteab640db2009-03-26 15:24:38 +01001085 snprintf(exception, BUFSIZE, "0x%02x",
1086 sense->fmt.f70.emc);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001087 break;
1088 }
1089 /* Service Message */
1090 switch (sense->fmt.f70.smc) {
1091 case 0x02:
Carsten Otteab640db2009-03-26 15:24:38 +01001092 snprintf(service, BUFSIZE, "Reference Media maintenance "
1093 "procedure %i", sense->fmt.f70.md);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001094 break;
1095 default:
Carsten Otteab640db2009-03-26 15:24:38 +01001096 snprintf(service, BUFSIZE, "0x%02x",
1097 sense->fmt.f70.smc);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001098 break;
1099 }
Carsten Otteab640db2009-03-26 15:24:38 +01001100
1101 dev_warn (&device->cdev->dev, "Tape media information: exception %s, "
1102 "service %s\n", exception, service);
1103
1104out_nomem:
1105 kfree(exception);
1106 kfree(service);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001107}
1108
1109/*
1110 * Print an I/O Subsystem Service Information Message (message code f1)
1111 */
1112static void
1113tape_3590_print_io_sim_msg_f1(struct tape_device *device, struct irb *irb)
1114{
1115 struct tape_3590_sense *sense;
Carsten Otteab640db2009-03-26 15:24:38 +01001116 char *exception, *service;
1117
1118 exception = kmalloc(BUFSIZE, GFP_ATOMIC);
1119 service = kmalloc(BUFSIZE, GFP_ATOMIC);
1120
1121 if (!exception || !service)
1122 goto out_nomem;
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001123
1124 sense = (struct tape_3590_sense *) irb->ecw;
1125 /* Exception Message */
1126 switch (sense->fmt.f71.emc) {
1127 case 0x01:
Carsten Otteab640db2009-03-26 15:24:38 +01001128 snprintf(exception, BUFSIZE, "Effect of failure is unknown");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001129 break;
1130 case 0x02:
Carsten Otteab640db2009-03-26 15:24:38 +01001131 snprintf(exception, BUFSIZE, "CU Exception - no performance "
1132 "impact");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001133 break;
1134 case 0x03:
Carsten Otteab640db2009-03-26 15:24:38 +01001135 snprintf(exception, BUFSIZE, "CU Exception on channel "
1136 "interface 0x%02x", sense->fmt.f71.md[0]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001137 break;
1138 case 0x04:
Carsten Otteab640db2009-03-26 15:24:38 +01001139 snprintf(exception, BUFSIZE, "CU Exception on device path "
1140 "0x%02x", sense->fmt.f71.md[0]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001141 break;
1142 case 0x05:
Carsten Otteab640db2009-03-26 15:24:38 +01001143 snprintf(exception, BUFSIZE, "CU Exception on library path "
1144 "0x%02x", sense->fmt.f71.md[0]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001145 break;
1146 case 0x06:
Carsten Otteab640db2009-03-26 15:24:38 +01001147 snprintf(exception, BUFSIZE, "CU Exception on node 0x%02x",
1148 sense->fmt.f71.md[0]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001149 break;
1150 case 0x07:
Carsten Otteab640db2009-03-26 15:24:38 +01001151 snprintf(exception, BUFSIZE, "CU Exception on partition "
1152 "0x%02x", sense->fmt.f71.md[0]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001153 break;
1154 default:
Carsten Otteab640db2009-03-26 15:24:38 +01001155 snprintf(exception, BUFSIZE, "0x%02x",
1156 sense->fmt.f71.emc);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001157 }
1158 /* Service Message */
1159 switch (sense->fmt.f71.smc) {
1160 case 0x01:
Carsten Otteab640db2009-03-26 15:24:38 +01001161 snprintf(service, BUFSIZE, "Repair impact is unknown");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001162 break;
1163 case 0x02:
Carsten Otteab640db2009-03-26 15:24:38 +01001164 snprintf(service, BUFSIZE, "Repair will not impact cu "
1165 "performance");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001166 break;
1167 case 0x03:
1168 if (sense->fmt.f71.mdf == 0)
Carsten Otteab640db2009-03-26 15:24:38 +01001169 snprintf(service, BUFSIZE, "Repair will disable node "
1170 "0x%x on CU", sense->fmt.f71.md[1]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001171 else
Carsten Otteab640db2009-03-26 15:24:38 +01001172 snprintf(service, BUFSIZE, "Repair will disable "
1173 "nodes (0x%x-0x%x) on CU", sense->fmt.f71.md[1],
1174 sense->fmt.f71.md[2]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001175 break;
1176 case 0x04:
1177 if (sense->fmt.f71.mdf == 0)
Carsten Otteab640db2009-03-26 15:24:38 +01001178 snprintf(service, BUFSIZE, "Repair will disable "
1179 "channel path 0x%x on CU",
1180 sense->fmt.f71.md[1]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001181 else
Carsten Otteab640db2009-03-26 15:24:38 +01001182 snprintf(service, BUFSIZE, "Repair will disable cannel"
1183 " paths (0x%x-0x%x) on CU",
1184 sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001185 break;
1186 case 0x05:
1187 if (sense->fmt.f71.mdf == 0)
Carsten Otteab640db2009-03-26 15:24:38 +01001188 snprintf(service, BUFSIZE, "Repair will disable device"
1189 " path 0x%x on CU", sense->fmt.f71.md[1]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001190 else
Carsten Otteab640db2009-03-26 15:24:38 +01001191 snprintf(service, BUFSIZE, "Repair will disable device"
1192 " paths (0x%x-0x%x) on CU",
1193 sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001194 break;
1195 case 0x06:
1196 if (sense->fmt.f71.mdf == 0)
Carsten Otteab640db2009-03-26 15:24:38 +01001197 snprintf(service, BUFSIZE, "Repair will disable "
1198 "library path 0x%x on CU",
1199 sense->fmt.f71.md[1]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001200 else
Carsten Otteab640db2009-03-26 15:24:38 +01001201 snprintf(service, BUFSIZE, "Repair will disable "
1202 "library paths (0x%x-0x%x) on CU",
1203 sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001204 break;
1205 case 0x07:
Carsten Otteab640db2009-03-26 15:24:38 +01001206 snprintf(service, BUFSIZE, "Repair will disable access to CU");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001207 break;
1208 default:
Carsten Otteab640db2009-03-26 15:24:38 +01001209 snprintf(service, BUFSIZE, "0x%02x",
1210 sense->fmt.f71.smc);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001211 }
Carsten Otteab640db2009-03-26 15:24:38 +01001212
1213 dev_warn (&device->cdev->dev, "I/O subsystem information: exception"
1214 " %s, service %s\n", exception, service);
1215out_nomem:
1216 kfree(exception);
1217 kfree(service);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001218}
1219
1220/*
1221 * Print an Device Subsystem Service Information Message (message code f2)
1222 */
1223static void
1224tape_3590_print_dev_sim_msg_f2(struct tape_device *device, struct irb *irb)
1225{
1226 struct tape_3590_sense *sense;
Carsten Otteab640db2009-03-26 15:24:38 +01001227 char *exception, *service;
1228
1229 exception = kmalloc(BUFSIZE, GFP_ATOMIC);
1230 service = kmalloc(BUFSIZE, GFP_ATOMIC);
1231
1232 if (!exception || !service)
1233 goto out_nomem;
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001234
1235 sense = (struct tape_3590_sense *) irb->ecw;
1236 /* Exception Message */
1237 switch (sense->fmt.f71.emc) {
1238 case 0x01:
Carsten Otteab640db2009-03-26 15:24:38 +01001239 snprintf(exception, BUFSIZE, "Effect of failure is unknown");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001240 break;
1241 case 0x02:
Carsten Otteab640db2009-03-26 15:24:38 +01001242 snprintf(exception, BUFSIZE, "DV Exception - no performance"
1243 " impact");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001244 break;
1245 case 0x03:
Carsten Otteab640db2009-03-26 15:24:38 +01001246 snprintf(exception, BUFSIZE, "DV Exception on channel "
1247 "interface 0x%02x", sense->fmt.f71.md[0]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001248 break;
1249 case 0x04:
Carsten Otteab640db2009-03-26 15:24:38 +01001250 snprintf(exception, BUFSIZE, "DV Exception on loader 0x%02x",
1251 sense->fmt.f71.md[0]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001252 break;
1253 case 0x05:
Carsten Otteab640db2009-03-26 15:24:38 +01001254 snprintf(exception, BUFSIZE, "DV Exception on message display"
1255 " 0x%02x", sense->fmt.f71.md[0]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001256 break;
1257 case 0x06:
Carsten Otteab640db2009-03-26 15:24:38 +01001258 snprintf(exception, BUFSIZE, "DV Exception in tape path");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001259 break;
1260 case 0x07:
Carsten Otteab640db2009-03-26 15:24:38 +01001261 snprintf(exception, BUFSIZE, "DV Exception in drive");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001262 break;
1263 default:
Carsten Otteab640db2009-03-26 15:24:38 +01001264 snprintf(exception, BUFSIZE, "0x%02x",
1265 sense->fmt.f71.emc);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001266 }
1267 /* Service Message */
1268 switch (sense->fmt.f71.smc) {
1269 case 0x01:
Carsten Otteab640db2009-03-26 15:24:38 +01001270 snprintf(service, BUFSIZE, "Repair impact is unknown");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001271 break;
1272 case 0x02:
Carsten Otteab640db2009-03-26 15:24:38 +01001273 snprintf(service, BUFSIZE, "Repair will not impact device "
1274 "performance");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001275 break;
1276 case 0x03:
1277 if (sense->fmt.f71.mdf == 0)
Carsten Otteab640db2009-03-26 15:24:38 +01001278 snprintf(service, BUFSIZE, "Repair will disable "
1279 "channel path 0x%x on DV",
1280 sense->fmt.f71.md[1]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001281 else
Carsten Otteab640db2009-03-26 15:24:38 +01001282 snprintf(service, BUFSIZE, "Repair will disable "
1283 "channel path (0x%x-0x%x) on DV",
1284 sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001285 break;
1286 case 0x04:
1287 if (sense->fmt.f71.mdf == 0)
Carsten Otteab640db2009-03-26 15:24:38 +01001288 snprintf(service, BUFSIZE, "Repair will disable "
1289 "interface 0x%x on DV", sense->fmt.f71.md[1]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001290 else
Carsten Otteab640db2009-03-26 15:24:38 +01001291 snprintf(service, BUFSIZE, "Repair will disable "
1292 "interfaces (0x%x-0x%x) on DV",
1293 sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001294 break;
1295 case 0x05:
1296 if (sense->fmt.f71.mdf == 0)
Carsten Otteab640db2009-03-26 15:24:38 +01001297 snprintf(service, BUFSIZE, "Repair will disable loader"
1298 " 0x%x on DV", sense->fmt.f71.md[1]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001299 else
Carsten Otteab640db2009-03-26 15:24:38 +01001300 snprintf(service, BUFSIZE, "Repair will disable loader"
1301 " (0x%x-0x%x) on DV",
1302 sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001303 break;
1304 case 0x07:
Carsten Otteab640db2009-03-26 15:24:38 +01001305 snprintf(service, BUFSIZE, "Repair will disable access to DV");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001306 break;
1307 case 0x08:
1308 if (sense->fmt.f71.mdf == 0)
Carsten Otteab640db2009-03-26 15:24:38 +01001309 snprintf(service, BUFSIZE, "Repair will disable "
1310 "message display 0x%x on DV",
1311 sense->fmt.f71.md[1]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001312 else
Carsten Otteab640db2009-03-26 15:24:38 +01001313 snprintf(service, BUFSIZE, "Repair will disable "
1314 "message displays (0x%x-0x%x) on DV",
1315 sense->fmt.f71.md[1], sense->fmt.f71.md[2]);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001316 break;
1317 case 0x09:
Carsten Otteab640db2009-03-26 15:24:38 +01001318 snprintf(service, BUFSIZE, "Clean DV");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001319 break;
1320 default:
Carsten Otteab640db2009-03-26 15:24:38 +01001321 snprintf(service, BUFSIZE, "0x%02x",
1322 sense->fmt.f71.smc);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001323 }
Carsten Otteab640db2009-03-26 15:24:38 +01001324
1325 dev_warn (&device->cdev->dev, "Device subsystem information: exception"
1326 " %s, service %s\n", exception, service);
1327out_nomem:
1328 kfree(exception);
1329 kfree(service);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001330}
1331
1332/*
1333 * Print standard ERA Message
1334 */
1335static void
1336tape_3590_print_era_msg(struct tape_device *device, struct irb *irb)
1337{
1338 struct tape_3590_sense *sense;
1339
1340 sense = (struct tape_3590_sense *) irb->ecw;
1341 if (sense->mc == 0)
1342 return;
1343 if ((sense->mc > 0) && (sense->mc < TAPE_3590_MAX_MSG)) {
1344 if (tape_3590_msg[sense->mc] != NULL)
Carsten Otteab640db2009-03-26 15:24:38 +01001345 dev_warn (&device->cdev->dev, "The tape unit has "
1346 "issued sense message %s\n",
1347 tape_3590_msg[sense->mc]);
1348 else
1349 dev_warn (&device->cdev->dev, "The tape unit has "
1350 "issued an unknown sense message code 0x%x\n",
1351 sense->mc);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001352 return;
1353 }
1354 if (sense->mc == 0xf0) {
1355 /* Standard Media Information Message */
Carsten Otteab640db2009-03-26 15:24:38 +01001356 dev_warn (&device->cdev->dev, "MIM SEV=%i, MC=%02x, ES=%x/%x, "
1357 "RC=%02x-%04x-%02x\n", sense->fmt.f70.sev, sense->mc,
1358 sense->fmt.f70.emc, sense->fmt.f70.smc,
1359 sense->fmt.f70.refcode, sense->fmt.f70.mid,
1360 sense->fmt.f70.fid);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001361 tape_3590_print_mim_msg_f0(device, irb);
1362 return;
1363 }
1364 if (sense->mc == 0xf1) {
1365 /* Standard I/O Subsystem Service Information Message */
Carsten Otteab640db2009-03-26 15:24:38 +01001366 dev_warn (&device->cdev->dev, "IOSIM SEV=%i, DEVTYPE=3590/%02x,"
1367 " MC=%02x, ES=%x/%x, REF=0x%04x-0x%04x-0x%04x\n",
1368 sense->fmt.f71.sev, device->cdev->id.dev_model,
1369 sense->mc, sense->fmt.f71.emc, sense->fmt.f71.smc,
1370 sense->fmt.f71.refcode1, sense->fmt.f71.refcode2,
1371 sense->fmt.f71.refcode3);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001372 tape_3590_print_io_sim_msg_f1(device, irb);
1373 return;
1374 }
1375 if (sense->mc == 0xf2) {
1376 /* Standard Device Service Information Message */
Carsten Otteab640db2009-03-26 15:24:38 +01001377 dev_warn (&device->cdev->dev, "DEVSIM SEV=%i, DEVTYPE=3590/%02x"
1378 ", MC=%02x, ES=%x/%x, REF=0x%04x-0x%04x-0x%04x\n",
1379 sense->fmt.f71.sev, device->cdev->id.dev_model,
1380 sense->mc, sense->fmt.f71.emc, sense->fmt.f71.smc,
1381 sense->fmt.f71.refcode1, sense->fmt.f71.refcode2,
1382 sense->fmt.f71.refcode3);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001383 tape_3590_print_dev_sim_msg_f2(device, irb);
1384 return;
1385 }
1386 if (sense->mc == 0xf3) {
1387 /* Standard Library Service Information Message */
1388 return;
1389 }
Carsten Otteab640db2009-03-26 15:24:38 +01001390 dev_warn (&device->cdev->dev, "The tape unit has issued an unknown "
1391 "sense message code %x\n", sense->mc);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001392}
1393
Michael Holzheucced1dd2007-02-05 21:18:26 +01001394static int tape_3590_crypt_error(struct tape_device *device,
1395 struct tape_request *request, struct irb *irb)
1396{
1397 u8 cu_rc, ekm_rc1;
1398 u16 ekm_rc2;
1399 u32 drv_rc;
Kay Sievers2a0217d2008-10-10 21:33:09 +02001400 const char *bus_id;
1401 char *sense;
Michael Holzheucced1dd2007-02-05 21:18:26 +01001402
1403 sense = ((struct tape_3590_sense *) irb->ecw)->fmt.data;
Kay Sievers2a0217d2008-10-10 21:33:09 +02001404 bus_id = dev_name(&device->cdev->dev);
Michael Holzheucced1dd2007-02-05 21:18:26 +01001405 cu_rc = sense[0];
1406 drv_rc = *((u32*) &sense[5]) & 0xffffff;
1407 ekm_rc1 = sense[9];
1408 ekm_rc2 = *((u16*) &sense[10]);
1409 if ((cu_rc == 0) && (ekm_rc2 == 0xee31))
1410 /* key not defined on EKM */
1411 return tape_3590_erp_basic(device, request, irb, -EKEYREJECTED);
1412 if ((cu_rc == 1) || (cu_rc == 2))
1413 /* No connection to EKM */
1414 return tape_3590_erp_basic(device, request, irb, -ENOTCONN);
1415
Carsten Otteab640db2009-03-26 15:24:38 +01001416 dev_err (&device->cdev->dev, "The tape unit failed to obtain the "
1417 "encryption key from EKM\n");
Michael Holzheucced1dd2007-02-05 21:18:26 +01001418
1419 return tape_3590_erp_basic(device, request, irb, -ENOKEY);
1420}
1421
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001422/*
1423 * 3590 error Recovery routine:
1424 * If possible, it tries to recover from the error. If this is not possible,
1425 * inform the user about the problem.
1426 */
1427static int
1428tape_3590_unit_check(struct tape_device *device, struct tape_request *request,
1429 struct irb *irb)
1430{
1431 struct tape_3590_sense *sense;
1432 int rc;
1433
1434#ifdef CONFIG_S390_TAPE_BLOCK
1435 if (request->op == TO_BLOCK) {
1436 /*
1437 * Recovery for block device requests. Set the block_position
1438 * to something invalid and retry.
1439 */
1440 device->blk_data.block_position = -1;
1441 if (request->retries-- <= 0)
1442 return tape_3590_erp_failed(device, request, irb, -EIO);
1443 else
1444 return tape_3590_erp_retry(device, request, irb);
1445 }
1446#endif
1447
1448 sense = (struct tape_3590_sense *) irb->ecw;
1449
Michael Holzheucced1dd2007-02-05 21:18:26 +01001450 DBF_EVENT(6, "Unit Check: RQC = %x\n", sense->rc_rqc);
1451
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001452 /*
1453 * First check all RC-QRCs where we want to do something special
1454 * - "break": basic error recovery is done
1455 * - "goto out:": just print error message if available
1456 */
1457 rc = -EIO;
1458 switch (sense->rc_rqc) {
1459
1460 case 0x1110:
1461 tape_3590_print_era_msg(device, irb);
1462 return tape_3590_erp_read_buf_log(device, request, irb);
1463
1464 case 0x2011:
1465 tape_3590_print_era_msg(device, irb);
1466 return tape_3590_erp_read_alternate(device, request, irb);
1467
1468 case 0x2230:
1469 case 0x2231:
1470 tape_3590_print_era_msg(device, irb);
1471 return tape_3590_erp_special_interrupt(device, request, irb);
Michael Holzheucced1dd2007-02-05 21:18:26 +01001472 case 0x2240:
1473 return tape_3590_crypt_error(device, request, irb);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001474
1475 case 0x3010:
1476 DBF_EVENT(2, "(%08x): Backward at Beginning of Partition\n",
1477 device->cdev_id);
1478 return tape_3590_erp_basic(device, request, irb, -ENOSPC);
1479 case 0x3012:
1480 DBF_EVENT(2, "(%08x): Forward at End of Partition\n",
1481 device->cdev_id);
1482 return tape_3590_erp_basic(device, request, irb, -ENOSPC);
1483 case 0x3020:
1484 DBF_EVENT(2, "(%08x): End of Data Mark\n", device->cdev_id);
1485 return tape_3590_erp_basic(device, request, irb, -ENOSPC);
1486
1487 case 0x3122:
1488 DBF_EVENT(2, "(%08x): Rewind Unload initiated\n",
1489 device->cdev_id);
1490 return tape_3590_erp_basic(device, request, irb, -EIO);
1491 case 0x3123:
1492 DBF_EVENT(2, "(%08x): Rewind Unload complete\n",
1493 device->cdev_id);
1494 tape_med_state_set(device, MS_UNLOADED);
Michael Holzheucced1dd2007-02-05 21:18:26 +01001495 tape_3590_schedule_work(device, TO_CRYPT_OFF);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001496 return tape_3590_erp_basic(device, request, irb, 0);
1497
1498 case 0x4010:
1499 /*
1500 * print additional msg since default msg
1501 * "device intervention" is not very meaningfull
1502 */
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001503 tape_med_state_set(device, MS_UNLOADED);
Michael Holzheucced1dd2007-02-05 21:18:26 +01001504 tape_3590_schedule_work(device, TO_CRYPT_OFF);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001505 return tape_3590_erp_basic(device, request, irb, -ENOMEDIUM);
1506 case 0x4012: /* Device Long Busy */
Michael Holzheucced1dd2007-02-05 21:18:26 +01001507 /* XXX: Also use long busy handling here? */
1508 DBF_EVENT(6, "(%08x): LONG BUSY\n", device->cdev_id);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001509 tape_3590_print_era_msg(device, irb);
Michael Holzheucced1dd2007-02-05 21:18:26 +01001510 return tape_3590_erp_basic(device, request, irb, -EBUSY);
1511 case 0x4014:
1512 DBF_EVENT(6, "(%08x): Crypto LONG BUSY\n", device->cdev_id);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001513 return tape_3590_erp_long_busy(device, request, irb);
1514
1515 case 0x5010:
1516 if (sense->rac == 0xd0) {
1517 /* Swap */
1518 tape_3590_print_era_msg(device, irb);
1519 return tape_3590_erp_swap(device, request, irb);
1520 }
1521 if (sense->rac == 0x26) {
1522 /* Read Opposite */
1523 tape_3590_print_era_msg(device, irb);
1524 return tape_3590_erp_read_opposite(device, request,
1525 irb);
1526 }
1527 return tape_3590_erp_basic(device, request, irb, -EIO);
1528 case 0x5020:
1529 case 0x5021:
1530 case 0x5022:
1531 case 0x5040:
1532 case 0x5041:
1533 case 0x5042:
1534 tape_3590_print_era_msg(device, irb);
1535 return tape_3590_erp_swap(device, request, irb);
1536
1537 case 0x5110:
1538 case 0x5111:
1539 return tape_3590_erp_basic(device, request, irb, -EMEDIUMTYPE);
1540
1541 case 0x5120:
1542 case 0x1120:
1543 tape_med_state_set(device, MS_UNLOADED);
Michael Holzheucced1dd2007-02-05 21:18:26 +01001544 tape_3590_schedule_work(device, TO_CRYPT_OFF);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001545 return tape_3590_erp_basic(device, request, irb, -ENOMEDIUM);
1546
1547 case 0x6020:
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001548 return tape_3590_erp_basic(device, request, irb, -EMEDIUMTYPE);
1549
1550 case 0x8011:
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001551 return tape_3590_erp_basic(device, request, irb, -EPERM);
1552 case 0x8013:
Carsten Otteab640db2009-03-26 15:24:38 +01001553 dev_warn (&device->cdev->dev, "A different host has privileged"
1554 " access to the tape unit\n");
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001555 return tape_3590_erp_basic(device, request, irb, -EPERM);
1556 default:
1557 return tape_3590_erp_basic(device, request, irb, -EIO);
1558 }
1559}
1560
1561/*
1562 * 3590 interrupt handler:
1563 */
1564static int
1565tape_3590_irq(struct tape_device *device, struct tape_request *request,
1566 struct irb *irb)
1567{
1568 if (request == NULL)
1569 return tape_3590_unsolicited_irq(device, irb);
1570
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001571 if ((irb->scsw.cmd.dstat & DEV_STAT_UNIT_EXCEP) &&
1572 (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) &&
1573 (request->op == TO_WRI)) {
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001574 /* Write at end of volume */
1575 DBF_EVENT(2, "End of volume\n");
1576 return tape_3590_erp_failed(device, request, irb, -ENOSPC);
1577 }
1578
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001579 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001580 return tape_3590_unit_check(device, request, irb);
1581
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001582 if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
1583 if (irb->scsw.cmd.dstat == DEV_STAT_UNIT_EXCEP) {
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001584 if (request->op == TO_FSB || request->op == TO_BSB)
1585 request->rescnt++;
1586 else
1587 DBF_EVENT(5, "Unit Exception!\n");
1588 }
1589
1590 return tape_3590_done(device, request);
1591 }
1592
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001593 if (irb->scsw.cmd.dstat & DEV_STAT_CHN_END) {
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001594 DBF_EVENT(2, "cannel end\n");
1595 return TAPE_IO_PENDING;
1596 }
1597
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001598 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001599 DBF_EVENT(2, "Unit Attention when busy..\n");
1600 return TAPE_IO_PENDING;
1601 }
1602
1603 DBF_EVENT(6, "xunknownirq\n");
Carsten Otteab640db2009-03-26 15:24:38 +01001604 tape_dump_sense_dbf(device, request, irb);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001605 return TAPE_IO_STOP;
1606}
1607
Michael Holzheue2963062007-05-04 18:47:53 +02001608
1609static int tape_3590_read_dev_chars(struct tape_device *device,
1610 struct tape_3590_rdc_data *rdc_data)
1611{
1612 int rc;
1613 struct tape_request *request;
1614
1615 request = tape_alloc_request(1, sizeof(*rdc_data));
1616 if (IS_ERR(request))
1617 return PTR_ERR(request);
1618 request->op = TO_RDC;
1619 tape_ccw_end(request->cpaddr, CCW_CMD_RDC, sizeof(*rdc_data),
1620 request->cpdata);
1621 rc = tape_do_io(device, request);
1622 if (rc == 0)
1623 memcpy(rdc_data, request->cpdata, sizeof(*rdc_data));
1624 tape_free_request(request);
1625 return rc;
1626}
1627
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001628/*
1629 * Setup device function
1630 */
1631static int
1632tape_3590_setup_device(struct tape_device *device)
1633{
1634 int rc;
1635 struct tape_3590_disc_data *data;
Michael Holzheue2963062007-05-04 18:47:53 +02001636 struct tape_3590_rdc_data *rdc_data;
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001637
1638 DBF_EVENT(6, "3590 device setup\n");
Michael Holzheucced1dd2007-02-05 21:18:26 +01001639 data = kzalloc(sizeof(struct tape_3590_disc_data), GFP_KERNEL | GFP_DMA);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001640 if (data == NULL)
1641 return -ENOMEM;
1642 data->read_back_op = READ_PREVIOUS;
1643 device->discdata = data;
1644
Michael Holzheue2963062007-05-04 18:47:53 +02001645 rdc_data = kmalloc(sizeof(*rdc_data), GFP_KERNEL | GFP_DMA);
Michael Holzheucced1dd2007-02-05 21:18:26 +01001646 if (!rdc_data) {
1647 rc = -ENOMEM;
1648 goto fail_kmalloc;
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001649 }
Michael Holzheue2963062007-05-04 18:47:53 +02001650 rc = tape_3590_read_dev_chars(device, rdc_data);
Michael Holzheucced1dd2007-02-05 21:18:26 +01001651 if (rc) {
1652 DBF_LH(3, "Read device characteristics failed!\n");
Julia Lawall1783e602008-06-10 10:03:25 +02001653 goto fail_rdc_data;
Michael Holzheucced1dd2007-02-05 21:18:26 +01001654 }
1655 rc = tape_std_assign(device);
1656 if (rc)
1657 goto fail_rdc_data;
Michael Holzheue2963062007-05-04 18:47:53 +02001658 if (rdc_data->data[31] == 0x13) {
Michael Holzheucced1dd2007-02-05 21:18:26 +01001659 data->crypt_info.capability |= TAPE390_CRYPT_SUPPORTED_MASK;
1660 tape_3592_disable_crypt(device);
1661 } else {
1662 DBF_EVENT(6, "Device has NO crypto support\n");
1663 }
1664 /* Try to find out if medium is loaded */
1665 rc = tape_3590_sense_medium(device);
1666 if (rc) {
1667 DBF_LH(3, "3590 medium sense returned %d\n", rc);
1668 goto fail_rdc_data;
1669 }
1670 return 0;
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001671
Michael Holzheucced1dd2007-02-05 21:18:26 +01001672fail_rdc_data:
1673 kfree(rdc_data);
1674fail_kmalloc:
1675 kfree(data);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001676 return rc;
1677}
1678
1679/*
1680 * Cleanup device function
1681 */
1682static void
1683tape_3590_cleanup_device(struct tape_device *device)
1684{
Tejun Heo6dca4672010-12-24 16:00:18 +01001685 flush_workqueue(tape_3590_wq);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001686 tape_std_unassign(device);
1687
1688 kfree(device->discdata);
1689 device->discdata = NULL;
1690}
1691
1692/*
1693 * List of 3590 magnetic tape commands.
1694 */
1695static tape_mtop_fn tape_3590_mtop[TAPE_NR_MTOPS] = {
1696 [MTRESET] = tape_std_mtreset,
1697 [MTFSF] = tape_std_mtfsf,
1698 [MTBSF] = tape_std_mtbsf,
1699 [MTFSR] = tape_std_mtfsr,
1700 [MTBSR] = tape_std_mtbsr,
1701 [MTWEOF] = tape_std_mtweof,
1702 [MTREW] = tape_std_mtrew,
1703 [MTOFFL] = tape_std_mtoffl,
1704 [MTNOP] = tape_std_mtnop,
1705 [MTRETEN] = tape_std_mtreten,
1706 [MTBSFM] = tape_std_mtbsfm,
1707 [MTFSFM] = tape_std_mtfsfm,
1708 [MTEOM] = tape_std_mteom,
1709 [MTERASE] = tape_std_mterase,
1710 [MTRAS1] = NULL,
1711 [MTRAS2] = NULL,
1712 [MTRAS3] = NULL,
1713 [MTSETBLK] = tape_std_mtsetblk,
1714 [MTSETDENSITY] = NULL,
1715 [MTSEEK] = tape_3590_mtseek,
1716 [MTTELL] = tape_3590_mttell,
1717 [MTSETDRVBUFFER] = NULL,
1718 [MTFSS] = NULL,
1719 [MTBSS] = NULL,
1720 [MTWSM] = NULL,
1721 [MTLOCK] = NULL,
1722 [MTUNLOCK] = NULL,
1723 [MTLOAD] = tape_std_mtload,
1724 [MTUNLOAD] = tape_std_mtunload,
1725 [MTCOMPRESSION] = tape_std_mtcompression,
1726 [MTSETPART] = NULL,
1727 [MTMKPART] = NULL
1728};
1729
1730/*
1731 * Tape discipline structure for 3590.
1732 */
1733static struct tape_discipline tape_discipline_3590 = {
1734 .owner = THIS_MODULE,
1735 .setup_device = tape_3590_setup_device,
1736 .cleanup_device = tape_3590_cleanup_device,
1737 .process_eov = tape_std_process_eov,
1738 .irq = tape_3590_irq,
1739 .read_block = tape_std_read_block,
1740 .write_block = tape_std_write_block,
1741#ifdef CONFIG_S390_TAPE_BLOCK
1742 .bread = tape_3590_bread,
1743 .free_bread = tape_3590_free_bread,
1744 .check_locate = tape_3590_check_locate,
1745#endif
1746 .ioctl_fn = tape_3590_ioctl,
1747 .mtop_array = tape_3590_mtop
1748};
1749
1750static struct ccw_device_id tape_3590_ids[] = {
1751 {CCW_DEVICE_DEVTYPE(0x3590, 0, 0x3590, 0), .driver_info = tape_3590},
Stefan Bader2cc924b2006-04-27 18:40:16 -07001752 {CCW_DEVICE_DEVTYPE(0x3592, 0, 0x3592, 0), .driver_info = tape_3592},
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001753 { /* end of list */ }
1754};
1755
1756static int
1757tape_3590_online(struct ccw_device *cdev)
1758{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001759 return tape_generic_online(dev_get_drvdata(&cdev->dev),
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001760 &tape_discipline_3590);
1761}
1762
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001763static struct ccw_driver tape_3590_driver = {
Sebastian Ott3bda0582011-03-23 10:16:02 +01001764 .driver = {
1765 .name = "tape_3590",
1766 .owner = THIS_MODULE,
1767 },
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001768 .ids = tape_3590_ids,
1769 .probe = tape_generic_probe,
1770 .remove = tape_generic_remove,
Frank Munzert4d7a3cd2009-04-23 13:58:09 +02001771 .set_offline = tape_generic_offline,
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001772 .set_online = tape_3590_online,
Frank Munzert3ef32e622009-06-16 10:30:39 +02001773 .freeze = tape_generic_pm_suspend,
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001774};
1775
1776/*
1777 * Setup discipline structure.
1778 */
1779static int
1780tape_3590_init(void)
1781{
1782 int rc;
1783
1784 TAPE_DBF_AREA = debug_register("tape_3590", 2, 2, 4 * sizeof(long));
1785 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1786#ifdef DBF_LIKE_HELL
1787 debug_set_level(TAPE_DBF_AREA, 6);
1788#endif
1789
1790 DBF_EVENT(3, "3590 init\n");
Tejun Heo6dca4672010-12-24 16:00:18 +01001791
1792 tape_3590_wq = alloc_workqueue("tape_3590", 0, 0);
1793 if (!tape_3590_wq)
1794 return -ENOMEM;
1795
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001796 /* Register driver for 3590 tapes. */
1797 rc = ccw_driver_register(&tape_3590_driver);
Tejun Heo6dca4672010-12-24 16:00:18 +01001798 if (rc) {
1799 destroy_workqueue(tape_3590_wq);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001800 DBF_EVENT(3, "3590 init failed\n");
Tejun Heo6dca4672010-12-24 16:00:18 +01001801 } else
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001802 DBF_EVENT(3, "3590 registered\n");
1803 return rc;
1804}
1805
1806static void
1807tape_3590_exit(void)
1808{
1809 ccw_driver_unregister(&tape_3590_driver);
Tejun Heo6dca4672010-12-24 16:00:18 +01001810 destroy_workqueue(tape_3590_wq);
Stefan Baderb6cba4e2006-03-24 03:15:29 -08001811 debug_unregister(TAPE_DBF_AREA);
1812}
1813
1814MODULE_DEVICE_TABLE(ccw, tape_3590_ids);
1815MODULE_AUTHOR("(C) 2001,2006 IBM Corporation");
1816MODULE_DESCRIPTION("Linux on zSeries channel attached 3590 tape device driver");
1817MODULE_LICENSE("GPL");
1818
1819module_init(tape_3590_init);
1820module_exit(tape_3590_exit);