2 * drivers/s390/char/tape_core.c
3 * basic function of the tape device driver
5 * S390 and zSeries version
6 * Copyright (C) 2001,2005 IBM Deutschland Entwicklung GmbH, IBM Corporation
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/config.h>
15 #include <linux/module.h>
16 #include <linux/init.h> // for kernel parameters
17 #include <linux/kmod.h> // for requesting modules
18 #include <linux/spinlock.h> // for locks
19 #include <linux/vmalloc.h>
20 #include <linux/list.h>
22 #include <asm/types.h> // for variable types
24 #define TAPE_DBF_AREA tape_core_dbf
29 #define PRINTK_HEADER "TAPE_CORE: "
31 static void __tape_do_irq (struct ccw_device
*, unsigned long, struct irb
*);
32 static void tape_delayed_next_request(void * data
);
35 * One list to contain all tape devices of all disciplines, so
36 * we can assign the devices to minor numbers of the same major
37 * The list is protected by the rwlock
39 static struct list_head tape_device_list
= LIST_HEAD_INIT(tape_device_list
);
40 static DEFINE_RWLOCK(tape_device_lock
);
43 * Pointer to debug area.
45 debug_info_t
*TAPE_DBF_AREA
= NULL
;
46 EXPORT_SYMBOL(TAPE_DBF_AREA
);
49 * Printable strings for tape enumerations.
51 const char *tape_state_verbose
[TS_SIZE
] =
53 [TS_UNUSED
] = "UNUSED",
54 [TS_IN_USE
] = "IN_USE",
55 [TS_BLKUSE
] = "BLKUSE",
57 [TS_NOT_OPER
] = "NOT_OP"
60 const char *tape_op_verbose
[TO_SIZE
] =
62 [TO_BLOCK
] = "BLK", [TO_BSB
] = "BSB",
63 [TO_BSF
] = "BSF", [TO_DSE
] = "DSE",
64 [TO_FSB
] = "FSB", [TO_FSF
] = "FSF",
65 [TO_LBL
] = "LBL", [TO_NOP
] = "NOP",
66 [TO_RBA
] = "RBA", [TO_RBI
] = "RBI",
67 [TO_RFO
] = "RFO", [TO_REW
] = "REW",
68 [TO_RUN
] = "RUN", [TO_WRI
] = "WRI",
69 [TO_WTM
] = "WTM", [TO_MSEN
] = "MSN",
70 [TO_LOAD
] = "LOA", [TO_READ_CONFIG
] = "RCF",
71 [TO_READ_ATTMSG
] = "RAT",
72 [TO_DIS
] = "DIS", [TO_ASSIGN
] = "ASS",
77 busid_to_int(char *bus_id
)
83 for(s
= bus_id
, d
= 0; *s
!= '\0' && *s
!= '.'; s
++)
84 d
= (d
* 10) + (*s
- '0');
86 for(s
++, d
= 0; *s
!= '\0' && *s
!= '.'; s
++)
87 d
= (d
* 10) + (*s
- '0');
90 for(s
++; *s
!= '\0'; s
++) {
91 if (*s
>= '0' && *s
<= '9') {
93 } else if (*s
>= 'a' && *s
<= 'f') {
105 * Some channel attached tape specific attributes.
107 * FIXME: In the future the first_minor and blocksize attribute should be
108 * replaced by a link to the cdev tree.
111 tape_medium_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
113 struct tape_device
*tdev
;
115 tdev
= (struct tape_device
*) dev
->driver_data
;
116 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->medium_state
);
120 DEVICE_ATTR(medium_state
, 0444, tape_medium_state_show
, NULL
);
123 tape_first_minor_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
125 struct tape_device
*tdev
;
127 tdev
= (struct tape_device
*) dev
->driver_data
;
128 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->first_minor
);
132 DEVICE_ATTR(first_minor
, 0444, tape_first_minor_show
, NULL
);
135 tape_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
137 struct tape_device
*tdev
;
139 tdev
= (struct tape_device
*) dev
->driver_data
;
140 return scnprintf(buf
, PAGE_SIZE
, "%s\n", (tdev
->first_minor
< 0) ?
141 "OFFLINE" : tape_state_verbose
[tdev
->tape_state
]);
145 DEVICE_ATTR(state
, 0444, tape_state_show
, NULL
);
148 tape_operation_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
150 struct tape_device
*tdev
;
153 tdev
= (struct tape_device
*) dev
->driver_data
;
154 if (tdev
->first_minor
< 0)
155 return scnprintf(buf
, PAGE_SIZE
, "N/A\n");
157 spin_lock_irq(get_ccwdev_lock(tdev
->cdev
));
158 if (list_empty(&tdev
->req_queue
))
159 rc
= scnprintf(buf
, PAGE_SIZE
, "---\n");
161 struct tape_request
*req
;
163 req
= list_entry(tdev
->req_queue
.next
, struct tape_request
,
165 rc
= scnprintf(buf
,PAGE_SIZE
, "%s\n", tape_op_verbose
[req
->op
]);
167 spin_unlock_irq(get_ccwdev_lock(tdev
->cdev
));
172 DEVICE_ATTR(operation
, 0444, tape_operation_show
, NULL
);
175 tape_blocksize_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
177 struct tape_device
*tdev
;
179 tdev
= (struct tape_device
*) dev
->driver_data
;
181 return scnprintf(buf
, PAGE_SIZE
, "%i\n", tdev
->char_data
.block_size
);
185 DEVICE_ATTR(blocksize
, 0444, tape_blocksize_show
, NULL
);
187 static struct attribute
*tape_attrs
[] = {
188 &dev_attr_medium_state
.attr
,
189 &dev_attr_first_minor
.attr
,
190 &dev_attr_state
.attr
,
191 &dev_attr_operation
.attr
,
192 &dev_attr_blocksize
.attr
,
196 static struct attribute_group tape_attr_group
= {
201 * Tape state functions
204 tape_state_set(struct tape_device
*device
, enum tape_state newstate
)
208 if (device
->tape_state
== TS_NOT_OPER
) {
209 DBF_EVENT(3, "ts_set err: not oper\n");
212 DBF_EVENT(4, "ts. dev: %x\n", device
->first_minor
);
213 DBF_EVENT(4, "old ts:\t\n");
214 if (device
->tape_state
< TS_SIZE
&& device
->tape_state
>=0 )
215 str
= tape_state_verbose
[device
->tape_state
];
218 DBF_EVENT(4, "%s\n", str
);
219 DBF_EVENT(4, "new ts:\t\n");
220 if (newstate
< TS_SIZE
&& newstate
>= 0)
221 str
= tape_state_verbose
[newstate
];
224 DBF_EVENT(4, "%s\n", str
);
225 device
->tape_state
= newstate
;
226 wake_up(&device
->state_change_wq
);
230 tape_med_state_set(struct tape_device
*device
, enum tape_medium_state newstate
)
232 if (device
->medium_state
== newstate
)
236 device
->tape_generic_status
|= GMT_DR_OPEN(~0);
237 PRINT_INFO("(%s): Tape is unloaded\n",
238 device
->cdev
->dev
.bus_id
);
241 device
->tape_generic_status
&= ~GMT_DR_OPEN(~0);
242 PRINT_INFO("(%s): Tape has been mounted\n",
243 device
->cdev
->dev
.bus_id
);
249 device
->medium_state
= newstate
;
250 wake_up(&device
->state_change_wq
);
254 * Stop running ccw. Has to be called with the device lock held.
257 __tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
262 /* Check if interrupt has already been processed */
263 if (request
->callback
== NULL
)
267 for (retries
= 0; retries
< 5; retries
++) {
268 rc
= ccw_device_clear(device
->cdev
, (long) request
);
272 request
->status
= TAPE_REQUEST_DONE
;
275 request
->status
= TAPE_REQUEST_CANCEL
;
276 schedule_work(&device
->tape_dnr
);
279 DBF_EXCEPTION(2, "device gone, retry\n");
282 DBF_EXCEPTION(2, "I/O error, retry\n");
293 * Add device into the sorted list, giving it the first
294 * available minor number.
297 tape_assign_minor(struct tape_device
*device
)
299 struct tape_device
*tmp
;
303 write_lock(&tape_device_lock
);
304 list_for_each_entry(tmp
, &tape_device_list
, node
) {
305 if (minor
< tmp
->first_minor
)
307 minor
+= TAPE_MINORS_PER_DEV
;
310 write_unlock(&tape_device_lock
);
313 device
->first_minor
= minor
;
314 list_add_tail(&device
->node
, &tmp
->node
);
315 write_unlock(&tape_device_lock
);
319 /* remove device from the list */
321 tape_remove_minor(struct tape_device
*device
)
323 write_lock(&tape_device_lock
);
324 list_del_init(&device
->node
);
325 device
->first_minor
= -1;
326 write_unlock(&tape_device_lock
);
330 * Set a device online.
332 * This function is called by the common I/O layer to move a device from the
333 * detected but offline into the online state.
334 * If we return an error (RC < 0) the device remains in the offline state. This
335 * can happen if the device is assigned somewhere else, for example.
338 tape_generic_online(struct tape_device
*device
,
339 struct tape_discipline
*discipline
)
343 DBF_LH(6, "tape_enable_device(%p, %p)\n", device
, discipline
);
345 if (device
->tape_state
!= TS_INIT
) {
346 DBF_LH(3, "Tapestate not INIT (%d)\n", device
->tape_state
);
350 /* Let the discipline have a go at the device. */
351 device
->discipline
= discipline
;
352 if (!try_module_get(discipline
->owner
)) {
353 PRINT_ERR("Cannot get module. Module gone.\n");
357 rc
= discipline
->setup_device(device
);
360 rc
= tape_assign_minor(device
);
364 rc
= tapechar_setup_device(device
);
367 rc
= tapeblock_setup_device(device
);
371 tape_state_set(device
, TS_UNUSED
);
373 DBF_LH(3, "(%08x): Drive set online\n", device
->cdev_id
);
378 tapechar_cleanup_device(device
);
380 device
->discipline
->cleanup_device(device
);
381 device
->discipline
= NULL
;
383 tape_remove_minor(device
);
385 module_put(discipline
->owner
);
390 tape_cleanup_device(struct tape_device
*device
)
392 tapeblock_cleanup_device(device
);
393 tapechar_cleanup_device(device
);
394 device
->discipline
->cleanup_device(device
);
395 module_put(device
->discipline
->owner
);
396 tape_remove_minor(device
);
397 tape_med_state_set(device
, MS_UNKNOWN
);
401 * Set device offline.
403 * Called by the common I/O layer if the drive should set offline on user
404 * request. We may prevent this by returning an error.
405 * Manual offline is only allowed while the drive is not in use.
408 tape_generic_offline(struct tape_device
*device
)
411 PRINT_ERR("tape_generic_offline: no such device\n");
415 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
416 device
->cdev_id
, device
);
418 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
419 switch (device
->tape_state
) {
422 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
425 tape_state_set(device
, TS_INIT
);
426 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
427 tape_cleanup_device(device
);
430 DBF_EVENT(3, "(%08x): Set offline failed "
433 PRINT_WARN("(%s): Set offline failed "
435 device
->cdev
->dev
.bus_id
);
436 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
440 DBF_LH(3, "(%08x): Drive set offline.\n", device
->cdev_id
);
445 * Allocate memory for a new device structure.
447 static struct tape_device
*
448 tape_alloc_device(void)
450 struct tape_device
*device
;
452 device
= kzalloc(sizeof(struct tape_device
), GFP_KERNEL
);
453 if (device
== NULL
) {
454 DBF_EXCEPTION(2, "ti:no mem\n");
455 PRINT_INFO ("can't allocate memory for "
456 "tape info structure\n");
457 return ERR_PTR(-ENOMEM
);
459 device
->modeset_byte
= kmalloc(1, GFP_KERNEL
| GFP_DMA
);
460 if (device
->modeset_byte
== NULL
) {
461 DBF_EXCEPTION(2, "ti:no mem\n");
462 PRINT_INFO("can't allocate memory for modeset byte\n");
464 return ERR_PTR(-ENOMEM
);
466 INIT_LIST_HEAD(&device
->req_queue
);
467 INIT_LIST_HEAD(&device
->node
);
468 init_waitqueue_head(&device
->state_change_wq
);
469 device
->tape_state
= TS_INIT
;
470 device
->medium_state
= MS_UNKNOWN
;
471 *device
->modeset_byte
= 0;
472 device
->first_minor
= -1;
473 atomic_set(&device
->ref_count
, 1);
474 INIT_WORK(&device
->tape_dnr
, tape_delayed_next_request
, device
);
480 * Get a reference to an existing device structure. This will automatically
481 * increment the reference count.
484 tape_get_device_reference(struct tape_device
*device
)
486 DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device
,
487 atomic_inc_return(&device
->ref_count
));
493 * Decrease the reference counter of a devices structure. If the
494 * reference counter reaches zero free the device structure.
495 * The function returns a NULL pointer to be used by the caller
496 * for clearing reference pointers.
499 tape_put_device(struct tape_device
*device
)
503 remain
= atomic_dec_return(&device
->ref_count
);
505 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device
, remain
);
508 DBF_EVENT(4, "put device without reference\n");
509 PRINT_ERR("put device without reference\n");
511 DBF_EVENT(4, "tape_free_device(%p)\n", device
);
512 kfree(device
->modeset_byte
);
521 * Find tape device by a device index.
524 tape_get_device(int devindex
)
526 struct tape_device
*device
, *tmp
;
528 device
= ERR_PTR(-ENODEV
);
529 read_lock(&tape_device_lock
);
530 list_for_each_entry(tmp
, &tape_device_list
, node
) {
531 if (tmp
->first_minor
/ TAPE_MINORS_PER_DEV
== devindex
) {
532 device
= tape_get_device_reference(tmp
);
536 read_unlock(&tape_device_lock
);
541 * Driverfs tape probe function.
544 tape_generic_probe(struct ccw_device
*cdev
)
546 struct tape_device
*device
;
548 device
= tape_alloc_device();
551 PRINT_INFO("tape device %s found\n", cdev
->dev
.bus_id
);
552 cdev
->dev
.driver_data
= device
;
554 device
->cdev_id
= busid_to_int(cdev
->dev
.bus_id
);
555 cdev
->handler
= __tape_do_irq
;
557 ccw_device_set_options(cdev
, CCWDEV_DO_PATHGROUP
);
558 sysfs_create_group(&cdev
->dev
.kobj
, &tape_attr_group
);
564 __tape_discard_requests(struct tape_device
*device
)
566 struct tape_request
* request
;
567 struct list_head
* l
, *n
;
569 list_for_each_safe(l
, n
, &device
->req_queue
) {
570 request
= list_entry(l
, struct tape_request
, list
);
571 if (request
->status
== TAPE_REQUEST_IN_IO
)
572 request
->status
= TAPE_REQUEST_DONE
;
573 list_del(&request
->list
);
575 /* Decrease ref_count for removed request. */
576 request
->device
= tape_put_device(device
);
578 if (request
->callback
!= NULL
)
579 request
->callback(request
, request
->callback_data
);
584 * Driverfs tape remove function.
586 * This function is called whenever the common I/O layer detects the device
587 * gone. This can happen at any time and we cannot refuse.
590 tape_generic_remove(struct ccw_device
*cdev
)
592 struct tape_device
* device
;
594 device
= cdev
->dev
.driver_data
;
596 PRINT_ERR("No device pointer in tape_generic_remove!\n");
599 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device
->cdev_id
, cdev
);
601 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
602 switch (device
->tape_state
) {
604 tape_state_set(device
, TS_NOT_OPER
);
609 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
613 * Need only to release the device.
615 tape_state_set(device
, TS_NOT_OPER
);
616 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
617 tape_cleanup_device(device
);
621 * There may be requests on the queue. We will not get
622 * an interrupt for a request that was running. So we
623 * just post them all as I/O errors.
625 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
627 PRINT_WARN("(%s): Drive in use vanished - "
629 device
->cdev
->dev
.bus_id
);
630 PRINT_WARN("State was %i\n", device
->tape_state
);
631 tape_state_set(device
, TS_NOT_OPER
);
632 __tape_discard_requests(device
);
633 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
634 tape_cleanup_device(device
);
637 if (cdev
->dev
.driver_data
!= NULL
) {
638 sysfs_remove_group(&cdev
->dev
.kobj
, &tape_attr_group
);
639 cdev
->dev
.driver_data
= tape_put_device(cdev
->dev
.driver_data
);
644 * Allocate a new tape ccw request
646 struct tape_request
*
647 tape_alloc_request(int cplength
, int datasize
)
649 struct tape_request
*request
;
651 if (datasize
> PAGE_SIZE
|| (cplength
*sizeof(struct ccw1
)) > PAGE_SIZE
)
654 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength
, datasize
);
656 request
= kzalloc(sizeof(struct tape_request
), GFP_KERNEL
);
657 if (request
== NULL
) {
658 DBF_EXCEPTION(1, "cqra nomem\n");
659 return ERR_PTR(-ENOMEM
);
661 /* allocate channel program */
663 request
->cpaddr
= kcalloc(cplength
, sizeof(struct ccw1
),
664 GFP_ATOMIC
| GFP_DMA
);
665 if (request
->cpaddr
== NULL
) {
666 DBF_EXCEPTION(1, "cqra nomem\n");
668 return ERR_PTR(-ENOMEM
);
671 /* alloc small kernel buffer */
673 request
->cpdata
= kzalloc(datasize
, GFP_KERNEL
| GFP_DMA
);
674 if (request
->cpdata
== NULL
) {
675 DBF_EXCEPTION(1, "cqra nomem\n");
676 kfree(request
->cpaddr
);
678 return ERR_PTR(-ENOMEM
);
681 DBF_LH(6, "New request %p(%p/%p)\n", request
, request
->cpaddr
,
688 * Free tape ccw request
691 tape_free_request (struct tape_request
* request
)
693 DBF_LH(6, "Free request %p\n", request
);
695 if (request
->device
!= NULL
) {
696 request
->device
= tape_put_device(request
->device
);
698 kfree(request
->cpdata
);
699 kfree(request
->cpaddr
);
704 __tape_start_io(struct tape_device
*device
, struct tape_request
*request
)
708 #ifdef CONFIG_S390_TAPE_BLOCK
709 if (request
->op
== TO_BLOCK
)
710 device
->discipline
->check_locate(device
, request
);
712 rc
= ccw_device_start(
715 (unsigned long) request
,
720 request
->status
= TAPE_REQUEST_IN_IO
;
721 } else if (rc
== -EBUSY
) {
722 /* The common I/O subsystem is currently busy. Retry later. */
723 request
->status
= TAPE_REQUEST_QUEUED
;
724 schedule_work(&device
->tape_dnr
);
727 /* Start failed. Remove request and indicate failure. */
728 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc
);
734 __tape_start_next_request(struct tape_device
*device
)
736 struct list_head
*l
, *n
;
737 struct tape_request
*request
;
740 DBF_LH(6, "__tape_start_next_request(%p)\n", device
);
742 * Try to start each request on request queue until one is
743 * started successful.
745 list_for_each_safe(l
, n
, &device
->req_queue
) {
746 request
= list_entry(l
, struct tape_request
, list
);
749 * Avoid race condition if bottom-half was triggered more than
752 if (request
->status
== TAPE_REQUEST_IN_IO
)
755 * Request has already been stopped. We have to wait until
756 * the request is removed from the queue in the interrupt
759 if (request
->status
== TAPE_REQUEST_DONE
)
763 * We wanted to cancel the request but the common I/O layer
764 * was busy at that time. This can only happen if this
765 * function is called by delayed_next_request.
766 * Otherwise we start the next request on the queue.
768 if (request
->status
== TAPE_REQUEST_CANCEL
) {
769 rc
= __tape_cancel_io(device
, request
);
771 rc
= __tape_start_io(device
, request
);
776 /* Set ending status. */
778 request
->status
= TAPE_REQUEST_DONE
;
780 /* Remove from request queue. */
781 list_del(&request
->list
);
784 if (request
->callback
!= NULL
)
785 request
->callback(request
, request
->callback_data
);
790 tape_delayed_next_request(void *data
)
792 struct tape_device
* device
;
794 device
= (struct tape_device
*) data
;
795 DBF_LH(6, "tape_delayed_next_request(%p)\n", device
);
796 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
797 __tape_start_next_request(device
);
798 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
803 struct tape_device
* device
,
804 struct tape_request
* request
,
807 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device
, request
, rc
);
810 request
->status
= TAPE_REQUEST_DONE
;
812 /* Remove from request queue. */
813 list_del(&request
->list
);
816 if (request
->callback
!= NULL
)
817 request
->callback(request
, request
->callback_data
);
820 /* Start next request. */
821 if (!list_empty(&device
->req_queue
))
822 __tape_start_next_request(device
);
826 * Write sense data to console/dbf
829 tape_dump_sense(struct tape_device
* device
, struct tape_request
*request
,
834 PRINT_INFO("-------------------------------------------------\n");
835 PRINT_INFO("DSTAT : %02x CSTAT: %02x CPA: %04x\n",
836 irb
->scsw
.dstat
, irb
->scsw
.cstat
, irb
->scsw
.cpa
);
837 PRINT_INFO("DEVICE: %s\n", device
->cdev
->dev
.bus_id
);
839 PRINT_INFO("OP : %s\n", tape_op_verbose
[request
->op
]);
841 sptr
= (unsigned int *) irb
->ecw
;
842 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
843 sptr
[0], sptr
[1], sptr
[2], sptr
[3]);
844 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
845 sptr
[4], sptr
[5], sptr
[6], sptr
[7]);
846 PRINT_INFO("--------------------------------------------------\n");
850 * Write sense data to dbf
853 tape_dump_sense_dbf(struct tape_device
*device
, struct tape_request
*request
,
860 op
= tape_op_verbose
[request
->op
];
863 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
864 irb
->scsw
.dstat
,irb
->scsw
.cstat
);
865 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device
->cdev_id
, op
);
866 sptr
= (unsigned int *) irb
->ecw
;
867 DBF_EVENT(3, "%08x %08x\n", sptr
[0], sptr
[1]);
868 DBF_EVENT(3, "%08x %08x\n", sptr
[2], sptr
[3]);
869 DBF_EVENT(3, "%08x %08x\n", sptr
[4], sptr
[5]);
870 DBF_EVENT(3, "%08x %08x\n", sptr
[6], sptr
[7]);
874 * I/O helper function. Adds the request to the request queue
875 * and starts it if the tape is idle. Has to be called with
876 * the device lock held.
879 __tape_start_request(struct tape_device
*device
, struct tape_request
*request
)
883 switch (request
->op
) {
888 if (device
->tape_state
== TS_INIT
)
890 if (device
->tape_state
== TS_UNUSED
)
893 if (device
->tape_state
== TS_BLKUSE
)
895 if (device
->tape_state
!= TS_IN_USE
)
899 /* Increase use count of device for the added request. */
900 request
->device
= tape_get_device_reference(device
);
902 if (list_empty(&device
->req_queue
)) {
903 /* No other requests are on the queue. Start this one. */
904 rc
= __tape_start_io(device
, request
);
908 DBF_LH(5, "Request %p added for execution.\n", request
);
909 list_add(&request
->list
, &device
->req_queue
);
911 DBF_LH(5, "Request %p add to queue.\n", request
);
912 request
->status
= TAPE_REQUEST_QUEUED
;
913 list_add_tail(&request
->list
, &device
->req_queue
);
919 * Add the request to the request queue, try to start it if the
920 * tape is idle. Return without waiting for end of i/o.
923 tape_do_io_async(struct tape_device
*device
, struct tape_request
*request
)
927 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device
, request
);
929 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
930 /* Add request to request queue and try to start it. */
931 rc
= __tape_start_request(device
, request
);
932 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
937 * tape_do_io/__tape_wake_up
938 * Add the request to the request queue, try to start it if the
939 * tape is idle and wait uninterruptible for its completion.
942 __tape_wake_up(struct tape_request
*request
, void *data
)
944 request
->callback
= NULL
;
945 wake_up((wait_queue_head_t
*) data
);
949 tape_do_io(struct tape_device
*device
, struct tape_request
*request
)
951 wait_queue_head_t wq
;
954 init_waitqueue_head(&wq
);
955 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
957 request
->callback
= __tape_wake_up
;
958 request
->callback_data
= &wq
;
959 /* Add request to request queue and try to start it. */
960 rc
= __tape_start_request(device
, request
);
961 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
964 /* Request added to the queue. Wait for its completion. */
965 wait_event(wq
, (request
->callback
== NULL
));
966 /* Get rc from request */
971 * tape_do_io_interruptible/__tape_wake_up_interruptible
972 * Add the request to the request queue, try to start it if the
973 * tape is idle and wait uninterruptible for its completion.
976 __tape_wake_up_interruptible(struct tape_request
*request
, void *data
)
978 request
->callback
= NULL
;
979 wake_up_interruptible((wait_queue_head_t
*) data
);
983 tape_do_io_interruptible(struct tape_device
*device
,
984 struct tape_request
*request
)
986 wait_queue_head_t wq
;
989 init_waitqueue_head(&wq
);
990 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
992 request
->callback
= __tape_wake_up_interruptible
;
993 request
->callback_data
= &wq
;
994 rc
= __tape_start_request(device
, request
);
995 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
998 /* Request added to the queue. Wait for its completion. */
999 rc
= wait_event_interruptible(wq
, (request
->callback
== NULL
));
1000 if (rc
!= -ERESTARTSYS
)
1001 /* Request finished normally. */
1004 /* Interrupted by a signal. We have to stop the current request. */
1005 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1006 rc
= __tape_cancel_io(device
, request
);
1007 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1009 /* Wait for the interrupt that acknowledges the halt. */
1011 rc
= wait_event_interruptible(
1013 (request
->callback
== NULL
)
1015 } while (rc
== -ERESTARTSYS
);
1017 DBF_EVENT(3, "IO stopped on %08x\n", device
->cdev_id
);
1027 tape_cancel_io(struct tape_device
*device
, struct tape_request
*request
)
1031 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
1032 rc
= __tape_cancel_io(device
, request
);
1033 spin_unlock_irq(get_ccwdev_lock(device
->cdev
));
1038 * Tape interrupt routine, called from the ccw_device layer
1041 __tape_do_irq (struct ccw_device
*cdev
, unsigned long intparm
, struct irb
*irb
)
1043 struct tape_device
*device
;
1044 struct tape_request
*request
;
1047 device
= (struct tape_device
*) cdev
->dev
.driver_data
;
1048 if (device
== NULL
) {
1049 PRINT_ERR("could not get device structure for %s "
1050 "in interrupt\n", cdev
->dev
.bus_id
);
1053 request
= (struct tape_request
*) intparm
;
1055 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device
, request
);
1057 /* On special conditions irb is an error pointer */
1059 /* FIXME: What to do with the request? */
1060 switch (PTR_ERR(irb
)) {
1062 PRINT_WARN("(%s): Request timed out\n",
1065 __tape_end_request(device
, request
, -EIO
);
1068 PRINT_ERR("(%s): Unexpected i/o error %li\n",
1076 * If the condition code is not zero and the start function bit is
1077 * still set, this is an deferred error and the last start I/O did
1078 * not succeed. At this point the condition that caused the deferred
1079 * error might still apply. So we just schedule the request to be
1082 if (irb
->scsw
.cc
!= 0 && (irb
->scsw
.fctl
& SCSW_FCTL_START_FUNC
) &&
1083 (request
->status
== TAPE_REQUEST_IN_IO
)) {
1084 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1085 device
->cdev_id
, irb
->scsw
.cc
, irb
->scsw
.fctl
);
1086 request
->status
= TAPE_REQUEST_QUEUED
;
1087 schedule_delayed_work(&device
->tape_dnr
, HZ
);
1091 /* May be an unsolicited irq */
1093 request
->rescnt
= irb
->scsw
.count
;
1095 if (irb
->scsw
.dstat
!= 0x0c) {
1096 /* Set the 'ONLINE' flag depending on sense byte 1 */
1097 if(*(((__u8
*) irb
->ecw
) + 1) & SENSE_DRIVE_ONLINE
)
1098 device
->tape_generic_status
|= GMT_ONLINE(~0);
1100 device
->tape_generic_status
&= ~GMT_ONLINE(~0);
1103 * Any request that does not come back with channel end
1104 * and device end is unusual. Log the sense data.
1106 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1107 tape_dump_sense_dbf(device
, request
, irb
);
1109 /* Upon normal completion the device _is_ online */
1110 device
->tape_generic_status
|= GMT_ONLINE(~0);
1112 if (device
->tape_state
== TS_NOT_OPER
) {
1113 DBF_EVENT(6, "tape:device is not operational\n");
1118 * Request that were canceled still come back with an interrupt.
1119 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1121 if(request
!= NULL
&& request
->status
== TAPE_REQUEST_DONE
) {
1122 __tape_end_request(device
, request
, -EIO
);
1126 rc
= device
->discipline
->irq(device
, request
, irb
);
1128 * rc < 0 : request finished unsuccessfully.
1129 * rc == TAPE_IO_SUCCESS: request finished successfully.
1130 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1131 * rc == TAPE_IO_RETRY: request finished but needs another go.
1132 * rc == TAPE_IO_STOP: request needs to get terminated.
1135 case TAPE_IO_SUCCESS
:
1136 /* Upon normal completion the device _is_ online */
1137 device
->tape_generic_status
|= GMT_ONLINE(~0);
1138 __tape_end_request(device
, request
, rc
);
1140 case TAPE_IO_PENDING
:
1143 rc
= __tape_start_io(device
, request
);
1145 __tape_end_request(device
, request
, rc
);
1148 rc
= __tape_cancel_io(device
, request
);
1150 __tape_end_request(device
, request
, rc
);
1154 DBF_EVENT(6, "xunknownrc\n");
1155 PRINT_ERR("Invalid return code from discipline "
1156 "interrupt function.\n");
1157 __tape_end_request(device
, request
, -EIO
);
1159 __tape_end_request(device
, request
, rc
);
1166 * Tape device open function used by tape_char & tape_block frontends.
1169 tape_open(struct tape_device
*device
)
1173 spin_lock(get_ccwdev_lock(device
->cdev
));
1174 if (device
->tape_state
== TS_NOT_OPER
) {
1175 DBF_EVENT(6, "TAPE:nodev\n");
1177 } else if (device
->tape_state
== TS_IN_USE
) {
1178 DBF_EVENT(6, "TAPE:dbusy\n");
1180 } else if (device
->tape_state
== TS_BLKUSE
) {
1181 DBF_EVENT(6, "TAPE:dbusy\n");
1183 } else if (device
->discipline
!= NULL
&&
1184 !try_module_get(device
->discipline
->owner
)) {
1185 DBF_EVENT(6, "TAPE:nodisc\n");
1188 tape_state_set(device
, TS_IN_USE
);
1191 spin_unlock(get_ccwdev_lock(device
->cdev
));
1196 * Tape device release function used by tape_char & tape_block frontends.
1199 tape_release(struct tape_device
*device
)
1201 spin_lock(get_ccwdev_lock(device
->cdev
));
1202 if (device
->tape_state
== TS_IN_USE
)
1203 tape_state_set(device
, TS_UNUSED
);
1204 module_put(device
->discipline
->owner
);
1205 spin_unlock(get_ccwdev_lock(device
->cdev
));
1210 * Execute a magnetic tape command a number of times.
1213 tape_mtop(struct tape_device
*device
, int mt_op
, int mt_count
)
1218 DBF_EVENT(6, "TAPE:mtio\n");
1219 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op
);
1220 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count
);
1222 if (mt_op
< 0 || mt_op
>= TAPE_NR_MTOPS
)
1224 fn
= device
->discipline
->mtop_array
[mt_op
];
1228 /* We assume that the backends can handle count up to 500. */
1229 if (mt_op
== MTBSR
|| mt_op
== MTFSR
|| mt_op
== MTFSF
||
1230 mt_op
== MTBSF
|| mt_op
== MTFSFM
|| mt_op
== MTBSFM
) {
1232 for (; mt_count
> 500; mt_count
-= 500)
1233 if ((rc
= fn(device
, 500)) != 0)
1236 rc
= fn(device
, mt_count
);
1238 rc
= fn(device
, mt_count
);
1244 * Tape init function.
1249 TAPE_DBF_AREA
= debug_register ( "tape", 2, 2, 4*sizeof(long));
1250 debug_register_view(TAPE_DBF_AREA
, &debug_sprintf_view
);
1251 #ifdef DBF_LIKE_HELL
1252 debug_set_level(TAPE_DBF_AREA
, 6);
1254 DBF_EVENT(3, "tape init\n");
1262 * Tape exit function.
1267 DBF_EVENT(6, "tape exit\n");
1269 /* Get rid of the frontends */
1272 tape_proc_cleanup();
1273 debug_unregister (TAPE_DBF_AREA
);
1276 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1277 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1278 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1279 MODULE_LICENSE("GPL");
1281 module_init(tape_init
);
1282 module_exit(tape_exit
);
1284 EXPORT_SYMBOL(tape_generic_remove
);
1285 EXPORT_SYMBOL(tape_generic_probe
);
1286 EXPORT_SYMBOL(tape_generic_online
);
1287 EXPORT_SYMBOL(tape_generic_offline
);
1288 EXPORT_SYMBOL(tape_put_device
);
1289 EXPORT_SYMBOL(tape_get_device_reference
);
1290 EXPORT_SYMBOL(tape_state_verbose
);
1291 EXPORT_SYMBOL(tape_op_verbose
);
1292 EXPORT_SYMBOL(tape_state_set
);
1293 EXPORT_SYMBOL(tape_med_state_set
);
1294 EXPORT_SYMBOL(tape_alloc_request
);
1295 EXPORT_SYMBOL(tape_free_request
);
1296 EXPORT_SYMBOL(tape_dump_sense
);
1297 EXPORT_SYMBOL(tape_dump_sense_dbf
);
1298 EXPORT_SYMBOL(tape_do_io
);
1299 EXPORT_SYMBOL(tape_do_io_async
);
1300 EXPORT_SYMBOL(tape_do_io_interruptible
);
1301 EXPORT_SYMBOL(tape_cancel_io
);
1302 EXPORT_SYMBOL(tape_mtop
);