1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * basic function of the tape device driver
4 *
5 * S390 and zSeries version
6 * Copyright IBM Corp. 2001, 2009
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Michael Holzheu <holzheu@de.ibm.com>
9 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
10 * Martin Schwidefsky <schwidefsky@de.ibm.com>
11 * Stefan Bader <shbader@de.ibm.com>
12 */
13
14 #define KMSG_COMPONENT "tape"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16
17 #include <linux/export.h>
18 #include <linux/module.h>
19 #include <linux/init.h> // for kernel parameters
20 #include <linux/kmod.h> // for requesting modules
21 #include <linux/spinlock.h> // for locks
22 #include <linux/vmalloc.h>
23 #include <linux/list.h>
24 #include <linux/slab.h>
25
26 #include <asm/types.h> // for variable types
27
28 #define TAPE_DBF_AREA tape_core_dbf
29
30 #include "tape.h"
31 #include "tape_std.h"
32
33 #define LONG_BUSY_TIMEOUT 180 /* seconds */
34
35 static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *);
36 static void tape_delayed_next_request(struct work_struct *);
37 static void tape_long_busy_timeout(struct timer_list *t);
38
39 /*
40 * One list to contain all tape devices of all disciplines, so
41 * we can assign the devices to minor numbers of the same major
42 * The list is protected by the rwlock
43 */
44 static LIST_HEAD(tape_device_list);
45 static DEFINE_RWLOCK(tape_device_lock);
46
47 /*
48 * Pointer to debug area.
49 */
50 debug_info_t *TAPE_DBF_AREA = NULL;
51 EXPORT_SYMBOL(TAPE_DBF_AREA);
52
53 /*
54 * Printable strings for tape enumerations.
55 */
56 const char *tape_state_verbose[TS_SIZE] =
57 {
58 [TS_UNUSED] = "UNUSED",
59 [TS_IN_USE] = "IN_USE",
60 [TS_BLKUSE] = "BLKUSE",
61 [TS_INIT] = "INIT ",
62 [TS_NOT_OPER] = "NOT_OP"
63 };
64
65 const char *tape_op_verbose[TO_SIZE] =
66 {
67 [TO_BLOCK] = "BLK", [TO_BSB] = "BSB",
68 [TO_BSF] = "BSF", [TO_DSE] = "DSE",
69 [TO_FSB] = "FSB", [TO_FSF] = "FSF",
70 [TO_LBL] = "LBL", [TO_NOP] = "NOP",
71 [TO_RBA] = "RBA", [TO_RBI] = "RBI",
72 [TO_RFO] = "RFO", [TO_REW] = "REW",
73 [TO_RUN] = "RUN", [TO_WRI] = "WRI",
74 [TO_WTM] = "WTM", [TO_MSEN] = "MSN",
75 [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF",
76 [TO_READ_ATTMSG] = "RAT",
77 [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS",
78 [TO_UNASSIGN] = "UAS", [TO_CRYPT_ON] = "CON",
79 [TO_CRYPT_OFF] = "COF", [TO_KEKL_SET] = "KLS",
80 [TO_KEKL_QUERY] = "KLQ",[TO_RDC] = "RDC",
81 };
82
devid_to_int(struct ccw_dev_id * dev_id)83 static int devid_to_int(struct ccw_dev_id *dev_id)
84 {
85 return dev_id->devno + (dev_id->ssid << 16);
86 }
87
88 /*
89 * Some channel attached tape specific attributes.
90 *
91 * FIXME: In the future the first_minor and blocksize attribute should be
92 * replaced by a link to the cdev tree.
93 */
94 static ssize_t
tape_medium_state_show(struct device * dev,struct device_attribute * attr,char * buf)95 tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf)
96 {
97 struct tape_device *tdev;
98
99 tdev = dev_get_drvdata(dev);
100 return sysfs_emit(buf, "%i\n", tdev->medium_state);
101 }
102
103 static
104 DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
105
106 static ssize_t
tape_first_minor_show(struct device * dev,struct device_attribute * attr,char * buf)107 tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf)
108 {
109 struct tape_device *tdev;
110
111 tdev = dev_get_drvdata(dev);
112 return sysfs_emit(buf, "%i\n", tdev->first_minor);
113 }
114
115 static
116 DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
117
118 static ssize_t
tape_state_show(struct device * dev,struct device_attribute * attr,char * buf)119 tape_state_show(struct device *dev, struct device_attribute *attr, char *buf)
120 {
121 struct tape_device *tdev;
122
123 tdev = dev_get_drvdata(dev);
124 return sysfs_emit(buf, "%s\n", (tdev->first_minor < 0) ?
125 "OFFLINE" : tape_state_verbose[tdev->tape_state]);
126 }
127
128 static
129 DEVICE_ATTR(state, 0444, tape_state_show, NULL);
130
131 static ssize_t
tape_operation_show(struct device * dev,struct device_attribute * attr,char * buf)132 tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf)
133 {
134 struct tape_device *tdev;
135 ssize_t rc;
136
137 tdev = dev_get_drvdata(dev);
138 if (tdev->first_minor < 0)
139 return sysfs_emit(buf, "N/A\n");
140
141 spin_lock_irq(get_ccwdev_lock(tdev->cdev));
142 if (list_empty(&tdev->req_queue))
143 rc = sysfs_emit(buf, "---\n");
144 else {
145 struct tape_request *req;
146
147 req = list_entry(tdev->req_queue.next, struct tape_request,
148 list);
149 rc = sysfs_emit(buf, "%s\n", tape_op_verbose[req->op]);
150 }
151 spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
152 return rc;
153 }
154
155 static
156 DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
157
158 static ssize_t
tape_blocksize_show(struct device * dev,struct device_attribute * attr,char * buf)159 tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf)
160 {
161 struct tape_device *tdev;
162
163 tdev = dev_get_drvdata(dev);
164
165 return sysfs_emit(buf, "%i\n", tdev->char_data.block_size);
166 }
167
168 static
169 DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);
170
171 static struct attribute *tape_attrs[] = {
172 &dev_attr_medium_state.attr,
173 &dev_attr_first_minor.attr,
174 &dev_attr_state.attr,
175 &dev_attr_operation.attr,
176 &dev_attr_blocksize.attr,
177 NULL
178 };
179
180 static const struct attribute_group tape_attr_group = {
181 .attrs = tape_attrs,
182 };
183
184 /*
185 * Tape state functions
186 */
187 void
tape_state_set(struct tape_device * device,enum tape_state newstate)188 tape_state_set(struct tape_device *device, enum tape_state newstate)
189 {
190 const char *str;
191
192 if (device->tape_state == TS_NOT_OPER) {
193 DBF_EVENT(3, "ts_set err: not oper\n");
194 return;
195 }
196 DBF_EVENT(4, "ts. dev: %x\n", device->first_minor);
197 DBF_EVENT(4, "old ts:\t\n");
198 if (device->tape_state < TS_SIZE && device->tape_state >=0 )
199 str = tape_state_verbose[device->tape_state];
200 else
201 str = "UNKNOWN TS";
202 DBF_EVENT(4, "%s\n", str);
203 DBF_EVENT(4, "new ts:\t\n");
204 if (newstate < TS_SIZE && newstate >= 0)
205 str = tape_state_verbose[newstate];
206 else
207 str = "UNKNOWN TS";
208 DBF_EVENT(4, "%s\n", str);
209 device->tape_state = newstate;
210 wake_up(&device->state_change_wq);
211 }
212
213 struct tape_med_state_work_data {
214 struct tape_device *device;
215 enum tape_medium_state state;
216 struct work_struct work;
217 };
218
219 static void
tape_med_state_work_handler(struct work_struct * work)220 tape_med_state_work_handler(struct work_struct *work)
221 {
222 static char env_state_loaded[] = "MEDIUM_STATE=LOADED";
223 static char env_state_unloaded[] = "MEDIUM_STATE=UNLOADED";
224 struct tape_med_state_work_data *p =
225 container_of(work, struct tape_med_state_work_data, work);
226 struct tape_device *device = p->device;
227 char *envp[] = { NULL, NULL };
228
229 switch (p->state) {
230 case MS_UNLOADED:
231 pr_info("%s: The tape cartridge has been successfully "
232 "unloaded\n", dev_name(&device->cdev->dev));
233 envp[0] = env_state_unloaded;
234 kobject_uevent_env(&device->cdev->dev.kobj, KOBJ_CHANGE, envp);
235 break;
236 case MS_LOADED:
237 pr_info("%s: A tape cartridge has been mounted\n",
238 dev_name(&device->cdev->dev));
239 envp[0] = env_state_loaded;
240 kobject_uevent_env(&device->cdev->dev.kobj, KOBJ_CHANGE, envp);
241 break;
242 default:
243 break;
244 }
245 tape_put_device(device);
246 kfree(p);
247 }
248
249 static void
tape_med_state_work(struct tape_device * device,enum tape_medium_state state)250 tape_med_state_work(struct tape_device *device, enum tape_medium_state state)
251 {
252 struct tape_med_state_work_data *p;
253
254 p = kzalloc(sizeof(*p), GFP_ATOMIC);
255 if (p) {
256 INIT_WORK(&p->work, tape_med_state_work_handler);
257 p->device = tape_get_device(device);
258 p->state = state;
259 schedule_work(&p->work);
260 }
261 }
262
263 void
tape_med_state_set(struct tape_device * device,enum tape_medium_state newstate)264 tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
265 {
266 enum tape_medium_state oldstate;
267
268 oldstate = device->medium_state;
269 if (oldstate == newstate)
270 return;
271 device->medium_state = newstate;
272 switch(newstate){
273 case MS_UNLOADED:
274 device->tape_generic_status |= GMT_DR_OPEN(~0);
275 if (oldstate == MS_LOADED)
276 tape_med_state_work(device, MS_UNLOADED);
277 break;
278 case MS_LOADED:
279 device->tape_generic_status &= ~GMT_DR_OPEN(~0);
280 if (oldstate == MS_UNLOADED)
281 tape_med_state_work(device, MS_LOADED);
282 break;
283 default:
284 break;
285 }
286 wake_up(&device->state_change_wq);
287 }
288
289 /*
290 * Stop running ccw. Has to be called with the device lock held.
291 */
292 static int
__tape_cancel_io(struct tape_device * device,struct tape_request * request)293 __tape_cancel_io(struct tape_device *device, struct tape_request *request)
294 {
295 int retries;
296 int rc;
297
298 /* Check if interrupt has already been processed */
299 if (request->callback == NULL)
300 return 0;
301
302 rc = 0;
303 for (retries = 0; retries < 5; retries++) {
304 rc = ccw_device_clear(device->cdev, (long) request);
305
306 switch (rc) {
307 case 0:
308 request->status = TAPE_REQUEST_DONE;
309 return 0;
310 case -EBUSY:
311 request->status = TAPE_REQUEST_CANCEL;
312 schedule_delayed_work(&device->tape_dnr, 0);
313 return 0;
314 case -ENODEV:
315 DBF_EXCEPTION(2, "device gone, retry\n");
316 break;
317 case -EIO:
318 DBF_EXCEPTION(2, "I/O error, retry\n");
319 break;
320 default:
321 BUG();
322 }
323 }
324
325 return rc;
326 }
327
328 /*
329 * Add device into the sorted list, giving it the first
330 * available minor number.
331 */
332 static int
tape_assign_minor(struct tape_device * device)333 tape_assign_minor(struct tape_device *device)
334 {
335 struct tape_device *tmp;
336 int minor;
337
338 minor = 0;
339 write_lock(&tape_device_lock);
340 list_for_each_entry(tmp, &tape_device_list, node) {
341 if (minor < tmp->first_minor)
342 break;
343 minor += TAPE_MINORS_PER_DEV;
344 }
345 if (minor >= 256) {
346 write_unlock(&tape_device_lock);
347 return -ENODEV;
348 }
349 device->first_minor = minor;
350 list_add_tail(&device->node, &tmp->node);
351 write_unlock(&tape_device_lock);
352 return 0;
353 }
354
355 /* remove device from the list */
356 static void
tape_remove_minor(struct tape_device * device)357 tape_remove_minor(struct tape_device *device)
358 {
359 write_lock(&tape_device_lock);
360 list_del_init(&device->node);
361 device->first_minor = -1;
362 write_unlock(&tape_device_lock);
363 }
364
365 /*
366 * Set a device online.
367 *
368 * This function is called by the common I/O layer to move a device from the
369 * detected but offline into the online state.
370 * If we return an error (RC < 0) the device remains in the offline state. This
371 * can happen if the device is assigned somewhere else, for example.
372 */
373 int
tape_generic_online(struct tape_device * device,struct tape_discipline * discipline)374 tape_generic_online(struct tape_device *device,
375 struct tape_discipline *discipline)
376 {
377 int rc;
378
379 DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
380
381 if (device->tape_state != TS_INIT) {
382 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
383 return -EINVAL;
384 }
385
386 timer_setup(&device->lb_timeout, tape_long_busy_timeout, 0);
387
388 /* Let the discipline have a go at the device. */
389 device->discipline = discipline;
390 if (!try_module_get(discipline->owner)) {
391 return -EINVAL;
392 }
393
394 rc = discipline->setup_device(device);
395 if (rc)
396 goto out;
397 rc = tape_assign_minor(device);
398 if (rc)
399 goto out_discipline;
400
401 rc = tapechar_setup_device(device);
402 if (rc)
403 goto out_minor;
404
405 tape_state_set(device, TS_UNUSED);
406
407 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
408
409 return 0;
410
411 out_minor:
412 tape_remove_minor(device);
413 out_discipline:
414 device->discipline->cleanup_device(device);
415 device->discipline = NULL;
416 out:
417 module_put(discipline->owner);
418 return rc;
419 }
420
421 static void
tape_cleanup_device(struct tape_device * device)422 tape_cleanup_device(struct tape_device *device)
423 {
424 tapechar_cleanup_device(device);
425 device->discipline->cleanup_device(device);
426 module_put(device->discipline->owner);
427 tape_remove_minor(device);
428 tape_med_state_set(device, MS_UNKNOWN);
429 }
430
431 /*
432 * Set device offline.
433 *
434 * Called by the common I/O layer if the drive should set offline on user
435 * request. We may prevent this by returning an error.
436 * Manual offline is only allowed while the drive is not in use.
437 */
438 int
tape_generic_offline(struct ccw_device * cdev)439 tape_generic_offline(struct ccw_device *cdev)
440 {
441 struct tape_device *device;
442
443 device = dev_get_drvdata(&cdev->dev);
444 if (!device) {
445 return -ENODEV;
446 }
447
448 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
449 device->cdev_id, device);
450
451 spin_lock_irq(get_ccwdev_lock(device->cdev));
452 switch (device->tape_state) {
453 case TS_INIT:
454 case TS_NOT_OPER:
455 spin_unlock_irq(get_ccwdev_lock(device->cdev));
456 break;
457 case TS_UNUSED:
458 tape_state_set(device, TS_INIT);
459 spin_unlock_irq(get_ccwdev_lock(device->cdev));
460 tape_cleanup_device(device);
461 break;
462 default:
463 DBF_EVENT(3, "(%08x): Set offline failed "
464 "- drive in use.\n",
465 device->cdev_id);
466 spin_unlock_irq(get_ccwdev_lock(device->cdev));
467 return -EBUSY;
468 }
469
470 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
471 return 0;
472 }
473
474 /*
475 * Allocate memory for a new device structure.
476 */
477 static struct tape_device *
tape_alloc_device(void)478 tape_alloc_device(void)
479 {
480 struct tape_device *device;
481
482 device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
483 if (device == NULL) {
484 DBF_EXCEPTION(2, "ti:no mem\n");
485 return ERR_PTR(-ENOMEM);
486 }
487 device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
488 if (device->modeset_byte == NULL) {
489 DBF_EXCEPTION(2, "ti:no mem\n");
490 kfree(device);
491 return ERR_PTR(-ENOMEM);
492 }
493 mutex_init(&device->mutex);
494 INIT_LIST_HEAD(&device->req_queue);
495 INIT_LIST_HEAD(&device->node);
496 init_waitqueue_head(&device->state_change_wq);
497 init_waitqueue_head(&device->wait_queue);
498 device->tape_state = TS_INIT;
499 device->medium_state = MS_UNKNOWN;
500 *device->modeset_byte = 0;
501 device->first_minor = -1;
502 atomic_set(&device->ref_count, 1);
503 INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request);
504
505 return device;
506 }
507
508 /*
509 * Get a reference to an existing device structure. This will automatically
510 * increment the reference count.
511 */
512 struct tape_device *
tape_get_device(struct tape_device * device)513 tape_get_device(struct tape_device *device)
514 {
515 int count;
516
517 count = atomic_inc_return(&device->ref_count);
518 DBF_EVENT(4, "tape_get_device(%p) = %i\n", device, count);
519 return device;
520 }
521
522 /*
523 * Decrease the reference counter of a devices structure. If the
524 * reference counter reaches zero free the device structure.
525 * The function returns a NULL pointer to be used by the caller
526 * for clearing reference pointers.
527 */
528 void
tape_put_device(struct tape_device * device)529 tape_put_device(struct tape_device *device)
530 {
531 int count;
532
533 count = atomic_dec_return(&device->ref_count);
534 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, count);
535 BUG_ON(count < 0);
536 if (count == 0) {
537 kfree(device->modeset_byte);
538 kfree(device);
539 }
540 }
541
542 /*
543 * Find tape device by a device index.
544 */
545 struct tape_device *
tape_find_device(int devindex)546 tape_find_device(int devindex)
547 {
548 struct tape_device *device, *tmp;
549
550 device = ERR_PTR(-ENODEV);
551 read_lock(&tape_device_lock);
552 list_for_each_entry(tmp, &tape_device_list, node) {
553 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
554 device = tape_get_device(tmp);
555 break;
556 }
557 }
558 read_unlock(&tape_device_lock);
559 return device;
560 }
561
562 /*
563 * Driverfs tape probe function.
564 */
565 int
tape_generic_probe(struct ccw_device * cdev)566 tape_generic_probe(struct ccw_device *cdev)
567 {
568 struct tape_device *device;
569 int ret;
570 struct ccw_dev_id dev_id;
571
572 device = tape_alloc_device();
573 if (IS_ERR(device))
574 return -ENODEV;
575 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP |
576 CCWDEV_DO_MULTIPATH);
577 ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
578 if (ret) {
579 tape_put_device(device);
580 return ret;
581 }
582 dev_set_drvdata(&cdev->dev, device);
583 cdev->handler = __tape_do_irq;
584 device->cdev = cdev;
585 ccw_device_get_id(cdev, &dev_id);
586 device->cdev_id = devid_to_int(&dev_id);
587 return ret;
588 }
589
590 static void
__tape_discard_requests(struct tape_device * device)591 __tape_discard_requests(struct tape_device *device)
592 {
593 struct tape_request * request;
594 struct list_head * l, *n;
595
596 list_for_each_safe(l, n, &device->req_queue) {
597 request = list_entry(l, struct tape_request, list);
598 if (request->status == TAPE_REQUEST_IN_IO)
599 request->status = TAPE_REQUEST_DONE;
600 list_del(&request->list);
601
602 /* Decrease ref_count for removed request. */
603 request->device = NULL;
604 tape_put_device(device);
605 request->rc = -EIO;
606 if (request->callback != NULL)
607 request->callback(request, request->callback_data);
608 }
609 }
610
611 /*
612 * Driverfs tape remove function.
613 *
614 * This function is called whenever the common I/O layer detects the device
615 * gone. This can happen at any time and we cannot refuse.
616 */
617 void
tape_generic_remove(struct ccw_device * cdev)618 tape_generic_remove(struct ccw_device *cdev)
619 {
620 struct tape_device * device;
621
622 device = dev_get_drvdata(&cdev->dev);
623 if (!device) {
624 return;
625 }
626 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
627
628 spin_lock_irq(get_ccwdev_lock(device->cdev));
629 switch (device->tape_state) {
630 case TS_INIT:
631 tape_state_set(device, TS_NOT_OPER);
632 fallthrough;
633 case TS_NOT_OPER:
634 /*
635 * Nothing to do.
636 */
637 spin_unlock_irq(get_ccwdev_lock(device->cdev));
638 break;
639 case TS_UNUSED:
640 /*
641 * Need only to release the device.
642 */
643 tape_state_set(device, TS_NOT_OPER);
644 spin_unlock_irq(get_ccwdev_lock(device->cdev));
645 tape_cleanup_device(device);
646 break;
647 default:
648 /*
649 * There may be requests on the queue. We will not get
650 * an interrupt for a request that was running. So we
651 * just post them all as I/O errors.
652 */
653 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
654 device->cdev_id);
655 pr_warn("%s: A tape unit was detached while in use\n",
656 dev_name(&device->cdev->dev));
657 tape_state_set(device, TS_NOT_OPER);
658 __tape_discard_requests(device);
659 spin_unlock_irq(get_ccwdev_lock(device->cdev));
660 tape_cleanup_device(device);
661 }
662
663 device = dev_get_drvdata(&cdev->dev);
664 if (device) {
665 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
666 dev_set_drvdata(&cdev->dev, NULL);
667 tape_put_device(device);
668 }
669 }
670
671 /*
672 * Allocate a new tape ccw request
673 */
674 struct tape_request *
tape_alloc_request(int cplength,int datasize)675 tape_alloc_request(int cplength, int datasize)
676 {
677 struct tape_request *request;
678
679 BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
680
681 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
682
683 request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
684 if (request == NULL) {
685 DBF_EXCEPTION(1, "cqra nomem\n");
686 return ERR_PTR(-ENOMEM);
687 }
688 /* allocate channel program */
689 if (cplength > 0) {
690 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
691 GFP_ATOMIC | GFP_DMA);
692 if (request->cpaddr == NULL) {
693 DBF_EXCEPTION(1, "cqra nomem\n");
694 kfree(request);
695 return ERR_PTR(-ENOMEM);
696 }
697 }
698 /* alloc small kernel buffer */
699 if (datasize > 0) {
700 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
701 if (request->cpdata == NULL) {
702 DBF_EXCEPTION(1, "cqra nomem\n");
703 kfree(request->cpaddr);
704 kfree(request);
705 return ERR_PTR(-ENOMEM);
706 }
707 }
708 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
709 request->cpdata);
710
711 return request;
712 }
713
714 /*
715 * Free tape ccw request
716 */
717 void
tape_free_request(struct tape_request * request)718 tape_free_request (struct tape_request * request)
719 {
720 DBF_LH(6, "Free request %p\n", request);
721
722 if (request->device)
723 tape_put_device(request->device);
724 kfree(request->cpdata);
725 kfree(request->cpaddr);
726 kfree(request);
727 }
728
729 static int
__tape_start_io(struct tape_device * device,struct tape_request * request)730 __tape_start_io(struct tape_device *device, struct tape_request *request)
731 {
732 int rc;
733
734 rc = ccw_device_start(
735 device->cdev,
736 request->cpaddr,
737 (unsigned long) request,
738 0x00,
739 request->options
740 );
741 if (rc == 0) {
742 request->status = TAPE_REQUEST_IN_IO;
743 } else if (rc == -EBUSY) {
744 /* The common I/O subsystem is currently busy. Retry later. */
745 request->status = TAPE_REQUEST_QUEUED;
746 schedule_delayed_work(&device->tape_dnr, 0);
747 rc = 0;
748 } else {
749 /* Start failed. Remove request and indicate failure. */
750 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
751 }
752 return rc;
753 }
754
755 static void
__tape_start_next_request(struct tape_device * device)756 __tape_start_next_request(struct tape_device *device)
757 {
758 struct list_head *l, *n;
759 struct tape_request *request;
760 int rc;
761
762 DBF_LH(6, "__tape_start_next_request(%p)\n", device);
763 /*
764 * Try to start each request on request queue until one is
765 * started successful.
766 */
767 list_for_each_safe(l, n, &device->req_queue) {
768 request = list_entry(l, struct tape_request, list);
769
770 /*
771 * Avoid race condition if bottom-half was triggered more than
772 * once.
773 */
774 if (request->status == TAPE_REQUEST_IN_IO)
775 return;
776 /*
777 * Request has already been stopped. We have to wait until
778 * the request is removed from the queue in the interrupt
779 * handling.
780 */
781 if (request->status == TAPE_REQUEST_DONE)
782 return;
783
784 /*
785 * We wanted to cancel the request but the common I/O layer
786 * was busy at that time. This can only happen if this
787 * function is called by delayed_next_request.
788 * Otherwise we start the next request on the queue.
789 */
790 if (request->status == TAPE_REQUEST_CANCEL) {
791 rc = __tape_cancel_io(device, request);
792 } else {
793 rc = __tape_start_io(device, request);
794 }
795 if (rc == 0)
796 return;
797
798 /* Set ending status. */
799 request->rc = rc;
800 request->status = TAPE_REQUEST_DONE;
801
802 /* Remove from request queue. */
803 list_del(&request->list);
804
805 /* Do callback. */
806 if (request->callback != NULL)
807 request->callback(request, request->callback_data);
808 }
809 }
810
811 static void
tape_delayed_next_request(struct work_struct * work)812 tape_delayed_next_request(struct work_struct *work)
813 {
814 struct tape_device *device =
815 container_of(work, struct tape_device, tape_dnr.work);
816
817 DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
818 spin_lock_irq(get_ccwdev_lock(device->cdev));
819 __tape_start_next_request(device);
820 spin_unlock_irq(get_ccwdev_lock(device->cdev));
821 }
822
tape_long_busy_timeout(struct timer_list * t)823 static void tape_long_busy_timeout(struct timer_list *t)
824 {
825 struct tape_device *device = timer_container_of(device, t, lb_timeout);
826 struct tape_request *request;
827
828 spin_lock_irq(get_ccwdev_lock(device->cdev));
829 request = list_entry(device->req_queue.next, struct tape_request, list);
830 BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY);
831 DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
832 __tape_start_next_request(device);
833 tape_put_device(device);
834 spin_unlock_irq(get_ccwdev_lock(device->cdev));
835 }
836
837 static void
__tape_end_request(struct tape_device * device,struct tape_request * request,int rc)838 __tape_end_request(
839 struct tape_device * device,
840 struct tape_request * request,
841 int rc)
842 {
843 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
844 if (request) {
845 request->rc = rc;
846 request->status = TAPE_REQUEST_DONE;
847
848 /* Remove from request queue. */
849 list_del(&request->list);
850
851 /* Do callback. */
852 if (request->callback != NULL)
853 request->callback(request, request->callback_data);
854 }
855
856 /* Start next request. */
857 if (!list_empty(&device->req_queue))
858 __tape_start_next_request(device);
859 }
860
861 /*
862 * Write sense data to dbf
863 */
864 void
tape_dump_sense_dbf(struct tape_device * device,struct tape_request * request,struct irb * irb)865 tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
866 struct irb *irb)
867 {
868 unsigned int *sptr;
869 const char* op;
870
871 if (request != NULL)
872 op = tape_op_verbose[request->op];
873 else
874 op = "---";
875 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
876 irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
877 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
878 sptr = (unsigned int *) irb->ecw;
879 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
880 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
881 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
882 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
883 }
884
885 /*
886 * I/O helper function. Adds the request to the request queue
887 * and starts it if the tape is idle. Has to be called with
888 * the device lock held.
889 */
890 static int
__tape_start_request(struct tape_device * device,struct tape_request * request)891 __tape_start_request(struct tape_device *device, struct tape_request *request)
892 {
893 int rc;
894
895 switch (request->op) {
896 case TO_MSEN:
897 case TO_ASSIGN:
898 case TO_UNASSIGN:
899 case TO_READ_ATTMSG:
900 case TO_RDC:
901 if (device->tape_state == TS_INIT)
902 break;
903 if (device->tape_state == TS_UNUSED)
904 break;
905 fallthrough;
906 default:
907 if (device->tape_state == TS_BLKUSE)
908 break;
909 if (device->tape_state != TS_IN_USE)
910 return -ENODEV;
911 }
912
913 /* Increase use count of device for the added request. */
914 request->device = tape_get_device(device);
915
916 if (list_empty(&device->req_queue)) {
917 /* No other requests are on the queue. Start this one. */
918 rc = __tape_start_io(device, request);
919 if (rc)
920 return rc;
921
922 DBF_LH(5, "Request %p added for execution.\n", request);
923 list_add(&request->list, &device->req_queue);
924 } else {
925 DBF_LH(5, "Request %p add to queue.\n", request);
926 request->status = TAPE_REQUEST_QUEUED;
927 list_add_tail(&request->list, &device->req_queue);
928 }
929 return 0;
930 }
931
932 /*
933 * Add the request to the request queue, try to start it if the
934 * tape is idle. Return without waiting for end of i/o.
935 */
936 int
tape_do_io_async(struct tape_device * device,struct tape_request * request)937 tape_do_io_async(struct tape_device *device, struct tape_request *request)
938 {
939 int rc;
940
941 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
942
943 spin_lock_irq(get_ccwdev_lock(device->cdev));
944 /* Add request to request queue and try to start it. */
945 rc = __tape_start_request(device, request);
946 spin_unlock_irq(get_ccwdev_lock(device->cdev));
947 return rc;
948 }
949
950 /*
951 * tape_do_io/__tape_wake_up
952 * Add the request to the request queue, try to start it if the
953 * tape is idle and wait uninterruptible for its completion.
954 */
955 static void
__tape_wake_up(struct tape_request * request,void * data)956 __tape_wake_up(struct tape_request *request, void *data)
957 {
958 request->callback = NULL;
959 wake_up((wait_queue_head_t *) data);
960 }
961
962 int
tape_do_io(struct tape_device * device,struct tape_request * request)963 tape_do_io(struct tape_device *device, struct tape_request *request)
964 {
965 int rc;
966
967 spin_lock_irq(get_ccwdev_lock(device->cdev));
968 /* Setup callback */
969 request->callback = __tape_wake_up;
970 request->callback_data = &device->wait_queue;
971 /* Add request to request queue and try to start it. */
972 rc = __tape_start_request(device, request);
973 spin_unlock_irq(get_ccwdev_lock(device->cdev));
974 if (rc)
975 return rc;
976 /* Request added to the queue. Wait for its completion. */
977 wait_event(device->wait_queue, (request->callback == NULL));
978 /* Get rc from request */
979 return request->rc;
980 }
981
982 /*
983 * tape_do_io_interruptible/__tape_wake_up_interruptible
984 * Add the request to the request queue, try to start it if the
985 * tape is idle and wait uninterruptible for its completion.
986 */
987 static void
__tape_wake_up_interruptible(struct tape_request * request,void * data)988 __tape_wake_up_interruptible(struct tape_request *request, void *data)
989 {
990 request->callback = NULL;
991 wake_up_interruptible((wait_queue_head_t *) data);
992 }
993
994 int
tape_do_io_interruptible(struct tape_device * device,struct tape_request * request)995 tape_do_io_interruptible(struct tape_device *device,
996 struct tape_request *request)
997 {
998 int rc;
999
1000 spin_lock_irq(get_ccwdev_lock(device->cdev));
1001 /* Setup callback */
1002 request->callback = __tape_wake_up_interruptible;
1003 request->callback_data = &device->wait_queue;
1004 rc = __tape_start_request(device, request);
1005 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1006 if (rc)
1007 return rc;
1008 /* Request added to the queue. Wait for its completion. */
1009 rc = wait_event_interruptible(device->wait_queue,
1010 (request->callback == NULL));
1011 if (rc != -ERESTARTSYS)
1012 /* Request finished normally. */
1013 return request->rc;
1014
1015 /* Interrupted by a signal. We have to stop the current request. */
1016 spin_lock_irq(get_ccwdev_lock(device->cdev));
1017 rc = __tape_cancel_io(device, request);
1018 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1019 if (rc == 0) {
1020 /* Wait for the interrupt that acknowledges the halt. */
1021 do {
1022 rc = wait_event_interruptible(
1023 device->wait_queue,
1024 (request->callback == NULL)
1025 );
1026 } while (rc == -ERESTARTSYS);
1027
1028 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
1029 rc = -ERESTARTSYS;
1030 }
1031 return rc;
1032 }
1033
1034 /*
1035 * Stop running ccw.
1036 */
1037 int
tape_cancel_io(struct tape_device * device,struct tape_request * request)1038 tape_cancel_io(struct tape_device *device, struct tape_request *request)
1039 {
1040 int rc;
1041
1042 spin_lock_irq(get_ccwdev_lock(device->cdev));
1043 rc = __tape_cancel_io(device, request);
1044 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1045 return rc;
1046 }
1047
1048 /*
1049 * Tape interrupt routine, called from the ccw_device layer
1050 */
1051 static void
__tape_do_irq(struct ccw_device * cdev,unsigned long intparm,struct irb * irb)1052 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1053 {
1054 struct tape_device *device;
1055 struct tape_request *request;
1056 int rc;
1057
1058 device = dev_get_drvdata(&cdev->dev);
1059 if (device == NULL) {
1060 return;
1061 }
1062 request = (struct tape_request *) intparm;
1063
1064 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1065
1066 /* On special conditions irb is an error pointer */
1067 if (IS_ERR(irb)) {
1068 /* FIXME: What to do with the request? */
1069 switch (PTR_ERR(irb)) {
1070 case -ETIMEDOUT:
1071 DBF_LH(1, "(%08x): Request timed out\n",
1072 device->cdev_id);
1073 fallthrough;
1074 case -EIO:
1075 __tape_end_request(device, request, -EIO);
1076 break;
1077 default:
1078 DBF_LH(1, "(%08x): Unexpected i/o error %li\n",
1079 device->cdev_id, PTR_ERR(irb));
1080 }
1081 return;
1082 }
1083
1084 /*
1085 * If the condition code is not zero and the start function bit is
1086 * still set, this is an deferred error and the last start I/O did
1087 * not succeed. At this point the condition that caused the deferred
1088 * error might still apply. So we just schedule the request to be
1089 * started later.
1090 */
1091 if (irb->scsw.cmd.cc != 0 &&
1092 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
1093 (request->status == TAPE_REQUEST_IN_IO)) {
1094 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1095 device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
1096 request->status = TAPE_REQUEST_QUEUED;
1097 schedule_delayed_work(&device->tape_dnr, HZ);
1098 return;
1099 }
1100
1101 /* May be an unsolicited irq */
1102 if(request != NULL)
1103 request->rescnt = irb->scsw.cmd.count;
1104 else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
1105 !list_empty(&device->req_queue)) {
1106 /* Not Ready to Ready after long busy ? */
1107 struct tape_request *req;
1108 req = list_entry(device->req_queue.next,
1109 struct tape_request, list);
1110 if (req->status == TAPE_REQUEST_LONG_BUSY) {
1111 DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
1112 if (timer_delete(&device->lb_timeout)) {
1113 tape_put_device(device);
1114 __tape_start_next_request(device);
1115 }
1116 return;
1117 }
1118 }
1119 if (irb->scsw.cmd.dstat != 0x0c) {
1120 /* Set the 'ONLINE' flag depending on sense byte 1 */
1121 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1122 device->tape_generic_status |= GMT_ONLINE(~0);
1123 else
1124 device->tape_generic_status &= ~GMT_ONLINE(~0);
1125
1126 /*
1127 * Any request that does not come back with channel end
1128 * and device end is unusual. Log the sense data.
1129 */
1130 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1131 tape_dump_sense_dbf(device, request, irb);
1132 } else {
1133 /* Upon normal completion the device _is_ online */
1134 device->tape_generic_status |= GMT_ONLINE(~0);
1135 }
1136 if (device->tape_state == TS_NOT_OPER) {
1137 DBF_EVENT(6, "tape:device is not operational\n");
1138 return;
1139 }
1140
1141 /*
1142 * Request that were canceled still come back with an interrupt.
1143 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1144 */
1145 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
1146 __tape_end_request(device, request, -EIO);
1147 return;
1148 }
1149
1150 rc = device->discipline->irq(device, request, irb);
1151 /*
1152 * rc < 0 : request finished unsuccessfully.
1153 * rc == TAPE_IO_SUCCESS: request finished successfully.
1154 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1155 * rc == TAPE_IO_RETRY: request finished but needs another go.
1156 * rc == TAPE_IO_STOP: request needs to get terminated.
1157 */
1158 switch (rc) {
1159 case TAPE_IO_SUCCESS:
1160 /* Upon normal completion the device _is_ online */
1161 device->tape_generic_status |= GMT_ONLINE(~0);
1162 __tape_end_request(device, request, rc);
1163 break;
1164 case TAPE_IO_PENDING:
1165 break;
1166 case TAPE_IO_LONG_BUSY:
1167 device->lb_timeout.expires = jiffies +
1168 LONG_BUSY_TIMEOUT * HZ;
1169 DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
1170 add_timer(&device->lb_timeout);
1171 request->status = TAPE_REQUEST_LONG_BUSY;
1172 break;
1173 case TAPE_IO_RETRY:
1174 rc = __tape_start_io(device, request);
1175 if (rc)
1176 __tape_end_request(device, request, rc);
1177 break;
1178 case TAPE_IO_STOP:
1179 rc = __tape_cancel_io(device, request);
1180 if (rc)
1181 __tape_end_request(device, request, rc);
1182 break;
1183 default:
1184 if (rc > 0) {
1185 DBF_EVENT(6, "xunknownrc\n");
1186 __tape_end_request(device, request, -EIO);
1187 } else {
1188 __tape_end_request(device, request, rc);
1189 }
1190 break;
1191 }
1192 }
1193
1194 /*
1195 * Tape device open function used by tape_char frontend.
1196 */
1197 int
tape_open(struct tape_device * device)1198 tape_open(struct tape_device *device)
1199 {
1200 int rc;
1201
1202 spin_lock_irq(get_ccwdev_lock(device->cdev));
1203 if (device->tape_state == TS_NOT_OPER) {
1204 DBF_EVENT(6, "TAPE:nodev\n");
1205 rc = -ENODEV;
1206 } else if (device->tape_state == TS_IN_USE) {
1207 DBF_EVENT(6, "TAPE:dbusy\n");
1208 rc = -EBUSY;
1209 } else if (device->tape_state == TS_BLKUSE) {
1210 DBF_EVENT(6, "TAPE:dbusy\n");
1211 rc = -EBUSY;
1212 } else if (device->discipline != NULL &&
1213 !try_module_get(device->discipline->owner)) {
1214 DBF_EVENT(6, "TAPE:nodisc\n");
1215 rc = -ENODEV;
1216 } else {
1217 tape_state_set(device, TS_IN_USE);
1218 rc = 0;
1219 }
1220 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1221 return rc;
1222 }
1223
1224 /*
1225 * Tape device release function used by tape_char frontend.
1226 */
1227 int
tape_release(struct tape_device * device)1228 tape_release(struct tape_device *device)
1229 {
1230 spin_lock_irq(get_ccwdev_lock(device->cdev));
1231 if (device->tape_state == TS_IN_USE)
1232 tape_state_set(device, TS_UNUSED);
1233 module_put(device->discipline->owner);
1234 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1235 return 0;
1236 }
1237
1238 /*
1239 * Execute a magnetic tape command a number of times.
1240 */
1241 int
tape_mtop(struct tape_device * device,int mt_op,int mt_count)1242 tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1243 {
1244 tape_mtop_fn fn;
1245 int rc;
1246
1247 DBF_EVENT(6, "TAPE:mtio\n");
1248 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1249 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1250
1251 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1252 return -EINVAL;
1253 fn = device->discipline->mtop_array[mt_op];
1254 if (fn == NULL)
1255 return -EINVAL;
1256
1257 /* We assume that the backends can handle count up to 500. */
1258 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1259 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1260 rc = 0;
1261 for (; mt_count > 500; mt_count -= 500)
1262 if ((rc = fn(device, 500)) != 0)
1263 break;
1264 if (rc == 0)
1265 rc = fn(device, mt_count);
1266 } else
1267 rc = fn(device, mt_count);
1268 return rc;
1269
1270 }
1271
1272 /*
1273 * Tape init function.
1274 */
1275 static int
tape_init(void)1276 tape_init (void)
1277 {
1278 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
1279 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1280 #ifdef DBF_LIKE_HELL
1281 debug_set_level(TAPE_DBF_AREA, 6);
1282 #endif
1283 DBF_EVENT(3, "tape init\n");
1284 tape_proc_init();
1285 tapechar_init ();
1286 return 0;
1287 }
1288
1289 /*
1290 * Tape exit function.
1291 */
1292 static void
tape_exit(void)1293 tape_exit(void)
1294 {
1295 DBF_EVENT(6, "tape exit\n");
1296
1297 /* Get rid of the frontends */
1298 tapechar_exit();
1299 tape_proc_cleanup();
1300 debug_unregister (TAPE_DBF_AREA);
1301 }
1302
1303 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1304 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1305 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1306 MODULE_LICENSE("GPL");
1307
1308 module_init(tape_init);
1309 module_exit(tape_exit);
1310
1311 EXPORT_SYMBOL(tape_generic_remove);
1312 EXPORT_SYMBOL(tape_generic_probe);
1313 EXPORT_SYMBOL(tape_generic_online);
1314 EXPORT_SYMBOL(tape_generic_offline);
1315 EXPORT_SYMBOL(tape_put_device);
1316 EXPORT_SYMBOL(tape_get_device);
1317 EXPORT_SYMBOL(tape_state_verbose);
1318 EXPORT_SYMBOL(tape_op_verbose);
1319 EXPORT_SYMBOL(tape_state_set);
1320 EXPORT_SYMBOL(tape_med_state_set);
1321 EXPORT_SYMBOL(tape_alloc_request);
1322 EXPORT_SYMBOL(tape_free_request);
1323 EXPORT_SYMBOL(tape_dump_sense_dbf);
1324 EXPORT_SYMBOL(tape_do_io);
1325 EXPORT_SYMBOL(tape_do_io_async);
1326 EXPORT_SYMBOL(tape_do_io_interruptible);
1327 EXPORT_SYMBOL(tape_cancel_io);
1328 EXPORT_SYMBOL(tape_mtop);
1329