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>
23 #include <linux/slab.h>
25 #include <asm/types.h> // for variable types
27 #define TAPE_DBF_AREA tape_core_dbf
32 #define LONG_BUSY_TIMEOUT 180 /* seconds */
34 static void __tape_do_irq (struct ccw_device
*, unsigned long, struct irb
*);
35 static void tape_delayed_next_request(struct work_struct
*);
36 static void tape_long_busy_timeout(unsigned long data
);
39 * One list to contain all tape devices of all disciplines, so
40 * we can assign the devices to minor numbers of the same major
41 * The list is protected by the rwlock
43 static LIST_HEAD(tape_device_list
);
44 static DEFINE_RWLOCK(tape_device_lock
);
47 * Pointer to debug area.
49 debug_info_t
*TAPE_DBF_AREA
= NULL
;
50 EXPORT_SYMBOL(TAPE_DBF_AREA
);
53 * Printable strings for tape enumerations.
55 const char *tape_state_verbose
[TS_SIZE
] =
57 [TS_UNUSED
] = "UNUSED",
58 [TS_IN_USE
] = "IN_USE",
59 [TS_BLKUSE
] = "BLKUSE",
61 [TS_NOT_OPER
] = "NOT_OP"
64 const char *tape_op_verbose
[TO_SIZE
] =
66 [TO_BLOCK
] = "BLK", [TO_BSB
] = "BSB",
67 [TO_BSF
] = "BSF", [TO_DSE
] = "DSE",
68 [TO_FSB
] = "FSB", [TO_FSF
] = "FSF",
69 [TO_LBL
] = "LBL", [TO_NOP
] = "NOP",
70 [TO_RBA
] = "RBA", [TO_RBI
] = "RBI",
71 [TO_RFO
] = "RFO", [TO_REW
] = "REW",
72 [TO_RUN
] = "RUN", [TO_WRI
] = "WRI",
73 [TO_WTM
] = "WTM", [TO_MSEN
] = "MSN",
74 [TO_LOAD
] = "LOA", [TO_READ_CONFIG
] = "RCF",
75 [TO_READ_ATTMSG
] = "RAT",
76 [TO_DIS
] = "DIS", [TO_ASSIGN
] = "ASS",
77 [TO_UNASSIGN
] = "UAS", [TO_CRYPT_ON
] = "CON",
78 [TO_CRYPT_OFF
] = "COF", [TO_KEKL_SET
] = "KLS",
79 [TO_KEKL_QUERY
] = "KLQ",[TO_RDC
] = "RDC",
82 static int devid_to_int(struct ccw_dev_id
*dev_id
)
84 return dev_id
->devno
+ (dev_id
->ssid
<< 16);
88 tape_medium_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
90 struct tape_device
*tdev
;
92 tdev
= dev_get_drvdata(dev
);
93 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->medium_state
);
97 DEVICE_ATTR(medium_state
, 0444, tape_medium_state_show
, NULL
);
100 tape_first_minor_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
102 struct tape_device
*tdev
;
104 tdev
= dev_get_drvdata(dev
);
105 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->first_minor
);
109 DEVICE_ATTR(first_minor
, 0444, tape_first_minor_show
, NULL
);
112 tape_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
114 struct tape_device
*tdev
;
116 tdev
= dev_get_drvdata(dev
);
117 return scnprintf(buf
, PAGE_SIZE
, "%s\n", (tdev
->first_minor
< 0) ?
118 "OFFLINE" : tape_state_verbose
[tdev
->tape_state
]);
122 DEVICE_ATTR(state
, 0444, tape_state_show
, NULL
);
125 tape_operation_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
127 struct tape_device
*tdev
;
130 tdev
= dev_get_drvdata(dev
);
131 if (tdev
->first_minor
< 0)
132 return scnprintf(buf
, PAGE_SIZE
, "N/A\n");
134 spin_lock_irq(get_ccwdev_lock(tdev
->cdev
));
135 if (list_empty(&tdev
->req_queue
))
136 rc
= scnprintf(buf
, PAGE_SIZE
, "---\n");
138 struct tape_request
*req
;
140 req
= list_entry(tdev
->req_queue
.next
, struct tape_request
,
142 rc
= scnprintf(buf
,PAGE_SIZE
, "%s\n", tape_op_verbose
[req
->op
]);
144 spin_unlock_irq(get_ccwdev_lock(tdev
->cdev
));
149 DEVICE_ATTR(operation
, 0444, tape_operation_show
, NULL
);
152 tape_blocksize_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
154 struct tape_device
*tdev
;
156 tdev
= dev_get_drvdata(dev
);
158 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->char_data
.block_size
);
162 DEVICE_ATTR(blocksize
, 0444, tape_blocksize_show
, NULL
);
164 static struct attribute
*tape_attrs
[] = {
165 &dev_attr_medium_state
.attr
,
166 &dev_attr_first_minor
.attr
,
167 &dev_attr_state
.attr
,
168 &dev_attr_operation
.attr
,
169 &dev_attr_blocksize
.attr
,
173 static struct attribute_group tape_attr_group
= {
178 * Tape state functions
181 tape_state_set(struct tape_device
*device
, enum tape_state newstate
)
185 if (device
->tape_state
== TS_NOT_OPER
) {
186 DBF_EVENT(3, "ts_set err: not oper\n");
189 DBF_EVENT(4, "ts. dev: %x\n", device
->first_minor
);
190 DBF_EVENT(4, "old ts:\t\n");
191 if (device
->tape_state
< TS_SIZE
&& device
->tape_state
>=0 )
192 str
= tape_state_verbose
[device
->tape_state
];
195 DBF_EVENT(4, "%s\n", str
);
196 DBF_EVENT(4, "new ts:\t\n");
197 if (newstate
< TS_SIZE
&& newstate
>= 0)
198 str
= tape_state_verbose
[newstate
];
201 DBF_EVENT(4, "%s\n", str
);
202 device
->tape_state
= newstate
;
203 wake_up(&device
->state_change_wq
);
207 tape_med_state_set(struct tape_device
*device
, enum tape_medium_state newstate
)
209 if (device
->medium_state
== newstate
)
213 device
->tape_generic_status
|= GMT_DR_OPEN(~0);
214 if (device
->medium_state
== MS_LOADED
)
215 pr_info("%s: The tape cartridge has been successfully "
216 "unloaded\n", dev_name(&device
->cdev
->dev
));
219 device
->tape_generic_status
&= ~GMT_DR_OPEN(~0);
220 if (device
->medium_state
== MS_UNLOADED
)
221 pr_info("%s: A tape cartridge has been mounted\n",
222 dev_name(&device
->cdev
->dev
));
228 device
->medium_state
= newstate
;
229 wake_up(&device
->state_change_wq
);
233 * Stop running ccw. Has to be called with the device lock held.
236 __tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
241 /* Check if interrupt has already been processed */
242 if (request
->callback
== NULL
)
246 for (retries
= 0; retries
< 5; retries
++) {
247 rc
= ccw_device_clear(device
->cdev
, (long) request
);
251 request
->status
= TAPE_REQUEST_DONE
;
254 request
->status
= TAPE_REQUEST_CANCEL
;
255 schedule_delayed_work(&device
->tape_dnr
, 0);
258 DBF_EXCEPTION(2, "device gone, retry\n");
261 DBF_EXCEPTION(2, "I/O error, retry\n");
272 * Add device into the sorted list, giving it the first
273 * available minor number.
276 tape_assign_minor(struct tape_device
*device
)
278 struct tape_device
*tmp
;
282 write_lock(&tape_device_lock
);
283 list_for_each_entry(tmp
, &tape_device_list
, node
) {
284 if (minor
< tmp
->first_minor
)
286 minor
+= TAPE_MINORS_PER_DEV
;
289 write_unlock(&tape_device_lock
);
292 device
->first_minor
= minor
;
293 list_add_tail(&device
->node
, &tmp
->node
);
294 write_unlock(&tape_device_lock
);
298 /* remove device from the list */
300 tape_remove_minor(struct tape_device
*device
)
302 write_lock(&tape_device_lock
);
303 list_del_init(&device
->node
);
304 device
->first_minor
= -1;
305 write_unlock(&tape_device_lock
);
309 * Set a device online.
311 * This function is called by the common I/O layer to move a device from the
312 * detected but offline into the online state.
313 * If we return an error (RC < 0) the device remains in the offline state. This
314 * can happen if the device is assigned somewhere else, for example.
317 tape_generic_online(struct tape_device
*device
,
318 struct tape_discipline
*discipline
)
322 DBF_LH(6, "tape_enable_device(%p, %p)\n", device
, discipline
);
324 if (device
->tape_state
!= TS_INIT
) {
325 DBF_LH(3, "Tapestate not INIT (%d)\n", device
->tape_state
);
329 init_timer(&device
->lb_timeout
);
330 device
->lb_timeout
.function
= tape_long_busy_timeout
;
332 /* Let the discipline have a go at the device. */
333 device
->discipline
= discipline
;
334 if (!try_module_get(discipline
->owner
)) {
338 rc
= discipline
->setup_device(device
);
341 rc
= tape_assign_minor(device
);
345 rc
= tapechar_setup_device(device
);
348 rc
= tapeblock_setup_device(device
);
352 tape_state_set(device
, TS_UNUSED
);
354 DBF_LH(3, "(%08x): Drive set online\n", device
->cdev_id
);
359 tapechar_cleanup_device(device
);
361 tape_remove_minor(device
);
363 device
->discipline
->cleanup_device(device
);
364 device
->discipline
= NULL
;
366 module_put(discipline
->owner
);
371 tape_cleanup_device(struct tape_device
*device
)
373 tapeblock_cleanup_device(device
);
374 tapechar_cleanup_device(device
);
375 device
->discipline
->cleanup_device(device
);
376 module_put(device
->discipline
->owner
);
377 tape_remove_minor(device
);
378 tape_med_state_set(device
, MS_UNKNOWN
);
384 * Called by the common I/O layer if the drive should be suspended on user
385 * request. We refuse to suspend if the device is loaded or in use for the
387 * While the Linux guest is suspended, it might be logged off which causes
388 * devices to be detached. Tape devices are automatically rewound and unloaded
389 * during DETACH processing (unless the tape device was attached with the
390 * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to
391 * resume the original state of the tape device, since we would need to
392 * manually re-load the cartridge which was active at suspend time.
394 int tape_generic_pm_suspend(struct ccw_device
*cdev
)
396 struct tape_device
*device
;
398 device
= dev_get_drvdata(&cdev
->dev
);
403 DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n",
404 device
->cdev_id
, device
);
406 if (device
->medium_state
!= MS_UNLOADED
) {
407 pr_err("A cartridge is loaded in tape device %s, "
408 "refusing to suspend\n", dev_name(&cdev
->dev
));
412 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
413 switch (device
->tape_state
) {
417 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
420 pr_err("Tape device %s is busy, refusing to "
421 "suspend\n", dev_name(&cdev
->dev
));
422 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
426 DBF_LH(3, "(%08x): Drive suspended.\n", device
->cdev_id
);
431 * Set device offline.
433 * Called by the common I/O layer if the drive should set offline on user
434 * request. We may prevent this by returning an error.
435 * Manual offline is only allowed while the drive is not in use.
438 tape_generic_offline(struct ccw_device
*cdev
)
440 struct tape_device
*device
;
442 device
= dev_get_drvdata(&cdev
->dev
);
447 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
448 device
->cdev_id
, device
);
450 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
451 switch (device
->tape_state
) {
454 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
457 tape_state_set(device
, TS_INIT
);
458 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
459 tape_cleanup_device(device
);
462 DBF_EVENT(3, "(%08x): Set offline failed "
465 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
469 DBF_LH(3, "(%08x): Drive set offline.\n", device
->cdev_id
);
474 * Allocate memory for a new device structure.
476 static struct tape_device
*
477 tape_alloc_device(void)
479 struct tape_device
*device
;
481 device
= kzalloc(sizeof(struct tape_device
), GFP_KERNEL
);
482 if (device
== NULL
) {
483 DBF_EXCEPTION(2, "ti:no mem\n");
484 return ERR_PTR(-ENOMEM
);
486 device
->modeset_byte
= kmalloc(1, GFP_KERNEL
| GFP_DMA
);
487 if (device
->modeset_byte
== NULL
) {
488 DBF_EXCEPTION(2, "ti:no mem\n");
490 return ERR_PTR(-ENOMEM
);
492 mutex_init(&device
->mutex
);
493 INIT_LIST_HEAD(&device
->req_queue
);
494 INIT_LIST_HEAD(&device
->node
);
495 init_waitqueue_head(&device
->state_change_wq
);
496 init_waitqueue_head(&device
->wait_queue
);
497 device
->tape_state
= TS_INIT
;
498 device
->medium_state
= MS_UNKNOWN
;
499 *device
->modeset_byte
= 0;
500 device
->first_minor
= -1;
501 atomic_set(&device
->ref_count
, 1);
502 INIT_DELAYED_WORK(&device
->tape_dnr
, tape_delayed_next_request
);
508 * Get a reference to an existing device structure. This will automatically
509 * increment the reference count.
512 tape_get_device(struct tape_device
*device
)
516 count
= atomic_inc_return(&device
->ref_count
);
517 DBF_EVENT(4, "tape_get_device(%p) = %i\n", device
, count
);
522 * Decrease the reference counter of a devices structure. If the
523 * reference counter reaches zero free the device structure.
524 * The function returns a NULL pointer to be used by the caller
525 * for clearing reference pointers.
528 tape_put_device(struct tape_device
*device
)
532 count
= atomic_dec_return(&device
->ref_count
);
533 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device
, count
);
536 kfree(device
->modeset_byte
);
542 * Find tape device by a device index.
545 tape_find_device(int devindex
)
547 struct tape_device
*device
, *tmp
;
549 device
= ERR_PTR(-ENODEV
);
550 read_lock(&tape_device_lock
);
551 list_for_each_entry(tmp
, &tape_device_list
, node
) {
552 if (tmp
->first_minor
/ TAPE_MINORS_PER_DEV
== devindex
) {
553 device
= tape_get_device(tmp
);
557 read_unlock(&tape_device_lock
);
562 * Driverfs tape probe function.
565 tape_generic_probe(struct ccw_device
*cdev
)
567 struct tape_device
*device
;
569 struct ccw_dev_id dev_id
;
571 device
= tape_alloc_device();
574 ccw_device_set_options(cdev
, CCWDEV_DO_PATHGROUP
|
575 CCWDEV_DO_MULTIPATH
);
576 ret
= sysfs_create_group(&cdev
->dev
.kobj
, &tape_attr_group
);
578 tape_put_device(device
);
581 dev_set_drvdata(&cdev
->dev
, device
);
582 cdev
->handler
= __tape_do_irq
;
584 ccw_device_get_id(cdev
, &dev_id
);
585 device
->cdev_id
= devid_to_int(&dev_id
);
590 __tape_discard_requests(struct tape_device
*device
)
592 struct tape_request
* request
;
593 struct list_head
* l
, *n
;
595 list_for_each_safe(l
, n
, &device
->req_queue
) {
596 request
= list_entry(l
, struct tape_request
, list
);
597 if (request
->status
== TAPE_REQUEST_IN_IO
)
598 request
->status
= TAPE_REQUEST_DONE
;
599 list_del(&request
->list
);
601 /* Decrease ref_count for removed request. */
602 request
->device
= NULL
;
603 tape_put_device(device
);
605 if (request
->callback
!= NULL
)
606 request
->callback(request
, request
->callback_data
);
611 * Driverfs tape remove function.
613 * This function is called whenever the common I/O layer detects the device
614 * gone. This can happen at any time and we cannot refuse.
617 tape_generic_remove(struct ccw_device
*cdev
)
619 struct tape_device
* device
;
621 device
= dev_get_drvdata(&cdev
->dev
);
625 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device
->cdev_id
, cdev
);
627 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
628 switch (device
->tape_state
) {
630 tape_state_set(device
, TS_NOT_OPER
);
635 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
639 * Need only to release the device.
641 tape_state_set(device
, TS_NOT_OPER
);
642 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
643 tape_cleanup_device(device
);
647 * There may be requests on the queue. We will not get
648 * an interrupt for a request that was running. So we
649 * just post them all as I/O errors.
651 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
653 pr_warning("%s: A tape unit was detached while in "
654 "use\n", dev_name(&device
->cdev
->dev
));
655 tape_state_set(device
, TS_NOT_OPER
);
656 __tape_discard_requests(device
);
657 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
658 tape_cleanup_device(device
);
661 device
= dev_get_drvdata(&cdev
->dev
);
663 sysfs_remove_group(&cdev
->dev
.kobj
, &tape_attr_group
);
664 dev_set_drvdata(&cdev
->dev
, NULL
);
665 tape_put_device(device
);
670 * Allocate a new tape ccw request
672 struct tape_request
*
673 tape_alloc_request(int cplength
, int datasize
)
675 struct tape_request
*request
;
677 BUG_ON(datasize
> PAGE_SIZE
|| (cplength
*sizeof(struct ccw1
)) > PAGE_SIZE
);
679 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength
, datasize
);
681 request
= kzalloc(sizeof(struct tape_request
), GFP_KERNEL
);
682 if (request
== NULL
) {
683 DBF_EXCEPTION(1, "cqra nomem\n");
684 return ERR_PTR(-ENOMEM
);
686 /* allocate channel program */
688 request
->cpaddr
= kcalloc(cplength
, sizeof(struct ccw1
),
689 GFP_ATOMIC
| GFP_DMA
);
690 if (request
->cpaddr
== NULL
) {
691 DBF_EXCEPTION(1, "cqra nomem\n");
693 return ERR_PTR(-ENOMEM
);
696 /* alloc small kernel buffer */
698 request
->cpdata
= kzalloc(datasize
, GFP_KERNEL
| GFP_DMA
);
699 if (request
->cpdata
== NULL
) {
700 DBF_EXCEPTION(1, "cqra nomem\n");
701 kfree(request
->cpaddr
);
703 return ERR_PTR(-ENOMEM
);
706 DBF_LH(6, "New request %p(%p/%p)\n", request
, request
->cpaddr
,
713 * Free tape ccw request
716 tape_free_request (struct tape_request
* request
)
718 DBF_LH(6, "Free request %p\n", request
);
721 tape_put_device(request
->device
);
722 kfree(request
->cpdata
);
723 kfree(request
->cpaddr
);
728 __tape_start_io(struct tape_device
*device
, struct tape_request
*request
)
732 #ifdef CONFIG_S390_TAPE_BLOCK
733 if (request
->op
== TO_BLOCK
)
734 device
->discipline
->check_locate(device
, request
);
736 rc
= ccw_device_start(
739 (unsigned long) request
,
744 request
->status
= TAPE_REQUEST_IN_IO
;
745 } else if (rc
== -EBUSY
) {
746 /* The common I/O subsystem is currently busy. Retry later. */
747 request
->status
= TAPE_REQUEST_QUEUED
;
748 schedule_delayed_work(&device
->tape_dnr
, 0);
751 /* Start failed. Remove request and indicate failure. */
752 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc
);
758 __tape_start_next_request(struct tape_device
*device
)
760 struct list_head
*l
, *n
;
761 struct tape_request
*request
;
764 DBF_LH(6, "__tape_start_next_request(%p)\n", device
);
766 * Try to start each request on request queue until one is
767 * started successful.
769 list_for_each_safe(l
, n
, &device
->req_queue
) {
770 request
= list_entry(l
, struct tape_request
, list
);
773 * Avoid race condition if bottom-half was triggered more than
776 if (request
->status
== TAPE_REQUEST_IN_IO
)
779 * Request has already been stopped. We have to wait until
780 * the request is removed from the queue in the interrupt
783 if (request
->status
== TAPE_REQUEST_DONE
)
787 * We wanted to cancel the request but the common I/O layer
788 * was busy at that time. This can only happen if this
789 * function is called by delayed_next_request.
790 * Otherwise we start the next request on the queue.
792 if (request
->status
== TAPE_REQUEST_CANCEL
) {
793 rc
= __tape_cancel_io(device
, request
);
795 rc
= __tape_start_io(device
, request
);
800 /* Set ending status. */
802 request
->status
= TAPE_REQUEST_DONE
;
804 /* Remove from request queue. */
805 list_del(&request
->list
);
808 if (request
->callback
!= NULL
)
809 request
->callback(request
, request
->callback_data
);
814 tape_delayed_next_request(struct work_struct
*work
)
816 struct tape_device
*device
=
817 container_of(work
, struct tape_device
, tape_dnr
.work
);
819 DBF_LH(6, "tape_delayed_next_request(%p)\n", device
);
820 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
821 __tape_start_next_request(device
);
822 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
825 static void tape_long_busy_timeout(unsigned long data
)
827 struct tape_request
*request
;
828 struct tape_device
*device
;
830 device
= (struct tape_device
*) data
;
831 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
832 request
= list_entry(device
->req_queue
.next
, struct tape_request
, list
);
833 BUG_ON(request
->status
!= TAPE_REQUEST_LONG_BUSY
);
834 DBF_LH(6, "%08x: Long busy timeout.\n", device
->cdev_id
);
835 __tape_start_next_request(device
);
836 device
->lb_timeout
.data
= 0UL;
837 tape_put_device(device
);
838 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
843 struct tape_device
* device
,
844 struct tape_request
* request
,
847 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device
, request
, rc
);
850 request
->status
= TAPE_REQUEST_DONE
;
852 /* Remove from request queue. */
853 list_del(&request
->list
);
856 if (request
->callback
!= NULL
)
857 request
->callback(request
, request
->callback_data
);
860 /* Start next request. */
861 if (!list_empty(&device
->req_queue
))
862 __tape_start_next_request(device
);
866 * Write sense data to dbf
869 tape_dump_sense_dbf(struct tape_device
*device
, struct tape_request
*request
,
876 op
= tape_op_verbose
[request
->op
];
879 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
880 irb
->scsw
.cmd
.dstat
, irb
->scsw
.cmd
.cstat
);
881 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device
->cdev_id
, op
);
882 sptr
= (unsigned int *) irb
->ecw
;
883 DBF_EVENT(3, "%08x %08x\n", sptr
[0], sptr
[1]);
884 DBF_EVENT(3, "%08x %08x\n", sptr
[2], sptr
[3]);
885 DBF_EVENT(3, "%08x %08x\n", sptr
[4], sptr
[5]);
886 DBF_EVENT(3, "%08x %08x\n", sptr
[6], sptr
[7]);
890 * I/O helper function. Adds the request to the request queue
891 * and starts it if the tape is idle. Has to be called with
892 * the device lock held.
895 __tape_start_request(struct tape_device
*device
, struct tape_request
*request
)
899 switch (request
->op
) {
905 if (device
->tape_state
== TS_INIT
)
907 if (device
->tape_state
== TS_UNUSED
)
910 if (device
->tape_state
== TS_BLKUSE
)
912 if (device
->tape_state
!= TS_IN_USE
)
916 /* Increase use count of device for the added request. */
917 request
->device
= tape_get_device(device
);
919 if (list_empty(&device
->req_queue
)) {
920 /* No other requests are on the queue. Start this one. */
921 rc
= __tape_start_io(device
, request
);
925 DBF_LH(5, "Request %p added for execution.\n", request
);
926 list_add(&request
->list
, &device
->req_queue
);
928 DBF_LH(5, "Request %p add to queue.\n", request
);
929 request
->status
= TAPE_REQUEST_QUEUED
;
930 list_add_tail(&request
->list
, &device
->req_queue
);
936 * Add the request to the request queue, try to start it if the
937 * tape is idle. Return without waiting for end of i/o.
940 tape_do_io_async(struct tape_device
*device
, struct tape_request
*request
)
944 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device
, request
);
946 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
947 /* Add request to request queue and try to start it. */
948 rc
= __tape_start_request(device
, request
);
949 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
954 * tape_do_io/__tape_wake_up
955 * Add the request to the request queue, try to start it if the
956 * tape is idle and wait uninterruptible for its completion.
959 __tape_wake_up(struct tape_request
*request
, void *data
)
961 request
->callback
= NULL
;
962 wake_up((wait_queue_head_t
*) data
);
966 tape_do_io(struct tape_device
*device
, struct tape_request
*request
)
970 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
972 request
->callback
= __tape_wake_up
;
973 request
->callback_data
= &device
->wait_queue
;
974 /* Add request to request queue and try to start it. */
975 rc
= __tape_start_request(device
, request
);
976 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
979 /* Request added to the queue. Wait for its completion. */
980 wait_event(device
->wait_queue
, (request
->callback
== NULL
));
981 /* Get rc from request */
986 * tape_do_io_interruptible/__tape_wake_up_interruptible
987 * Add the request to the request queue, try to start it if the
988 * tape is idle and wait uninterruptible for its completion.
991 __tape_wake_up_interruptible(struct tape_request
*request
, void *data
)
993 request
->callback
= NULL
;
994 wake_up_interruptible((wait_queue_head_t
*) data
);
998 tape_do_io_interruptible(struct tape_device
*device
,
999 struct tape_request
*request
)
1003 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1004 /* Setup callback */
1005 request
->callback
= __tape_wake_up_interruptible
;
1006 request
->callback_data
= &device
->wait_queue
;
1007 rc
= __tape_start_request(device
, request
);
1008 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1011 /* Request added to the queue. Wait for its completion. */
1012 rc
= wait_event_interruptible(device
->wait_queue
,
1013 (request
->callback
== NULL
));
1014 if (rc
!= -ERESTARTSYS
)
1015 /* Request finished normally. */
1018 /* Interrupted by a signal. We have to stop the current request. */
1019 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1020 rc
= __tape_cancel_io(device
, request
);
1021 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1023 /* Wait for the interrupt that acknowledges the halt. */
1025 rc
= wait_event_interruptible(
1027 (request
->callback
== NULL
)
1029 } while (rc
== -ERESTARTSYS
);
1031 DBF_EVENT(3, "IO stopped on %08x\n", device
->cdev_id
);
1041 tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
1045 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1046 rc
= __tape_cancel_io(device
, request
);
1047 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1052 * Tape interrupt routine, called from the ccw_device layer
1055 __tape_do_irq (struct ccw_device
*cdev
, unsigned long intparm
, struct irb
*irb
)
1057 struct tape_device
*device
;
1058 struct tape_request
*request
;
1061 device
= dev_get_drvdata(&cdev
->dev
);
1062 if (device
== NULL
) {
1065 request
= (struct tape_request
*) intparm
;
1067 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device
, request
);
1069 /* On special conditions irb is an error pointer */
1071 switch (PTR_ERR(irb
)) {
1073 DBF_LH(1, "(%s): Request timed out\n",
1074 dev_name(&cdev
->dev
));
1076 __tape_end_request(device
, request
, -EIO
);
1079 DBF_LH(1, "(%s): Unexpected i/o error %li\n",
1080 dev_name(&cdev
->dev
),
1087 * If the condition code is not zero and the start function bit is
1088 * still set, this is an deferred error and the last start I/O did
1089 * not succeed. At this point the condition that caused the deferred
1090 * error might still apply. So we just schedule the request to be
1093 if (irb
->scsw
.cmd
.cc
!= 0 &&
1094 (irb
->scsw
.cmd
.fctl
& SCSW_FCTL_START_FUNC
) &&
1095 (request
->status
== TAPE_REQUEST_IN_IO
)) {
1096 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1097 device
->cdev_id
, irb
->scsw
.cmd
.cc
, irb
->scsw
.cmd
.fctl
);
1098 request
->status
= TAPE_REQUEST_QUEUED
;
1099 schedule_delayed_work(&device
->tape_dnr
, HZ
);
1103 /* May be an unsolicited irq */
1105 request
->rescnt
= irb
->scsw
.cmd
.count
;
1106 else if ((irb
->scsw
.cmd
.dstat
== 0x85 || irb
->scsw
.cmd
.dstat
== 0x80) &&
1107 !list_empty(&device
->req_queue
)) {
1108 /* Not Ready to Ready after long busy ? */
1109 struct tape_request
*req
;
1110 req
= list_entry(device
->req_queue
.next
,
1111 struct tape_request
, list
);
1112 if (req
->status
== TAPE_REQUEST_LONG_BUSY
) {
1113 DBF_EVENT(3, "(%08x): del timer\n", device
->cdev_id
);
1114 if (del_timer(&device
->lb_timeout
)) {
1115 device
->lb_timeout
.data
= 0UL;
1116 tape_put_device(device
);
1117 __tape_start_next_request(device
);
1122 if (irb
->scsw
.cmd
.dstat
!= 0x0c) {
1123 /* Set the 'ONLINE' flag depending on sense byte 1 */
1124 if(*(((__u8
*) irb
->ecw
) + 1) & SENSE_DRIVE_ONLINE
)
1125 device
->tape_generic_status
|= GMT_ONLINE(~0);
1127 device
->tape_generic_status
&= ~GMT_ONLINE(~0);
1130 * Any request that does not come back with channel end
1131 * and device end is unusual. Log the sense data.
1133 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1134 tape_dump_sense_dbf(device
, request
, irb
);
1136 /* Upon normal completion the device _is_ online */
1137 device
->tape_generic_status
|= GMT_ONLINE(~0);
1139 if (device
->tape_state
== TS_NOT_OPER
) {
1140 DBF_EVENT(6, "tape:device is not operational\n");
1145 * Request that were canceled still come back with an interrupt.
1146 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1148 if(request
!= NULL
&& request
->status
== TAPE_REQUEST_DONE
) {
1149 __tape_end_request(device
, request
, -EIO
);
1153 rc
= device
->discipline
->irq(device
, request
, irb
);
1155 * rc < 0 : request finished unsuccessfully.
1156 * rc == TAPE_IO_SUCCESS: request finished successfully.
1157 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1158 * rc == TAPE_IO_RETRY: request finished but needs another go.
1159 * rc == TAPE_IO_STOP: request needs to get terminated.
1162 case TAPE_IO_SUCCESS
:
1163 /* Upon normal completion the device _is_ online */
1164 device
->tape_generic_status
|= GMT_ONLINE(~0);
1165 __tape_end_request(device
, request
, rc
);
1167 case TAPE_IO_PENDING
:
1169 case TAPE_IO_LONG_BUSY
:
1170 device
->lb_timeout
.data
=
1171 (unsigned long) tape_get_device(device
);
1172 device
->lb_timeout
.expires
= jiffies
+
1173 LONG_BUSY_TIMEOUT
* HZ
;
1174 DBF_EVENT(3, "(%08x): add timer\n", device
->cdev_id
);
1175 add_timer(&device
->lb_timeout
);
1176 request
->status
= TAPE_REQUEST_LONG_BUSY
;
1179 rc
= __tape_start_io(device
, request
);
1181 __tape_end_request(device
, request
, rc
);
1184 rc
= __tape_cancel_io(device
, request
);
1186 __tape_end_request(device
, request
, rc
);
1190 DBF_EVENT(6, "xunknownrc\n");
1191 __tape_end_request(device
, request
, -EIO
);
1193 __tape_end_request(device
, request
, rc
);
1200 * Tape device open function used by tape_char & tape_block frontends.
1203 tape_open(struct tape_device
*device
)
1207 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1208 if (device
->tape_state
== TS_NOT_OPER
) {
1209 DBF_EVENT(6, "TAPE:nodev\n");
1211 } else if (device
->tape_state
== TS_IN_USE
) {
1212 DBF_EVENT(6, "TAPE:dbusy\n");
1214 } else if (device
->tape_state
== TS_BLKUSE
) {
1215 DBF_EVENT(6, "TAPE:dbusy\n");
1217 } else if (device
->discipline
!= NULL
&&
1218 !try_module_get(device
->discipline
->owner
)) {
1219 DBF_EVENT(6, "TAPE:nodisc\n");
1222 tape_state_set(device
, TS_IN_USE
);
1225 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1230 * Tape device release function used by tape_char & tape_block frontends.
1233 tape_release(struct tape_device
*device
)
1235 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1236 if (device
->tape_state
== TS_IN_USE
)
1237 tape_state_set(device
, TS_UNUSED
);
1238 module_put(device
->discipline
->owner
);
1239 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1244 * Execute a magnetic tape command a number of times.
1247 tape_mtop(struct tape_device
*device
, int mt_op
, int mt_count
)
1252 DBF_EVENT(6, "TAPE:mtio\n");
1253 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op
);
1254 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count
);
1256 if (mt_op
< 0 || mt_op
>= TAPE_NR_MTOPS
)
1258 fn
= device
->discipline
->mtop_array
[mt_op
];
1262 /* We assume that the backends can handle count up to 500. */
1263 if (mt_op
== MTBSR
|| mt_op
== MTFSR
|| mt_op
== MTFSF
||
1264 mt_op
== MTBSF
|| mt_op
== MTFSFM
|| mt_op
== MTBSFM
) {
1266 for (; mt_count
> 500; mt_count
-= 500)
1267 if ((rc
= fn(device
, 500)) != 0)
1270 rc
= fn(device
, mt_count
);
1272 rc
= fn(device
, mt_count
);
1278 * Tape init function.
1283 TAPE_DBF_AREA
= debug_register ( "tape", 2, 2, 4*sizeof(long));
1284 debug_register_view(TAPE_DBF_AREA
, &debug_sprintf_view
);
1285 #ifdef DBF_LIKE_HELL
1286 debug_set_level(TAPE_DBF_AREA
, 6);
1288 DBF_EVENT(3, "tape init\n");
1296 * Tape exit function.
1301 DBF_EVENT(6, "tape exit\n");
1303 /* Get rid of the frontends */
1306 tape_proc_cleanup();
1307 debug_unregister (TAPE_DBF_AREA
);
1310 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1311 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1312 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1313 MODULE_LICENSE("GPL");
1315 module_init(tape_init
);
1316 module_exit(tape_exit
);
1318 EXPORT_SYMBOL(tape_generic_remove
);
1319 EXPORT_SYMBOL(tape_generic_probe
);
1320 EXPORT_SYMBOL(tape_generic_online
);
1321 EXPORT_SYMBOL(tape_generic_offline
);
1322 EXPORT_SYMBOL(tape_generic_pm_suspend
);
1323 EXPORT_SYMBOL(tape_put_device
);
1324 EXPORT_SYMBOL(tape_get_device
);
1325 EXPORT_SYMBOL(tape_state_verbose
);
1326 EXPORT_SYMBOL(tape_op_verbose
);
1327 EXPORT_SYMBOL(tape_state_set
);
1328 EXPORT_SYMBOL(tape_med_state_set
);
1329 EXPORT_SYMBOL(tape_alloc_request
);
1330 EXPORT_SYMBOL(tape_free_request
);
1331 EXPORT_SYMBOL(tape_dump_sense_dbf
);
1332 EXPORT_SYMBOL(tape_do_io
);
1333 EXPORT_SYMBOL(tape_do_io_async
);
1334 EXPORT_SYMBOL(tape_do_io_interruptible
);
1335 EXPORT_SYMBOL(tape_cancel_io
);
1336 EXPORT_SYMBOL(tape_mtop
);