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