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