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 #define KMSG_COMPONENT "tape"
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 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 LIST_HEAD(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",
79 static int devid_to_int(struct ccw_dev_id *dev_id)
81 return dev_id->devno + (dev_id->ssid << 16);
85 * Some channel attached tape specific attributes.
87 * FIXME: In the future the first_minor and blocksize attribute should be
88 * replaced by a link to the cdev tree.
91 tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf)
93 struct tape_device *tdev;
95 tdev = (struct tape_device *) dev->driver_data;
96 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state);
100 DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
103 tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf)
105 struct tape_device *tdev;
107 tdev = (struct tape_device *) dev->driver_data;
108 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor);
112 DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
115 tape_state_show(struct device *dev, struct device_attribute *attr, char *buf)
117 struct tape_device *tdev;
119 tdev = (struct tape_device *) dev->driver_data;
120 return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ?
121 "OFFLINE" : tape_state_verbose[tdev->tape_state]);
125 DEVICE_ATTR(state, 0444, tape_state_show, NULL);
128 tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf)
130 struct tape_device *tdev;
133 tdev = (struct tape_device *) dev->driver_data;
134 if (tdev->first_minor < 0)
135 return scnprintf(buf, PAGE_SIZE, "N/A\n");
137 spin_lock_irq(get_ccwdev_lock(tdev->cdev));
138 if (list_empty(&tdev->req_queue))
139 rc = scnprintf(buf, PAGE_SIZE, "---\n");
141 struct tape_request *req;
143 req = list_entry(tdev->req_queue.next, struct tape_request,
145 rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
147 spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
152 DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
155 tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf)
157 struct tape_device *tdev;
159 tdev = (struct tape_device *) dev->driver_data;
161 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size);
165 DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);
167 static struct attribute *tape_attrs[] = {
168 &dev_attr_medium_state.attr,
169 &dev_attr_first_minor.attr,
170 &dev_attr_state.attr,
171 &dev_attr_operation.attr,
172 &dev_attr_blocksize.attr,
176 static struct attribute_group tape_attr_group = {
181 * Tape state functions
184 tape_state_set(struct tape_device *device, enum tape_state newstate)
188 if (device->tape_state == TS_NOT_OPER) {
189 DBF_EVENT(3, "ts_set err: not oper\n");
192 DBF_EVENT(4, "ts. dev: %x\n", device->first_minor);
193 DBF_EVENT(4, "old ts:\t\n");
194 if (device->tape_state < TS_SIZE && device->tape_state >=0 )
195 str = tape_state_verbose[device->tape_state];
198 DBF_EVENT(4, "%s\n", str);
199 DBF_EVENT(4, "new ts:\t\n");
200 if (newstate < TS_SIZE && newstate >= 0)
201 str = tape_state_verbose[newstate];
204 DBF_EVENT(4, "%s\n", str);
205 device->tape_state = newstate;
206 wake_up(&device->state_change_wq);
210 tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
212 if (device->medium_state == newstate)
216 device->tape_generic_status |= GMT_DR_OPEN(~0);
217 dev_info(&device->cdev->dev, "The tape cartridge has been "
218 "successfully unloaded\n");
221 device->tape_generic_status &= ~GMT_DR_OPEN(~0);
222 dev_info(&device->cdev->dev, "A tape cartridge has been "
229 device->medium_state = newstate;
230 wake_up(&device->state_change_wq);
234 * Stop running ccw. Has to be called with the device lock held.
237 __tape_cancel_io(struct tape_device *device, struct tape_request *request)
242 /* Check if interrupt has already been processed */
243 if (request->callback == NULL)
247 for (retries = 0; retries < 5; retries++) {
248 rc = ccw_device_clear(device->cdev, (long) request);
252 request->status = TAPE_REQUEST_DONE;
255 request->status = TAPE_REQUEST_CANCEL;
256 schedule_delayed_work(&device->tape_dnr, 0);
259 DBF_EXCEPTION(2, "device gone, retry\n");
262 DBF_EXCEPTION(2, "I/O error, retry\n");
273 * Add device into the sorted list, giving it the first
274 * available minor number.
277 tape_assign_minor(struct tape_device *device)
279 struct tape_device *tmp;
283 write_lock(&tape_device_lock);
284 list_for_each_entry(tmp, &tape_device_list, node) {
285 if (minor < tmp->first_minor)
287 minor += TAPE_MINORS_PER_DEV;
290 write_unlock(&tape_device_lock);
293 device->first_minor = minor;
294 list_add_tail(&device->node, &tmp->node);
295 write_unlock(&tape_device_lock);
299 /* remove device from the list */
301 tape_remove_minor(struct tape_device *device)
303 write_lock(&tape_device_lock);
304 list_del_init(&device->node);
305 device->first_minor = -1;
306 write_unlock(&tape_device_lock);
310 * Set a device online.
312 * This function is called by the common I/O layer to move a device from the
313 * detected but offline into the online state.
314 * If we return an error (RC < 0) the device remains in the offline state. This
315 * can happen if the device is assigned somewhere else, for example.
318 tape_generic_online(struct tape_device *device,
319 struct tape_discipline *discipline)
323 DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
325 if (device->tape_state != TS_INIT) {
326 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
330 init_timer(&device->lb_timeout);
331 device->lb_timeout.function = tape_long_busy_timeout;
333 /* Let the discipline have a go at the device. */
334 device->discipline = discipline;
335 if (!try_module_get(discipline->owner)) {
339 rc = discipline->setup_device(device);
342 rc = tape_assign_minor(device);
346 rc = tapechar_setup_device(device);
349 rc = tapeblock_setup_device(device);
353 tape_state_set(device, TS_UNUSED);
355 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
360 tapechar_cleanup_device(device);
362 device->discipline->cleanup_device(device);
363 device->discipline = NULL;
365 tape_remove_minor(device);
367 module_put(discipline->owner);
372 tape_cleanup_device(struct tape_device *device)
374 tapeblock_cleanup_device(device);
375 tapechar_cleanup_device(device);
376 device->discipline->cleanup_device(device);
377 module_put(device->discipline->owner);
378 tape_remove_minor(device);
379 tape_med_state_set(device, MS_UNKNOWN);
383 * Set device offline.
385 * Called by the common I/O layer if the drive should set offline on user
386 * request. We may prevent this by returning an error.
387 * Manual offline is only allowed while the drive is not in use.
390 tape_generic_offline(struct ccw_device *cdev)
392 struct tape_device *device;
394 device = cdev->dev.driver_data;
399 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
400 device->cdev_id, device);
402 spin_lock_irq(get_ccwdev_lock(device->cdev));
403 switch (device->tape_state) {
406 spin_unlock_irq(get_ccwdev_lock(device->cdev));
409 tape_state_set(device, TS_INIT);
410 spin_unlock_irq(get_ccwdev_lock(device->cdev));
411 tape_cleanup_device(device);
414 DBF_EVENT(3, "(%08x): Set offline failed "
417 spin_unlock_irq(get_ccwdev_lock(device->cdev));
421 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
426 * Allocate memory for a new device structure.
428 static struct tape_device *
429 tape_alloc_device(void)
431 struct tape_device *device;
433 device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
434 if (device == NULL) {
435 DBF_EXCEPTION(2, "ti:no mem\n");
436 return ERR_PTR(-ENOMEM);
438 device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
439 if (device->modeset_byte == NULL) {
440 DBF_EXCEPTION(2, "ti:no mem\n");
442 return ERR_PTR(-ENOMEM);
444 INIT_LIST_HEAD(&device->req_queue);
445 INIT_LIST_HEAD(&device->node);
446 init_waitqueue_head(&device->state_change_wq);
447 init_waitqueue_head(&device->wait_queue);
448 device->tape_state = TS_INIT;
449 device->medium_state = MS_UNKNOWN;
450 *device->modeset_byte = 0;
451 device->first_minor = -1;
452 atomic_set(&device->ref_count, 1);
453 INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request);
459 * Get a reference to an existing device structure. This will automatically
460 * increment the reference count.
463 tape_get_device_reference(struct tape_device *device)
465 DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device,
466 atomic_inc_return(&device->ref_count));
472 * Decrease the reference counter of a devices structure. If the
473 * reference counter reaches zero free the device structure.
474 * The function returns a NULL pointer to be used by the caller
475 * for clearing reference pointers.
478 tape_put_device(struct tape_device *device)
482 remain = atomic_dec_return(&device->ref_count);
484 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain);
487 DBF_EVENT(4, "put device without reference\n");
489 DBF_EVENT(4, "tape_free_device(%p)\n", device);
490 kfree(device->modeset_byte);
499 * Find tape device by a device index.
502 tape_get_device(int devindex)
504 struct tape_device *device, *tmp;
506 device = ERR_PTR(-ENODEV);
507 read_lock(&tape_device_lock);
508 list_for_each_entry(tmp, &tape_device_list, node) {
509 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
510 device = tape_get_device_reference(tmp);
514 read_unlock(&tape_device_lock);
519 * Driverfs tape probe function.
522 tape_generic_probe(struct ccw_device *cdev)
524 struct tape_device *device;
526 struct ccw_dev_id dev_id;
528 device = tape_alloc_device();
531 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
532 ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
534 tape_put_device(device);
537 cdev->dev.driver_data = device;
538 cdev->handler = __tape_do_irq;
540 ccw_device_get_id(cdev, &dev_id);
541 device->cdev_id = devid_to_int(&dev_id);
546 __tape_discard_requests(struct tape_device *device)
548 struct tape_request * request;
549 struct list_head * l, *n;
551 list_for_each_safe(l, n, &device->req_queue) {
552 request = list_entry(l, struct tape_request, list);
553 if (request->status == TAPE_REQUEST_IN_IO)
554 request->status = TAPE_REQUEST_DONE;
555 list_del(&request->list);
557 /* Decrease ref_count for removed request. */
558 request->device = tape_put_device(device);
560 if (request->callback != NULL)
561 request->callback(request, request->callback_data);
566 * Driverfs tape remove function.
568 * This function is called whenever the common I/O layer detects the device
569 * gone. This can happen at any time and we cannot refuse.
572 tape_generic_remove(struct ccw_device *cdev)
574 struct tape_device * device;
576 device = cdev->dev.driver_data;
580 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
582 spin_lock_irq(get_ccwdev_lock(device->cdev));
583 switch (device->tape_state) {
585 tape_state_set(device, TS_NOT_OPER);
590 spin_unlock_irq(get_ccwdev_lock(device->cdev));
594 * Need only to release the device.
596 tape_state_set(device, TS_NOT_OPER);
597 spin_unlock_irq(get_ccwdev_lock(device->cdev));
598 tape_cleanup_device(device);
602 * There may be requests on the queue. We will not get
603 * an interrupt for a request that was running. So we
604 * just post them all as I/O errors.
606 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
608 dev_warn(&device->cdev->dev, "A tape unit was detached"
610 tape_state_set(device, TS_NOT_OPER);
611 __tape_discard_requests(device);
612 spin_unlock_irq(get_ccwdev_lock(device->cdev));
613 tape_cleanup_device(device);
616 if (cdev->dev.driver_data != NULL) {
617 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
618 cdev->dev.driver_data = tape_put_device(cdev->dev.driver_data);
623 * Allocate a new tape ccw request
625 struct tape_request *
626 tape_alloc_request(int cplength, int datasize)
628 struct tape_request *request;
630 BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
632 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
634 request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
635 if (request == NULL) {
636 DBF_EXCEPTION(1, "cqra nomem\n");
637 return ERR_PTR(-ENOMEM);
639 /* allocate channel program */
641 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
642 GFP_ATOMIC | GFP_DMA);
643 if (request->cpaddr == NULL) {
644 DBF_EXCEPTION(1, "cqra nomem\n");
646 return ERR_PTR(-ENOMEM);
649 /* alloc small kernel buffer */
651 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
652 if (request->cpdata == NULL) {
653 DBF_EXCEPTION(1, "cqra nomem\n");
654 kfree(request->cpaddr);
656 return ERR_PTR(-ENOMEM);
659 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
666 * Free tape ccw request
669 tape_free_request (struct tape_request * request)
671 DBF_LH(6, "Free request %p\n", request);
673 if (request->device != NULL) {
674 request->device = tape_put_device(request->device);
676 kfree(request->cpdata);
677 kfree(request->cpaddr);
682 __tape_start_io(struct tape_device *device, struct tape_request *request)
686 #ifdef CONFIG_S390_TAPE_BLOCK
687 if (request->op == TO_BLOCK)
688 device->discipline->check_locate(device, request);
690 rc = ccw_device_start(
693 (unsigned long) request,
698 request->status = TAPE_REQUEST_IN_IO;
699 } else if (rc == -EBUSY) {
700 /* The common I/O subsystem is currently busy. Retry later. */
701 request->status = TAPE_REQUEST_QUEUED;
702 schedule_delayed_work(&device->tape_dnr, 0);
705 /* Start failed. Remove request and indicate failure. */
706 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
712 __tape_start_next_request(struct tape_device *device)
714 struct list_head *l, *n;
715 struct tape_request *request;
718 DBF_LH(6, "__tape_start_next_request(%p)\n", device);
720 * Try to start each request on request queue until one is
721 * started successful.
723 list_for_each_safe(l, n, &device->req_queue) {
724 request = list_entry(l, struct tape_request, list);
727 * Avoid race condition if bottom-half was triggered more than
730 if (request->status == TAPE_REQUEST_IN_IO)
733 * Request has already been stopped. We have to wait until
734 * the request is removed from the queue in the interrupt
737 if (request->status == TAPE_REQUEST_DONE)
741 * We wanted to cancel the request but the common I/O layer
742 * was busy at that time. This can only happen if this
743 * function is called by delayed_next_request.
744 * Otherwise we start the next request on the queue.
746 if (request->status == TAPE_REQUEST_CANCEL) {
747 rc = __tape_cancel_io(device, request);
749 rc = __tape_start_io(device, request);
754 /* Set ending status. */
756 request->status = TAPE_REQUEST_DONE;
758 /* Remove from request queue. */
759 list_del(&request->list);
762 if (request->callback != NULL)
763 request->callback(request, request->callback_data);
768 tape_delayed_next_request(struct work_struct *work)
770 struct tape_device *device =
771 container_of(work, struct tape_device, tape_dnr.work);
773 DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
774 spin_lock_irq(get_ccwdev_lock(device->cdev));
775 __tape_start_next_request(device);
776 spin_unlock_irq(get_ccwdev_lock(device->cdev));
779 static void tape_long_busy_timeout(unsigned long data)
781 struct tape_request *request;
782 struct tape_device *device;
784 device = (struct tape_device *) data;
785 spin_lock_irq(get_ccwdev_lock(device->cdev));
786 request = list_entry(device->req_queue.next, struct tape_request, list);
787 BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY);
788 DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
789 __tape_start_next_request(device);
790 device->lb_timeout.data = (unsigned long) tape_put_device(device);
791 spin_unlock_irq(get_ccwdev_lock(device->cdev));
796 struct tape_device * device,
797 struct tape_request * request,
800 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
803 request->status = TAPE_REQUEST_DONE;
805 /* Remove from request queue. */
806 list_del(&request->list);
809 if (request->callback != NULL)
810 request->callback(request, request->callback_data);
813 /* Start next request. */
814 if (!list_empty(&device->req_queue))
815 __tape_start_next_request(device);
819 * Write sense data to dbf
822 tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
829 op = tape_op_verbose[request->op];
832 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
833 irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
834 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
835 sptr = (unsigned int *) irb->ecw;
836 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
837 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
838 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
839 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
843 * I/O helper function. Adds the request to the request queue
844 * and starts it if the tape is idle. Has to be called with
845 * the device lock held.
848 __tape_start_request(struct tape_device *device, struct tape_request *request)
852 switch (request->op) {
858 if (device->tape_state == TS_INIT)
860 if (device->tape_state == TS_UNUSED)
863 if (device->tape_state == TS_BLKUSE)
865 if (device->tape_state != TS_IN_USE)
869 /* Increase use count of device for the added request. */
870 request->device = tape_get_device_reference(device);
872 if (list_empty(&device->req_queue)) {
873 /* No other requests are on the queue. Start this one. */
874 rc = __tape_start_io(device, request);
878 DBF_LH(5, "Request %p added for execution.\n", request);
879 list_add(&request->list, &device->req_queue);
881 DBF_LH(5, "Request %p add to queue.\n", request);
882 request->status = TAPE_REQUEST_QUEUED;
883 list_add_tail(&request->list, &device->req_queue);
889 * Add the request to the request queue, try to start it if the
890 * tape is idle. Return without waiting for end of i/o.
893 tape_do_io_async(struct tape_device *device, struct tape_request *request)
897 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
899 spin_lock_irq(get_ccwdev_lock(device->cdev));
900 /* Add request to request queue and try to start it. */
901 rc = __tape_start_request(device, request);
902 spin_unlock_irq(get_ccwdev_lock(device->cdev));
907 * tape_do_io/__tape_wake_up
908 * Add the request to the request queue, try to start it if the
909 * tape is idle and wait uninterruptible for its completion.
912 __tape_wake_up(struct tape_request *request, void *data)
914 request->callback = NULL;
915 wake_up((wait_queue_head_t *) data);
919 tape_do_io(struct tape_device *device, struct tape_request *request)
923 spin_lock_irq(get_ccwdev_lock(device->cdev));
925 request->callback = __tape_wake_up;
926 request->callback_data = &device->wait_queue;
927 /* Add request to request queue and try to start it. */
928 rc = __tape_start_request(device, request);
929 spin_unlock_irq(get_ccwdev_lock(device->cdev));
932 /* Request added to the queue. Wait for its completion. */
933 wait_event(device->wait_queue, (request->callback == NULL));
934 /* Get rc from request */
939 * tape_do_io_interruptible/__tape_wake_up_interruptible
940 * Add the request to the request queue, try to start it if the
941 * tape is idle and wait uninterruptible for its completion.
944 __tape_wake_up_interruptible(struct tape_request *request, void *data)
946 request->callback = NULL;
947 wake_up_interruptible((wait_queue_head_t *) data);
951 tape_do_io_interruptible(struct tape_device *device,
952 struct tape_request *request)
956 spin_lock_irq(get_ccwdev_lock(device->cdev));
958 request->callback = __tape_wake_up_interruptible;
959 request->callback_data = &device->wait_queue;
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 rc = wait_event_interruptible(device->wait_queue,
966 (request->callback == NULL));
967 if (rc != -ERESTARTSYS)
968 /* Request finished normally. */
971 /* Interrupted by a signal. We have to stop the current request. */
972 spin_lock_irq(get_ccwdev_lock(device->cdev));
973 rc = __tape_cancel_io(device, request);
974 spin_unlock_irq(get_ccwdev_lock(device->cdev));
976 /* Wait for the interrupt that acknowledges the halt. */
978 rc = wait_event_interruptible(
980 (request->callback == NULL)
982 } while (rc == -ERESTARTSYS);
984 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
994 tape_cancel_io(struct tape_device *device, struct tape_request *request)
998 spin_lock_irq(get_ccwdev_lock(device->cdev));
999 rc = __tape_cancel_io(device, request);
1000 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1005 * Tape interrupt routine, called from the ccw_device layer
1008 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1010 struct tape_device *device;
1011 struct tape_request *request;
1014 device = (struct tape_device *) cdev->dev.driver_data;
1015 if (device == NULL) {
1018 request = (struct tape_request *) intparm;
1020 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1022 /* On special conditions irb is an error pointer */
1024 /* FIXME: What to do with the request? */
1025 switch (PTR_ERR(irb)) {
1027 DBF_LH(1, "(%s): Request timed out\n",
1028 dev_name(&cdev->dev));
1030 __tape_end_request(device, request, -EIO);
1033 DBF_LH(1, "(%s): Unexpected i/o error %li\n",
1034 dev_name(&cdev->dev),
1041 * If the condition code is not zero and the start function bit is
1042 * still set, this is an deferred error and the last start I/O did
1043 * not succeed. At this point the condition that caused the deferred
1044 * error might still apply. So we just schedule the request to be
1047 if (irb->scsw.cmd.cc != 0 &&
1048 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
1049 (request->status == TAPE_REQUEST_IN_IO)) {
1050 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1051 device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
1052 request->status = TAPE_REQUEST_QUEUED;
1053 schedule_delayed_work(&device->tape_dnr, HZ);
1057 /* May be an unsolicited irq */
1059 request->rescnt = irb->scsw.cmd.count;
1060 else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
1061 !list_empty(&device->req_queue)) {
1062 /* Not Ready to Ready after long busy ? */
1063 struct tape_request *req;
1064 req = list_entry(device->req_queue.next,
1065 struct tape_request, list);
1066 if (req->status == TAPE_REQUEST_LONG_BUSY) {
1067 DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
1068 if (del_timer(&device->lb_timeout)) {
1069 device->lb_timeout.data = (unsigned long)
1070 tape_put_device(device);
1071 __tape_start_next_request(device);
1076 if (irb->scsw.cmd.dstat != 0x0c) {
1077 /* Set the 'ONLINE' flag depending on sense byte 1 */
1078 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1079 device->tape_generic_status |= GMT_ONLINE(~0);
1081 device->tape_generic_status &= ~GMT_ONLINE(~0);
1084 * Any request that does not come back with channel end
1085 * and device end is unusual. Log the sense data.
1087 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1088 tape_dump_sense_dbf(device, request, irb);
1090 /* Upon normal completion the device _is_ online */
1091 device->tape_generic_status |= GMT_ONLINE(~0);
1093 if (device->tape_state == TS_NOT_OPER) {
1094 DBF_EVENT(6, "tape:device is not operational\n");
1099 * Request that were canceled still come back with an interrupt.
1100 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1102 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
1103 __tape_end_request(device, request, -EIO);
1107 rc = device->discipline->irq(device, request, irb);
1109 * rc < 0 : request finished unsuccessfully.
1110 * rc == TAPE_IO_SUCCESS: request finished successfully.
1111 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1112 * rc == TAPE_IO_RETRY: request finished but needs another go.
1113 * rc == TAPE_IO_STOP: request needs to get terminated.
1116 case TAPE_IO_SUCCESS:
1117 /* Upon normal completion the device _is_ online */
1118 device->tape_generic_status |= GMT_ONLINE(~0);
1119 __tape_end_request(device, request, rc);
1121 case TAPE_IO_PENDING:
1123 case TAPE_IO_LONG_BUSY:
1124 device->lb_timeout.data =
1125 (unsigned long)tape_get_device_reference(device);
1126 device->lb_timeout.expires = jiffies +
1127 LONG_BUSY_TIMEOUT * HZ;
1128 DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
1129 add_timer(&device->lb_timeout);
1130 request->status = TAPE_REQUEST_LONG_BUSY;
1133 rc = __tape_start_io(device, request);
1135 __tape_end_request(device, request, rc);
1138 rc = __tape_cancel_io(device, request);
1140 __tape_end_request(device, request, rc);
1144 DBF_EVENT(6, "xunknownrc\n");
1145 __tape_end_request(device, request, -EIO);
1147 __tape_end_request(device, request, rc);
1154 * Tape device open function used by tape_char & tape_block frontends.
1157 tape_open(struct tape_device *device)
1161 spin_lock_irq(get_ccwdev_lock(device->cdev));
1162 if (device->tape_state == TS_NOT_OPER) {
1163 DBF_EVENT(6, "TAPE:nodev\n");
1165 } else if (device->tape_state == TS_IN_USE) {
1166 DBF_EVENT(6, "TAPE:dbusy\n");
1168 } else if (device->tape_state == TS_BLKUSE) {
1169 DBF_EVENT(6, "TAPE:dbusy\n");
1171 } else if (device->discipline != NULL &&
1172 !try_module_get(device->discipline->owner)) {
1173 DBF_EVENT(6, "TAPE:nodisc\n");
1176 tape_state_set(device, TS_IN_USE);
1179 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1184 * Tape device release function used by tape_char & tape_block frontends.
1187 tape_release(struct tape_device *device)
1189 spin_lock_irq(get_ccwdev_lock(device->cdev));
1190 if (device->tape_state == TS_IN_USE)
1191 tape_state_set(device, TS_UNUSED);
1192 module_put(device->discipline->owner);
1193 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1198 * Execute a magnetic tape command a number of times.
1201 tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1206 DBF_EVENT(6, "TAPE:mtio\n");
1207 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1208 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1210 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1212 fn = device->discipline->mtop_array[mt_op];
1216 /* We assume that the backends can handle count up to 500. */
1217 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1218 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1220 for (; mt_count > 500; mt_count -= 500)
1221 if ((rc = fn(device, 500)) != 0)
1224 rc = fn(device, mt_count);
1226 rc = fn(device, mt_count);
1232 * Tape init function.
1237 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
1238 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1239 #ifdef DBF_LIKE_HELL
1240 debug_set_level(TAPE_DBF_AREA, 6);
1242 DBF_EVENT(3, "tape init\n");
1250 * Tape exit function.
1255 DBF_EVENT(6, "tape exit\n");
1257 /* Get rid of the frontends */
1260 tape_proc_cleanup();
1261 debug_unregister (TAPE_DBF_AREA);
1264 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1265 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1266 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1267 MODULE_LICENSE("GPL");
1269 module_init(tape_init);
1270 module_exit(tape_exit);
1272 EXPORT_SYMBOL(tape_generic_remove);
1273 EXPORT_SYMBOL(tape_generic_probe);
1274 EXPORT_SYMBOL(tape_generic_online);
1275 EXPORT_SYMBOL(tape_generic_offline);
1276 EXPORT_SYMBOL(tape_put_device);
1277 EXPORT_SYMBOL(tape_get_device_reference);
1278 EXPORT_SYMBOL(tape_state_verbose);
1279 EXPORT_SYMBOL(tape_op_verbose);
1280 EXPORT_SYMBOL(tape_state_set);
1281 EXPORT_SYMBOL(tape_med_state_set);
1282 EXPORT_SYMBOL(tape_alloc_request);
1283 EXPORT_SYMBOL(tape_free_request);
1284 EXPORT_SYMBOL(tape_dump_sense_dbf);
1285 EXPORT_SYMBOL(tape_do_io);
1286 EXPORT_SYMBOL(tape_do_io_async);
1287 EXPORT_SYMBOL(tape_do_io_interruptible);
1288 EXPORT_SYMBOL(tape_cancel_io);
1289 EXPORT_SYMBOL(tape_mtop);