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/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_delayed_next_request(void * data);
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 DEFINE_RWLOCK(tape_device_lock);
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, struct device_attribute *attr, 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, struct device_attribute *attr, 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, struct device_attribute *attr, 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, struct device_attribute *attr, 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, struct device_attribute *attr, 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 DBF_EVENT(4, "old ts:\t\n");
213 if (device->tape_state < TS_SIZE && device->tape_state >=0 )
214 str = tape_state_verbose[device->tape_state];
217 DBF_EVENT(4, "%s\n", str);
218 DBF_EVENT(4, "new ts:\t\n");
219 if (newstate < TS_SIZE && newstate >= 0)
220 str = tape_state_verbose[newstate];
223 DBF_EVENT(4, "%s\n", str);
224 device->tape_state = newstate;
225 wake_up(&device->state_change_wq);
229 tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
231 if (device->medium_state == newstate)
235 device->tape_generic_status |= GMT_DR_OPEN(~0);
236 PRINT_INFO("(%s): Tape is unloaded\n",
237 device->cdev->dev.bus_id);
240 device->tape_generic_status &= ~GMT_DR_OPEN(~0);
241 PRINT_INFO("(%s): Tape has been mounted\n",
242 device->cdev->dev.bus_id);
248 device->medium_state = newstate;
249 wake_up(&device->state_change_wq);
253 * Stop running ccw. Has to be called with the device lock held.
256 __tape_cancel_io(struct tape_device *device, struct tape_request *request)
261 /* Check if interrupt has already been processed */
262 if (request->callback == NULL)
266 for (retries = 0; retries < 5; retries++) {
267 rc = ccw_device_clear(device->cdev, (long) request);
271 request->status = TAPE_REQUEST_DONE;
274 request->status = TAPE_REQUEST_CANCEL;
275 schedule_work(&device->tape_dnr);
278 DBF_EXCEPTION(2, "device gone, retry\n");
281 DBF_EXCEPTION(2, "I/O error, retry\n");
292 * Add device into the sorted list, giving it the first
293 * available minor number.
296 tape_assign_minor(struct tape_device *device)
298 struct tape_device *tmp;
302 write_lock(&tape_device_lock);
303 list_for_each_entry(tmp, &tape_device_list, node) {
304 if (minor < tmp->first_minor)
306 minor += TAPE_MINORS_PER_DEV;
309 write_unlock(&tape_device_lock);
312 device->first_minor = minor;
313 list_add_tail(&device->node, &tmp->node);
314 write_unlock(&tape_device_lock);
318 /* remove device from the list */
320 tape_remove_minor(struct tape_device *device)
322 write_lock(&tape_device_lock);
323 list_del_init(&device->node);
324 device->first_minor = -1;
325 write_unlock(&tape_device_lock);
329 * Set a device online.
331 * This function is called by the common I/O layer to move a device from the
332 * detected but offline into the online state.
333 * If we return an error (RC < 0) the device remains in the offline state. This
334 * can happen if the device is assigned somewhere else, for example.
337 tape_generic_online(struct tape_device *device,
338 struct tape_discipline *discipline)
342 DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
344 if (device->tape_state != TS_INIT) {
345 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
349 /* Let the discipline have a go at the device. */
350 device->discipline = discipline;
351 if (!try_module_get(discipline->owner)) {
352 PRINT_ERR("Cannot get module. Module gone.\n");
356 rc = discipline->setup_device(device);
359 rc = tape_assign_minor(device);
363 rc = tapechar_setup_device(device);
366 rc = tapeblock_setup_device(device);
370 tape_state_set(device, TS_UNUSED);
372 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
377 tapechar_cleanup_device(device);
379 device->discipline->cleanup_device(device);
380 device->discipline = NULL;
382 tape_remove_minor(device);
384 module_put(discipline->owner);
389 tape_cleanup_device(struct tape_device *device)
391 tapeblock_cleanup_device(device);
392 tapechar_cleanup_device(device);
393 device->discipline->cleanup_device(device);
394 module_put(device->discipline->owner);
395 tape_remove_minor(device);
396 tape_med_state_set(device, MS_UNKNOWN);
400 * Set device offline.
402 * Called by the common I/O layer if the drive should set offline on user
403 * request. We may prevent this by returning an error.
404 * Manual offline is only allowed while the drive is not in use.
407 tape_generic_offline(struct tape_device *device)
410 PRINT_ERR("tape_generic_offline: no such device\n");
414 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
415 device->cdev_id, device);
417 spin_lock_irq(get_ccwdev_lock(device->cdev));
418 switch (device->tape_state) {
421 spin_unlock_irq(get_ccwdev_lock(device->cdev));
424 tape_state_set(device, TS_INIT);
425 spin_unlock_irq(get_ccwdev_lock(device->cdev));
426 tape_cleanup_device(device);
429 DBF_EVENT(3, "(%08x): Set offline failed "
432 PRINT_WARN("(%s): Set offline failed "
434 device->cdev->dev.bus_id);
435 spin_unlock_irq(get_ccwdev_lock(device->cdev));
439 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
444 * Allocate memory for a new device structure.
446 static struct tape_device *
447 tape_alloc_device(void)
449 struct tape_device *device;
451 device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
452 if (device == NULL) {
453 DBF_EXCEPTION(2, "ti:no mem\n");
454 PRINT_INFO ("can't allocate memory for "
455 "tape info structure\n");
456 return ERR_PTR(-ENOMEM);
458 device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
459 if (device->modeset_byte == NULL) {
460 DBF_EXCEPTION(2, "ti:no mem\n");
461 PRINT_INFO("can't allocate memory for modeset byte\n");
463 return ERR_PTR(-ENOMEM);
465 INIT_LIST_HEAD(&device->req_queue);
466 INIT_LIST_HEAD(&device->node);
467 init_waitqueue_head(&device->state_change_wq);
468 device->tape_state = TS_INIT;
469 device->medium_state = MS_UNKNOWN;
470 *device->modeset_byte = 0;
471 device->first_minor = -1;
472 atomic_set(&device->ref_count, 1);
473 INIT_WORK(&device->tape_dnr, tape_delayed_next_request, device);
479 * Get a reference to an existing device structure. This will automatically
480 * increment the reference count.
483 tape_get_device_reference(struct tape_device *device)
485 DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device,
486 atomic_inc_return(&device->ref_count));
492 * Decrease the reference counter of a devices structure. If the
493 * reference counter reaches zero free the device structure.
494 * The function returns a NULL pointer to be used by the caller
495 * for clearing reference pointers.
498 tape_put_device(struct tape_device *device)
502 remain = atomic_dec_return(&device->ref_count);
504 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain);
507 DBF_EVENT(4, "put device without reference\n");
508 PRINT_ERR("put device without reference\n");
510 DBF_EVENT(4, "tape_free_device(%p)\n", device);
511 kfree(device->modeset_byte);
520 * Find tape device by a device index.
523 tape_get_device(int devindex)
525 struct tape_device *device, *tmp;
527 device = ERR_PTR(-ENODEV);
528 read_lock(&tape_device_lock);
529 list_for_each_entry(tmp, &tape_device_list, node) {
530 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
531 device = tape_get_device_reference(tmp);
535 read_unlock(&tape_device_lock);
540 * Driverfs tape probe function.
543 tape_generic_probe(struct ccw_device *cdev)
545 struct tape_device *device;
548 device = tape_alloc_device();
551 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
552 ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
554 tape_put_device(device);
555 PRINT_ERR("probe failed for tape device %s\n", cdev->dev.bus_id);
558 cdev->dev.driver_data = device;
559 cdev->handler = __tape_do_irq;
561 device->cdev_id = busid_to_int(cdev->dev.bus_id);
562 PRINT_INFO("tape device %s found\n", cdev->dev.bus_id);
567 __tape_discard_requests(struct tape_device *device)
569 struct tape_request * request;
570 struct list_head * l, *n;
572 list_for_each_safe(l, n, &device->req_queue) {
573 request = list_entry(l, struct tape_request, list);
574 if (request->status == TAPE_REQUEST_IN_IO)
575 request->status = TAPE_REQUEST_DONE;
576 list_del(&request->list);
578 /* Decrease ref_count for removed request. */
579 request->device = tape_put_device(device);
581 if (request->callback != NULL)
582 request->callback(request, request->callback_data);
587 * Driverfs tape remove function.
589 * This function is called whenever the common I/O layer detects the device
590 * gone. This can happen at any time and we cannot refuse.
593 tape_generic_remove(struct ccw_device *cdev)
595 struct tape_device * device;
597 device = cdev->dev.driver_data;
599 PRINT_ERR("No device pointer in tape_generic_remove!\n");
602 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
604 spin_lock_irq(get_ccwdev_lock(device->cdev));
605 switch (device->tape_state) {
607 tape_state_set(device, TS_NOT_OPER);
612 spin_unlock_irq(get_ccwdev_lock(device->cdev));
616 * Need only to release the device.
618 tape_state_set(device, TS_NOT_OPER);
619 spin_unlock_irq(get_ccwdev_lock(device->cdev));
620 tape_cleanup_device(device);
624 * There may be requests on the queue. We will not get
625 * an interrupt for a request that was running. So we
626 * just post them all as I/O errors.
628 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
630 PRINT_WARN("(%s): Drive in use vanished - "
632 device->cdev->dev.bus_id);
633 PRINT_WARN("State was %i\n", device->tape_state);
634 tape_state_set(device, TS_NOT_OPER);
635 __tape_discard_requests(device);
636 spin_unlock_irq(get_ccwdev_lock(device->cdev));
637 tape_cleanup_device(device);
640 if (cdev->dev.driver_data != NULL) {
641 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
642 cdev->dev.driver_data = tape_put_device(cdev->dev.driver_data);
647 * Allocate a new tape ccw request
649 struct tape_request *
650 tape_alloc_request(int cplength, int datasize)
652 struct tape_request *request;
654 if (datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
657 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
659 request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
660 if (request == NULL) {
661 DBF_EXCEPTION(1, "cqra nomem\n");
662 return ERR_PTR(-ENOMEM);
664 /* allocate channel program */
666 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
667 GFP_ATOMIC | GFP_DMA);
668 if (request->cpaddr == NULL) {
669 DBF_EXCEPTION(1, "cqra nomem\n");
671 return ERR_PTR(-ENOMEM);
674 /* alloc small kernel buffer */
676 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
677 if (request->cpdata == NULL) {
678 DBF_EXCEPTION(1, "cqra nomem\n");
679 kfree(request->cpaddr);
681 return ERR_PTR(-ENOMEM);
684 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
691 * Free tape ccw request
694 tape_free_request (struct tape_request * request)
696 DBF_LH(6, "Free request %p\n", request);
698 if (request->device != NULL) {
699 request->device = tape_put_device(request->device);
701 kfree(request->cpdata);
702 kfree(request->cpaddr);
707 __tape_start_io(struct tape_device *device, struct tape_request *request)
711 #ifdef CONFIG_S390_TAPE_BLOCK
712 if (request->op == TO_BLOCK)
713 device->discipline->check_locate(device, request);
715 rc = ccw_device_start(
718 (unsigned long) request,
723 request->status = TAPE_REQUEST_IN_IO;
724 } else if (rc == -EBUSY) {
725 /* The common I/O subsystem is currently busy. Retry later. */
726 request->status = TAPE_REQUEST_QUEUED;
727 schedule_work(&device->tape_dnr);
730 /* Start failed. Remove request and indicate failure. */
731 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
737 __tape_start_next_request(struct tape_device *device)
739 struct list_head *l, *n;
740 struct tape_request *request;
743 DBF_LH(6, "__tape_start_next_request(%p)\n", device);
745 * Try to start each request on request queue until one is
746 * started successful.
748 list_for_each_safe(l, n, &device->req_queue) {
749 request = list_entry(l, struct tape_request, list);
752 * Avoid race condition if bottom-half was triggered more than
755 if (request->status == TAPE_REQUEST_IN_IO)
758 * Request has already been stopped. We have to wait until
759 * the request is removed from the queue in the interrupt
762 if (request->status == TAPE_REQUEST_DONE)
766 * We wanted to cancel the request but the common I/O layer
767 * was busy at that time. This can only happen if this
768 * function is called by delayed_next_request.
769 * Otherwise we start the next request on the queue.
771 if (request->status == TAPE_REQUEST_CANCEL) {
772 rc = __tape_cancel_io(device, request);
774 rc = __tape_start_io(device, request);
779 /* Set ending status. */
781 request->status = TAPE_REQUEST_DONE;
783 /* Remove from request queue. */
784 list_del(&request->list);
787 if (request->callback != NULL)
788 request->callback(request, request->callback_data);
793 tape_delayed_next_request(void *data)
795 struct tape_device * device;
797 device = (struct tape_device *) data;
798 DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
799 spin_lock_irq(get_ccwdev_lock(device->cdev));
800 __tape_start_next_request(device);
801 spin_unlock_irq(get_ccwdev_lock(device->cdev));
806 struct tape_device * device,
807 struct tape_request * request,
810 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
813 request->status = TAPE_REQUEST_DONE;
815 /* Remove from request queue. */
816 list_del(&request->list);
819 if (request->callback != NULL)
820 request->callback(request, request->callback_data);
823 /* Start next request. */
824 if (!list_empty(&device->req_queue))
825 __tape_start_next_request(device);
829 * Write sense data to console/dbf
832 tape_dump_sense(struct tape_device* device, struct tape_request *request,
837 PRINT_INFO("-------------------------------------------------\n");
838 PRINT_INFO("DSTAT : %02x CSTAT: %02x CPA: %04x\n",
839 irb->scsw.dstat, irb->scsw.cstat, irb->scsw.cpa);
840 PRINT_INFO("DEVICE: %s\n", device->cdev->dev.bus_id);
842 PRINT_INFO("OP : %s\n", tape_op_verbose[request->op]);
844 sptr = (unsigned int *) irb->ecw;
845 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
846 sptr[0], sptr[1], sptr[2], sptr[3]);
847 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
848 sptr[4], sptr[5], sptr[6], sptr[7]);
849 PRINT_INFO("--------------------------------------------------\n");
853 * Write sense data to dbf
856 tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
863 op = tape_op_verbose[request->op];
866 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
867 irb->scsw.dstat,irb->scsw.cstat);
868 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
869 sptr = (unsigned int *) irb->ecw;
870 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
871 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
872 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
873 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
877 * I/O helper function. Adds the request to the request queue
878 * and starts it if the tape is idle. Has to be called with
879 * the device lock held.
882 __tape_start_request(struct tape_device *device, struct tape_request *request)
886 switch (request->op) {
891 if (device->tape_state == TS_INIT)
893 if (device->tape_state == TS_UNUSED)
896 if (device->tape_state == TS_BLKUSE)
898 if (device->tape_state != TS_IN_USE)
902 /* Increase use count of device for the added request. */
903 request->device = tape_get_device_reference(device);
905 if (list_empty(&device->req_queue)) {
906 /* No other requests are on the queue. Start this one. */
907 rc = __tape_start_io(device, request);
911 DBF_LH(5, "Request %p added for execution.\n", request);
912 list_add(&request->list, &device->req_queue);
914 DBF_LH(5, "Request %p add to queue.\n", request);
915 request->status = TAPE_REQUEST_QUEUED;
916 list_add_tail(&request->list, &device->req_queue);
922 * Add the request to the request queue, try to start it if the
923 * tape is idle. Return without waiting for end of i/o.
926 tape_do_io_async(struct tape_device *device, struct tape_request *request)
930 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
932 spin_lock_irq(get_ccwdev_lock(device->cdev));
933 /* Add request to request queue and try to start it. */
934 rc = __tape_start_request(device, request);
935 spin_unlock_irq(get_ccwdev_lock(device->cdev));
940 * tape_do_io/__tape_wake_up
941 * Add the request to the request queue, try to start it if the
942 * tape is idle and wait uninterruptible for its completion.
945 __tape_wake_up(struct tape_request *request, void *data)
947 request->callback = NULL;
948 wake_up((wait_queue_head_t *) data);
952 tape_do_io(struct tape_device *device, struct tape_request *request)
954 wait_queue_head_t wq;
957 init_waitqueue_head(&wq);
958 spin_lock_irq(get_ccwdev_lock(device->cdev));
960 request->callback = __tape_wake_up;
961 request->callback_data = &wq;
962 /* Add request to request queue and try to start it. */
963 rc = __tape_start_request(device, request);
964 spin_unlock_irq(get_ccwdev_lock(device->cdev));
967 /* Request added to the queue. Wait for its completion. */
968 wait_event(wq, (request->callback == NULL));
969 /* Get rc from request */
974 * tape_do_io_interruptible/__tape_wake_up_interruptible
975 * Add the request to the request queue, try to start it if the
976 * tape is idle and wait uninterruptible for its completion.
979 __tape_wake_up_interruptible(struct tape_request *request, void *data)
981 request->callback = NULL;
982 wake_up_interruptible((wait_queue_head_t *) data);
986 tape_do_io_interruptible(struct tape_device *device,
987 struct tape_request *request)
989 wait_queue_head_t wq;
992 init_waitqueue_head(&wq);
993 spin_lock_irq(get_ccwdev_lock(device->cdev));
995 request->callback = __tape_wake_up_interruptible;
996 request->callback_data = &wq;
997 rc = __tape_start_request(device, request);
998 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1001 /* Request added to the queue. Wait for its completion. */
1002 rc = wait_event_interruptible(wq, (request->callback == NULL));
1003 if (rc != -ERESTARTSYS)
1004 /* Request finished normally. */
1007 /* Interrupted by a signal. We have to stop the current request. */
1008 spin_lock_irq(get_ccwdev_lock(device->cdev));
1009 rc = __tape_cancel_io(device, request);
1010 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1012 /* Wait for the interrupt that acknowledges the halt. */
1014 rc = wait_event_interruptible(
1016 (request->callback == NULL)
1018 } while (rc == -ERESTARTSYS);
1020 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
1030 tape_cancel_io(struct tape_device *device, struct tape_request *request)
1034 spin_lock_irq(get_ccwdev_lock(device->cdev));
1035 rc = __tape_cancel_io(device, request);
1036 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1041 * Tape interrupt routine, called from the ccw_device layer
1044 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1046 struct tape_device *device;
1047 struct tape_request *request;
1050 device = (struct tape_device *) cdev->dev.driver_data;
1051 if (device == NULL) {
1052 PRINT_ERR("could not get device structure for %s "
1053 "in interrupt\n", cdev->dev.bus_id);
1056 request = (struct tape_request *) intparm;
1058 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1060 /* On special conditions irb is an error pointer */
1062 /* FIXME: What to do with the request? */
1063 switch (PTR_ERR(irb)) {
1065 PRINT_WARN("(%s): Request timed out\n",
1068 __tape_end_request(device, request, -EIO);
1071 PRINT_ERR("(%s): Unexpected i/o error %li\n",
1079 * If the condition code is not zero and the start function bit is
1080 * still set, this is an deferred error and the last start I/O did
1081 * not succeed. At this point the condition that caused the deferred
1082 * error might still apply. So we just schedule the request to be
1085 if (irb->scsw.cc != 0 && (irb->scsw.fctl & SCSW_FCTL_START_FUNC) &&
1086 (request->status == TAPE_REQUEST_IN_IO)) {
1087 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1088 device->cdev_id, irb->scsw.cc, irb->scsw.fctl);
1089 request->status = TAPE_REQUEST_QUEUED;
1090 schedule_delayed_work(&device->tape_dnr, HZ);
1094 /* May be an unsolicited irq */
1096 request->rescnt = irb->scsw.count;
1098 if (irb->scsw.dstat != 0x0c) {
1099 /* Set the 'ONLINE' flag depending on sense byte 1 */
1100 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1101 device->tape_generic_status |= GMT_ONLINE(~0);
1103 device->tape_generic_status &= ~GMT_ONLINE(~0);
1106 * Any request that does not come back with channel end
1107 * and device end is unusual. Log the sense data.
1109 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1110 tape_dump_sense_dbf(device, request, irb);
1112 /* Upon normal completion the device _is_ online */
1113 device->tape_generic_status |= GMT_ONLINE(~0);
1115 if (device->tape_state == TS_NOT_OPER) {
1116 DBF_EVENT(6, "tape:device is not operational\n");
1121 * Request that were canceled still come back with an interrupt.
1122 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1124 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
1125 __tape_end_request(device, request, -EIO);
1129 rc = device->discipline->irq(device, request, irb);
1131 * rc < 0 : request finished unsuccessfully.
1132 * rc == TAPE_IO_SUCCESS: request finished successfully.
1133 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1134 * rc == TAPE_IO_RETRY: request finished but needs another go.
1135 * rc == TAPE_IO_STOP: request needs to get terminated.
1138 case TAPE_IO_SUCCESS:
1139 /* Upon normal completion the device _is_ online */
1140 device->tape_generic_status |= GMT_ONLINE(~0);
1141 __tape_end_request(device, request, rc);
1143 case TAPE_IO_PENDING:
1146 rc = __tape_start_io(device, request);
1148 __tape_end_request(device, request, rc);
1151 rc = __tape_cancel_io(device, request);
1153 __tape_end_request(device, request, rc);
1157 DBF_EVENT(6, "xunknownrc\n");
1158 PRINT_ERR("Invalid return code from discipline "
1159 "interrupt function.\n");
1160 __tape_end_request(device, request, -EIO);
1162 __tape_end_request(device, request, rc);
1169 * Tape device open function used by tape_char & tape_block frontends.
1172 tape_open(struct tape_device *device)
1176 spin_lock(get_ccwdev_lock(device->cdev));
1177 if (device->tape_state == TS_NOT_OPER) {
1178 DBF_EVENT(6, "TAPE:nodev\n");
1180 } else if (device->tape_state == TS_IN_USE) {
1181 DBF_EVENT(6, "TAPE:dbusy\n");
1183 } else if (device->tape_state == TS_BLKUSE) {
1184 DBF_EVENT(6, "TAPE:dbusy\n");
1186 } else if (device->discipline != NULL &&
1187 !try_module_get(device->discipline->owner)) {
1188 DBF_EVENT(6, "TAPE:nodisc\n");
1191 tape_state_set(device, TS_IN_USE);
1194 spin_unlock(get_ccwdev_lock(device->cdev));
1199 * Tape device release function used by tape_char & tape_block frontends.
1202 tape_release(struct tape_device *device)
1204 spin_lock(get_ccwdev_lock(device->cdev));
1205 if (device->tape_state == TS_IN_USE)
1206 tape_state_set(device, TS_UNUSED);
1207 module_put(device->discipline->owner);
1208 spin_unlock(get_ccwdev_lock(device->cdev));
1213 * Execute a magnetic tape command a number of times.
1216 tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1221 DBF_EVENT(6, "TAPE:mtio\n");
1222 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1223 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1225 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1227 fn = device->discipline->mtop_array[mt_op];
1231 /* We assume that the backends can handle count up to 500. */
1232 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1233 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1235 for (; mt_count > 500; mt_count -= 500)
1236 if ((rc = fn(device, 500)) != 0)
1239 rc = fn(device, mt_count);
1241 rc = fn(device, mt_count);
1247 * Tape init function.
1252 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
1253 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1254 #ifdef DBF_LIKE_HELL
1255 debug_set_level(TAPE_DBF_AREA, 6);
1257 DBF_EVENT(3, "tape init\n");
1265 * Tape exit function.
1270 DBF_EVENT(6, "tape exit\n");
1272 /* Get rid of the frontends */
1275 tape_proc_cleanup();
1276 debug_unregister (TAPE_DBF_AREA);
1279 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1280 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1281 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1282 MODULE_LICENSE("GPL");
1284 module_init(tape_init);
1285 module_exit(tape_exit);
1287 EXPORT_SYMBOL(tape_generic_remove);
1288 EXPORT_SYMBOL(tape_generic_probe);
1289 EXPORT_SYMBOL(tape_generic_online);
1290 EXPORT_SYMBOL(tape_generic_offline);
1291 EXPORT_SYMBOL(tape_put_device);
1292 EXPORT_SYMBOL(tape_get_device_reference);
1293 EXPORT_SYMBOL(tape_state_verbose);
1294 EXPORT_SYMBOL(tape_op_verbose);
1295 EXPORT_SYMBOL(tape_state_set);
1296 EXPORT_SYMBOL(tape_med_state_set);
1297 EXPORT_SYMBOL(tape_alloc_request);
1298 EXPORT_SYMBOL(tape_free_request);
1299 EXPORT_SYMBOL(tape_dump_sense);
1300 EXPORT_SYMBOL(tape_dump_sense_dbf);
1301 EXPORT_SYMBOL(tape_do_io);
1302 EXPORT_SYMBOL(tape_do_io_async);
1303 EXPORT_SYMBOL(tape_do_io_interruptible);
1304 EXPORT_SYMBOL(tape_cancel_io);
1305 EXPORT_SYMBOL(tape_mtop);