2 * drivers/s390/char/tape_core.c
3 * basic function of the tape device driver
5 * S390 and zSeries version
6 * Copyright (C) 2001,2002 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>
13 #include <linux/config.h>
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
26 #define PRINTK_HEADER "TAPE_CORE: "
28 static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *);
29 static void __tape_remove_request(struct tape_device *, struct tape_request *);
32 * One list to contain all tape devices of all disciplines, so
33 * we can assign the devices to minor numbers of the same major
34 * The list is protected by the rwlock
36 static struct list_head tape_device_list = LIST_HEAD_INIT(tape_device_list);
37 static rwlock_t tape_device_lock = RW_LOCK_UNLOCKED;
40 * Pointer to debug area.
42 debug_info_t *tape_dbf_area = NULL;
45 * Printable strings for tape enumerations.
47 const char *tape_state_verbose[TS_SIZE] =
49 [TS_UNUSED] = "UNUSED",
50 [TS_IN_USE] = "IN_USE",
51 [TS_BLKUSE] = "BLKUSE",
53 [TS_NOT_OPER] = "NOT_OP"
56 const char *tape_op_verbose[TO_SIZE] =
58 [TO_BLOCK] = "BLK", [TO_BSB] = "BSB",
59 [TO_BSF] = "BSF", [TO_DSE] = "DSE",
60 [TO_FSB] = "FSB", [TO_FSF] = "FSF",
61 [TO_LBL] = "LBL", [TO_NOP] = "NOP",
62 [TO_RBA] = "RBA", [TO_RBI] = "RBI",
63 [TO_RFO] = "RFO", [TO_REW] = "REW",
64 [TO_RUN] = "RUN", [TO_WRI] = "WRI",
65 [TO_WTM] = "WTM", [TO_MSEN] = "MSN",
66 [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF",
67 [TO_READ_ATTMSG] = "RAT",
68 [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS",
73 busid_to_int(char *bus_id)
79 for(s = bus_id, d = 0; *s != '\0' && *s != '.'; s++)
80 d = (d * 10) + (*s - '0');
82 for(s++, d = 0; *s != '\0' && *s != '.'; s++)
83 d = (d * 10) + (*s - '0');
86 for(s++; *s != '\0'; s++) {
87 if (*s >= '0' && *s <= '9') {
89 } else if (*s >= 'a' && *s <= 'f') {
101 * Some channel attached tape specific attributes.
103 * FIXME: In the future the first_minor and blocksize attribute should be
104 * replaced by a link to the cdev tree.
107 tape_medium_state_show(struct device *dev, char *buf)
109 struct tape_device *tdev;
111 tdev = (struct tape_device *) dev->driver_data;
112 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state);
116 DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
119 tape_first_minor_show(struct device *dev, char *buf)
121 struct tape_device *tdev;
123 tdev = (struct tape_device *) dev->driver_data;
124 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor);
128 DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
131 tape_state_show(struct device *dev, char *buf)
133 struct tape_device *tdev;
135 tdev = (struct tape_device *) dev->driver_data;
136 return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ?
137 "OFFLINE" : tape_state_verbose[tdev->tape_state]);
141 DEVICE_ATTR(state, 0444, tape_state_show, NULL);
144 tape_operation_show(struct device *dev, char *buf)
146 struct tape_device *tdev;
149 tdev = (struct tape_device *) dev->driver_data;
150 if (tdev->first_minor < 0)
151 return scnprintf(buf, PAGE_SIZE, "N/A\n");
153 spin_lock_irq(get_ccwdev_lock(tdev->cdev));
154 if (list_empty(&tdev->req_queue))
155 rc = scnprintf(buf, PAGE_SIZE, "---\n");
157 struct tape_request *req;
159 req = list_entry(tdev->req_queue.next, struct tape_request,
161 rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
163 spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
168 DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
171 tape_blocksize_show(struct device *dev, char *buf)
173 struct tape_device *tdev;
175 tdev = (struct tape_device *) dev->driver_data;
177 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size);
181 DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);
183 static struct attribute *tape_attrs[] = {
184 &dev_attr_medium_state.attr,
185 &dev_attr_first_minor.attr,
186 &dev_attr_state.attr,
187 &dev_attr_operation.attr,
188 &dev_attr_blocksize.attr,
192 static struct attribute_group tape_attr_group = {
197 * Tape state functions
200 tape_state_set(struct tape_device *device, enum tape_state newstate)
204 if (device->tape_state == TS_NOT_OPER) {
205 DBF_EVENT(3, "ts_set err: not oper\n");
208 DBF_EVENT(4, "ts. dev: %x\n", device->first_minor);
209 if (device->tape_state < TO_SIZE && device->tape_state >= 0)
210 str = tape_state_verbose[device->tape_state];
213 DBF_EVENT(4, "old ts: %s\n", str);
214 if (device->tape_state < TO_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 < TO_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_halt_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);
270 if (rc == 0) { /* Termination successful */
272 request->status = TAPE_REQUEST_DONE;
277 DBF_EXCEPTION(2, "device gone, retry\n");
279 DBF_EXCEPTION(2, "I/O error, retry\n");
280 else if (rc == -EBUSY)
281 DBF_EXCEPTION(2, "device busy, retry late\n");
290 * Add device into the sorted list, giving it the first
291 * available minor number.
294 tape_assign_minor(struct tape_device *device)
296 struct tape_device *tmp;
300 write_lock(&tape_device_lock);
301 list_for_each_entry(tmp, &tape_device_list, node) {
302 if (minor < tmp->first_minor)
304 minor += TAPE_MINORS_PER_DEV;
307 write_unlock(&tape_device_lock);
310 device->first_minor = minor;
311 list_add_tail(&device->node, &tmp->node);
312 write_unlock(&tape_device_lock);
316 /* remove device from the list */
318 tape_remove_minor(struct tape_device *device)
320 write_lock(&tape_device_lock);
321 list_del_init(&device->node);
322 device->first_minor = -1;
323 write_unlock(&tape_device_lock);
327 * Set a device online.
329 * This function is called by the common I/O layer to move a device from the
330 * detected but offline into the online state.
331 * If we return an error (RC < 0) the device remains in the offline state. This
332 * can happen if the device is assigned somewhere else, for example.
335 tape_generic_online(struct tape_device *device,
336 struct tape_discipline *discipline)
340 DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
342 if (device->tape_state != TS_INIT) {
343 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
347 /* Let the discipline have a go at the device. */
348 device->discipline = discipline;
349 rc = discipline->setup_device(device);
352 rc = tape_assign_minor(device);
356 rc = tapechar_setup_device(device);
359 rc = tapeblock_setup_device(device);
363 tape_state_set(device, TS_UNUSED);
365 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
370 tapechar_cleanup_device(device);
372 device->discipline->cleanup_device(device);
373 device->discipline = NULL;
375 tape_remove_minor(device);
381 tape_cleanup_device(struct tape_device *device)
383 tapeblock_cleanup_device(device);
384 tapechar_cleanup_device(device);
385 device->discipline->cleanup_device(device);
386 tape_remove_minor(device);
387 tape_med_state_set(device, MS_UNKNOWN);
391 * Set device offline.
393 * Called by the common I/O layer if the drive should set offline on user
394 * request. We may prevent this by returning an error.
395 * Manual offline is only allowed while the drive is not in use.
398 tape_generic_offline(struct tape_device *device)
401 PRINT_ERR("tape_generic_offline: no such device\n");
405 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
406 device->cdev_id, device);
408 spin_lock_irq(get_ccwdev_lock(device->cdev));
409 switch (device->tape_state) {
412 spin_unlock_irq(get_ccwdev_lock(device->cdev));
415 tape_state_set(device, TS_INIT);
416 spin_unlock_irq(get_ccwdev_lock(device->cdev));
417 tape_cleanup_device(device);
420 DBF_EVENT(3, "(%08x): Set offline failed "
423 PRINT_WARN("(%s): Set offline failed "
425 device->cdev->dev.bus_id);
426 spin_unlock_irq(get_ccwdev_lock(device->cdev));
430 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
435 * Allocate memory for a new device structure.
437 static struct tape_device *
438 tape_alloc_device(void)
440 struct tape_device *device;
442 device = (struct tape_device *)
443 kmalloc(sizeof(struct tape_device), GFP_KERNEL);
444 if (device == NULL) {
445 DBF_EXCEPTION(2, "ti:no mem\n");
446 PRINT_INFO ("can't allocate memory for "
447 "tape info structure\n");
448 return ERR_PTR(-ENOMEM);
450 memset(device, 0, sizeof(struct tape_device));
451 device->modeset_byte = (char *) kmalloc(1, GFP_KERNEL | GFP_DMA);
452 if (device->modeset_byte == NULL) {
453 DBF_EXCEPTION(2, "ti:no mem\n");
454 PRINT_INFO("can't allocate memory for modeset byte\n");
456 return ERR_PTR(-ENOMEM);
458 INIT_LIST_HEAD(&device->req_queue);
459 INIT_LIST_HEAD(&device->node);
460 init_waitqueue_head(&device->state_change_wq);
461 device->tape_state = TS_INIT;
462 device->medium_state = MS_UNKNOWN;
463 *device->modeset_byte = 0;
464 device->first_minor = -1;
465 atomic_set(&device->ref_count, 1);
471 * Get a reference to an existing device structure. This will automatically
472 * increment the reference count.
475 tape_get_device_reference(struct tape_device *device)
477 DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device,
478 atomic_inc_return(&device->ref_count));
484 * Decrease the reference counter of a devices structure. If the
485 * reference counter reaches zero free the device structure.
486 * The function returns a NULL pointer to be used by the caller
487 * for clearing reference pointers.
490 tape_put_device(struct tape_device *device)
494 remain = atomic_dec_return(&device->ref_count);
496 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain);
499 DBF_EVENT(4, "put device without reference\n");
500 PRINT_ERR("put device without reference\n");
502 DBF_EVENT(4, "tape_free_device(%p)\n", device);
503 kfree(device->modeset_byte);
512 * Find tape device by a device index.
515 tape_get_device(int devindex)
517 struct tape_device *device, *tmp;
519 device = ERR_PTR(-ENODEV);
520 read_lock(&tape_device_lock);
521 list_for_each_entry(tmp, &tape_device_list, node) {
522 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
523 device = tape_get_device_reference(tmp);
527 read_unlock(&tape_device_lock);
532 * Driverfs tape probe function.
535 tape_generic_probe(struct ccw_device *cdev)
537 struct tape_device *device;
539 device = tape_alloc_device();
542 PRINT_INFO("tape device %s found\n", cdev->dev.bus_id);
543 cdev->dev.driver_data = device;
545 device->cdev_id = busid_to_int(cdev->dev.bus_id);
546 cdev->handler = __tape_do_irq;
548 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
549 sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
555 __tape_discard_requests(struct tape_device *device)
557 struct tape_request * request;
558 struct list_head * l, *n;
560 list_for_each_safe(l, n, &device->req_queue) {
561 request = list_entry(l, struct tape_request, list);
562 if (request->status == TAPE_REQUEST_IN_IO)
563 request->status = TAPE_REQUEST_DONE;
564 list_del(&request->list);
566 /* Decrease ref_count for removed request. */
567 request->device = tape_put_device(device);
569 if (request->callback != NULL)
570 request->callback(request, request->callback_data);
575 * Driverfs tape remove function.
577 * This function is called whenever the common I/O layer detects the device
578 * gone. This can happen at any time and we cannot refuse.
581 tape_generic_remove(struct ccw_device *cdev)
583 struct tape_device * device;
585 device = cdev->dev.driver_data;
587 PRINT_ERR("No device pointer in tape_generic_remove!\n");
590 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
592 spin_lock_irq(get_ccwdev_lock(device->cdev));
593 switch (device->tape_state) {
595 tape_state_set(device, TS_NOT_OPER);
600 spin_unlock_irq(get_ccwdev_lock(device->cdev));
604 * Need only to release the device.
606 tape_state_set(device, TS_NOT_OPER);
607 spin_unlock_irq(get_ccwdev_lock(device->cdev));
608 tape_cleanup_device(device);
612 * There may be requests on the queue. We will not get
613 * an interrupt for a request that was running. So we
614 * just post them all as I/O errors.
616 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
618 PRINT_WARN("(%s): Drive in use vanished - "
620 device->cdev->dev.bus_id);
621 PRINT_WARN("State was %i\n", device->tape_state);
622 tape_state_set(device, TS_NOT_OPER);
623 __tape_discard_requests(device);
624 spin_unlock_irq(get_ccwdev_lock(device->cdev));
625 tape_cleanup_device(device);
628 if (cdev->dev.driver_data != NULL) {
629 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
630 cdev->dev.driver_data = tape_put_device(cdev->dev.driver_data);
635 * Allocate a new tape ccw request
637 struct tape_request *
638 tape_alloc_request(int cplength, int datasize)
640 struct tape_request *request;
642 if (datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
645 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
647 request = (struct tape_request *) kmalloc(sizeof(struct tape_request),
649 if (request == NULL) {
650 DBF_EXCEPTION(1, "cqra nomem\n");
651 return ERR_PTR(-ENOMEM);
653 memset(request, 0, sizeof(struct tape_request));
654 /* allocate channel program */
656 request->cpaddr = kmalloc(cplength*sizeof(struct ccw1),
657 GFP_ATOMIC | GFP_DMA);
658 if (request->cpaddr == NULL) {
659 DBF_EXCEPTION(1, "cqra nomem\n");
661 return ERR_PTR(-ENOMEM);
663 memset(request->cpaddr, 0, cplength*sizeof(struct ccw1));
665 /* alloc small kernel buffer */
667 request->cpdata = kmalloc(datasize, GFP_KERNEL | GFP_DMA);
668 if (request->cpdata == NULL) {
669 DBF_EXCEPTION(1, "cqra nomem\n");
670 if (request->cpaddr != NULL)
671 kfree(request->cpaddr);
673 return ERR_PTR(-ENOMEM);
675 memset(request->cpdata, 0, datasize);
677 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
684 * Free tape ccw request
687 tape_free_request (struct tape_request * request)
689 DBF_LH(6, "Free request %p\n", request);
691 if (request->device != NULL) {
692 request->device = tape_put_device(request->device);
694 if (request->cpdata != NULL)
695 kfree(request->cpdata);
696 if (request->cpaddr != NULL)
697 kfree(request->cpaddr);
702 __tape_do_io_list(struct tape_device *device)
704 struct list_head *l, *n;
705 struct tape_request *request;
708 DBF_LH(6, "__tape_do_io_list(%p)\n", device);
710 * Try to start each request on request queue until one is
711 * started successful.
713 list_for_each_safe(l, n, &device->req_queue) {
714 request = list_entry(l, struct tape_request, list);
715 #ifdef CONFIG_S390_TAPE_BLOCK
716 if (request->op == TO_BLOCK)
717 device->discipline->check_locate(device, request);
719 rc = ccw_device_start(device->cdev, request->cpaddr,
720 (unsigned long) request, 0x00,
723 request->status = TAPE_REQUEST_IN_IO;
726 /* Start failed. Remove request and indicate failure. */
727 DBF_EVENT(1, "tape: DOIO failed with er = %i\n", rc);
729 /* Set ending status and do callback. */
731 request->status = TAPE_REQUEST_DONE;
732 __tape_remove_request(device, request);
737 __tape_remove_request(struct tape_device *device, struct tape_request *request)
739 /* Remove from request queue. */
740 list_del(&request->list);
743 if (request->callback != NULL)
744 request->callback(request, request->callback_data);
746 /* Start next request. */
747 if (!list_empty(&device->req_queue))
748 __tape_do_io_list(device);
752 * Write sense data to console/dbf
755 tape_dump_sense(struct tape_device* device, struct tape_request *request,
760 PRINT_INFO("-------------------------------------------------\n");
761 PRINT_INFO("DSTAT : %02x CSTAT: %02x CPA: %04x\n",
762 irb->scsw.dstat, irb->scsw.cstat, irb->scsw.cpa);
763 PRINT_INFO("DEVICE: %s\n", device->cdev->dev.bus_id);
765 PRINT_INFO("OP : %s\n", tape_op_verbose[request->op]);
767 sptr = (unsigned int *) irb->ecw;
768 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
769 sptr[0], sptr[1], sptr[2], sptr[3]);
770 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
771 sptr[4], sptr[5], sptr[6], sptr[7]);
772 PRINT_INFO("--------------------------------------------------\n");
776 * Write sense data to dbf
779 tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
786 op = tape_op_verbose[request->op];
789 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
790 irb->scsw.dstat,irb->scsw.cstat);
791 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
792 sptr = (unsigned int *) irb->ecw;
793 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
794 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
795 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
796 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
800 * I/O helper function. Adds the request to the request queue
801 * and starts it if the tape is idle. Has to be called with
802 * the device lock held.
805 __tape_do_io(struct tape_device *device, struct tape_request *request)
809 switch (request->op) {
814 if (device->tape_state == TS_INIT)
816 if (device->tape_state == TS_UNUSED)
819 if (device->tape_state == TS_BLKUSE)
821 if (device->tape_state != TS_IN_USE)
825 /* Increase use count of device for the added request. */
826 request->device = tape_get_device_reference(device);
828 if (list_empty(&device->req_queue)) {
829 /* No other requests are on the queue. Start this one. */
830 #ifdef CONFIG_S390_TAPE_BLOCK
831 if (request->op == TO_BLOCK)
832 device->discipline->check_locate(device, request);
834 rc = ccw_device_start(device->cdev, request->cpaddr,
835 (unsigned long) request, 0x00,
838 DBF_EVENT(1, "tape: DOIO failed with rc = %i\n", rc);
841 DBF_LH(5, "Request %p added for execution.\n", request);
842 list_add(&request->list, &device->req_queue);
843 request->status = TAPE_REQUEST_IN_IO;
845 DBF_LH(5, "Request %p add to queue.\n", request);
846 list_add_tail(&request->list, &device->req_queue);
847 request->status = TAPE_REQUEST_QUEUED;
853 * Add the request to the request queue, try to start it if the
854 * tape is idle. Return without waiting for end of i/o.
857 tape_do_io_async(struct tape_device *device, struct tape_request *request)
861 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
863 spin_lock_irq(get_ccwdev_lock(device->cdev));
864 /* Add request to request queue and try to start it. */
865 rc = __tape_do_io(device, request);
866 spin_unlock_irq(get_ccwdev_lock(device->cdev));
871 * tape_do_io/__tape_wake_up
872 * Add the request to the request queue, try to start it if the
873 * tape is idle and wait uninterruptible for its completion.
876 __tape_wake_up(struct tape_request *request, void *data)
878 request->callback = NULL;
879 wake_up((wait_queue_head_t *) data);
883 tape_do_io(struct tape_device *device, struct tape_request *request)
885 wait_queue_head_t wq;
888 init_waitqueue_head(&wq);
889 spin_lock_irq(get_ccwdev_lock(device->cdev));
891 request->callback = __tape_wake_up;
892 request->callback_data = &wq;
893 /* Add request to request queue and try to start it. */
894 rc = __tape_do_io(device, request);
895 spin_unlock_irq(get_ccwdev_lock(device->cdev));
898 /* Request added to the queue. Wait for its completion. */
899 wait_event(wq, (request->callback == NULL));
900 /* Get rc from request */
905 * tape_do_io_interruptible/__tape_wake_up_interruptible
906 * Add the request to the request queue, try to start it if the
907 * tape is idle and wait uninterruptible for its completion.
910 __tape_wake_up_interruptible(struct tape_request *request, void *data)
912 request->callback = NULL;
913 wake_up_interruptible((wait_queue_head_t *) data);
917 tape_do_io_interruptible(struct tape_device *device,
918 struct tape_request *request)
920 wait_queue_head_t wq;
923 init_waitqueue_head(&wq);
924 spin_lock_irq(get_ccwdev_lock(device->cdev));
926 request->callback = __tape_wake_up_interruptible;
927 request->callback_data = &wq;
928 rc = __tape_do_io(device, request);
929 spin_unlock_irq(get_ccwdev_lock(device->cdev));
932 /* Request added to the queue. Wait for its completion. */
933 rc = wait_event_interruptible(wq, (request->callback == NULL));
934 if (rc != -ERESTARTSYS)
935 /* Request finished normally. */
937 /* Interrupted by a signal. We have to stop the current request. */
938 spin_lock_irq(get_ccwdev_lock(device->cdev));
939 rc = __tape_halt_io(device, request);
941 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
944 spin_unlock_irq(get_ccwdev_lock(device->cdev));
949 * Handle requests that return an i/o error in the irb.
952 tape_handle_killed_request(
953 struct tape_device *device,
954 struct tape_request *request)
956 if(request != NULL) {
957 /* Set ending status. FIXME: Should the request be retried? */
959 request->status = TAPE_REQUEST_DONE;
960 __tape_remove_request(device, request);
962 __tape_do_io_list(device);
967 * Tape interrupt routine, called from the ccw_device layer
970 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
972 struct tape_device *device;
973 struct tape_request *request;
977 device = (struct tape_device *) cdev->dev.driver_data;
978 if (device == NULL) {
979 PRINT_ERR("could not get device structure for %s "
980 "in interrupt\n", cdev->dev.bus_id);
983 request = (struct tape_request *) intparm;
985 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
987 /* On special conditions irb is an error pointer */
989 switch (PTR_ERR(irb)) {
991 PRINT_WARN("(%s): Request timed out\n",
994 tape_handle_killed_request(device, request);
997 PRINT_ERR("(%s): Unexpected i/o error %li\n",
1004 /* May be an unsolicited irq */
1006 request->rescnt = irb->scsw.count;
1008 if (irb->scsw.dstat != 0x0c) {
1009 /* Set the 'ONLINE' flag depending on sense byte 1 */
1010 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1011 device->tape_generic_status |= GMT_ONLINE(~0);
1013 device->tape_generic_status &= ~GMT_ONLINE(~0);
1016 * Any request that does not come back with channel end
1017 * and device end is unusual. Log the sense data.
1019 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1020 tape_dump_sense_dbf(device, request, irb);
1022 /* Upon normal completion the device _is_ online */
1023 device->tape_generic_status |= GMT_ONLINE(~0);
1025 if (device->tape_state == TS_NOT_OPER) {
1026 DBF_EVENT(6, "tape:device is not operational\n");
1031 * Request that were canceled still come back with an interrupt.
1032 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1034 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
1035 __tape_remove_request(device, request);
1039 rc = device->discipline->irq(device, request, irb);
1041 * rc < 0 : request finished unsuccessfully.
1042 * rc == TAPE_IO_SUCCESS: request finished successfully.
1043 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1044 * rc == TAPE_IO_RETRY: request finished but needs another go.
1045 * rc == TAPE_IO_STOP: request needs to get terminated.
1049 case TAPE_IO_SUCCESS:
1050 /* Upon normal completion the device _is_ online */
1051 device->tape_generic_status |= GMT_ONLINE(~0);
1054 case TAPE_IO_PENDING:
1057 #ifdef CONFIG_S390_TAPE_BLOCK
1058 if (request->op == TO_BLOCK)
1059 device->discipline->check_locate(device, request);
1061 rc = ccw_device_start(cdev, request->cpaddr,
1062 (unsigned long) request, 0x00,
1065 DBF_EVENT(1, "tape: DOIO failed with er = %i\n", rc);
1070 __tape_halt_io(device, request);
1074 DBF_EVENT(6, "xunknownrc\n");
1075 PRINT_ERR("Invalid return code from discipline "
1076 "interrupt function.\n");
1083 /* May be an unsolicited irq */
1084 if(request != NULL) {
1085 /* Set ending status. */
1087 request->status = TAPE_REQUEST_DONE;
1088 __tape_remove_request(device, request);
1090 __tape_do_io_list(device);
1096 * Tape device open function used by tape_char & tape_block frontends.
1099 tape_open(struct tape_device *device)
1103 spin_lock(get_ccwdev_lock(device->cdev));
1104 if (device->tape_state == TS_NOT_OPER) {
1105 DBF_EVENT(6, "TAPE:nodev\n");
1107 } else if (device->tape_state == TS_IN_USE) {
1108 DBF_EVENT(6, "TAPE:dbusy\n");
1110 } else if (device->tape_state == TS_BLKUSE) {
1111 DBF_EVENT(6, "TAPE:dbusy\n");
1113 } else if (device->discipline != NULL &&
1114 !try_module_get(device->discipline->owner)) {
1115 DBF_EVENT(6, "TAPE:nodisc\n");
1118 tape_state_set(device, TS_IN_USE);
1121 spin_unlock(get_ccwdev_lock(device->cdev));
1126 * Tape device release function used by tape_char & tape_block frontends.
1129 tape_release(struct tape_device *device)
1131 spin_lock(get_ccwdev_lock(device->cdev));
1132 if (device->tape_state == TS_IN_USE)
1133 tape_state_set(device, TS_UNUSED);
1134 module_put(device->discipline->owner);
1135 spin_unlock(get_ccwdev_lock(device->cdev));
1140 * Execute a magnetic tape command a number of times.
1143 tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1148 DBF_EVENT(6, "TAPE:mtio\n");
1149 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1150 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1152 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1154 fn = device->discipline->mtop_array[mt_op];
1158 /* We assume that the backends can handle count up to 500. */
1159 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1160 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1162 for (; mt_count > 500; mt_count -= 500)
1163 if ((rc = fn(device, 500)) != 0)
1166 rc = fn(device, mt_count);
1168 rc = fn(device, mt_count);
1174 * Tape init function.
1179 tape_dbf_area = debug_register ( "tape", 1, 2, 4*sizeof(long));
1180 debug_register_view(tape_dbf_area, &debug_sprintf_view);
1181 #ifdef DBF_LIKE_HELL
1182 debug_set_level(tape_dbf_area, 6);
1184 DBF_EVENT(3, "tape init: ($Revision: 1.49 $)\n");
1192 * Tape exit function.
1197 DBF_EVENT(6, "tape exit\n");
1199 /* Get rid of the frontends */
1202 tape_proc_cleanup();
1203 debug_unregister (tape_dbf_area);
1206 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1207 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1208 MODULE_DESCRIPTION("Linux on zSeries channel attached "
1209 "tape device driver ($Revision: 1.49 $)");
1210 MODULE_LICENSE("GPL");
1212 module_init(tape_init);
1213 module_exit(tape_exit);
1215 EXPORT_SYMBOL(tape_dbf_area);
1216 EXPORT_SYMBOL(tape_generic_remove);
1217 EXPORT_SYMBOL(tape_generic_probe);
1218 EXPORT_SYMBOL(tape_generic_online);
1219 EXPORT_SYMBOL(tape_generic_offline);
1220 EXPORT_SYMBOL(tape_put_device);
1221 EXPORT_SYMBOL(tape_get_device_reference);
1222 EXPORT_SYMBOL(tape_state_verbose);
1223 EXPORT_SYMBOL(tape_op_verbose);
1224 EXPORT_SYMBOL(tape_state_set);
1225 EXPORT_SYMBOL(tape_med_state_set);
1226 EXPORT_SYMBOL(tape_alloc_request);
1227 EXPORT_SYMBOL(tape_free_request);
1228 EXPORT_SYMBOL(tape_dump_sense);
1229 EXPORT_SYMBOL(tape_dump_sense_dbf);
1230 EXPORT_SYMBOL(tape_do_io);
1231 EXPORT_SYMBOL(tape_do_io_async);
1232 EXPORT_SYMBOL(tape_do_io_interruptible);
1233 EXPORT_SYMBOL(tape_mtop);