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/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>
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
);
212 tape_med_state_set(struct tape_device
*device
, enum tape_medium_state newstate
)
214 if (device
->medium_state
== newstate
)
218 device
->tape_generic_status
|= GMT_DR_OPEN(~0);
219 if (device
->medium_state
== MS_LOADED
)
220 pr_info("%s: The tape cartridge has been successfully "
221 "unloaded\n", dev_name(&device
->cdev
->dev
));
224 device
->tape_generic_status
&= ~GMT_DR_OPEN(~0);
225 if (device
->medium_state
== MS_UNLOADED
)
226 pr_info("%s: A tape cartridge has been mounted\n",
227 dev_name(&device
->cdev
->dev
));
233 device
->medium_state
= newstate
;
234 wake_up(&device
->state_change_wq
);
238 * Stop running ccw. Has to be called with the device lock held.
241 __tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
246 /* Check if interrupt has already been processed */
247 if (request
->callback
== NULL
)
251 for (retries
= 0; retries
< 5; retries
++) {
252 rc
= ccw_device_clear(device
->cdev
, (long) request
);
256 request
->status
= TAPE_REQUEST_DONE
;
259 request
->status
= TAPE_REQUEST_CANCEL
;
260 schedule_delayed_work(&device
->tape_dnr
, 0);
263 DBF_EXCEPTION(2, "device gone, retry\n");
266 DBF_EXCEPTION(2, "I/O error, retry\n");
277 * Add device into the sorted list, giving it the first
278 * available minor number.
281 tape_assign_minor(struct tape_device
*device
)
283 struct tape_device
*tmp
;
287 write_lock(&tape_device_lock
);
288 list_for_each_entry(tmp
, &tape_device_list
, node
) {
289 if (minor
< tmp
->first_minor
)
291 minor
+= TAPE_MINORS_PER_DEV
;
294 write_unlock(&tape_device_lock
);
297 device
->first_minor
= minor
;
298 list_add_tail(&device
->node
, &tmp
->node
);
299 write_unlock(&tape_device_lock
);
303 /* remove device from the list */
305 tape_remove_minor(struct tape_device
*device
)
307 write_lock(&tape_device_lock
);
308 list_del_init(&device
->node
);
309 device
->first_minor
= -1;
310 write_unlock(&tape_device_lock
);
314 * Set a device online.
316 * This function is called by the common I/O layer to move a device from the
317 * detected but offline into the online state.
318 * If we return an error (RC < 0) the device remains in the offline state. This
319 * can happen if the device is assigned somewhere else, for example.
322 tape_generic_online(struct tape_device
*device
,
323 struct tape_discipline
*discipline
)
327 DBF_LH(6, "tape_enable_device(%p, %p)\n", device
, discipline
);
329 if (device
->tape_state
!= TS_INIT
) {
330 DBF_LH(3, "Tapestate not INIT (%d)\n", device
->tape_state
);
334 init_timer(&device
->lb_timeout
);
335 device
->lb_timeout
.function
= tape_long_busy_timeout
;
337 /* Let the discipline have a go at the device. */
338 device
->discipline
= discipline
;
339 if (!try_module_get(discipline
->owner
)) {
343 rc
= discipline
->setup_device(device
);
346 rc
= tape_assign_minor(device
);
350 rc
= tapechar_setup_device(device
);
353 rc
= tapeblock_setup_device(device
);
357 tape_state_set(device
, TS_UNUSED
);
359 DBF_LH(3, "(%08x): Drive set online\n", device
->cdev_id
);
364 tapechar_cleanup_device(device
);
366 tape_remove_minor(device
);
368 device
->discipline
->cleanup_device(device
);
369 device
->discipline
= NULL
;
371 module_put(discipline
->owner
);
376 tape_cleanup_device(struct tape_device
*device
)
378 tapeblock_cleanup_device(device
);
379 tapechar_cleanup_device(device
);
380 device
->discipline
->cleanup_device(device
);
381 module_put(device
->discipline
->owner
);
382 tape_remove_minor(device
);
383 tape_med_state_set(device
, MS_UNKNOWN
);
389 * Called by the common I/O layer if the drive should be suspended on user
390 * request. We refuse to suspend if the device is loaded or in use for the
392 * While the Linux guest is suspended, it might be logged off which causes
393 * devices to be detached. Tape devices are automatically rewound and unloaded
394 * during DETACH processing (unless the tape device was attached with the
395 * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to
396 * resume the original state of the tape device, since we would need to
397 * manually re-load the cartridge which was active at suspend time.
399 int tape_generic_pm_suspend(struct ccw_device
*cdev
)
401 struct tape_device
*device
;
403 device
= dev_get_drvdata(&cdev
->dev
);
408 DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n",
409 device
->cdev_id
, device
);
411 if (device
->medium_state
!= MS_UNLOADED
) {
412 pr_err("A cartridge is loaded in tape device %s, "
413 "refusing to suspend\n", dev_name(&cdev
->dev
));
417 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
418 switch (device
->tape_state
) {
422 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
425 pr_err("Tape device %s is busy, refusing to "
426 "suspend\n", dev_name(&cdev
->dev
));
427 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
431 DBF_LH(3, "(%08x): Drive suspended.\n", device
->cdev_id
);
436 * Set device offline.
438 * Called by the common I/O layer if the drive should set offline on user
439 * request. We may prevent this by returning an error.
440 * Manual offline is only allowed while the drive is not in use.
443 tape_generic_offline(struct ccw_device
*cdev
)
445 struct tape_device
*device
;
447 device
= dev_get_drvdata(&cdev
->dev
);
452 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
453 device
->cdev_id
, device
);
455 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
456 switch (device
->tape_state
) {
459 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
462 tape_state_set(device
, TS_INIT
);
463 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
464 tape_cleanup_device(device
);
467 DBF_EVENT(3, "(%08x): Set offline failed "
470 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
474 DBF_LH(3, "(%08x): Drive set offline.\n", device
->cdev_id
);
479 * Allocate memory for a new device structure.
481 static struct tape_device
*
482 tape_alloc_device(void)
484 struct tape_device
*device
;
486 device
= kzalloc(sizeof(struct tape_device
), GFP_KERNEL
);
487 if (device
== NULL
) {
488 DBF_EXCEPTION(2, "ti:no mem\n");
489 return ERR_PTR(-ENOMEM
);
491 device
->modeset_byte
= kmalloc(1, GFP_KERNEL
| GFP_DMA
);
492 if (device
->modeset_byte
== NULL
) {
493 DBF_EXCEPTION(2, "ti:no mem\n");
495 return ERR_PTR(-ENOMEM
);
497 mutex_init(&device
->mutex
);
498 INIT_LIST_HEAD(&device
->req_queue
);
499 INIT_LIST_HEAD(&device
->node
);
500 init_waitqueue_head(&device
->state_change_wq
);
501 init_waitqueue_head(&device
->wait_queue
);
502 device
->tape_state
= TS_INIT
;
503 device
->medium_state
= MS_UNKNOWN
;
504 *device
->modeset_byte
= 0;
505 device
->first_minor
= -1;
506 atomic_set(&device
->ref_count
, 1);
507 INIT_DELAYED_WORK(&device
->tape_dnr
, tape_delayed_next_request
);
513 * Get a reference to an existing device structure. This will automatically
514 * increment the reference count.
517 tape_get_device(struct tape_device
*device
)
521 count
= atomic_inc_return(&device
->ref_count
);
522 DBF_EVENT(4, "tape_get_device(%p) = %i\n", device
, count
);
527 * Decrease the reference counter of a devices structure. If the
528 * reference counter reaches zero free the device structure.
529 * The function returns a NULL pointer to be used by the caller
530 * for clearing reference pointers.
533 tape_put_device(struct tape_device
*device
)
537 count
= atomic_dec_return(&device
->ref_count
);
538 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device
, count
);
541 kfree(device
->modeset_byte
);
547 * Find tape device by a device index.
550 tape_find_device(int devindex
)
552 struct tape_device
*device
, *tmp
;
554 device
= ERR_PTR(-ENODEV
);
555 read_lock(&tape_device_lock
);
556 list_for_each_entry(tmp
, &tape_device_list
, node
) {
557 if (tmp
->first_minor
/ TAPE_MINORS_PER_DEV
== devindex
) {
558 device
= tape_get_device(tmp
);
562 read_unlock(&tape_device_lock
);
567 * Driverfs tape probe function.
570 tape_generic_probe(struct ccw_device
*cdev
)
572 struct tape_device
*device
;
574 struct ccw_dev_id dev_id
;
576 device
= tape_alloc_device();
579 ccw_device_set_options(cdev
, CCWDEV_DO_PATHGROUP
|
580 CCWDEV_DO_MULTIPATH
);
581 ret
= sysfs_create_group(&cdev
->dev
.kobj
, &tape_attr_group
);
583 tape_put_device(device
);
586 dev_set_drvdata(&cdev
->dev
, device
);
587 cdev
->handler
= __tape_do_irq
;
589 ccw_device_get_id(cdev
, &dev_id
);
590 device
->cdev_id
= devid_to_int(&dev_id
);
595 __tape_discard_requests(struct tape_device
*device
)
597 struct tape_request
* request
;
598 struct list_head
* l
, *n
;
600 list_for_each_safe(l
, n
, &device
->req_queue
) {
601 request
= list_entry(l
, struct tape_request
, list
);
602 if (request
->status
== TAPE_REQUEST_IN_IO
)
603 request
->status
= TAPE_REQUEST_DONE
;
604 list_del(&request
->list
);
606 /* Decrease ref_count for removed request. */
607 request
->device
= NULL
;
608 tape_put_device(device
);
610 if (request
->callback
!= NULL
)
611 request
->callback(request
, request
->callback_data
);
616 * Driverfs tape remove function.
618 * This function is called whenever the common I/O layer detects the device
619 * gone. This can happen at any time and we cannot refuse.
622 tape_generic_remove(struct ccw_device
*cdev
)
624 struct tape_device
* device
;
626 device
= dev_get_drvdata(&cdev
->dev
);
630 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device
->cdev_id
, cdev
);
632 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
633 switch (device
->tape_state
) {
635 tape_state_set(device
, TS_NOT_OPER
);
640 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
644 * Need only to release the device.
646 tape_state_set(device
, TS_NOT_OPER
);
647 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
648 tape_cleanup_device(device
);
652 * There may be requests on the queue. We will not get
653 * an interrupt for a request that was running. So we
654 * just post them all as I/O errors.
656 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
658 pr_warning("%s: A tape unit was detached while in "
659 "use\n", dev_name(&device
->cdev
->dev
));
660 tape_state_set(device
, TS_NOT_OPER
);
661 __tape_discard_requests(device
);
662 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
663 tape_cleanup_device(device
);
666 device
= dev_get_drvdata(&cdev
->dev
);
668 sysfs_remove_group(&cdev
->dev
.kobj
, &tape_attr_group
);
669 dev_set_drvdata(&cdev
->dev
, NULL
);
670 tape_put_device(device
);
675 * Allocate a new tape ccw request
677 struct tape_request
*
678 tape_alloc_request(int cplength
, int datasize
)
680 struct tape_request
*request
;
682 BUG_ON(datasize
> PAGE_SIZE
|| (cplength
*sizeof(struct ccw1
)) > PAGE_SIZE
);
684 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength
, datasize
);
686 request
= kzalloc(sizeof(struct tape_request
), GFP_KERNEL
);
687 if (request
== NULL
) {
688 DBF_EXCEPTION(1, "cqra nomem\n");
689 return ERR_PTR(-ENOMEM
);
691 /* allocate channel program */
693 request
->cpaddr
= kcalloc(cplength
, sizeof(struct ccw1
),
694 GFP_ATOMIC
| GFP_DMA
);
695 if (request
->cpaddr
== NULL
) {
696 DBF_EXCEPTION(1, "cqra nomem\n");
698 return ERR_PTR(-ENOMEM
);
701 /* alloc small kernel buffer */
703 request
->cpdata
= kzalloc(datasize
, GFP_KERNEL
| GFP_DMA
);
704 if (request
->cpdata
== NULL
) {
705 DBF_EXCEPTION(1, "cqra nomem\n");
706 kfree(request
->cpaddr
);
708 return ERR_PTR(-ENOMEM
);
711 DBF_LH(6, "New request %p(%p/%p)\n", request
, request
->cpaddr
,
718 * Free tape ccw request
721 tape_free_request (struct tape_request
* request
)
723 DBF_LH(6, "Free request %p\n", request
);
726 tape_put_device(request
->device
);
727 kfree(request
->cpdata
);
728 kfree(request
->cpaddr
);
733 __tape_start_io(struct tape_device
*device
, struct tape_request
*request
)
737 #ifdef CONFIG_S390_TAPE_BLOCK
738 if (request
->op
== TO_BLOCK
)
739 device
->discipline
->check_locate(device
, request
);
741 rc
= ccw_device_start(
744 (unsigned long) request
,
749 request
->status
= TAPE_REQUEST_IN_IO
;
750 } else if (rc
== -EBUSY
) {
751 /* The common I/O subsystem is currently busy. Retry later. */
752 request
->status
= TAPE_REQUEST_QUEUED
;
753 schedule_delayed_work(&device
->tape_dnr
, 0);
756 /* Start failed. Remove request and indicate failure. */
757 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc
);
763 __tape_start_next_request(struct tape_device
*device
)
765 struct list_head
*l
, *n
;
766 struct tape_request
*request
;
769 DBF_LH(6, "__tape_start_next_request(%p)\n", device
);
771 * Try to start each request on request queue until one is
772 * started successful.
774 list_for_each_safe(l
, n
, &device
->req_queue
) {
775 request
= list_entry(l
, struct tape_request
, list
);
778 * Avoid race condition if bottom-half was triggered more than
781 if (request
->status
== TAPE_REQUEST_IN_IO
)
784 * Request has already been stopped. We have to wait until
785 * the request is removed from the queue in the interrupt
788 if (request
->status
== TAPE_REQUEST_DONE
)
792 * We wanted to cancel the request but the common I/O layer
793 * was busy at that time. This can only happen if this
794 * function is called by delayed_next_request.
795 * Otherwise we start the next request on the queue.
797 if (request
->status
== TAPE_REQUEST_CANCEL
) {
798 rc
= __tape_cancel_io(device
, request
);
800 rc
= __tape_start_io(device
, request
);
805 /* Set ending status. */
807 request
->status
= TAPE_REQUEST_DONE
;
809 /* Remove from request queue. */
810 list_del(&request
->list
);
813 if (request
->callback
!= NULL
)
814 request
->callback(request
, request
->callback_data
);
819 tape_delayed_next_request(struct work_struct
*work
)
821 struct tape_device
*device
=
822 container_of(work
, struct tape_device
, tape_dnr
.work
);
824 DBF_LH(6, "tape_delayed_next_request(%p)\n", device
);
825 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
826 __tape_start_next_request(device
);
827 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
830 static void tape_long_busy_timeout(unsigned long data
)
832 struct tape_request
*request
;
833 struct tape_device
*device
;
835 device
= (struct tape_device
*) data
;
836 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
837 request
= list_entry(device
->req_queue
.next
, struct tape_request
, list
);
838 BUG_ON(request
->status
!= TAPE_REQUEST_LONG_BUSY
);
839 DBF_LH(6, "%08x: Long busy timeout.\n", device
->cdev_id
);
840 __tape_start_next_request(device
);
841 device
->lb_timeout
.data
= 0UL;
842 tape_put_device(device
);
843 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
848 struct tape_device
* device
,
849 struct tape_request
* request
,
852 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device
, request
, rc
);
855 request
->status
= TAPE_REQUEST_DONE
;
857 /* Remove from request queue. */
858 list_del(&request
->list
);
861 if (request
->callback
!= NULL
)
862 request
->callback(request
, request
->callback_data
);
865 /* Start next request. */
866 if (!list_empty(&device
->req_queue
))
867 __tape_start_next_request(device
);
871 * Write sense data to dbf
874 tape_dump_sense_dbf(struct tape_device
*device
, struct tape_request
*request
,
881 op
= tape_op_verbose
[request
->op
];
884 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
885 irb
->scsw
.cmd
.dstat
, irb
->scsw
.cmd
.cstat
);
886 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device
->cdev_id
, op
);
887 sptr
= (unsigned int *) irb
->ecw
;
888 DBF_EVENT(3, "%08x %08x\n", sptr
[0], sptr
[1]);
889 DBF_EVENT(3, "%08x %08x\n", sptr
[2], sptr
[3]);
890 DBF_EVENT(3, "%08x %08x\n", sptr
[4], sptr
[5]);
891 DBF_EVENT(3, "%08x %08x\n", sptr
[6], sptr
[7]);
895 * I/O helper function. Adds the request to the request queue
896 * and starts it if the tape is idle. Has to be called with
897 * the device lock held.
900 __tape_start_request(struct tape_device
*device
, struct tape_request
*request
)
904 switch (request
->op
) {
910 if (device
->tape_state
== TS_INIT
)
912 if (device
->tape_state
== TS_UNUSED
)
915 if (device
->tape_state
== TS_BLKUSE
)
917 if (device
->tape_state
!= TS_IN_USE
)
921 /* Increase use count of device for the added request. */
922 request
->device
= tape_get_device(device
);
924 if (list_empty(&device
->req_queue
)) {
925 /* No other requests are on the queue. Start this one. */
926 rc
= __tape_start_io(device
, request
);
930 DBF_LH(5, "Request %p added for execution.\n", request
);
931 list_add(&request
->list
, &device
->req_queue
);
933 DBF_LH(5, "Request %p add to queue.\n", request
);
934 request
->status
= TAPE_REQUEST_QUEUED
;
935 list_add_tail(&request
->list
, &device
->req_queue
);
941 * Add the request to the request queue, try to start it if the
942 * tape is idle. Return without waiting for end of i/o.
945 tape_do_io_async(struct tape_device
*device
, struct tape_request
*request
)
949 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device
, request
);
951 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
952 /* Add request to request queue and try to start it. */
953 rc
= __tape_start_request(device
, request
);
954 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
959 * tape_do_io/__tape_wake_up
960 * Add the request to the request queue, try to start it if the
961 * tape is idle and wait uninterruptible for its completion.
964 __tape_wake_up(struct tape_request
*request
, void *data
)
966 request
->callback
= NULL
;
967 wake_up((wait_queue_head_t
*) data
);
971 tape_do_io(struct tape_device
*device
, struct tape_request
*request
)
975 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
977 request
->callback
= __tape_wake_up
;
978 request
->callback_data
= &device
->wait_queue
;
979 /* Add request to request queue and try to start it. */
980 rc
= __tape_start_request(device
, request
);
981 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
984 /* Request added to the queue. Wait for its completion. */
985 wait_event(device
->wait_queue
, (request
->callback
== NULL
));
986 /* Get rc from request */
991 * tape_do_io_interruptible/__tape_wake_up_interruptible
992 * Add the request to the request queue, try to start it if the
993 * tape is idle and wait uninterruptible for its completion.
996 __tape_wake_up_interruptible(struct tape_request
*request
, void *data
)
998 request
->callback
= NULL
;
999 wake_up_interruptible((wait_queue_head_t
*) data
);
1003 tape_do_io_interruptible(struct tape_device
*device
,
1004 struct tape_request
*request
)
1008 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1009 /* Setup callback */
1010 request
->callback
= __tape_wake_up_interruptible
;
1011 request
->callback_data
= &device
->wait_queue
;
1012 rc
= __tape_start_request(device
, request
);
1013 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1016 /* Request added to the queue. Wait for its completion. */
1017 rc
= wait_event_interruptible(device
->wait_queue
,
1018 (request
->callback
== NULL
));
1019 if (rc
!= -ERESTARTSYS
)
1020 /* Request finished normally. */
1023 /* Interrupted by a signal. We have to stop the current request. */
1024 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1025 rc
= __tape_cancel_io(device
, request
);
1026 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1028 /* Wait for the interrupt that acknowledges the halt. */
1030 rc
= wait_event_interruptible(
1032 (request
->callback
== NULL
)
1034 } while (rc
== -ERESTARTSYS
);
1036 DBF_EVENT(3, "IO stopped on %08x\n", device
->cdev_id
);
1046 tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
1050 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1051 rc
= __tape_cancel_io(device
, request
);
1052 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1057 * Tape interrupt routine, called from the ccw_device layer
1060 __tape_do_irq (struct ccw_device
*cdev
, unsigned long intparm
, struct irb
*irb
)
1062 struct tape_device
*device
;
1063 struct tape_request
*request
;
1066 device
= dev_get_drvdata(&cdev
->dev
);
1067 if (device
== NULL
) {
1070 request
= (struct tape_request
*) intparm
;
1072 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device
, request
);
1074 /* On special conditions irb is an error pointer */
1076 /* FIXME: What to do with the request? */
1077 switch (PTR_ERR(irb
)) {
1079 DBF_LH(1, "(%s): Request timed out\n",
1080 dev_name(&cdev
->dev
));
1082 __tape_end_request(device
, request
, -EIO
);
1085 DBF_LH(1, "(%s): Unexpected i/o error %li\n",
1086 dev_name(&cdev
->dev
),
1093 * If the condition code is not zero and the start function bit is
1094 * still set, this is an deferred error and the last start I/O did
1095 * not succeed. At this point the condition that caused the deferred
1096 * error might still apply. So we just schedule the request to be
1099 if (irb
->scsw
.cmd
.cc
!= 0 &&
1100 (irb
->scsw
.cmd
.fctl
& SCSW_FCTL_START_FUNC
) &&
1101 (request
->status
== TAPE_REQUEST_IN_IO
)) {
1102 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1103 device
->cdev_id
, irb
->scsw
.cmd
.cc
, irb
->scsw
.cmd
.fctl
);
1104 request
->status
= TAPE_REQUEST_QUEUED
;
1105 schedule_delayed_work(&device
->tape_dnr
, HZ
);
1109 /* May be an unsolicited irq */
1111 request
->rescnt
= irb
->scsw
.cmd
.count
;
1112 else if ((irb
->scsw
.cmd
.dstat
== 0x85 || irb
->scsw
.cmd
.dstat
== 0x80) &&
1113 !list_empty(&device
->req_queue
)) {
1114 /* Not Ready to Ready after long busy ? */
1115 struct tape_request
*req
;
1116 req
= list_entry(device
->req_queue
.next
,
1117 struct tape_request
, list
);
1118 if (req
->status
== TAPE_REQUEST_LONG_BUSY
) {
1119 DBF_EVENT(3, "(%08x): del timer\n", device
->cdev_id
);
1120 if (del_timer(&device
->lb_timeout
)) {
1121 device
->lb_timeout
.data
= 0UL;
1122 tape_put_device(device
);
1123 __tape_start_next_request(device
);
1128 if (irb
->scsw
.cmd
.dstat
!= 0x0c) {
1129 /* Set the 'ONLINE' flag depending on sense byte 1 */
1130 if(*(((__u8
*) irb
->ecw
) + 1) & SENSE_DRIVE_ONLINE
)
1131 device
->tape_generic_status
|= GMT_ONLINE(~0);
1133 device
->tape_generic_status
&= ~GMT_ONLINE(~0);
1136 * Any request that does not come back with channel end
1137 * and device end is unusual. Log the sense data.
1139 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1140 tape_dump_sense_dbf(device
, request
, irb
);
1142 /* Upon normal completion the device _is_ online */
1143 device
->tape_generic_status
|= GMT_ONLINE(~0);
1145 if (device
->tape_state
== TS_NOT_OPER
) {
1146 DBF_EVENT(6, "tape:device is not operational\n");
1151 * Request that were canceled still come back with an interrupt.
1152 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1154 if(request
!= NULL
&& request
->status
== TAPE_REQUEST_DONE
) {
1155 __tape_end_request(device
, request
, -EIO
);
1159 rc
= device
->discipline
->irq(device
, request
, irb
);
1161 * rc < 0 : request finished unsuccessfully.
1162 * rc == TAPE_IO_SUCCESS: request finished successfully.
1163 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1164 * rc == TAPE_IO_RETRY: request finished but needs another go.
1165 * rc == TAPE_IO_STOP: request needs to get terminated.
1168 case TAPE_IO_SUCCESS
:
1169 /* Upon normal completion the device _is_ online */
1170 device
->tape_generic_status
|= GMT_ONLINE(~0);
1171 __tape_end_request(device
, request
, rc
);
1173 case TAPE_IO_PENDING
:
1175 case TAPE_IO_LONG_BUSY
:
1176 device
->lb_timeout
.data
=
1177 (unsigned long) tape_get_device(device
);
1178 device
->lb_timeout
.expires
= jiffies
+
1179 LONG_BUSY_TIMEOUT
* HZ
;
1180 DBF_EVENT(3, "(%08x): add timer\n", device
->cdev_id
);
1181 add_timer(&device
->lb_timeout
);
1182 request
->status
= TAPE_REQUEST_LONG_BUSY
;
1185 rc
= __tape_start_io(device
, request
);
1187 __tape_end_request(device
, request
, rc
);
1190 rc
= __tape_cancel_io(device
, request
);
1192 __tape_end_request(device
, request
, rc
);
1196 DBF_EVENT(6, "xunknownrc\n");
1197 __tape_end_request(device
, request
, -EIO
);
1199 __tape_end_request(device
, request
, rc
);
1206 * Tape device open function used by tape_char & tape_block frontends.
1209 tape_open(struct tape_device
*device
)
1213 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1214 if (device
->tape_state
== TS_NOT_OPER
) {
1215 DBF_EVENT(6, "TAPE:nodev\n");
1217 } else if (device
->tape_state
== TS_IN_USE
) {
1218 DBF_EVENT(6, "TAPE:dbusy\n");
1220 } else if (device
->tape_state
== TS_BLKUSE
) {
1221 DBF_EVENT(6, "TAPE:dbusy\n");
1223 } else if (device
->discipline
!= NULL
&&
1224 !try_module_get(device
->discipline
->owner
)) {
1225 DBF_EVENT(6, "TAPE:nodisc\n");
1228 tape_state_set(device
, TS_IN_USE
);
1231 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1236 * Tape device release function used by tape_char & tape_block frontends.
1239 tape_release(struct tape_device
*device
)
1241 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1242 if (device
->tape_state
== TS_IN_USE
)
1243 tape_state_set(device
, TS_UNUSED
);
1244 module_put(device
->discipline
->owner
);
1245 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1250 * Execute a magnetic tape command a number of times.
1253 tape_mtop(struct tape_device
*device
, int mt_op
, int mt_count
)
1258 DBF_EVENT(6, "TAPE:mtio\n");
1259 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op
);
1260 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count
);
1262 if (mt_op
< 0 || mt_op
>= TAPE_NR_MTOPS
)
1264 fn
= device
->discipline
->mtop_array
[mt_op
];
1268 /* We assume that the backends can handle count up to 500. */
1269 if (mt_op
== MTBSR
|| mt_op
== MTFSR
|| mt_op
== MTFSF
||
1270 mt_op
== MTBSF
|| mt_op
== MTFSFM
|| mt_op
== MTBSFM
) {
1272 for (; mt_count
> 500; mt_count
-= 500)
1273 if ((rc
= fn(device
, 500)) != 0)
1276 rc
= fn(device
, mt_count
);
1278 rc
= fn(device
, mt_count
);
1284 * Tape init function.
1289 TAPE_DBF_AREA
= debug_register ( "tape", 2, 2, 4*sizeof(long));
1290 debug_register_view(TAPE_DBF_AREA
, &debug_sprintf_view
);
1291 #ifdef DBF_LIKE_HELL
1292 debug_set_level(TAPE_DBF_AREA
, 6);
1294 DBF_EVENT(3, "tape init\n");
1302 * Tape exit function.
1307 DBF_EVENT(6, "tape exit\n");
1309 /* Get rid of the frontends */
1312 tape_proc_cleanup();
1313 debug_unregister (TAPE_DBF_AREA
);
1316 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1317 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1318 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1319 MODULE_LICENSE("GPL");
1321 module_init(tape_init
);
1322 module_exit(tape_exit
);
1324 EXPORT_SYMBOL(tape_generic_remove
);
1325 EXPORT_SYMBOL(tape_generic_probe
);
1326 EXPORT_SYMBOL(tape_generic_online
);
1327 EXPORT_SYMBOL(tape_generic_offline
);
1328 EXPORT_SYMBOL(tape_generic_pm_suspend
);
1329 EXPORT_SYMBOL(tape_put_device
);
1330 EXPORT_SYMBOL(tape_get_device
);
1331 EXPORT_SYMBOL(tape_state_verbose
);
1332 EXPORT_SYMBOL(tape_op_verbose
);
1333 EXPORT_SYMBOL(tape_state_set
);
1334 EXPORT_SYMBOL(tape_med_state_set
);
1335 EXPORT_SYMBOL(tape_alloc_request
);
1336 EXPORT_SYMBOL(tape_free_request
);
1337 EXPORT_SYMBOL(tape_dump_sense_dbf
);
1338 EXPORT_SYMBOL(tape_do_io
);
1339 EXPORT_SYMBOL(tape_do_io_async
);
1340 EXPORT_SYMBOL(tape_do_io_interruptible
);
1341 EXPORT_SYMBOL(tape_cancel_io
);
1342 EXPORT_SYMBOL(tape_mtop
);