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,2006
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 #include <linux/module.h>
15 #include <linux/init.h> // for kernel parameters
16 #include <linux/kmod.h> // for requesting modules
17 #include <linux/spinlock.h> // for locks
18 #include <linux/vmalloc.h>
19 #include <linux/list.h>
21 #include <asm/types.h> // for variable types
23 #define TAPE_DBF_AREA tape_core_dbf
28 #define PRINTK_HEADER "TAPE_CORE: "
29 #define LONG_BUSY_TIMEOUT 180 /* seconds */
31 static void __tape_do_irq (struct ccw_device
*, unsigned long, struct irb
*);
32 static void tape_delayed_next_request(struct work_struct
*);
33 static void tape_long_busy_timeout(unsigned long data
);
36 * One list to contain all tape devices of all disciplines, so
37 * we can assign the devices to minor numbers of the same major
38 * The list is protected by the rwlock
40 static struct list_head tape_device_list
= LIST_HEAD_INIT(tape_device_list
);
41 static DEFINE_RWLOCK(tape_device_lock
);
44 * Pointer to debug area.
46 debug_info_t
*TAPE_DBF_AREA
= NULL
;
47 EXPORT_SYMBOL(TAPE_DBF_AREA
);
50 * Printable strings for tape enumerations.
52 const char *tape_state_verbose
[TS_SIZE
] =
54 [TS_UNUSED
] = "UNUSED",
55 [TS_IN_USE
] = "IN_USE",
56 [TS_BLKUSE
] = "BLKUSE",
58 [TS_NOT_OPER
] = "NOT_OP"
61 const char *tape_op_verbose
[TO_SIZE
] =
63 [TO_BLOCK
] = "BLK", [TO_BSB
] = "BSB",
64 [TO_BSF
] = "BSF", [TO_DSE
] = "DSE",
65 [TO_FSB
] = "FSB", [TO_FSF
] = "FSF",
66 [TO_LBL
] = "LBL", [TO_NOP
] = "NOP",
67 [TO_RBA
] = "RBA", [TO_RBI
] = "RBI",
68 [TO_RFO
] = "RFO", [TO_REW
] = "REW",
69 [TO_RUN
] = "RUN", [TO_WRI
] = "WRI",
70 [TO_WTM
] = "WTM", [TO_MSEN
] = "MSN",
71 [TO_LOAD
] = "LOA", [TO_READ_CONFIG
] = "RCF",
72 [TO_READ_ATTMSG
] = "RAT",
73 [TO_DIS
] = "DIS", [TO_ASSIGN
] = "ASS",
74 [TO_UNASSIGN
] = "UAS", [TO_CRYPT_ON
] = "CON",
75 [TO_CRYPT_OFF
] = "COF", [TO_KEKL_SET
] = "KLS",
76 [TO_KEKL_QUERY
] = "KLQ",[TO_RDC
] = "RDC",
80 busid_to_int(char *bus_id
)
86 for(s
= bus_id
, d
= 0; *s
!= '\0' && *s
!= '.'; s
++)
87 d
= (d
* 10) + (*s
- '0');
89 for(s
++, d
= 0; *s
!= '\0' && *s
!= '.'; s
++)
90 d
= (d
* 10) + (*s
- '0');
93 for(s
++; *s
!= '\0'; s
++) {
94 if (*s
>= '0' && *s
<= '9') {
96 } else if (*s
>= 'a' && *s
<= 'f') {
101 dec
= (dec
<< 4) + d
;
108 * Some channel attached tape specific attributes.
110 * FIXME: In the future the first_minor and blocksize attribute should be
111 * replaced by a link to the cdev tree.
114 tape_medium_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
116 struct tape_device
*tdev
;
118 tdev
= (struct tape_device
*) dev
->driver_data
;
119 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->medium_state
);
123 DEVICE_ATTR(medium_state
, 0444, tape_medium_state_show
, NULL
);
126 tape_first_minor_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
128 struct tape_device
*tdev
;
130 tdev
= (struct tape_device
*) dev
->driver_data
;
131 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->first_minor
);
135 DEVICE_ATTR(first_minor
, 0444, tape_first_minor_show
, NULL
);
138 tape_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
140 struct tape_device
*tdev
;
142 tdev
= (struct tape_device
*) dev
->driver_data
;
143 return scnprintf(buf
, PAGE_SIZE
, "%s\n", (tdev
->first_minor
< 0) ?
144 "OFFLINE" : tape_state_verbose
[tdev
->tape_state
]);
148 DEVICE_ATTR(state
, 0444, tape_state_show
, NULL
);
151 tape_operation_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
153 struct tape_device
*tdev
;
156 tdev
= (struct tape_device
*) dev
->driver_data
;
157 if (tdev
->first_minor
< 0)
158 return scnprintf(buf
, PAGE_SIZE
, "N/A\n");
160 spin_lock_irq(get_ccwdev_lock(tdev
->cdev
));
161 if (list_empty(&tdev
->req_queue
))
162 rc
= scnprintf(buf
, PAGE_SIZE
, "---\n");
164 struct tape_request
*req
;
166 req
= list_entry(tdev
->req_queue
.next
, struct tape_request
,
168 rc
= scnprintf(buf
,PAGE_SIZE
, "%s\n", tape_op_verbose
[req
->op
]);
170 spin_unlock_irq(get_ccwdev_lock(tdev
->cdev
));
175 DEVICE_ATTR(operation
, 0444, tape_operation_show
, NULL
);
178 tape_blocksize_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
180 struct tape_device
*tdev
;
182 tdev
= (struct tape_device
*) dev
->driver_data
;
184 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->char_data
.block_size
);
188 DEVICE_ATTR(blocksize
, 0444, tape_blocksize_show
, NULL
);
190 static struct attribute
*tape_attrs
[] = {
191 &dev_attr_medium_state
.attr
,
192 &dev_attr_first_minor
.attr
,
193 &dev_attr_state
.attr
,
194 &dev_attr_operation
.attr
,
195 &dev_attr_blocksize
.attr
,
199 static struct attribute_group tape_attr_group
= {
204 * Tape state functions
207 tape_state_set(struct tape_device
*device
, enum tape_state newstate
)
211 if (device
->tape_state
== TS_NOT_OPER
) {
212 DBF_EVENT(3, "ts_set err: not oper\n");
215 DBF_EVENT(4, "ts. dev: %x\n", device
->first_minor
);
216 DBF_EVENT(4, "old ts:\t\n");
217 if (device
->tape_state
< TS_SIZE
&& device
->tape_state
>=0 )
218 str
= tape_state_verbose
[device
->tape_state
];
221 DBF_EVENT(4, "%s\n", str
);
222 DBF_EVENT(4, "new ts:\t\n");
223 if (newstate
< TS_SIZE
&& newstate
>= 0)
224 str
= tape_state_verbose
[newstate
];
227 DBF_EVENT(4, "%s\n", str
);
228 device
->tape_state
= newstate
;
229 wake_up(&device
->state_change_wq
);
233 tape_med_state_set(struct tape_device
*device
, enum tape_medium_state newstate
)
235 if (device
->medium_state
== newstate
)
239 device
->tape_generic_status
|= GMT_DR_OPEN(~0);
240 PRINT_INFO("(%s): Tape is unloaded\n",
241 device
->cdev
->dev
.bus_id
);
244 device
->tape_generic_status
&= ~GMT_DR_OPEN(~0);
245 PRINT_INFO("(%s): Tape has been mounted\n",
246 device
->cdev
->dev
.bus_id
);
252 device
->medium_state
= newstate
;
253 wake_up(&device
->state_change_wq
);
257 * Stop running ccw. Has to be called with the device lock held.
260 __tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
265 /* Check if interrupt has already been processed */
266 if (request
->callback
== NULL
)
270 for (retries
= 0; retries
< 5; retries
++) {
271 rc
= ccw_device_clear(device
->cdev
, (long) request
);
275 request
->status
= TAPE_REQUEST_DONE
;
278 request
->status
= TAPE_REQUEST_CANCEL
;
279 schedule_delayed_work(&device
->tape_dnr
, 0);
282 DBF_EXCEPTION(2, "device gone, retry\n");
285 DBF_EXCEPTION(2, "I/O error, retry\n");
296 * Add device into the sorted list, giving it the first
297 * available minor number.
300 tape_assign_minor(struct tape_device
*device
)
302 struct tape_device
*tmp
;
306 write_lock(&tape_device_lock
);
307 list_for_each_entry(tmp
, &tape_device_list
, node
) {
308 if (minor
< tmp
->first_minor
)
310 minor
+= TAPE_MINORS_PER_DEV
;
313 write_unlock(&tape_device_lock
);
316 device
->first_minor
= minor
;
317 list_add_tail(&device
->node
, &tmp
->node
);
318 write_unlock(&tape_device_lock
);
322 /* remove device from the list */
324 tape_remove_minor(struct tape_device
*device
)
326 write_lock(&tape_device_lock
);
327 list_del_init(&device
->node
);
328 device
->first_minor
= -1;
329 write_unlock(&tape_device_lock
);
333 * Set a device online.
335 * This function is called by the common I/O layer to move a device from the
336 * detected but offline into the online state.
337 * If we return an error (RC < 0) the device remains in the offline state. This
338 * can happen if the device is assigned somewhere else, for example.
341 tape_generic_online(struct tape_device
*device
,
342 struct tape_discipline
*discipline
)
346 DBF_LH(6, "tape_enable_device(%p, %p)\n", device
, discipline
);
348 if (device
->tape_state
!= TS_INIT
) {
349 DBF_LH(3, "Tapestate not INIT (%d)\n", device
->tape_state
);
353 init_timer(&device
->lb_timeout
);
354 device
->lb_timeout
.function
= tape_long_busy_timeout
;
356 /* Let the discipline have a go at the device. */
357 device
->discipline
= discipline
;
358 if (!try_module_get(discipline
->owner
)) {
359 PRINT_ERR("Cannot get module. Module gone.\n");
363 rc
= discipline
->setup_device(device
);
366 rc
= tape_assign_minor(device
);
370 rc
= tapechar_setup_device(device
);
373 rc
= tapeblock_setup_device(device
);
377 tape_state_set(device
, TS_UNUSED
);
379 DBF_LH(3, "(%08x): Drive set online\n", device
->cdev_id
);
384 tapechar_cleanup_device(device
);
386 device
->discipline
->cleanup_device(device
);
387 device
->discipline
= NULL
;
389 tape_remove_minor(device
);
391 module_put(discipline
->owner
);
396 tape_cleanup_device(struct tape_device
*device
)
398 tapeblock_cleanup_device(device
);
399 tapechar_cleanup_device(device
);
400 device
->discipline
->cleanup_device(device
);
401 module_put(device
->discipline
->owner
);
402 tape_remove_minor(device
);
403 tape_med_state_set(device
, MS_UNKNOWN
);
407 * Set device offline.
409 * Called by the common I/O layer if the drive should set offline on user
410 * request. We may prevent this by returning an error.
411 * Manual offline is only allowed while the drive is not in use.
414 tape_generic_offline(struct tape_device
*device
)
417 PRINT_ERR("tape_generic_offline: no such device\n");
421 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
422 device
->cdev_id
, device
);
424 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
425 switch (device
->tape_state
) {
428 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
431 tape_state_set(device
, TS_INIT
);
432 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
433 tape_cleanup_device(device
);
436 DBF_EVENT(3, "(%08x): Set offline failed "
439 PRINT_WARN("(%s): Set offline failed "
441 device
->cdev
->dev
.bus_id
);
442 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
446 DBF_LH(3, "(%08x): Drive set offline.\n", device
->cdev_id
);
451 * Allocate memory for a new device structure.
453 static struct tape_device
*
454 tape_alloc_device(void)
456 struct tape_device
*device
;
458 device
= kzalloc(sizeof(struct tape_device
), GFP_KERNEL
);
459 if (device
== NULL
) {
460 DBF_EXCEPTION(2, "ti:no mem\n");
461 PRINT_INFO ("can't allocate memory for "
462 "tape info structure\n");
463 return ERR_PTR(-ENOMEM
);
465 device
->modeset_byte
= kmalloc(1, GFP_KERNEL
| GFP_DMA
);
466 if (device
->modeset_byte
== NULL
) {
467 DBF_EXCEPTION(2, "ti:no mem\n");
468 PRINT_INFO("can't allocate memory for modeset byte\n");
470 return ERR_PTR(-ENOMEM
);
472 INIT_LIST_HEAD(&device
->req_queue
);
473 INIT_LIST_HEAD(&device
->node
);
474 init_waitqueue_head(&device
->state_change_wq
);
475 device
->tape_state
= TS_INIT
;
476 device
->medium_state
= MS_UNKNOWN
;
477 *device
->modeset_byte
= 0;
478 device
->first_minor
= -1;
479 atomic_set(&device
->ref_count
, 1);
480 INIT_DELAYED_WORK(&device
->tape_dnr
, tape_delayed_next_request
);
486 * Get a reference to an existing device structure. This will automatically
487 * increment the reference count.
490 tape_get_device_reference(struct tape_device
*device
)
492 DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device
,
493 atomic_inc_return(&device
->ref_count
));
499 * Decrease the reference counter of a devices structure. If the
500 * reference counter reaches zero free the device structure.
501 * The function returns a NULL pointer to be used by the caller
502 * for clearing reference pointers.
505 tape_put_device(struct tape_device
*device
)
509 remain
= atomic_dec_return(&device
->ref_count
);
511 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device
, remain
);
514 DBF_EVENT(4, "put device without reference\n");
515 PRINT_ERR("put device without reference\n");
517 DBF_EVENT(4, "tape_free_device(%p)\n", device
);
518 kfree(device
->modeset_byte
);
527 * Find tape device by a device index.
530 tape_get_device(int devindex
)
532 struct tape_device
*device
, *tmp
;
534 device
= ERR_PTR(-ENODEV
);
535 read_lock(&tape_device_lock
);
536 list_for_each_entry(tmp
, &tape_device_list
, node
) {
537 if (tmp
->first_minor
/ TAPE_MINORS_PER_DEV
== devindex
) {
538 device
= tape_get_device_reference(tmp
);
542 read_unlock(&tape_device_lock
);
547 * Driverfs tape probe function.
550 tape_generic_probe(struct ccw_device
*cdev
)
552 struct tape_device
*device
;
555 device
= tape_alloc_device();
558 ccw_device_set_options(cdev
, CCWDEV_DO_PATHGROUP
);
559 ret
= sysfs_create_group(&cdev
->dev
.kobj
, &tape_attr_group
);
561 tape_put_device(device
);
562 PRINT_ERR("probe failed for tape device %s\n", cdev
->dev
.bus_id
);
565 cdev
->dev
.driver_data
= device
;
566 cdev
->handler
= __tape_do_irq
;
568 device
->cdev_id
= busid_to_int(cdev
->dev
.bus_id
);
569 PRINT_INFO("tape device %s found\n", cdev
->dev
.bus_id
);
574 __tape_discard_requests(struct tape_device
*device
)
576 struct tape_request
* request
;
577 struct list_head
* l
, *n
;
579 list_for_each_safe(l
, n
, &device
->req_queue
) {
580 request
= list_entry(l
, struct tape_request
, list
);
581 if (request
->status
== TAPE_REQUEST_IN_IO
)
582 request
->status
= TAPE_REQUEST_DONE
;
583 list_del(&request
->list
);
585 /* Decrease ref_count for removed request. */
586 request
->device
= tape_put_device(device
);
588 if (request
->callback
!= NULL
)
589 request
->callback(request
, request
->callback_data
);
594 * Driverfs tape remove function.
596 * This function is called whenever the common I/O layer detects the device
597 * gone. This can happen at any time and we cannot refuse.
600 tape_generic_remove(struct ccw_device
*cdev
)
602 struct tape_device
* device
;
604 device
= cdev
->dev
.driver_data
;
606 PRINT_ERR("No device pointer in tape_generic_remove!\n");
609 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device
->cdev_id
, cdev
);
611 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
612 switch (device
->tape_state
) {
614 tape_state_set(device
, TS_NOT_OPER
);
619 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
623 * Need only to release the device.
625 tape_state_set(device
, TS_NOT_OPER
);
626 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
627 tape_cleanup_device(device
);
631 * There may be requests on the queue. We will not get
632 * an interrupt for a request that was running. So we
633 * just post them all as I/O errors.
635 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
637 PRINT_WARN("(%s): Drive in use vanished - "
639 device
->cdev
->dev
.bus_id
);
640 PRINT_WARN("State was %i\n", device
->tape_state
);
641 tape_state_set(device
, TS_NOT_OPER
);
642 __tape_discard_requests(device
);
643 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
644 tape_cleanup_device(device
);
647 if (cdev
->dev
.driver_data
!= NULL
) {
648 sysfs_remove_group(&cdev
->dev
.kobj
, &tape_attr_group
);
649 cdev
->dev
.driver_data
= tape_put_device(cdev
->dev
.driver_data
);
654 * Allocate a new tape ccw request
656 struct tape_request
*
657 tape_alloc_request(int cplength
, int datasize
)
659 struct tape_request
*request
;
661 if (datasize
> PAGE_SIZE
|| (cplength
*sizeof(struct ccw1
)) > PAGE_SIZE
)
664 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength
, datasize
);
666 request
= kzalloc(sizeof(struct tape_request
), GFP_KERNEL
);
667 if (request
== NULL
) {
668 DBF_EXCEPTION(1, "cqra nomem\n");
669 return ERR_PTR(-ENOMEM
);
671 /* allocate channel program */
673 request
->cpaddr
= kcalloc(cplength
, sizeof(struct ccw1
),
674 GFP_ATOMIC
| GFP_DMA
);
675 if (request
->cpaddr
== NULL
) {
676 DBF_EXCEPTION(1, "cqra nomem\n");
678 return ERR_PTR(-ENOMEM
);
681 /* alloc small kernel buffer */
683 request
->cpdata
= kzalloc(datasize
, GFP_KERNEL
| GFP_DMA
);
684 if (request
->cpdata
== NULL
) {
685 DBF_EXCEPTION(1, "cqra nomem\n");
686 kfree(request
->cpaddr
);
688 return ERR_PTR(-ENOMEM
);
691 DBF_LH(6, "New request %p(%p/%p)\n", request
, request
->cpaddr
,
698 * Free tape ccw request
701 tape_free_request (struct tape_request
* request
)
703 DBF_LH(6, "Free request %p\n", request
);
705 if (request
->device
!= NULL
) {
706 request
->device
= tape_put_device(request
->device
);
708 kfree(request
->cpdata
);
709 kfree(request
->cpaddr
);
714 __tape_start_io(struct tape_device
*device
, struct tape_request
*request
)
718 #ifdef CONFIG_S390_TAPE_BLOCK
719 if (request
->op
== TO_BLOCK
)
720 device
->discipline
->check_locate(device
, request
);
722 rc
= ccw_device_start(
725 (unsigned long) request
,
730 request
->status
= TAPE_REQUEST_IN_IO
;
731 } else if (rc
== -EBUSY
) {
732 /* The common I/O subsystem is currently busy. Retry later. */
733 request
->status
= TAPE_REQUEST_QUEUED
;
734 schedule_delayed_work(&device
->tape_dnr
, 0);
737 /* Start failed. Remove request and indicate failure. */
738 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc
);
744 __tape_start_next_request(struct tape_device
*device
)
746 struct list_head
*l
, *n
;
747 struct tape_request
*request
;
750 DBF_LH(6, "__tape_start_next_request(%p)\n", device
);
752 * Try to start each request on request queue until one is
753 * started successful.
755 list_for_each_safe(l
, n
, &device
->req_queue
) {
756 request
= list_entry(l
, struct tape_request
, list
);
759 * Avoid race condition if bottom-half was triggered more than
762 if (request
->status
== TAPE_REQUEST_IN_IO
)
765 * Request has already been stopped. We have to wait until
766 * the request is removed from the queue in the interrupt
769 if (request
->status
== TAPE_REQUEST_DONE
)
773 * We wanted to cancel the request but the common I/O layer
774 * was busy at that time. This can only happen if this
775 * function is called by delayed_next_request.
776 * Otherwise we start the next request on the queue.
778 if (request
->status
== TAPE_REQUEST_CANCEL
) {
779 rc
= __tape_cancel_io(device
, request
);
781 rc
= __tape_start_io(device
, request
);
786 /* Set ending status. */
788 request
->status
= TAPE_REQUEST_DONE
;
790 /* Remove from request queue. */
791 list_del(&request
->list
);
794 if (request
->callback
!= NULL
)
795 request
->callback(request
, request
->callback_data
);
800 tape_delayed_next_request(struct work_struct
*work
)
802 struct tape_device
*device
=
803 container_of(work
, struct tape_device
, tape_dnr
.work
);
805 DBF_LH(6, "tape_delayed_next_request(%p)\n", device
);
806 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
807 __tape_start_next_request(device
);
808 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
811 static void tape_long_busy_timeout(unsigned long data
)
813 struct tape_request
*request
;
814 struct tape_device
*device
;
816 device
= (struct tape_device
*) data
;
817 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
818 request
= list_entry(device
->req_queue
.next
, struct tape_request
, list
);
819 if (request
->status
!= TAPE_REQUEST_LONG_BUSY
)
821 DBF_LH(6, "%08x: Long busy timeout.\n", device
->cdev_id
);
822 __tape_start_next_request(device
);
823 device
->lb_timeout
.data
= (unsigned long) tape_put_device(device
);
824 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
829 struct tape_device
* device
,
830 struct tape_request
* request
,
833 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device
, request
, rc
);
836 request
->status
= TAPE_REQUEST_DONE
;
838 /* Remove from request queue. */
839 list_del(&request
->list
);
842 if (request
->callback
!= NULL
)
843 request
->callback(request
, request
->callback_data
);
846 /* Start next request. */
847 if (!list_empty(&device
->req_queue
))
848 __tape_start_next_request(device
);
852 * Write sense data to console/dbf
855 tape_dump_sense(struct tape_device
* device
, struct tape_request
*request
,
860 PRINT_INFO("-------------------------------------------------\n");
861 PRINT_INFO("DSTAT : %02x CSTAT: %02x CPA: %04x\n",
862 irb
->scsw
.dstat
, irb
->scsw
.cstat
, irb
->scsw
.cpa
);
863 PRINT_INFO("DEVICE: %s\n", device
->cdev
->dev
.bus_id
);
865 PRINT_INFO("OP : %s\n", tape_op_verbose
[request
->op
]);
867 sptr
= (unsigned int *) irb
->ecw
;
868 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
869 sptr
[0], sptr
[1], sptr
[2], sptr
[3]);
870 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
871 sptr
[4], sptr
[5], sptr
[6], sptr
[7]);
872 PRINT_INFO("--------------------------------------------------\n");
876 * Write sense data to dbf
879 tape_dump_sense_dbf(struct tape_device
*device
, struct tape_request
*request
,
886 op
= tape_op_verbose
[request
->op
];
889 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
890 irb
->scsw
.dstat
,irb
->scsw
.cstat
);
891 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device
->cdev_id
, op
);
892 sptr
= (unsigned int *) irb
->ecw
;
893 DBF_EVENT(3, "%08x %08x\n", sptr
[0], sptr
[1]);
894 DBF_EVENT(3, "%08x %08x\n", sptr
[2], sptr
[3]);
895 DBF_EVENT(3, "%08x %08x\n", sptr
[4], sptr
[5]);
896 DBF_EVENT(3, "%08x %08x\n", sptr
[6], sptr
[7]);
900 * I/O helper function. Adds the request to the request queue
901 * and starts it if the tape is idle. Has to be called with
902 * the device lock held.
905 __tape_start_request(struct tape_device
*device
, struct tape_request
*request
)
909 switch (request
->op
) {
915 if (device
->tape_state
== TS_INIT
)
917 if (device
->tape_state
== TS_UNUSED
)
920 if (device
->tape_state
== TS_BLKUSE
)
922 if (device
->tape_state
!= TS_IN_USE
)
926 /* Increase use count of device for the added request. */
927 request
->device
= tape_get_device_reference(device
);
929 if (list_empty(&device
->req_queue
)) {
930 /* No other requests are on the queue. Start this one. */
931 rc
= __tape_start_io(device
, request
);
935 DBF_LH(5, "Request %p added for execution.\n", request
);
936 list_add(&request
->list
, &device
->req_queue
);
938 DBF_LH(5, "Request %p add to queue.\n", request
);
939 request
->status
= TAPE_REQUEST_QUEUED
;
940 list_add_tail(&request
->list
, &device
->req_queue
);
946 * Add the request to the request queue, try to start it if the
947 * tape is idle. Return without waiting for end of i/o.
950 tape_do_io_async(struct tape_device
*device
, struct tape_request
*request
)
954 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device
, request
);
956 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
957 /* Add request to request queue and try to start it. */
958 rc
= __tape_start_request(device
, request
);
959 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
964 * tape_do_io/__tape_wake_up
965 * Add the request to the request queue, try to start it if the
966 * tape is idle and wait uninterruptible for its completion.
969 __tape_wake_up(struct tape_request
*request
, void *data
)
971 request
->callback
= NULL
;
972 wake_up((wait_queue_head_t
*) data
);
976 tape_do_io(struct tape_device
*device
, struct tape_request
*request
)
978 wait_queue_head_t wq
;
981 init_waitqueue_head(&wq
);
982 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
984 request
->callback
= __tape_wake_up
;
985 request
->callback_data
= &wq
;
986 /* Add request to request queue and try to start it. */
987 rc
= __tape_start_request(device
, request
);
988 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
991 /* Request added to the queue. Wait for its completion. */
992 wait_event(wq
, (request
->callback
== NULL
));
993 /* Get rc from request */
998 * tape_do_io_interruptible/__tape_wake_up_interruptible
999 * Add the request to the request queue, try to start it if the
1000 * tape is idle and wait uninterruptible for its completion.
1003 __tape_wake_up_interruptible(struct tape_request
*request
, void *data
)
1005 request
->callback
= NULL
;
1006 wake_up_interruptible((wait_queue_head_t
*) data
);
1010 tape_do_io_interruptible(struct tape_device
*device
,
1011 struct tape_request
*request
)
1013 wait_queue_head_t wq
;
1016 init_waitqueue_head(&wq
);
1017 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1018 /* Setup callback */
1019 request
->callback
= __tape_wake_up_interruptible
;
1020 request
->callback_data
= &wq
;
1021 rc
= __tape_start_request(device
, request
);
1022 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1025 /* Request added to the queue. Wait for its completion. */
1026 rc
= wait_event_interruptible(wq
, (request
->callback
== NULL
));
1027 if (rc
!= -ERESTARTSYS
)
1028 /* Request finished normally. */
1031 /* Interrupted by a signal. We have to stop the current request. */
1032 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1033 rc
= __tape_cancel_io(device
, request
);
1034 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1036 /* Wait for the interrupt that acknowledges the halt. */
1038 rc
= wait_event_interruptible(
1040 (request
->callback
== NULL
)
1042 } while (rc
== -ERESTARTSYS
);
1044 DBF_EVENT(3, "IO stopped on %08x\n", device
->cdev_id
);
1054 tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
1058 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1059 rc
= __tape_cancel_io(device
, request
);
1060 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1065 * Tape interrupt routine, called from the ccw_device layer
1068 __tape_do_irq (struct ccw_device
*cdev
, unsigned long intparm
, struct irb
*irb
)
1070 struct tape_device
*device
;
1071 struct tape_request
*request
;
1074 device
= (struct tape_device
*) cdev
->dev
.driver_data
;
1075 if (device
== NULL
) {
1076 PRINT_ERR("could not get device structure for %s "
1077 "in interrupt\n", cdev
->dev
.bus_id
);
1080 request
= (struct tape_request
*) intparm
;
1082 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device
, request
);
1084 /* On special conditions irb is an error pointer */
1086 /* FIXME: What to do with the request? */
1087 switch (PTR_ERR(irb
)) {
1089 PRINT_WARN("(%s): Request timed out\n",
1092 __tape_end_request(device
, request
, -EIO
);
1095 PRINT_ERR("(%s): Unexpected i/o error %li\n",
1103 * If the condition code is not zero and the start function bit is
1104 * still set, this is an deferred error and the last start I/O did
1105 * not succeed. At this point the condition that caused the deferred
1106 * error might still apply. So we just schedule the request to be
1109 if (irb
->scsw
.cc
!= 0 && (irb
->scsw
.fctl
& SCSW_FCTL_START_FUNC
) &&
1110 (request
->status
== TAPE_REQUEST_IN_IO
)) {
1111 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1112 device
->cdev_id
, irb
->scsw
.cc
, irb
->scsw
.fctl
);
1113 request
->status
= TAPE_REQUEST_QUEUED
;
1114 schedule_delayed_work(&device
->tape_dnr
, HZ
);
1118 /* May be an unsolicited irq */
1120 request
->rescnt
= irb
->scsw
.count
;
1121 else if ((irb
->scsw
.dstat
== 0x85 || irb
->scsw
.dstat
== 0x80) &&
1122 !list_empty(&device
->req_queue
)) {
1123 /* Not Ready to Ready after long busy ? */
1124 struct tape_request
*req
;
1125 req
= list_entry(device
->req_queue
.next
,
1126 struct tape_request
, list
);
1127 if (req
->status
== TAPE_REQUEST_LONG_BUSY
) {
1128 DBF_EVENT(3, "(%08x): del timer\n", device
->cdev_id
);
1129 if (del_timer(&device
->lb_timeout
)) {
1130 device
->lb_timeout
.data
= (unsigned long)
1131 tape_put_device(device
);
1132 __tape_start_next_request(device
);
1137 if (irb
->scsw
.dstat
!= 0x0c) {
1138 /* Set the 'ONLINE' flag depending on sense byte 1 */
1139 if(*(((__u8
*) irb
->ecw
) + 1) & SENSE_DRIVE_ONLINE
)
1140 device
->tape_generic_status
|= GMT_ONLINE(~0);
1142 device
->tape_generic_status
&= ~GMT_ONLINE(~0);
1145 * Any request that does not come back with channel end
1146 * and device end is unusual. Log the sense data.
1148 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1149 tape_dump_sense_dbf(device
, request
, irb
);
1151 /* Upon normal completion the device _is_ online */
1152 device
->tape_generic_status
|= GMT_ONLINE(~0);
1154 if (device
->tape_state
== TS_NOT_OPER
) {
1155 DBF_EVENT(6, "tape:device is not operational\n");
1160 * Request that were canceled still come back with an interrupt.
1161 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1163 if(request
!= NULL
&& request
->status
== TAPE_REQUEST_DONE
) {
1164 __tape_end_request(device
, request
, -EIO
);
1168 rc
= device
->discipline
->irq(device
, request
, irb
);
1170 * rc < 0 : request finished unsuccessfully.
1171 * rc == TAPE_IO_SUCCESS: request finished successfully.
1172 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1173 * rc == TAPE_IO_RETRY: request finished but needs another go.
1174 * rc == TAPE_IO_STOP: request needs to get terminated.
1177 case TAPE_IO_SUCCESS
:
1178 /* Upon normal completion the device _is_ online */
1179 device
->tape_generic_status
|= GMT_ONLINE(~0);
1180 __tape_end_request(device
, request
, rc
);
1182 case TAPE_IO_PENDING
:
1184 case TAPE_IO_LONG_BUSY
:
1185 device
->lb_timeout
.data
=
1186 (unsigned long)tape_get_device_reference(device
);
1187 device
->lb_timeout
.expires
= jiffies
+
1188 LONG_BUSY_TIMEOUT
* HZ
;
1189 DBF_EVENT(3, "(%08x): add timer\n", device
->cdev_id
);
1190 add_timer(&device
->lb_timeout
);
1191 request
->status
= TAPE_REQUEST_LONG_BUSY
;
1194 rc
= __tape_start_io(device
, request
);
1196 __tape_end_request(device
, request
, rc
);
1199 rc
= __tape_cancel_io(device
, request
);
1201 __tape_end_request(device
, request
, rc
);
1205 DBF_EVENT(6, "xunknownrc\n");
1206 PRINT_ERR("Invalid return code from discipline "
1207 "interrupt function.\n");
1208 __tape_end_request(device
, request
, -EIO
);
1210 __tape_end_request(device
, request
, rc
);
1217 * Tape device open function used by tape_char & tape_block frontends.
1220 tape_open(struct tape_device
*device
)
1224 spin_lock(get_ccwdev_lock(device
->cdev
));
1225 if (device
->tape_state
== TS_NOT_OPER
) {
1226 DBF_EVENT(6, "TAPE:nodev\n");
1228 } else if (device
->tape_state
== TS_IN_USE
) {
1229 DBF_EVENT(6, "TAPE:dbusy\n");
1231 } else if (device
->tape_state
== TS_BLKUSE
) {
1232 DBF_EVENT(6, "TAPE:dbusy\n");
1234 } else if (device
->discipline
!= NULL
&&
1235 !try_module_get(device
->discipline
->owner
)) {
1236 DBF_EVENT(6, "TAPE:nodisc\n");
1239 tape_state_set(device
, TS_IN_USE
);
1242 spin_unlock(get_ccwdev_lock(device
->cdev
));
1247 * Tape device release function used by tape_char & tape_block frontends.
1250 tape_release(struct tape_device
*device
)
1252 spin_lock(get_ccwdev_lock(device
->cdev
));
1253 if (device
->tape_state
== TS_IN_USE
)
1254 tape_state_set(device
, TS_UNUSED
);
1255 module_put(device
->discipline
->owner
);
1256 spin_unlock(get_ccwdev_lock(device
->cdev
));
1261 * Execute a magnetic tape command a number of times.
1264 tape_mtop(struct tape_device
*device
, int mt_op
, int mt_count
)
1269 DBF_EVENT(6, "TAPE:mtio\n");
1270 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op
);
1271 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count
);
1273 if (mt_op
< 0 || mt_op
>= TAPE_NR_MTOPS
)
1275 fn
= device
->discipline
->mtop_array
[mt_op
];
1279 /* We assume that the backends can handle count up to 500. */
1280 if (mt_op
== MTBSR
|| mt_op
== MTFSR
|| mt_op
== MTFSF
||
1281 mt_op
== MTBSF
|| mt_op
== MTFSFM
|| mt_op
== MTBSFM
) {
1283 for (; mt_count
> 500; mt_count
-= 500)
1284 if ((rc
= fn(device
, 500)) != 0)
1287 rc
= fn(device
, mt_count
);
1289 rc
= fn(device
, mt_count
);
1295 * Tape init function.
1300 TAPE_DBF_AREA
= debug_register ( "tape", 2, 2, 4*sizeof(long));
1301 debug_register_view(TAPE_DBF_AREA
, &debug_sprintf_view
);
1302 #ifdef DBF_LIKE_HELL
1303 debug_set_level(TAPE_DBF_AREA
, 6);
1305 DBF_EVENT(3, "tape init\n");
1313 * Tape exit function.
1318 DBF_EVENT(6, "tape exit\n");
1320 /* Get rid of the frontends */
1323 tape_proc_cleanup();
1324 debug_unregister (TAPE_DBF_AREA
);
1327 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1328 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1329 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1330 MODULE_LICENSE("GPL");
1332 module_init(tape_init
);
1333 module_exit(tape_exit
);
1335 EXPORT_SYMBOL(tape_generic_remove
);
1336 EXPORT_SYMBOL(tape_generic_probe
);
1337 EXPORT_SYMBOL(tape_generic_online
);
1338 EXPORT_SYMBOL(tape_generic_offline
);
1339 EXPORT_SYMBOL(tape_put_device
);
1340 EXPORT_SYMBOL(tape_get_device_reference
);
1341 EXPORT_SYMBOL(tape_state_verbose
);
1342 EXPORT_SYMBOL(tape_op_verbose
);
1343 EXPORT_SYMBOL(tape_state_set
);
1344 EXPORT_SYMBOL(tape_med_state_set
);
1345 EXPORT_SYMBOL(tape_alloc_request
);
1346 EXPORT_SYMBOL(tape_free_request
);
1347 EXPORT_SYMBOL(tape_dump_sense
);
1348 EXPORT_SYMBOL(tape_dump_sense_dbf
);
1349 EXPORT_SYMBOL(tape_do_io
);
1350 EXPORT_SYMBOL(tape_do_io_async
);
1351 EXPORT_SYMBOL(tape_do_io_interruptible
);
1352 EXPORT_SYMBOL(tape_cancel_io
);
1353 EXPORT_SYMBOL(tape_mtop
);