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