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