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
23 #define TAPE_DBF_AREA tape_core_dbf
28 #define PRINTK_HEADER "TAPE_CORE: "
30 static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *);
31 static void __tape_remove_request(struct tape_device *, struct tape_request *);
34 * One list to contain all tape devices of all disciplines, so
35 * we can assign the devices to minor numbers of the same major
36 * The list is protected by the rwlock
38 static struct list_head tape_device_list = LIST_HEAD_INIT(tape_device_list);
39 static rwlock_t tape_device_lock = RW_LOCK_UNLOCKED;
42 * Pointer to debug area.
44 debug_info_t *TAPE_DBF_AREA = NULL;
45 EXPORT_SYMBOL(TAPE_DBF_AREA);
48 * Printable strings for tape enumerations.
50 const char *tape_state_verbose[TS_SIZE] =
52 [TS_UNUSED] = "UNUSED",
53 [TS_IN_USE] = "IN_USE",
54 [TS_BLKUSE] = "BLKUSE",
56 [TS_NOT_OPER] = "NOT_OP"
59 const char *tape_op_verbose[TO_SIZE] =
61 [TO_BLOCK] = "BLK", [TO_BSB] = "BSB",
62 [TO_BSF] = "BSF", [TO_DSE] = "DSE",
63 [TO_FSB] = "FSB", [TO_FSF] = "FSF",
64 [TO_LBL] = "LBL", [TO_NOP] = "NOP",
65 [TO_RBA] = "RBA", [TO_RBI] = "RBI",
66 [TO_RFO] = "RFO", [TO_REW] = "REW",
67 [TO_RUN] = "RUN", [TO_WRI] = "WRI",
68 [TO_WTM] = "WTM", [TO_MSEN] = "MSN",
69 [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF",
70 [TO_READ_ATTMSG] = "RAT",
71 [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS",
76 busid_to_int(char *bus_id)
82 for(s = bus_id, d = 0; *s != '\0' && *s != '.'; s++)
83 d = (d * 10) + (*s - '0');
85 for(s++, d = 0; *s != '\0' && *s != '.'; s++)
86 d = (d * 10) + (*s - '0');
89 for(s++; *s != '\0'; s++) {
90 if (*s >= '0' && *s <= '9') {
92 } else if (*s >= 'a' && *s <= 'f') {
104 * Some channel attached tape specific attributes.
106 * FIXME: In the future the first_minor and blocksize attribute should be
107 * replaced by a link to the cdev tree.
110 tape_medium_state_show(struct device *dev, char *buf)
112 struct tape_device *tdev;
114 tdev = (struct tape_device *) dev->driver_data;
115 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state);
119 DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
122 tape_first_minor_show(struct device *dev, char *buf)
124 struct tape_device *tdev;
126 tdev = (struct tape_device *) dev->driver_data;
127 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor);
131 DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
134 tape_state_show(struct device *dev, char *buf)
136 struct tape_device *tdev;
138 tdev = (struct tape_device *) dev->driver_data;
139 return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ?
140 "OFFLINE" : tape_state_verbose[tdev->tape_state]);
144 DEVICE_ATTR(state, 0444, tape_state_show, NULL);
147 tape_operation_show(struct device *dev, char *buf)
149 struct tape_device *tdev;
152 tdev = (struct tape_device *) dev->driver_data;
153 if (tdev->first_minor < 0)
154 return scnprintf(buf, PAGE_SIZE, "N/A\n");
156 spin_lock_irq(get_ccwdev_lock(tdev->cdev));
157 if (list_empty(&tdev->req_queue))
158 rc = scnprintf(buf, PAGE_SIZE, "---\n");
160 struct tape_request *req;
162 req = list_entry(tdev->req_queue.next, struct tape_request,
164 rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
166 spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
171 DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
174 tape_blocksize_show(struct device *dev, char *buf)
176 struct tape_device *tdev;
178 tdev = (struct tape_device *) dev->driver_data;
180 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size);
184 DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);
186 static struct attribute *tape_attrs[] = {
187 &dev_attr_medium_state.attr,
188 &dev_attr_first_minor.attr,
189 &dev_attr_state.attr,
190 &dev_attr_operation.attr,
191 &dev_attr_blocksize.attr,
195 static struct attribute_group tape_attr_group = {
200 * Tape state functions
203 tape_state_set(struct tape_device *device, enum tape_state newstate)
207 if (device->tape_state == TS_NOT_OPER) {
208 DBF_EVENT(3, "ts_set err: not oper\n");
211 DBF_EVENT(4, "ts. dev: %x\n", device->first_minor);
212 if (device->tape_state < TO_SIZE && device->tape_state >= 0)
213 str = tape_state_verbose[device->tape_state];
216 DBF_EVENT(4, "old ts: %s\n", str);
217 if (device->tape_state < TO_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 < TO_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_halt_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);
273 if (rc == 0) { /* Termination successful */
275 request->status = TAPE_REQUEST_DONE;
280 DBF_EXCEPTION(2, "device gone, retry\n");
282 DBF_EXCEPTION(2, "I/O error, retry\n");
283 else if (rc == -EBUSY)
284 DBF_EXCEPTION(2, "device busy, retry late\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 rc = discipline->setup_device(device);
355 rc = tape_assign_minor(device);
359 rc = tapechar_setup_device(device);
362 rc = tapeblock_setup_device(device);
366 tape_state_set(device, TS_UNUSED);
368 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
373 tapechar_cleanup_device(device);
375 device->discipline->cleanup_device(device);
376 device->discipline = NULL;
378 tape_remove_minor(device);
384 tape_cleanup_device(struct tape_device *device)
386 tapeblock_cleanup_device(device);
387 tapechar_cleanup_device(device);
388 device->discipline->cleanup_device(device);
389 tape_remove_minor(device);
390 tape_med_state_set(device, MS_UNKNOWN);
394 * Set device offline.
396 * Called by the common I/O layer if the drive should set offline on user
397 * request. We may prevent this by returning an error.
398 * Manual offline is only allowed while the drive is not in use.
401 tape_generic_offline(struct tape_device *device)
404 PRINT_ERR("tape_generic_offline: no such device\n");
408 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
409 device->cdev_id, device);
411 spin_lock_irq(get_ccwdev_lock(device->cdev));
412 switch (device->tape_state) {
415 spin_unlock_irq(get_ccwdev_lock(device->cdev));
418 tape_state_set(device, TS_INIT);
419 spin_unlock_irq(get_ccwdev_lock(device->cdev));
420 tape_cleanup_device(device);
423 DBF_EVENT(3, "(%08x): Set offline failed "
426 PRINT_WARN("(%s): Set offline failed "
428 device->cdev->dev.bus_id);
429 spin_unlock_irq(get_ccwdev_lock(device->cdev));
433 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
438 * Allocate memory for a new device structure.
440 static struct tape_device *
441 tape_alloc_device(void)
443 struct tape_device *device;
445 device = (struct tape_device *)
446 kmalloc(sizeof(struct tape_device), GFP_KERNEL);
447 if (device == NULL) {
448 DBF_EXCEPTION(2, "ti:no mem\n");
449 PRINT_INFO ("can't allocate memory for "
450 "tape info structure\n");
451 return ERR_PTR(-ENOMEM);
453 memset(device, 0, sizeof(struct tape_device));
454 device->modeset_byte = (char *) kmalloc(1, GFP_KERNEL | GFP_DMA);
455 if (device->modeset_byte == NULL) {
456 DBF_EXCEPTION(2, "ti:no mem\n");
457 PRINT_INFO("can't allocate memory for modeset byte\n");
459 return ERR_PTR(-ENOMEM);
461 INIT_LIST_HEAD(&device->req_queue);
462 INIT_LIST_HEAD(&device->node);
463 init_waitqueue_head(&device->state_change_wq);
464 device->tape_state = TS_INIT;
465 device->medium_state = MS_UNKNOWN;
466 *device->modeset_byte = 0;
467 device->first_minor = -1;
468 atomic_set(&device->ref_count, 1);
474 * Get a reference to an existing device structure. This will automatically
475 * increment the reference count.
478 tape_get_device_reference(struct tape_device *device)
480 DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device,
481 atomic_inc_return(&device->ref_count));
487 * Decrease the reference counter of a devices structure. If the
488 * reference counter reaches zero free the device structure.
489 * The function returns a NULL pointer to be used by the caller
490 * for clearing reference pointers.
493 tape_put_device(struct tape_device *device)
497 remain = atomic_dec_return(&device->ref_count);
499 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain);
502 DBF_EVENT(4, "put device without reference\n");
503 PRINT_ERR("put device without reference\n");
505 DBF_EVENT(4, "tape_free_device(%p)\n", device);
506 kfree(device->modeset_byte);
515 * Find tape device by a device index.
518 tape_get_device(int devindex)
520 struct tape_device *device, *tmp;
522 device = ERR_PTR(-ENODEV);
523 read_lock(&tape_device_lock);
524 list_for_each_entry(tmp, &tape_device_list, node) {
525 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
526 device = tape_get_device_reference(tmp);
530 read_unlock(&tape_device_lock);
535 * Driverfs tape probe function.
538 tape_generic_probe(struct ccw_device *cdev)
540 struct tape_device *device;
542 device = tape_alloc_device();
545 PRINT_INFO("tape device %s found\n", cdev->dev.bus_id);
546 cdev->dev.driver_data = device;
548 device->cdev_id = busid_to_int(cdev->dev.bus_id);
549 cdev->handler = __tape_do_irq;
551 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
552 sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
558 __tape_discard_requests(struct tape_device *device)
560 struct tape_request * request;
561 struct list_head * l, *n;
563 list_for_each_safe(l, n, &device->req_queue) {
564 request = list_entry(l, struct tape_request, list);
565 if (request->status == TAPE_REQUEST_IN_IO)
566 request->status = TAPE_REQUEST_DONE;
567 list_del(&request->list);
569 /* Decrease ref_count for removed request. */
570 request->device = tape_put_device(device);
572 if (request->callback != NULL)
573 request->callback(request, request->callback_data);
578 * Driverfs tape remove function.
580 * This function is called whenever the common I/O layer detects the device
581 * gone. This can happen at any time and we cannot refuse.
584 tape_generic_remove(struct ccw_device *cdev)
586 struct tape_device * device;
588 device = cdev->dev.driver_data;
590 PRINT_ERR("No device pointer in tape_generic_remove!\n");
593 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
595 spin_lock_irq(get_ccwdev_lock(device->cdev));
596 switch (device->tape_state) {
598 tape_state_set(device, TS_NOT_OPER);
603 spin_unlock_irq(get_ccwdev_lock(device->cdev));
607 * Need only to release the device.
609 tape_state_set(device, TS_NOT_OPER);
610 spin_unlock_irq(get_ccwdev_lock(device->cdev));
611 tape_cleanup_device(device);
615 * There may be requests on the queue. We will not get
616 * an interrupt for a request that was running. So we
617 * just post them all as I/O errors.
619 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
621 PRINT_WARN("(%s): Drive in use vanished - "
623 device->cdev->dev.bus_id);
624 PRINT_WARN("State was %i\n", device->tape_state);
625 tape_state_set(device, TS_NOT_OPER);
626 __tape_discard_requests(device);
627 spin_unlock_irq(get_ccwdev_lock(device->cdev));
628 tape_cleanup_device(device);
631 if (cdev->dev.driver_data != NULL) {
632 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
633 cdev->dev.driver_data = tape_put_device(cdev->dev.driver_data);
638 * Allocate a new tape ccw request
640 struct tape_request *
641 tape_alloc_request(int cplength, int datasize)
643 struct tape_request *request;
645 if (datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
648 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
650 request = (struct tape_request *) kmalloc(sizeof(struct tape_request),
652 if (request == NULL) {
653 DBF_EXCEPTION(1, "cqra nomem\n");
654 return ERR_PTR(-ENOMEM);
656 memset(request, 0, sizeof(struct tape_request));
657 /* allocate channel program */
659 request->cpaddr = kmalloc(cplength*sizeof(struct ccw1),
660 GFP_ATOMIC | GFP_DMA);
661 if (request->cpaddr == NULL) {
662 DBF_EXCEPTION(1, "cqra nomem\n");
664 return ERR_PTR(-ENOMEM);
666 memset(request->cpaddr, 0, cplength*sizeof(struct ccw1));
668 /* alloc small kernel buffer */
670 request->cpdata = kmalloc(datasize, GFP_KERNEL | GFP_DMA);
671 if (request->cpdata == NULL) {
672 DBF_EXCEPTION(1, "cqra nomem\n");
673 if (request->cpaddr != NULL)
674 kfree(request->cpaddr);
676 return ERR_PTR(-ENOMEM);
678 memset(request->cpdata, 0, datasize);
680 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
687 * Free tape ccw request
690 tape_free_request (struct tape_request * request)
692 DBF_LH(6, "Free request %p\n", request);
694 if (request->device != NULL) {
695 request->device = tape_put_device(request->device);
697 if (request->cpdata != NULL)
698 kfree(request->cpdata);
699 if (request->cpaddr != NULL)
700 kfree(request->cpaddr);
705 __tape_do_io_list(struct tape_device *device)
707 struct list_head *l, *n;
708 struct tape_request *request;
711 DBF_LH(6, "__tape_do_io_list(%p)\n", device);
713 * Try to start each request on request queue until one is
714 * started successful.
716 list_for_each_safe(l, n, &device->req_queue) {
717 request = list_entry(l, struct tape_request, list);
718 #ifdef CONFIG_S390_TAPE_BLOCK
719 if (request->op == TO_BLOCK)
720 device->discipline->check_locate(device, request);
722 rc = ccw_device_start(device->cdev, request->cpaddr,
723 (unsigned long) request, 0x00,
726 request->status = TAPE_REQUEST_IN_IO;
729 /* Start failed. Remove request and indicate failure. */
730 DBF_EVENT(1, "tape: DOIO failed with er = %i\n", rc);
732 /* Set ending status and do callback. */
734 request->status = TAPE_REQUEST_DONE;
735 __tape_remove_request(device, request);
740 __tape_remove_request(struct tape_device *device, struct tape_request *request)
742 /* Remove from request queue. */
743 list_del(&request->list);
746 if (request->callback != NULL)
747 request->callback(request, request->callback_data);
749 /* Start next request. */
750 if (!list_empty(&device->req_queue))
751 __tape_do_io_list(device);
755 * Write sense data to console/dbf
758 tape_dump_sense(struct tape_device* device, struct tape_request *request,
763 PRINT_INFO("-------------------------------------------------\n");
764 PRINT_INFO("DSTAT : %02x CSTAT: %02x CPA: %04x\n",
765 irb->scsw.dstat, irb->scsw.cstat, irb->scsw.cpa);
766 PRINT_INFO("DEVICE: %s\n", device->cdev->dev.bus_id);
768 PRINT_INFO("OP : %s\n", tape_op_verbose[request->op]);
770 sptr = (unsigned int *) irb->ecw;
771 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
772 sptr[0], sptr[1], sptr[2], sptr[3]);
773 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
774 sptr[4], sptr[5], sptr[6], sptr[7]);
775 PRINT_INFO("--------------------------------------------------\n");
779 * Write sense data to dbf
782 tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
789 op = tape_op_verbose[request->op];
792 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
793 irb->scsw.dstat,irb->scsw.cstat);
794 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
795 sptr = (unsigned int *) irb->ecw;
796 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
797 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
798 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
799 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
803 * I/O helper function. Adds the request to the request queue
804 * and starts it if the tape is idle. Has to be called with
805 * the device lock held.
808 __tape_do_io(struct tape_device *device, struct tape_request *request)
812 switch (request->op) {
817 if (device->tape_state == TS_INIT)
819 if (device->tape_state == TS_UNUSED)
822 if (device->tape_state == TS_BLKUSE)
824 if (device->tape_state != TS_IN_USE)
828 /* Increase use count of device for the added request. */
829 request->device = tape_get_device_reference(device);
831 if (list_empty(&device->req_queue)) {
832 /* No other requests are on the queue. Start this one. */
833 #ifdef CONFIG_S390_TAPE_BLOCK
834 if (request->op == TO_BLOCK)
835 device->discipline->check_locate(device, request);
837 rc = ccw_device_start(device->cdev, request->cpaddr,
838 (unsigned long) request, 0x00,
841 DBF_EVENT(1, "tape: DOIO failed with rc = %i\n", rc);
844 DBF_LH(5, "Request %p added for execution.\n", request);
845 list_add(&request->list, &device->req_queue);
846 request->status = TAPE_REQUEST_IN_IO;
848 DBF_LH(5, "Request %p add to queue.\n", request);
849 list_add_tail(&request->list, &device->req_queue);
850 request->status = TAPE_REQUEST_QUEUED;
856 * Add the request to the request queue, try to start it if the
857 * tape is idle. Return without waiting for end of i/o.
860 tape_do_io_async(struct tape_device *device, struct tape_request *request)
864 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
866 spin_lock_irq(get_ccwdev_lock(device->cdev));
867 /* Add request to request queue and try to start it. */
868 rc = __tape_do_io(device, request);
869 spin_unlock_irq(get_ccwdev_lock(device->cdev));
874 * tape_do_io/__tape_wake_up
875 * Add the request to the request queue, try to start it if the
876 * tape is idle and wait uninterruptible for its completion.
879 __tape_wake_up(struct tape_request *request, void *data)
881 request->callback = NULL;
882 wake_up((wait_queue_head_t *) data);
886 tape_do_io(struct tape_device *device, struct tape_request *request)
888 wait_queue_head_t wq;
891 init_waitqueue_head(&wq);
892 spin_lock_irq(get_ccwdev_lock(device->cdev));
894 request->callback = __tape_wake_up;
895 request->callback_data = &wq;
896 /* Add request to request queue and try to start it. */
897 rc = __tape_do_io(device, request);
898 spin_unlock_irq(get_ccwdev_lock(device->cdev));
901 /* Request added to the queue. Wait for its completion. */
902 wait_event(wq, (request->callback == NULL));
903 /* Get rc from request */
908 * tape_do_io_interruptible/__tape_wake_up_interruptible
909 * Add the request to the request queue, try to start it if the
910 * tape is idle and wait uninterruptible for its completion.
913 __tape_wake_up_interruptible(struct tape_request *request, void *data)
915 request->callback = NULL;
916 wake_up_interruptible((wait_queue_head_t *) data);
920 tape_do_io_interruptible(struct tape_device *device,
921 struct tape_request *request)
923 wait_queue_head_t wq;
926 init_waitqueue_head(&wq);
927 spin_lock_irq(get_ccwdev_lock(device->cdev));
929 request->callback = __tape_wake_up_interruptible;
930 request->callback_data = &wq;
931 rc = __tape_do_io(device, request);
932 spin_unlock_irq(get_ccwdev_lock(device->cdev));
935 /* Request added to the queue. Wait for its completion. */
936 rc = wait_event_interruptible(wq, (request->callback == NULL));
937 if (rc != -ERESTARTSYS)
938 /* Request finished normally. */
940 /* Interrupted by a signal. We have to stop the current request. */
941 spin_lock_irq(get_ccwdev_lock(device->cdev));
942 rc = __tape_halt_io(device, request);
944 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
947 spin_unlock_irq(get_ccwdev_lock(device->cdev));
952 * Handle requests that return an i/o error in the irb.
955 tape_handle_killed_request(
956 struct tape_device *device,
957 struct tape_request *request)
959 if(request != NULL) {
960 /* Set ending status. FIXME: Should the request be retried? */
962 request->status = TAPE_REQUEST_DONE;
963 __tape_remove_request(device, request);
965 __tape_do_io_list(device);
970 * Tape interrupt routine, called from the ccw_device layer
973 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
975 struct tape_device *device;
976 struct tape_request *request;
980 device = (struct tape_device *) cdev->dev.driver_data;
981 if (device == NULL) {
982 PRINT_ERR("could not get device structure for %s "
983 "in interrupt\n", cdev->dev.bus_id);
986 request = (struct tape_request *) intparm;
988 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
990 /* On special conditions irb is an error pointer */
992 switch (PTR_ERR(irb)) {
994 PRINT_WARN("(%s): Request timed out\n",
997 tape_handle_killed_request(device, request);
1000 PRINT_ERR("(%s): Unexpected i/o error %li\n",
1007 /* May be an unsolicited irq */
1009 request->rescnt = irb->scsw.count;
1011 if (irb->scsw.dstat != 0x0c) {
1012 /* Set the 'ONLINE' flag depending on sense byte 1 */
1013 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1014 device->tape_generic_status |= GMT_ONLINE(~0);
1016 device->tape_generic_status &= ~GMT_ONLINE(~0);
1019 * Any request that does not come back with channel end
1020 * and device end is unusual. Log the sense data.
1022 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1023 tape_dump_sense_dbf(device, request, irb);
1025 /* Upon normal completion the device _is_ online */
1026 device->tape_generic_status |= GMT_ONLINE(~0);
1028 if (device->tape_state == TS_NOT_OPER) {
1029 DBF_EVENT(6, "tape:device is not operational\n");
1034 * Request that were canceled still come back with an interrupt.
1035 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1037 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
1038 __tape_remove_request(device, request);
1042 rc = device->discipline->irq(device, request, irb);
1044 * rc < 0 : request finished unsuccessfully.
1045 * rc == TAPE_IO_SUCCESS: request finished successfully.
1046 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1047 * rc == TAPE_IO_RETRY: request finished but needs another go.
1048 * rc == TAPE_IO_STOP: request needs to get terminated.
1052 case TAPE_IO_SUCCESS:
1053 /* Upon normal completion the device _is_ online */
1054 device->tape_generic_status |= GMT_ONLINE(~0);
1057 case TAPE_IO_PENDING:
1060 #ifdef CONFIG_S390_TAPE_BLOCK
1061 if (request->op == TO_BLOCK)
1062 device->discipline->check_locate(device, request);
1064 rc = ccw_device_start(cdev, request->cpaddr,
1065 (unsigned long) request, 0x00,
1068 DBF_EVENT(1, "tape: DOIO failed with er = %i\n", rc);
1073 __tape_halt_io(device, request);
1077 DBF_EVENT(6, "xunknownrc\n");
1078 PRINT_ERR("Invalid return code from discipline "
1079 "interrupt function.\n");
1086 /* May be an unsolicited irq */
1087 if(request != NULL) {
1088 /* Set ending status. */
1090 request->status = TAPE_REQUEST_DONE;
1091 __tape_remove_request(device, request);
1093 __tape_do_io_list(device);
1099 * Tape device open function used by tape_char & tape_block frontends.
1102 tape_open(struct tape_device *device)
1106 spin_lock(get_ccwdev_lock(device->cdev));
1107 if (device->tape_state == TS_NOT_OPER) {
1108 DBF_EVENT(6, "TAPE:nodev\n");
1110 } else if (device->tape_state == TS_IN_USE) {
1111 DBF_EVENT(6, "TAPE:dbusy\n");
1113 } else if (device->tape_state == TS_BLKUSE) {
1114 DBF_EVENT(6, "TAPE:dbusy\n");
1116 } else if (device->discipline != NULL &&
1117 !try_module_get(device->discipline->owner)) {
1118 DBF_EVENT(6, "TAPE:nodisc\n");
1121 tape_state_set(device, TS_IN_USE);
1124 spin_unlock(get_ccwdev_lock(device->cdev));
1129 * Tape device release function used by tape_char & tape_block frontends.
1132 tape_release(struct tape_device *device)
1134 spin_lock(get_ccwdev_lock(device->cdev));
1135 if (device->tape_state == TS_IN_USE)
1136 tape_state_set(device, TS_UNUSED);
1137 module_put(device->discipline->owner);
1138 spin_unlock(get_ccwdev_lock(device->cdev));
1143 * Execute a magnetic tape command a number of times.
1146 tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1151 DBF_EVENT(6, "TAPE:mtio\n");
1152 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1153 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1155 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1157 fn = device->discipline->mtop_array[mt_op];
1161 /* We assume that the backends can handle count up to 500. */
1162 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1163 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1165 for (; mt_count > 500; mt_count -= 500)
1166 if ((rc = fn(device, 500)) != 0)
1169 rc = fn(device, mt_count);
1171 rc = fn(device, mt_count);
1177 * Tape init function.
1182 TAPE_DBF_AREA = debug_register ( "tape", 1, 2, 4*sizeof(long));
1183 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1184 #ifdef DBF_LIKE_HELL
1185 debug_set_level(TAPE_DBF_AREA, 6);
1187 DBF_EVENT(3, "tape init: ($Revision: 1.50 $)\n");
1195 * Tape exit function.
1200 DBF_EVENT(6, "tape exit\n");
1202 /* Get rid of the frontends */
1205 tape_proc_cleanup();
1206 debug_unregister (TAPE_DBF_AREA);
1209 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1210 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1211 MODULE_DESCRIPTION("Linux on zSeries channel attached "
1212 "tape device driver ($Revision: 1.50 $)");
1213 MODULE_LICENSE("GPL");
1215 module_init(tape_init);
1216 module_exit(tape_exit);
1218 EXPORT_SYMBOL(tape_generic_remove);
1219 EXPORT_SYMBOL(tape_generic_probe);
1220 EXPORT_SYMBOL(tape_generic_online);
1221 EXPORT_SYMBOL(tape_generic_offline);
1222 EXPORT_SYMBOL(tape_put_device);
1223 EXPORT_SYMBOL(tape_get_device_reference);
1224 EXPORT_SYMBOL(tape_state_verbose);
1225 EXPORT_SYMBOL(tape_op_verbose);
1226 EXPORT_SYMBOL(tape_state_set);
1227 EXPORT_SYMBOL(tape_med_state_set);
1228 EXPORT_SYMBOL(tape_alloc_request);
1229 EXPORT_SYMBOL(tape_free_request);
1230 EXPORT_SYMBOL(tape_dump_sense);
1231 EXPORT_SYMBOL(tape_dump_sense_dbf);
1232 EXPORT_SYMBOL(tape_do_io);
1233 EXPORT_SYMBOL(tape_do_io_async);
1234 EXPORT_SYMBOL(tape_do_io_interruptible);
1235 EXPORT_SYMBOL(tape_mtop);