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