2 * basic function of the tape device driver
4 * S390 and zSeries version
5 * Copyright IBM Corp. 2001, 2009
6 * Author(s): Carsten Otte <cotte@de.ibm.com>
7 * Michael Holzheu <holzheu@de.ibm.com>
8 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
9 * Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Stefan Bader <shbader@de.ibm.com>
13 #define KMSG_COMPONENT "tape"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16 #include <linux/module.h>
17 #include <linux/init.h> // for kernel parameters
18 #include <linux/kmod.h> // for requesting modules
19 #include <linux/spinlock.h> // for locks
20 #include <linux/vmalloc.h>
21 #include <linux/list.h>
22 #include <linux/slab.h>
24 #include <asm/types.h> // for variable types
26 #define TAPE_DBF_AREA tape_core_dbf
31 #define LONG_BUSY_TIMEOUT 180 /* seconds */
33 static void __tape_do_irq (struct ccw_device
*, unsigned long, struct irb
*);
34 static void tape_delayed_next_request(struct work_struct
*);
35 static void tape_long_busy_timeout(unsigned long data
);
38 * One list to contain all tape devices of all disciplines, so
39 * we can assign the devices to minor numbers of the same major
40 * The list is protected by the rwlock
42 static LIST_HEAD(tape_device_list
);
43 static DEFINE_RWLOCK(tape_device_lock
);
46 * Pointer to debug area.
48 debug_info_t
*TAPE_DBF_AREA
= NULL
;
49 EXPORT_SYMBOL(TAPE_DBF_AREA
);
52 * Printable strings for tape enumerations.
54 const char *tape_state_verbose
[TS_SIZE
] =
56 [TS_UNUSED
] = "UNUSED",
57 [TS_IN_USE
] = "IN_USE",
58 [TS_BLKUSE
] = "BLKUSE",
60 [TS_NOT_OPER
] = "NOT_OP"
63 const char *tape_op_verbose
[TO_SIZE
] =
65 [TO_BLOCK
] = "BLK", [TO_BSB
] = "BSB",
66 [TO_BSF
] = "BSF", [TO_DSE
] = "DSE",
67 [TO_FSB
] = "FSB", [TO_FSF
] = "FSF",
68 [TO_LBL
] = "LBL", [TO_NOP
] = "NOP",
69 [TO_RBA
] = "RBA", [TO_RBI
] = "RBI",
70 [TO_RFO
] = "RFO", [TO_REW
] = "REW",
71 [TO_RUN
] = "RUN", [TO_WRI
] = "WRI",
72 [TO_WTM
] = "WTM", [TO_MSEN
] = "MSN",
73 [TO_LOAD
] = "LOA", [TO_READ_CONFIG
] = "RCF",
74 [TO_READ_ATTMSG
] = "RAT",
75 [TO_DIS
] = "DIS", [TO_ASSIGN
] = "ASS",
76 [TO_UNASSIGN
] = "UAS", [TO_CRYPT_ON
] = "CON",
77 [TO_CRYPT_OFF
] = "COF", [TO_KEKL_SET
] = "KLS",
78 [TO_KEKL_QUERY
] = "KLQ",[TO_RDC
] = "RDC",
81 static int devid_to_int(struct ccw_dev_id
*dev_id
)
83 return dev_id
->devno
+ (dev_id
->ssid
<< 16);
87 * Some channel attached tape specific attributes.
89 * FIXME: In the future the first_minor and blocksize attribute should be
90 * replaced by a link to the cdev tree.
93 tape_medium_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
95 struct tape_device
*tdev
;
97 tdev
= dev_get_drvdata(dev
);
98 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->medium_state
);
102 DEVICE_ATTR(medium_state
, 0444, tape_medium_state_show
, NULL
);
105 tape_first_minor_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
107 struct tape_device
*tdev
;
109 tdev
= dev_get_drvdata(dev
);
110 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->first_minor
);
114 DEVICE_ATTR(first_minor
, 0444, tape_first_minor_show
, NULL
);
117 tape_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
119 struct tape_device
*tdev
;
121 tdev
= dev_get_drvdata(dev
);
122 return scnprintf(buf
, PAGE_SIZE
, "%s\n", (tdev
->first_minor
< 0) ?
123 "OFFLINE" : tape_state_verbose
[tdev
->tape_state
]);
127 DEVICE_ATTR(state
, 0444, tape_state_show
, NULL
);
130 tape_operation_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
132 struct tape_device
*tdev
;
135 tdev
= dev_get_drvdata(dev
);
136 if (tdev
->first_minor
< 0)
137 return scnprintf(buf
, PAGE_SIZE
, "N/A\n");
139 spin_lock_irq(get_ccwdev_lock(tdev
->cdev
));
140 if (list_empty(&tdev
->req_queue
))
141 rc
= scnprintf(buf
, PAGE_SIZE
, "---\n");
143 struct tape_request
*req
;
145 req
= list_entry(tdev
->req_queue
.next
, struct tape_request
,
147 rc
= scnprintf(buf
,PAGE_SIZE
, "%s\n", tape_op_verbose
[req
->op
]);
149 spin_unlock_irq(get_ccwdev_lock(tdev
->cdev
));
154 DEVICE_ATTR(operation
, 0444, tape_operation_show
, NULL
);
157 tape_blocksize_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
159 struct tape_device
*tdev
;
161 tdev
= dev_get_drvdata(dev
);
163 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->char_data
.block_size
);
167 DEVICE_ATTR(blocksize
, 0444, tape_blocksize_show
, NULL
);
169 static struct attribute
*tape_attrs
[] = {
170 &dev_attr_medium_state
.attr
,
171 &dev_attr_first_minor
.attr
,
172 &dev_attr_state
.attr
,
173 &dev_attr_operation
.attr
,
174 &dev_attr_blocksize
.attr
,
178 static struct attribute_group tape_attr_group
= {
183 * Tape state functions
186 tape_state_set(struct tape_device
*device
, enum tape_state newstate
)
190 if (device
->tape_state
== TS_NOT_OPER
) {
191 DBF_EVENT(3, "ts_set err: not oper\n");
194 DBF_EVENT(4, "ts. dev: %x\n", device
->first_minor
);
195 DBF_EVENT(4, "old ts:\t\n");
196 if (device
->tape_state
< TS_SIZE
&& device
->tape_state
>=0 )
197 str
= tape_state_verbose
[device
->tape_state
];
200 DBF_EVENT(4, "%s\n", str
);
201 DBF_EVENT(4, "new ts:\t\n");
202 if (newstate
< TS_SIZE
&& newstate
>= 0)
203 str
= tape_state_verbose
[newstate
];
206 DBF_EVENT(4, "%s\n", str
);
207 device
->tape_state
= newstate
;
208 wake_up(&device
->state_change_wq
);
211 struct tape_med_state_work_data
{
212 struct tape_device
*device
;
213 enum tape_medium_state state
;
214 struct work_struct work
;
218 tape_med_state_work_handler(struct work_struct
*work
)
220 static char env_state_loaded
[] = "MEDIUM_STATE=LOADED";
221 static char env_state_unloaded
[] = "MEDIUM_STATE=UNLOADED";
222 struct tape_med_state_work_data
*p
=
223 container_of(work
, struct tape_med_state_work_data
, work
);
224 struct tape_device
*device
= p
->device
;
225 char *envp
[] = { NULL
, NULL
};
229 pr_info("%s: The tape cartridge has been successfully "
230 "unloaded\n", dev_name(&device
->cdev
->dev
));
231 envp
[0] = env_state_unloaded
;
232 kobject_uevent_env(&device
->cdev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
235 pr_info("%s: A tape cartridge has been mounted\n",
236 dev_name(&device
->cdev
->dev
));
237 envp
[0] = env_state_loaded
;
238 kobject_uevent_env(&device
->cdev
->dev
.kobj
, KOBJ_CHANGE
, envp
);
243 tape_put_device(device
);
248 tape_med_state_work(struct tape_device
*device
, enum tape_medium_state state
)
250 struct tape_med_state_work_data
*p
;
252 p
= kzalloc(sizeof(*p
), GFP_ATOMIC
);
254 INIT_WORK(&p
->work
, tape_med_state_work_handler
);
255 p
->device
= tape_get_device(device
);
257 schedule_work(&p
->work
);
262 tape_med_state_set(struct tape_device
*device
, enum tape_medium_state newstate
)
264 enum tape_medium_state oldstate
;
266 oldstate
= device
->medium_state
;
267 if (oldstate
== newstate
)
269 device
->medium_state
= newstate
;
272 device
->tape_generic_status
|= GMT_DR_OPEN(~0);
273 if (oldstate
== MS_LOADED
)
274 tape_med_state_work(device
, MS_UNLOADED
);
277 device
->tape_generic_status
&= ~GMT_DR_OPEN(~0);
278 if (oldstate
== MS_UNLOADED
)
279 tape_med_state_work(device
, MS_LOADED
);
284 wake_up(&device
->state_change_wq
);
288 * Stop running ccw. Has to be called with the device lock held.
291 __tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
296 /* Check if interrupt has already been processed */
297 if (request
->callback
== NULL
)
301 for (retries
= 0; retries
< 5; retries
++) {
302 rc
= ccw_device_clear(device
->cdev
, (long) request
);
306 request
->status
= TAPE_REQUEST_DONE
;
309 request
->status
= TAPE_REQUEST_CANCEL
;
310 schedule_delayed_work(&device
->tape_dnr
, 0);
313 DBF_EXCEPTION(2, "device gone, retry\n");
316 DBF_EXCEPTION(2, "I/O error, retry\n");
327 * Add device into the sorted list, giving it the first
328 * available minor number.
331 tape_assign_minor(struct tape_device
*device
)
333 struct tape_device
*tmp
;
337 write_lock(&tape_device_lock
);
338 list_for_each_entry(tmp
, &tape_device_list
, node
) {
339 if (minor
< tmp
->first_minor
)
341 minor
+= TAPE_MINORS_PER_DEV
;
344 write_unlock(&tape_device_lock
);
347 device
->first_minor
= minor
;
348 list_add_tail(&device
->node
, &tmp
->node
);
349 write_unlock(&tape_device_lock
);
353 /* remove device from the list */
355 tape_remove_minor(struct tape_device
*device
)
357 write_lock(&tape_device_lock
);
358 list_del_init(&device
->node
);
359 device
->first_minor
= -1;
360 write_unlock(&tape_device_lock
);
364 * Set a device online.
366 * This function is called by the common I/O layer to move a device from the
367 * detected but offline into the online state.
368 * If we return an error (RC < 0) the device remains in the offline state. This
369 * can happen if the device is assigned somewhere else, for example.
372 tape_generic_online(struct tape_device
*device
,
373 struct tape_discipline
*discipline
)
377 DBF_LH(6, "tape_enable_device(%p, %p)\n", device
, discipline
);
379 if (device
->tape_state
!= TS_INIT
) {
380 DBF_LH(3, "Tapestate not INIT (%d)\n", device
->tape_state
);
384 init_timer(&device
->lb_timeout
);
385 device
->lb_timeout
.function
= tape_long_busy_timeout
;
387 /* Let the discipline have a go at the device. */
388 device
->discipline
= discipline
;
389 if (!try_module_get(discipline
->owner
)) {
393 rc
= discipline
->setup_device(device
);
396 rc
= tape_assign_minor(device
);
400 rc
= tapechar_setup_device(device
);
404 tape_state_set(device
, TS_UNUSED
);
406 DBF_LH(3, "(%08x): Drive set online\n", device
->cdev_id
);
411 tape_remove_minor(device
);
413 device
->discipline
->cleanup_device(device
);
414 device
->discipline
= NULL
;
416 module_put(discipline
->owner
);
421 tape_cleanup_device(struct tape_device
*device
)
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
);
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
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.
443 int tape_generic_pm_suspend(struct ccw_device
*cdev
)
445 struct tape_device
*device
;
447 device
= dev_get_drvdata(&cdev
->dev
);
452 DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n",
453 device
->cdev_id
, device
);
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
));
461 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
462 switch (device
->tape_state
) {
466 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
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
));
475 DBF_LH(3, "(%08x): Drive suspended.\n", device
->cdev_id
);
480 * Set device offline.
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.
487 tape_generic_offline(struct ccw_device
*cdev
)
489 struct tape_device
*device
;
491 device
= dev_get_drvdata(&cdev
->dev
);
496 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
497 device
->cdev_id
, device
);
499 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
500 switch (device
->tape_state
) {
503 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
506 tape_state_set(device
, TS_INIT
);
507 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
508 tape_cleanup_device(device
);
511 DBF_EVENT(3, "(%08x): Set offline failed "
514 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
518 DBF_LH(3, "(%08x): Drive set offline.\n", device
->cdev_id
);
523 * Allocate memory for a new device structure.
525 static struct tape_device
*
526 tape_alloc_device(void)
528 struct tape_device
*device
;
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
);
535 device
->modeset_byte
= kmalloc(1, GFP_KERNEL
| GFP_DMA
);
536 if (device
->modeset_byte
== NULL
) {
537 DBF_EXCEPTION(2, "ti:no mem\n");
539 return ERR_PTR(-ENOMEM
);
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
);
557 * Get a reference to an existing device structure. This will automatically
558 * increment the reference count.
561 tape_get_device(struct tape_device
*device
)
565 count
= atomic_inc_return(&device
->ref_count
);
566 DBF_EVENT(4, "tape_get_device(%p) = %i\n", device
, count
);
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.
577 tape_put_device(struct tape_device
*device
)
581 count
= atomic_dec_return(&device
->ref_count
);
582 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device
, count
);
585 kfree(device
->modeset_byte
);
591 * Find tape device by a device index.
594 tape_find_device(int devindex
)
596 struct tape_device
*device
, *tmp
;
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
);
606 read_unlock(&tape_device_lock
);
611 * Driverfs tape probe function.
614 tape_generic_probe(struct ccw_device
*cdev
)
616 struct tape_device
*device
;
618 struct ccw_dev_id dev_id
;
620 device
= tape_alloc_device();
623 ccw_device_set_options(cdev
, CCWDEV_DO_PATHGROUP
|
624 CCWDEV_DO_MULTIPATH
);
625 ret
= sysfs_create_group(&cdev
->dev
.kobj
, &tape_attr_group
);
627 tape_put_device(device
);
630 dev_set_drvdata(&cdev
->dev
, device
);
631 cdev
->handler
= __tape_do_irq
;
633 ccw_device_get_id(cdev
, &dev_id
);
634 device
->cdev_id
= devid_to_int(&dev_id
);
639 __tape_discard_requests(struct tape_device
*device
)
641 struct tape_request
* request
;
642 struct list_head
* l
, *n
;
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
);
650 /* Decrease ref_count for removed request. */
651 request
->device
= NULL
;
652 tape_put_device(device
);
654 if (request
->callback
!= NULL
)
655 request
->callback(request
, request
->callback_data
);
660 * Driverfs tape remove function.
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.
666 tape_generic_remove(struct ccw_device
*cdev
)
668 struct tape_device
* device
;
670 device
= dev_get_drvdata(&cdev
->dev
);
674 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device
->cdev_id
, cdev
);
676 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
677 switch (device
->tape_state
) {
679 tape_state_set(device
, TS_NOT_OPER
);
684 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
688 * Need only to release the device.
690 tape_state_set(device
, TS_NOT_OPER
);
691 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
692 tape_cleanup_device(device
);
696 * There may be requests on the queue. We will not get
697 * an interrupt for a request that was running. So we
698 * just post them all as I/O errors.
700 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
702 pr_warn("%s: A tape unit was detached while in use\n",
703 dev_name(&device
->cdev
->dev
));
704 tape_state_set(device
, TS_NOT_OPER
);
705 __tape_discard_requests(device
);
706 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
707 tape_cleanup_device(device
);
710 device
= dev_get_drvdata(&cdev
->dev
);
712 sysfs_remove_group(&cdev
->dev
.kobj
, &tape_attr_group
);
713 dev_set_drvdata(&cdev
->dev
, NULL
);
714 tape_put_device(device
);
719 * Allocate a new tape ccw request
721 struct tape_request
*
722 tape_alloc_request(int cplength
, int datasize
)
724 struct tape_request
*request
;
726 BUG_ON(datasize
> PAGE_SIZE
|| (cplength
*sizeof(struct ccw1
)) > PAGE_SIZE
);
728 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength
, datasize
);
730 request
= kzalloc(sizeof(struct tape_request
), GFP_KERNEL
);
731 if (request
== NULL
) {
732 DBF_EXCEPTION(1, "cqra nomem\n");
733 return ERR_PTR(-ENOMEM
);
735 /* allocate channel program */
737 request
->cpaddr
= kcalloc(cplength
, sizeof(struct ccw1
),
738 GFP_ATOMIC
| GFP_DMA
);
739 if (request
->cpaddr
== NULL
) {
740 DBF_EXCEPTION(1, "cqra nomem\n");
742 return ERR_PTR(-ENOMEM
);
745 /* alloc small kernel buffer */
747 request
->cpdata
= kzalloc(datasize
, GFP_KERNEL
| GFP_DMA
);
748 if (request
->cpdata
== NULL
) {
749 DBF_EXCEPTION(1, "cqra nomem\n");
750 kfree(request
->cpaddr
);
752 return ERR_PTR(-ENOMEM
);
755 DBF_LH(6, "New request %p(%p/%p)\n", request
, request
->cpaddr
,
762 * Free tape ccw request
765 tape_free_request (struct tape_request
* request
)
767 DBF_LH(6, "Free request %p\n", request
);
770 tape_put_device(request
->device
);
771 kfree(request
->cpdata
);
772 kfree(request
->cpaddr
);
777 __tape_start_io(struct tape_device
*device
, struct tape_request
*request
)
781 rc
= ccw_device_start(
784 (unsigned long) request
,
789 request
->status
= TAPE_REQUEST_IN_IO
;
790 } else if (rc
== -EBUSY
) {
791 /* The common I/O subsystem is currently busy. Retry later. */
792 request
->status
= TAPE_REQUEST_QUEUED
;
793 schedule_delayed_work(&device
->tape_dnr
, 0);
796 /* Start failed. Remove request and indicate failure. */
797 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc
);
803 __tape_start_next_request(struct tape_device
*device
)
805 struct list_head
*l
, *n
;
806 struct tape_request
*request
;
809 DBF_LH(6, "__tape_start_next_request(%p)\n", device
);
811 * Try to start each request on request queue until one is
812 * started successful.
814 list_for_each_safe(l
, n
, &device
->req_queue
) {
815 request
= list_entry(l
, struct tape_request
, list
);
818 * Avoid race condition if bottom-half was triggered more than
821 if (request
->status
== TAPE_REQUEST_IN_IO
)
824 * Request has already been stopped. We have to wait until
825 * the request is removed from the queue in the interrupt
828 if (request
->status
== TAPE_REQUEST_DONE
)
832 * We wanted to cancel the request but the common I/O layer
833 * was busy at that time. This can only happen if this
834 * function is called by delayed_next_request.
835 * Otherwise we start the next request on the queue.
837 if (request
->status
== TAPE_REQUEST_CANCEL
) {
838 rc
= __tape_cancel_io(device
, request
);
840 rc
= __tape_start_io(device
, request
);
845 /* Set ending status. */
847 request
->status
= TAPE_REQUEST_DONE
;
849 /* Remove from request queue. */
850 list_del(&request
->list
);
853 if (request
->callback
!= NULL
)
854 request
->callback(request
, request
->callback_data
);
859 tape_delayed_next_request(struct work_struct
*work
)
861 struct tape_device
*device
=
862 container_of(work
, struct tape_device
, tape_dnr
.work
);
864 DBF_LH(6, "tape_delayed_next_request(%p)\n", device
);
865 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
866 __tape_start_next_request(device
);
867 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
870 static void tape_long_busy_timeout(unsigned long data
)
872 struct tape_request
*request
;
873 struct tape_device
*device
;
875 device
= (struct tape_device
*) data
;
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 device
->lb_timeout
.data
= 0UL;
882 tape_put_device(device
);
883 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
888 struct tape_device
* device
,
889 struct tape_request
* request
,
892 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device
, request
, rc
);
895 request
->status
= TAPE_REQUEST_DONE
;
897 /* Remove from request queue. */
898 list_del(&request
->list
);
901 if (request
->callback
!= NULL
)
902 request
->callback(request
, request
->callback_data
);
905 /* Start next request. */
906 if (!list_empty(&device
->req_queue
))
907 __tape_start_next_request(device
);
911 * Write sense data to dbf
914 tape_dump_sense_dbf(struct tape_device
*device
, struct tape_request
*request
,
921 op
= tape_op_verbose
[request
->op
];
924 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
925 irb
->scsw
.cmd
.dstat
, irb
->scsw
.cmd
.cstat
);
926 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device
->cdev_id
, op
);
927 sptr
= (unsigned int *) irb
->ecw
;
928 DBF_EVENT(3, "%08x %08x\n", sptr
[0], sptr
[1]);
929 DBF_EVENT(3, "%08x %08x\n", sptr
[2], sptr
[3]);
930 DBF_EVENT(3, "%08x %08x\n", sptr
[4], sptr
[5]);
931 DBF_EVENT(3, "%08x %08x\n", sptr
[6], sptr
[7]);
935 * I/O helper function. Adds the request to the request queue
936 * and starts it if the tape is idle. Has to be called with
937 * the device lock held.
940 __tape_start_request(struct tape_device
*device
, struct tape_request
*request
)
944 switch (request
->op
) {
950 if (device
->tape_state
== TS_INIT
)
952 if (device
->tape_state
== TS_UNUSED
)
955 if (device
->tape_state
== TS_BLKUSE
)
957 if (device
->tape_state
!= TS_IN_USE
)
961 /* Increase use count of device for the added request. */
962 request
->device
= tape_get_device(device
);
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
);
970 DBF_LH(5, "Request %p added for execution.\n", request
);
971 list_add(&request
->list
, &device
->req_queue
);
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
);
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.
985 tape_do_io_async(struct tape_device
*device
, struct tape_request
*request
)
989 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device
, request
);
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
));
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.
1004 __tape_wake_up(struct tape_request
*request
, void *data
)
1006 request
->callback
= NULL
;
1007 wake_up((wait_queue_head_t
*) data
);
1011 tape_do_io(struct tape_device
*device
, struct tape_request
*request
)
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
));
1024 /* Request added to the queue. Wait for its completion. */
1025 wait_event(device
->wait_queue
, (request
->callback
== NULL
));
1026 /* Get rc from request */
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.
1036 __tape_wake_up_interruptible(struct tape_request
*request
, void *data
)
1038 request
->callback
= NULL
;
1039 wake_up_interruptible((wait_queue_head_t
*) data
);
1043 tape_do_io_interruptible(struct tape_device
*device
,
1044 struct tape_request
*request
)
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
));
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. */
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
));
1068 /* Wait for the interrupt that acknowledges the halt. */
1070 rc
= wait_event_interruptible(
1072 (request
->callback
== NULL
)
1074 } while (rc
== -ERESTARTSYS
);
1076 DBF_EVENT(3, "IO stopped on %08x\n", device
->cdev_id
);
1086 tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
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
));
1097 * Tape interrupt routine, called from the ccw_device layer
1100 __tape_do_irq (struct ccw_device
*cdev
, unsigned long intparm
, struct irb
*irb
)
1102 struct tape_device
*device
;
1103 struct tape_request
*request
;
1106 device
= dev_get_drvdata(&cdev
->dev
);
1107 if (device
== NULL
) {
1110 request
= (struct tape_request
*) intparm
;
1112 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device
, request
);
1114 /* On special conditions irb is an error pointer */
1116 /* FIXME: What to do with the request? */
1117 switch (PTR_ERR(irb
)) {
1119 DBF_LH(1, "(%08x): Request timed out\n",
1122 __tape_end_request(device
, request
, -EIO
);
1125 DBF_LH(1, "(%08x): Unexpected i/o error %li\n",
1126 device
->cdev_id
, PTR_ERR(irb
));
1132 * If the condition code is not zero and the start function bit is
1133 * still set, this is an deferred error and the last start I/O did
1134 * not succeed. At this point the condition that caused the deferred
1135 * error might still apply. So we just schedule the request to be
1138 if (irb
->scsw
.cmd
.cc
!= 0 &&
1139 (irb
->scsw
.cmd
.fctl
& SCSW_FCTL_START_FUNC
) &&
1140 (request
->status
== TAPE_REQUEST_IN_IO
)) {
1141 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1142 device
->cdev_id
, irb
->scsw
.cmd
.cc
, irb
->scsw
.cmd
.fctl
);
1143 request
->status
= TAPE_REQUEST_QUEUED
;
1144 schedule_delayed_work(&device
->tape_dnr
, HZ
);
1148 /* May be an unsolicited irq */
1150 request
->rescnt
= irb
->scsw
.cmd
.count
;
1151 else if ((irb
->scsw
.cmd
.dstat
== 0x85 || irb
->scsw
.cmd
.dstat
== 0x80) &&
1152 !list_empty(&device
->req_queue
)) {
1153 /* Not Ready to Ready after long busy ? */
1154 struct tape_request
*req
;
1155 req
= list_entry(device
->req_queue
.next
,
1156 struct tape_request
, list
);
1157 if (req
->status
== TAPE_REQUEST_LONG_BUSY
) {
1158 DBF_EVENT(3, "(%08x): del timer\n", device
->cdev_id
);
1159 if (del_timer(&device
->lb_timeout
)) {
1160 device
->lb_timeout
.data
= 0UL;
1161 tape_put_device(device
);
1162 __tape_start_next_request(device
);
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);
1172 device
->tape_generic_status
&= ~GMT_ONLINE(~0);
1175 * Any request that does not come back with channel end
1176 * and device end is unusual. Log the sense data.
1178 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1179 tape_dump_sense_dbf(device
, request
, irb
);
1181 /* Upon normal completion the device _is_ online */
1182 device
->tape_generic_status
|= GMT_ONLINE(~0);
1184 if (device
->tape_state
== TS_NOT_OPER
) {
1185 DBF_EVENT(6, "tape:device is not operational\n");
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.
1193 if(request
!= NULL
&& request
->status
== TAPE_REQUEST_DONE
) {
1194 __tape_end_request(device
, request
, -EIO
);
1198 rc
= device
->discipline
->irq(device
, request
, irb
);
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.
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
);
1212 case TAPE_IO_PENDING
:
1214 case TAPE_IO_LONG_BUSY
:
1215 device
->lb_timeout
.data
=
1216 (unsigned long) tape_get_device(device
);
1217 device
->lb_timeout
.expires
= jiffies
+
1218 LONG_BUSY_TIMEOUT
* HZ
;
1219 DBF_EVENT(3, "(%08x): add timer\n", device
->cdev_id
);
1220 add_timer(&device
->lb_timeout
);
1221 request
->status
= TAPE_REQUEST_LONG_BUSY
;
1224 rc
= __tape_start_io(device
, request
);
1226 __tape_end_request(device
, request
, rc
);
1229 rc
= __tape_cancel_io(device
, request
);
1231 __tape_end_request(device
, request
, rc
);
1235 DBF_EVENT(6, "xunknownrc\n");
1236 __tape_end_request(device
, request
, -EIO
);
1238 __tape_end_request(device
, request
, rc
);
1245 * Tape device open function used by tape_char frontend.
1248 tape_open(struct tape_device
*device
)
1252 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1253 if (device
->tape_state
== TS_NOT_OPER
) {
1254 DBF_EVENT(6, "TAPE:nodev\n");
1256 } else if (device
->tape_state
== TS_IN_USE
) {
1257 DBF_EVENT(6, "TAPE:dbusy\n");
1259 } else if (device
->tape_state
== TS_BLKUSE
) {
1260 DBF_EVENT(6, "TAPE:dbusy\n");
1262 } else if (device
->discipline
!= NULL
&&
1263 !try_module_get(device
->discipline
->owner
)) {
1264 DBF_EVENT(6, "TAPE:nodisc\n");
1267 tape_state_set(device
, TS_IN_USE
);
1270 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1275 * Tape device release function used by tape_char frontend.
1278 tape_release(struct tape_device
*device
)
1280 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1281 if (device
->tape_state
== TS_IN_USE
)
1282 tape_state_set(device
, TS_UNUSED
);
1283 module_put(device
->discipline
->owner
);
1284 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1289 * Execute a magnetic tape command a number of times.
1292 tape_mtop(struct tape_device
*device
, int mt_op
, int mt_count
)
1297 DBF_EVENT(6, "TAPE:mtio\n");
1298 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op
);
1299 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count
);
1301 if (mt_op
< 0 || mt_op
>= TAPE_NR_MTOPS
)
1303 fn
= device
->discipline
->mtop_array
[mt_op
];
1307 /* We assume that the backends can handle count up to 500. */
1308 if (mt_op
== MTBSR
|| mt_op
== MTFSR
|| mt_op
== MTFSF
||
1309 mt_op
== MTBSF
|| mt_op
== MTFSFM
|| mt_op
== MTBSFM
) {
1311 for (; mt_count
> 500; mt_count
-= 500)
1312 if ((rc
= fn(device
, 500)) != 0)
1315 rc
= fn(device
, mt_count
);
1317 rc
= fn(device
, mt_count
);
1323 * Tape init function.
1328 TAPE_DBF_AREA
= debug_register ( "tape", 2, 2, 4*sizeof(long));
1329 debug_register_view(TAPE_DBF_AREA
, &debug_sprintf_view
);
1330 #ifdef DBF_LIKE_HELL
1331 debug_set_level(TAPE_DBF_AREA
, 6);
1333 DBF_EVENT(3, "tape init\n");
1340 * Tape exit function.
1345 DBF_EVENT(6, "tape exit\n");
1347 /* Get rid of the frontends */
1349 tape_proc_cleanup();
1350 debug_unregister (TAPE_DBF_AREA
);
1353 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1354 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1355 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1356 MODULE_LICENSE("GPL");
1358 module_init(tape_init
);
1359 module_exit(tape_exit
);
1361 EXPORT_SYMBOL(tape_generic_remove
);
1362 EXPORT_SYMBOL(tape_generic_probe
);
1363 EXPORT_SYMBOL(tape_generic_online
);
1364 EXPORT_SYMBOL(tape_generic_offline
);
1365 EXPORT_SYMBOL(tape_generic_pm_suspend
);
1366 EXPORT_SYMBOL(tape_put_device
);
1367 EXPORT_SYMBOL(tape_get_device
);
1368 EXPORT_SYMBOL(tape_state_verbose
);
1369 EXPORT_SYMBOL(tape_op_verbose
);
1370 EXPORT_SYMBOL(tape_state_set
);
1371 EXPORT_SYMBOL(tape_med_state_set
);
1372 EXPORT_SYMBOL(tape_alloc_request
);
1373 EXPORT_SYMBOL(tape_free_request
);
1374 EXPORT_SYMBOL(tape_dump_sense_dbf
);
1375 EXPORT_SYMBOL(tape_do_io
);
1376 EXPORT_SYMBOL(tape_do_io_async
);
1377 EXPORT_SYMBOL(tape_do_io_interruptible
);
1378 EXPORT_SYMBOL(tape_cancel_io
);
1379 EXPORT_SYMBOL(tape_mtop
);