2 * drivers/s390/char/tape_core.c
3 * basic function of the tape device driver
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>
14 #define KMSG_COMPONENT "tape"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
26 #include <asm/types.h> // for variable types
28 #define TAPE_DBF_AREA tape_core_dbf
33 #define LONG_BUSY_TIMEOUT 180 /* seconds */
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(unsigned long data
);
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
44 static LIST_HEAD(tape_device_list
);
45 static DEFINE_RWLOCK(tape_device_lock
);
48 * Pointer to debug area.
50 debug_info_t
*TAPE_DBF_AREA
= NULL
;
51 EXPORT_SYMBOL(TAPE_DBF_AREA
);
54 * Printable strings for tape enumerations.
56 const char *tape_state_verbose
[TS_SIZE
] =
58 [TS_UNUSED
] = "UNUSED",
59 [TS_IN_USE
] = "IN_USE",
60 [TS_BLKUSE
] = "BLKUSE",
62 [TS_NOT_OPER
] = "NOT_OP"
65 const char *tape_op_verbose
[TO_SIZE
] =
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",
83 static int devid_to_int(struct ccw_dev_id
*dev_id
)
85 return dev_id
->devno
+ (dev_id
->ssid
<< 16);
89 * Some channel attached tape specific attributes.
91 * FIXME: In the future the first_minor and blocksize attribute should be
92 * replaced by a link to the cdev tree.
95 tape_medium_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
97 struct tape_device
*tdev
;
99 tdev
= dev_get_drvdata(dev
);
100 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->medium_state
);
104 DEVICE_ATTR(medium_state
, 0444, tape_medium_state_show
, NULL
);
107 tape_first_minor_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
109 struct tape_device
*tdev
;
111 tdev
= dev_get_drvdata(dev
);
112 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->first_minor
);
116 DEVICE_ATTR(first_minor
, 0444, tape_first_minor_show
, NULL
);
119 tape_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
121 struct tape_device
*tdev
;
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
]);
129 DEVICE_ATTR(state
, 0444, tape_state_show
, NULL
);
132 tape_operation_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
134 struct tape_device
*tdev
;
137 tdev
= dev_get_drvdata(dev
);
138 if (tdev
->first_minor
< 0)
139 return scnprintf(buf
, PAGE_SIZE
, "N/A\n");
141 spin_lock_irq(get_ccwdev_lock(tdev
->cdev
));
142 if (list_empty(&tdev
->req_queue
))
143 rc
= scnprintf(buf
, PAGE_SIZE
, "---\n");
145 struct tape_request
*req
;
147 req
= list_entry(tdev
->req_queue
.next
, struct tape_request
,
149 rc
= scnprintf(buf
,PAGE_SIZE
, "%s\n", tape_op_verbose
[req
->op
]);
151 spin_unlock_irq(get_ccwdev_lock(tdev
->cdev
));
156 DEVICE_ATTR(operation
, 0444, tape_operation_show
, NULL
);
159 tape_blocksize_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
161 struct tape_device
*tdev
;
163 tdev
= dev_get_drvdata(dev
);
165 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->char_data
.block_size
);
169 DEVICE_ATTR(blocksize
, 0444, tape_blocksize_show
, NULL
);
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
,
180 static struct attribute_group tape_attr_group
= {
185 * Tape state functions
188 tape_state_set(struct tape_device
*device
, enum tape_state newstate
)
192 if (device
->tape_state
== TS_NOT_OPER
) {
193 DBF_EVENT(3, "ts_set err: not oper\n");
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
];
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
];
208 DBF_EVENT(4, "%s\n", str
);
209 device
->tape_state
= newstate
;
210 wake_up(&device
->state_change_wq
);
213 struct tape_med_state_work_data
{
214 struct tape_device
*device
;
215 enum tape_medium_state state
;
216 struct work_struct work
;
220 tape_med_state_work_handler(struct work_struct
*work
)
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
};
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
);
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
);
245 tape_put_device(device
);
250 tape_med_state_work(struct tape_device
*device
, enum tape_medium_state state
)
252 struct tape_med_state_work_data
*p
;
254 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
256 INIT_WORK(&p
->work
, tape_med_state_work_handler
);
257 p
->device
= tape_get_device(device
);
259 schedule_work(&p
->work
);
264 tape_med_state_set(struct tape_device
*device
, enum tape_medium_state newstate
)
266 enum tape_medium_state oldstate
;
268 oldstate
= device
->medium_state
;
269 if (oldstate
== newstate
)
271 device
->medium_state
= newstate
;
274 device
->tape_generic_status
|= GMT_DR_OPEN(~0);
275 if (oldstate
== MS_LOADED
)
276 tape_med_state_work(device
, MS_UNLOADED
);
279 device
->tape_generic_status
&= ~GMT_DR_OPEN(~0);
280 if (oldstate
== MS_UNLOADED
)
281 tape_med_state_work(device
, MS_LOADED
);
286 wake_up(&device
->state_change_wq
);
290 * Stop running ccw. Has to be called with the device lock held.
293 __tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
298 /* Check if interrupt has already been processed */
299 if (request
->callback
== NULL
)
303 for (retries
= 0; retries
< 5; retries
++) {
304 rc
= ccw_device_clear(device
->cdev
, (long) request
);
308 request
->status
= TAPE_REQUEST_DONE
;
311 request
->status
= TAPE_REQUEST_CANCEL
;
312 schedule_delayed_work(&device
->tape_dnr
, 0);
315 DBF_EXCEPTION(2, "device gone, retry\n");
318 DBF_EXCEPTION(2, "I/O error, retry\n");
329 * Add device into the sorted list, giving it the first
330 * available minor number.
333 tape_assign_minor(struct tape_device
*device
)
335 struct tape_device
*tmp
;
339 write_lock(&tape_device_lock
);
340 list_for_each_entry(tmp
, &tape_device_list
, node
) {
341 if (minor
< tmp
->first_minor
)
343 minor
+= TAPE_MINORS_PER_DEV
;
346 write_unlock(&tape_device_lock
);
349 device
->first_minor
= minor
;
350 list_add_tail(&device
->node
, &tmp
->node
);
351 write_unlock(&tape_device_lock
);
355 /* remove device from the list */
357 tape_remove_minor(struct tape_device
*device
)
359 write_lock(&tape_device_lock
);
360 list_del_init(&device
->node
);
361 device
->first_minor
= -1;
362 write_unlock(&tape_device_lock
);
366 * Set a device online.
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.
374 tape_generic_online(struct tape_device
*device
,
375 struct tape_discipline
*discipline
)
379 DBF_LH(6, "tape_enable_device(%p, %p)\n", device
, discipline
);
381 if (device
->tape_state
!= TS_INIT
) {
382 DBF_LH(3, "Tapestate not INIT (%d)\n", device
->tape_state
);
386 init_timer(&device
->lb_timeout
);
387 device
->lb_timeout
.function
= tape_long_busy_timeout
;
389 /* Let the discipline have a go at the device. */
390 device
->discipline
= discipline
;
391 if (!try_module_get(discipline
->owner
)) {
395 rc
= discipline
->setup_device(device
);
398 rc
= tape_assign_minor(device
);
402 rc
= tapechar_setup_device(device
);
405 rc
= tapeblock_setup_device(device
);
409 tape_state_set(device
, TS_UNUSED
);
411 DBF_LH(3, "(%08x): Drive set online\n", device
->cdev_id
);
416 tapechar_cleanup_device(device
);
418 tape_remove_minor(device
);
420 device
->discipline
->cleanup_device(device
);
421 device
->discipline
= NULL
;
423 module_put(discipline
->owner
);
428 tape_cleanup_device(struct tape_device
*device
)
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
);
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
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.
451 int tape_generic_pm_suspend(struct ccw_device
*cdev
)
453 struct tape_device
*device
;
455 device
= dev_get_drvdata(&cdev
->dev
);
460 DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n",
461 device
->cdev_id
, device
);
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
));
469 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
470 switch (device
->tape_state
) {
474 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
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
));
483 DBF_LH(3, "(%08x): Drive suspended.\n", device
->cdev_id
);
488 * Set device offline.
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.
495 tape_generic_offline(struct ccw_device
*cdev
)
497 struct tape_device
*device
;
499 device
= dev_get_drvdata(&cdev
->dev
);
504 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
505 device
->cdev_id
, device
);
507 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
508 switch (device
->tape_state
) {
511 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
514 tape_state_set(device
, TS_INIT
);
515 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
516 tape_cleanup_device(device
);
519 DBF_EVENT(3, "(%08x): Set offline failed "
522 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
526 DBF_LH(3, "(%08x): Drive set offline.\n", device
->cdev_id
);
531 * Allocate memory for a new device structure.
533 static struct tape_device
*
534 tape_alloc_device(void)
536 struct tape_device
*device
;
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
);
543 device
->modeset_byte
= kmalloc(1, GFP_KERNEL
| GFP_DMA
);
544 if (device
->modeset_byte
== NULL
) {
545 DBF_EXCEPTION(2, "ti:no mem\n");
547 return ERR_PTR(-ENOMEM
);
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
);
565 * Get a reference to an existing device structure. This will automatically
566 * increment the reference count.
569 tape_get_device(struct tape_device
*device
)
573 count
= atomic_inc_return(&device
->ref_count
);
574 DBF_EVENT(4, "tape_get_device(%p) = %i\n", device
, count
);
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.
585 tape_put_device(struct tape_device
*device
)
589 count
= atomic_dec_return(&device
->ref_count
);
590 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device
, count
);
593 kfree(device
->modeset_byte
);
599 * Find tape device by a device index.
602 tape_find_device(int devindex
)
604 struct tape_device
*device
, *tmp
;
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
);
614 read_unlock(&tape_device_lock
);
619 * Driverfs tape probe function.
622 tape_generic_probe(struct ccw_device
*cdev
)
624 struct tape_device
*device
;
626 struct ccw_dev_id dev_id
;
628 device
= tape_alloc_device();
631 ccw_device_set_options(cdev
, CCWDEV_DO_PATHGROUP
|
632 CCWDEV_DO_MULTIPATH
);
633 ret
= sysfs_create_group(&cdev
->dev
.kobj
, &tape_attr_group
);
635 tape_put_device(device
);
638 dev_set_drvdata(&cdev
->dev
, device
);
639 cdev
->handler
= __tape_do_irq
;
641 ccw_device_get_id(cdev
, &dev_id
);
642 device
->cdev_id
= devid_to_int(&dev_id
);
647 __tape_discard_requests(struct tape_device
*device
)
649 struct tape_request
* request
;
650 struct list_head
* l
, *n
;
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
);
658 /* Decrease ref_count for removed request. */
659 request
->device
= NULL
;
660 tape_put_device(device
);
662 if (request
->callback
!= NULL
)
663 request
->callback(request
, request
->callback_data
);
668 * Driverfs tape remove function.
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.
674 tape_generic_remove(struct ccw_device
*cdev
)
676 struct tape_device
* device
;
678 device
= dev_get_drvdata(&cdev
->dev
);
682 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device
->cdev_id
, cdev
);
684 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
685 switch (device
->tape_state
) {
687 tape_state_set(device
, TS_NOT_OPER
);
692 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
696 * Need only to release the device.
698 tape_state_set(device
, TS_NOT_OPER
);
699 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
700 tape_cleanup_device(device
);
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.
708 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
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
);
718 device
= dev_get_drvdata(&cdev
->dev
);
720 sysfs_remove_group(&cdev
->dev
.kobj
, &tape_attr_group
);
721 dev_set_drvdata(&cdev
->dev
, NULL
);
722 tape_put_device(device
);
727 * Allocate a new tape ccw request
729 struct tape_request
*
730 tape_alloc_request(int cplength
, int datasize
)
732 struct tape_request
*request
;
734 BUG_ON(datasize
> PAGE_SIZE
|| (cplength
*sizeof(struct ccw1
)) > PAGE_SIZE
);
736 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength
, datasize
);
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
);
743 /* allocate channel program */
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");
750 return ERR_PTR(-ENOMEM
);
753 /* alloc small kernel buffer */
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
);
760 return ERR_PTR(-ENOMEM
);
763 DBF_LH(6, "New request %p(%p/%p)\n", request
, request
->cpaddr
,
770 * Free tape ccw request
773 tape_free_request (struct tape_request
* request
)
775 DBF_LH(6, "Free request %p\n", request
);
778 tape_put_device(request
->device
);
779 kfree(request
->cpdata
);
780 kfree(request
->cpaddr
);
785 __tape_start_io(struct tape_device
*device
, struct tape_request
*request
)
789 #ifdef CONFIG_S390_TAPE_BLOCK
790 if (request
->op
== TO_BLOCK
)
791 device
->discipline
->check_locate(device
, request
);
793 rc
= ccw_device_start(
796 (unsigned long) request
,
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);
808 /* Start failed. Remove request and indicate failure. */
809 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc
);
815 __tape_start_next_request(struct tape_device
*device
)
817 struct list_head
*l
, *n
;
818 struct tape_request
*request
;
821 DBF_LH(6, "__tape_start_next_request(%p)\n", device
);
823 * Try to start each request on request queue until one is
824 * started successful.
826 list_for_each_safe(l
, n
, &device
->req_queue
) {
827 request
= list_entry(l
, struct tape_request
, list
);
830 * Avoid race condition if bottom-half was triggered more than
833 if (request
->status
== TAPE_REQUEST_IN_IO
)
836 * Request has already been stopped. We have to wait until
837 * the request is removed from the queue in the interrupt
840 if (request
->status
== TAPE_REQUEST_DONE
)
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.
849 if (request
->status
== TAPE_REQUEST_CANCEL
) {
850 rc
= __tape_cancel_io(device
, request
);
852 rc
= __tape_start_io(device
, request
);
857 /* Set ending status. */
859 request
->status
= TAPE_REQUEST_DONE
;
861 /* Remove from request queue. */
862 list_del(&request
->list
);
865 if (request
->callback
!= NULL
)
866 request
->callback(request
, request
->callback_data
);
871 tape_delayed_next_request(struct work_struct
*work
)
873 struct tape_device
*device
=
874 container_of(work
, struct tape_device
, tape_dnr
.work
);
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
));
882 static void tape_long_busy_timeout(unsigned long data
)
884 struct tape_request
*request
;
885 struct tape_device
*device
;
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
));
900 struct tape_device
* device
,
901 struct tape_request
* request
,
904 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device
, request
, rc
);
907 request
->status
= TAPE_REQUEST_DONE
;
909 /* Remove from request queue. */
910 list_del(&request
->list
);
913 if (request
->callback
!= NULL
)
914 request
->callback(request
, request
->callback_data
);
917 /* Start next request. */
918 if (!list_empty(&device
->req_queue
))
919 __tape_start_next_request(device
);
923 * Write sense data to dbf
926 tape_dump_sense_dbf(struct tape_device
*device
, struct tape_request
*request
,
933 op
= tape_op_verbose
[request
->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]);
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.
952 __tape_start_request(struct tape_device
*device
, struct tape_request
*request
)
956 switch (request
->op
) {
962 if (device
->tape_state
== TS_INIT
)
964 if (device
->tape_state
== TS_UNUSED
)
967 if (device
->tape_state
== TS_BLKUSE
)
969 if (device
->tape_state
!= TS_IN_USE
)
973 /* Increase use count of device for the added request. */
974 request
->device
= tape_get_device(device
);
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
);
982 DBF_LH(5, "Request %p added for execution.\n", request
);
983 list_add(&request
->list
, &device
->req_queue
);
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
);
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.
997 tape_do_io_async(struct tape_device
*device
, struct tape_request
*request
)
1001 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device
, request
);
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
));
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.
1016 __tape_wake_up(struct tape_request
*request
, void *data
)
1018 request
->callback
= NULL
;
1019 wake_up((wait_queue_head_t
*) data
);
1023 tape_do_io(struct tape_device
*device
, struct tape_request
*request
)
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
));
1036 /* Request added to the queue. Wait for its completion. */
1037 wait_event(device
->wait_queue
, (request
->callback
== NULL
));
1038 /* Get rc from request */
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.
1048 __tape_wake_up_interruptible(struct tape_request
*request
, void *data
)
1050 request
->callback
= NULL
;
1051 wake_up_interruptible((wait_queue_head_t
*) data
);
1055 tape_do_io_interruptible(struct tape_device
*device
,
1056 struct tape_request
*request
)
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
));
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. */
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
));
1080 /* Wait for the interrupt that acknowledges the halt. */
1082 rc
= wait_event_interruptible(
1084 (request
->callback
== NULL
)
1086 } while (rc
== -ERESTARTSYS
);
1088 DBF_EVENT(3, "IO stopped on %08x\n", device
->cdev_id
);
1098 tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
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
));
1109 * Tape interrupt routine, called from the ccw_device layer
1112 __tape_do_irq (struct ccw_device
*cdev
, unsigned long intparm
, struct irb
*irb
)
1114 struct tape_device
*device
;
1115 struct tape_request
*request
;
1118 kstat_cpu(smp_processor_id()).irqs
[IOINT_TAP
]++;
1119 device
= dev_get_drvdata(&cdev
->dev
);
1120 if (device
== NULL
) {
1123 request
= (struct tape_request
*) intparm
;
1125 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device
, request
);
1127 /* On special conditions irb is an error pointer */
1129 /* FIXME: What to do with the request? */
1130 switch (PTR_ERR(irb
)) {
1132 DBF_LH(1, "(%08x): Request timed out\n",
1135 __tape_end_request(device
, request
, -EIO
);
1138 DBF_LH(1, "(%08x): Unexpected i/o error %li\n",
1139 device
->cdev_id
, PTR_ERR(irb
));
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
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
);
1161 /* May be an unsolicited irq */
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
);
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);
1185 device
->tape_generic_status
&= ~GMT_ONLINE(~0);
1188 * Any request that does not come back with channel end
1189 * and device end is unusual. Log the sense data.
1191 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1192 tape_dump_sense_dbf(device
, request
, irb
);
1194 /* Upon normal completion the device _is_ online */
1195 device
->tape_generic_status
|= GMT_ONLINE(~0);
1197 if (device
->tape_state
== TS_NOT_OPER
) {
1198 DBF_EVENT(6, "tape:device is not operational\n");
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.
1206 if(request
!= NULL
&& request
->status
== TAPE_REQUEST_DONE
) {
1207 __tape_end_request(device
, request
, -EIO
);
1211 rc
= device
->discipline
->irq(device
, request
, irb
);
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.
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
);
1225 case TAPE_IO_PENDING
:
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
;
1237 rc
= __tape_start_io(device
, request
);
1239 __tape_end_request(device
, request
, rc
);
1242 rc
= __tape_cancel_io(device
, request
);
1244 __tape_end_request(device
, request
, rc
);
1248 DBF_EVENT(6, "xunknownrc\n");
1249 __tape_end_request(device
, request
, -EIO
);
1251 __tape_end_request(device
, request
, rc
);
1258 * Tape device open function used by tape_char & tape_block frontends.
1261 tape_open(struct tape_device
*device
)
1265 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1266 if (device
->tape_state
== TS_NOT_OPER
) {
1267 DBF_EVENT(6, "TAPE:nodev\n");
1269 } else if (device
->tape_state
== TS_IN_USE
) {
1270 DBF_EVENT(6, "TAPE:dbusy\n");
1272 } else if (device
->tape_state
== TS_BLKUSE
) {
1273 DBF_EVENT(6, "TAPE:dbusy\n");
1275 } else if (device
->discipline
!= NULL
&&
1276 !try_module_get(device
->discipline
->owner
)) {
1277 DBF_EVENT(6, "TAPE:nodisc\n");
1280 tape_state_set(device
, TS_IN_USE
);
1283 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1288 * Tape device release function used by tape_char & tape_block frontends.
1291 tape_release(struct tape_device
*device
)
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
));
1302 * Execute a magnetic tape command a number of times.
1305 tape_mtop(struct tape_device
*device
, int mt_op
, int mt_count
)
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
);
1314 if (mt_op
< 0 || mt_op
>= TAPE_NR_MTOPS
)
1316 fn
= device
->discipline
->mtop_array
[mt_op
];
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
) {
1324 for (; mt_count
> 500; mt_count
-= 500)
1325 if ((rc
= fn(device
, 500)) != 0)
1328 rc
= fn(device
, mt_count
);
1330 rc
= fn(device
, mt_count
);
1336 * Tape init function.
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);
1346 DBF_EVENT(3, "tape init\n");
1354 * Tape exit function.
1359 DBF_EVENT(6, "tape exit\n");
1361 /* Get rid of the frontends */
1364 tape_proc_cleanup();
1365 debug_unregister (TAPE_DBF_AREA
);
1368 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1369 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1370 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1371 MODULE_LICENSE("GPL");
1373 module_init(tape_init
);
1374 module_exit(tape_exit
);
1376 EXPORT_SYMBOL(tape_generic_remove
);
1377 EXPORT_SYMBOL(tape_generic_probe
);
1378 EXPORT_SYMBOL(tape_generic_online
);
1379 EXPORT_SYMBOL(tape_generic_offline
);
1380 EXPORT_SYMBOL(tape_generic_pm_suspend
);
1381 EXPORT_SYMBOL(tape_put_device
);
1382 EXPORT_SYMBOL(tape_get_device
);
1383 EXPORT_SYMBOL(tape_state_verbose
);
1384 EXPORT_SYMBOL(tape_op_verbose
);
1385 EXPORT_SYMBOL(tape_state_set
);
1386 EXPORT_SYMBOL(tape_med_state_set
);
1387 EXPORT_SYMBOL(tape_alloc_request
);
1388 EXPORT_SYMBOL(tape_free_request
);
1389 EXPORT_SYMBOL(tape_dump_sense_dbf
);
1390 EXPORT_SYMBOL(tape_do_io
);
1391 EXPORT_SYMBOL(tape_do_io_async
);
1392 EXPORT_SYMBOL(tape_do_io_interruptible
);
1393 EXPORT_SYMBOL(tape_cancel_io
);
1394 EXPORT_SYMBOL(tape_mtop
);