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, 2009
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 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17 #include <linux/module.h>
18 #include <linux/init.h> // for kernel parameters
19 #include <linux/kmod.h> // for requesting modules
20 #include <linux/spinlock.h> // for locks
21 #include <linux/vmalloc.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
25 #include <asm/types.h> // for variable types
27 #define TAPE_DBF_AREA tape_core_dbf
32 #define LONG_BUSY_TIMEOUT 180 /* seconds */
34 static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *);
35 static void tape_delayed_next_request(struct work_struct *);
36 static void tape_long_busy_timeout(unsigned long data);
39 * One list to contain all tape devices of all disciplines, so
40 * we can assign the devices to minor numbers of the same major
41 * The list is protected by the rwlock
43 static LIST_HEAD(tape_device_list);
44 static DEFINE_RWLOCK(tape_device_lock);
47 * Pointer to debug area.
49 debug_info_t *TAPE_DBF_AREA = NULL;
50 EXPORT_SYMBOL(TAPE_DBF_AREA);
53 * Printable strings for tape enumerations.
55 const char *tape_state_verbose[TS_SIZE] =
57 [TS_UNUSED] = "UNUSED",
58 [TS_IN_USE] = "IN_USE",
59 [TS_BLKUSE] = "BLKUSE",
61 [TS_NOT_OPER] = "NOT_OP"
64 const char *tape_op_verbose[TO_SIZE] =
66 [TO_BLOCK] = "BLK", [TO_BSB] = "BSB",
67 [TO_BSF] = "BSF", [TO_DSE] = "DSE",
68 [TO_FSB] = "FSB", [TO_FSF] = "FSF",
69 [TO_LBL] = "LBL", [TO_NOP] = "NOP",
70 [TO_RBA] = "RBA", [TO_RBI] = "RBI",
71 [TO_RFO] = "RFO", [TO_REW] = "REW",
72 [TO_RUN] = "RUN", [TO_WRI] = "WRI",
73 [TO_WTM] = "WTM", [TO_MSEN] = "MSN",
74 [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF",
75 [TO_READ_ATTMSG] = "RAT",
76 [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS",
77 [TO_UNASSIGN] = "UAS", [TO_CRYPT_ON] = "CON",
78 [TO_CRYPT_OFF] = "COF", [TO_KEKL_SET] = "KLS",
79 [TO_KEKL_QUERY] = "KLQ",[TO_RDC] = "RDC",
82 static int devid_to_int(struct ccw_dev_id *dev_id)
84 return dev_id->devno + (dev_id->ssid << 16);
88 * Some channel attached tape specific attributes.
90 * FIXME: In the future the first_minor and blocksize attribute should be
91 * replaced by a link to the cdev tree.
94 tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf)
96 struct tape_device *tdev;
98 tdev = dev_get_drvdata(dev);
99 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state);
103 DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
106 tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf)
108 struct tape_device *tdev;
110 tdev = dev_get_drvdata(dev);
111 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor);
115 DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
118 tape_state_show(struct device *dev, struct device_attribute *attr, char *buf)
120 struct tape_device *tdev;
122 tdev = dev_get_drvdata(dev);
123 return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ?
124 "OFFLINE" : tape_state_verbose[tdev->tape_state]);
128 DEVICE_ATTR(state, 0444, tape_state_show, NULL);
131 tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf)
133 struct tape_device *tdev;
136 tdev = dev_get_drvdata(dev);
137 if (tdev->first_minor < 0)
138 return scnprintf(buf, PAGE_SIZE, "N/A\n");
140 spin_lock_irq(get_ccwdev_lock(tdev->cdev));
141 if (list_empty(&tdev->req_queue))
142 rc = scnprintf(buf, PAGE_SIZE, "---\n");
144 struct tape_request *req;
146 req = list_entry(tdev->req_queue.next, struct tape_request,
148 rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
150 spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
155 DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
158 tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf)
160 struct tape_device *tdev;
162 tdev = dev_get_drvdata(dev);
164 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size);
168 DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);
170 static struct attribute *tape_attrs[] = {
171 &dev_attr_medium_state.attr,
172 &dev_attr_first_minor.attr,
173 &dev_attr_state.attr,
174 &dev_attr_operation.attr,
175 &dev_attr_blocksize.attr,
179 static struct attribute_group tape_attr_group = {
184 * Tape state functions
187 tape_state_set(struct tape_device *device, enum tape_state newstate)
191 if (device->tape_state == TS_NOT_OPER) {
192 DBF_EVENT(3, "ts_set err: not oper\n");
195 DBF_EVENT(4, "ts. dev: %x\n", device->first_minor);
196 DBF_EVENT(4, "old ts:\t\n");
197 if (device->tape_state < TS_SIZE && device->tape_state >=0 )
198 str = tape_state_verbose[device->tape_state];
201 DBF_EVENT(4, "%s\n", str);
202 DBF_EVENT(4, "new ts:\t\n");
203 if (newstate < TS_SIZE && newstate >= 0)
204 str = tape_state_verbose[newstate];
207 DBF_EVENT(4, "%s\n", str);
208 device->tape_state = newstate;
209 wake_up(&device->state_change_wq);
212 struct tape_med_state_work_data {
213 struct tape_device *device;
214 enum tape_medium_state state;
215 struct work_struct work;
219 tape_med_state_work_handler(struct work_struct *work)
221 static char env_state_loaded[] = "MEDIUM_STATE=LOADED";
222 static char env_state_unloaded[] = "MEDIUM_STATE=UNLOADED";
223 struct tape_med_state_work_data *p =
224 container_of(work, struct tape_med_state_work_data, work);
225 struct tape_device *device = p->device;
226 char *envp[] = { NULL, NULL };
230 pr_info("%s: The tape cartridge has been successfully "
231 "unloaded\n", dev_name(&device->cdev->dev));
232 envp[0] = env_state_unloaded;
233 kobject_uevent_env(&device->cdev->dev.kobj, KOBJ_CHANGE, envp);
236 pr_info("%s: A tape cartridge has been mounted\n",
237 dev_name(&device->cdev->dev));
238 envp[0] = env_state_loaded;
239 kobject_uevent_env(&device->cdev->dev.kobj, KOBJ_CHANGE, envp);
244 tape_put_device(device);
249 tape_med_state_work(struct tape_device *device, enum tape_medium_state state)
251 struct tape_med_state_work_data *p;
253 p = kzalloc(sizeof(*p), GFP_ATOMIC);
255 INIT_WORK(&p->work, tape_med_state_work_handler);
256 p->device = tape_get_device(device);
258 schedule_work(&p->work);
263 tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
265 enum tape_medium_state oldstate;
267 oldstate = device->medium_state;
268 if (oldstate == newstate)
270 device->medium_state = newstate;
273 device->tape_generic_status |= GMT_DR_OPEN(~0);
274 if (oldstate == MS_LOADED)
275 tape_med_state_work(device, MS_UNLOADED);
278 device->tape_generic_status &= ~GMT_DR_OPEN(~0);
279 if (oldstate == MS_UNLOADED)
280 tape_med_state_work(device, MS_LOADED);
285 wake_up(&device->state_change_wq);
289 * Stop running ccw. Has to be called with the device lock held.
292 __tape_cancel_io(struct tape_device *device, struct tape_request *request)
297 /* Check if interrupt has already been processed */
298 if (request->callback == NULL)
302 for (retries = 0; retries < 5; retries++) {
303 rc = ccw_device_clear(device->cdev, (long) request);
307 request->status = TAPE_REQUEST_DONE;
310 request->status = TAPE_REQUEST_CANCEL;
311 schedule_delayed_work(&device->tape_dnr, 0);
314 DBF_EXCEPTION(2, "device gone, retry\n");
317 DBF_EXCEPTION(2, "I/O error, retry\n");
328 * Add device into the sorted list, giving it the first
329 * available minor number.
332 tape_assign_minor(struct tape_device *device)
334 struct tape_device *tmp;
338 write_lock(&tape_device_lock);
339 list_for_each_entry(tmp, &tape_device_list, node) {
340 if (minor < tmp->first_minor)
342 minor += TAPE_MINORS_PER_DEV;
345 write_unlock(&tape_device_lock);
348 device->first_minor = minor;
349 list_add_tail(&device->node, &tmp->node);
350 write_unlock(&tape_device_lock);
354 /* remove device from the list */
356 tape_remove_minor(struct tape_device *device)
358 write_lock(&tape_device_lock);
359 list_del_init(&device->node);
360 device->first_minor = -1;
361 write_unlock(&tape_device_lock);
365 * Set a device online.
367 * This function is called by the common I/O layer to move a device from the
368 * detected but offline into the online state.
369 * If we return an error (RC < 0) the device remains in the offline state. This
370 * can happen if the device is assigned somewhere else, for example.
373 tape_generic_online(struct tape_device *device,
374 struct tape_discipline *discipline)
378 DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
380 if (device->tape_state != TS_INIT) {
381 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
385 init_timer(&device->lb_timeout);
386 device->lb_timeout.function = tape_long_busy_timeout;
388 /* Let the discipline have a go at the device. */
389 device->discipline = discipline;
390 if (!try_module_get(discipline->owner)) {
394 rc = discipline->setup_device(device);
397 rc = tape_assign_minor(device);
401 rc = tapechar_setup_device(device);
405 tape_state_set(device, TS_UNUSED);
407 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
412 tape_remove_minor(device);
414 device->discipline->cleanup_device(device);
415 device->discipline = NULL;
417 module_put(discipline->owner);
422 tape_cleanup_device(struct tape_device *device)
424 tapechar_cleanup_device(device);
425 device->discipline->cleanup_device(device);
426 module_put(device->discipline->owner);
427 tape_remove_minor(device);
428 tape_med_state_set(device, MS_UNKNOWN);
434 * Called by the common I/O layer if the drive should be suspended on user
435 * request. We refuse to suspend if the device is loaded or in use for the
437 * While the Linux guest is suspended, it might be logged off which causes
438 * devices to be detached. Tape devices are automatically rewound and unloaded
439 * during DETACH processing (unless the tape device was attached with the
440 * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to
441 * resume the original state of the tape device, since we would need to
442 * manually re-load the cartridge which was active at suspend time.
444 int tape_generic_pm_suspend(struct ccw_device *cdev)
446 struct tape_device *device;
448 device = dev_get_drvdata(&cdev->dev);
453 DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n",
454 device->cdev_id, device);
456 if (device->medium_state != MS_UNLOADED) {
457 pr_err("A cartridge is loaded in tape device %s, "
458 "refusing to suspend\n", dev_name(&cdev->dev));
462 spin_lock_irq(get_ccwdev_lock(device->cdev));
463 switch (device->tape_state) {
467 spin_unlock_irq(get_ccwdev_lock(device->cdev));
470 pr_err("Tape device %s is busy, refusing to "
471 "suspend\n", dev_name(&cdev->dev));
472 spin_unlock_irq(get_ccwdev_lock(device->cdev));
476 DBF_LH(3, "(%08x): Drive suspended.\n", device->cdev_id);
481 * Set device offline.
483 * Called by the common I/O layer if the drive should set offline on user
484 * request. We may prevent this by returning an error.
485 * Manual offline is only allowed while the drive is not in use.
488 tape_generic_offline(struct ccw_device *cdev)
490 struct tape_device *device;
492 device = dev_get_drvdata(&cdev->dev);
497 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
498 device->cdev_id, device);
500 spin_lock_irq(get_ccwdev_lock(device->cdev));
501 switch (device->tape_state) {
504 spin_unlock_irq(get_ccwdev_lock(device->cdev));
507 tape_state_set(device, TS_INIT);
508 spin_unlock_irq(get_ccwdev_lock(device->cdev));
509 tape_cleanup_device(device);
512 DBF_EVENT(3, "(%08x): Set offline failed "
515 spin_unlock_irq(get_ccwdev_lock(device->cdev));
519 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
524 * Allocate memory for a new device structure.
526 static struct tape_device *
527 tape_alloc_device(void)
529 struct tape_device *device;
531 device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
532 if (device == NULL) {
533 DBF_EXCEPTION(2, "ti:no mem\n");
534 return ERR_PTR(-ENOMEM);
536 device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
537 if (device->modeset_byte == NULL) {
538 DBF_EXCEPTION(2, "ti:no mem\n");
540 return ERR_PTR(-ENOMEM);
542 mutex_init(&device->mutex);
543 INIT_LIST_HEAD(&device->req_queue);
544 INIT_LIST_HEAD(&device->node);
545 init_waitqueue_head(&device->state_change_wq);
546 init_waitqueue_head(&device->wait_queue);
547 device->tape_state = TS_INIT;
548 device->medium_state = MS_UNKNOWN;
549 *device->modeset_byte = 0;
550 device->first_minor = -1;
551 atomic_set(&device->ref_count, 1);
552 INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request);
558 * Get a reference to an existing device structure. This will automatically
559 * increment the reference count.
562 tape_get_device(struct tape_device *device)
566 count = atomic_inc_return(&device->ref_count);
567 DBF_EVENT(4, "tape_get_device(%p) = %i\n", device, count);
572 * Decrease the reference counter of a devices structure. If the
573 * reference counter reaches zero free the device structure.
574 * The function returns a NULL pointer to be used by the caller
575 * for clearing reference pointers.
578 tape_put_device(struct tape_device *device)
582 count = atomic_dec_return(&device->ref_count);
583 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, count);
586 kfree(device->modeset_byte);
592 * Find tape device by a device index.
595 tape_find_device(int devindex)
597 struct tape_device *device, *tmp;
599 device = ERR_PTR(-ENODEV);
600 read_lock(&tape_device_lock);
601 list_for_each_entry(tmp, &tape_device_list, node) {
602 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
603 device = tape_get_device(tmp);
607 read_unlock(&tape_device_lock);
612 * Driverfs tape probe function.
615 tape_generic_probe(struct ccw_device *cdev)
617 struct tape_device *device;
619 struct ccw_dev_id dev_id;
621 device = tape_alloc_device();
624 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP |
625 CCWDEV_DO_MULTIPATH);
626 ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
628 tape_put_device(device);
631 dev_set_drvdata(&cdev->dev, device);
632 cdev->handler = __tape_do_irq;
634 ccw_device_get_id(cdev, &dev_id);
635 device->cdev_id = devid_to_int(&dev_id);
640 __tape_discard_requests(struct tape_device *device)
642 struct tape_request * request;
643 struct list_head * l, *n;
645 list_for_each_safe(l, n, &device->req_queue) {
646 request = list_entry(l, struct tape_request, list);
647 if (request->status == TAPE_REQUEST_IN_IO)
648 request->status = TAPE_REQUEST_DONE;
649 list_del(&request->list);
651 /* Decrease ref_count for removed request. */
652 request->device = NULL;
653 tape_put_device(device);
655 if (request->callback != NULL)
656 request->callback(request, request->callback_data);
661 * Driverfs tape remove function.
663 * This function is called whenever the common I/O layer detects the device
664 * gone. This can happen at any time and we cannot refuse.
667 tape_generic_remove(struct ccw_device *cdev)
669 struct tape_device * device;
671 device = dev_get_drvdata(&cdev->dev);
675 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
677 spin_lock_irq(get_ccwdev_lock(device->cdev));
678 switch (device->tape_state) {
680 tape_state_set(device, TS_NOT_OPER);
685 spin_unlock_irq(get_ccwdev_lock(device->cdev));
689 * Need only to release the device.
691 tape_state_set(device, TS_NOT_OPER);
692 spin_unlock_irq(get_ccwdev_lock(device->cdev));
693 tape_cleanup_device(device);
697 * There may be requests on the queue. We will not get
698 * an interrupt for a request that was running. So we
699 * just post them all as I/O errors.
701 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
703 pr_warning("%s: A tape unit was detached while in "
704 "use\n", dev_name(&device->cdev->dev));
705 tape_state_set(device, TS_NOT_OPER);
706 __tape_discard_requests(device);
707 spin_unlock_irq(get_ccwdev_lock(device->cdev));
708 tape_cleanup_device(device);
711 device = dev_get_drvdata(&cdev->dev);
713 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
714 dev_set_drvdata(&cdev->dev, NULL);
715 tape_put_device(device);
720 * Allocate a new tape ccw request
722 struct tape_request *
723 tape_alloc_request(int cplength, int datasize)
725 struct tape_request *request;
727 BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
729 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
731 request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
732 if (request == NULL) {
733 DBF_EXCEPTION(1, "cqra nomem\n");
734 return ERR_PTR(-ENOMEM);
736 /* allocate channel program */
738 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
739 GFP_ATOMIC | GFP_DMA);
740 if (request->cpaddr == NULL) {
741 DBF_EXCEPTION(1, "cqra nomem\n");
743 return ERR_PTR(-ENOMEM);
746 /* alloc small kernel buffer */
748 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
749 if (request->cpdata == NULL) {
750 DBF_EXCEPTION(1, "cqra nomem\n");
751 kfree(request->cpaddr);
753 return ERR_PTR(-ENOMEM);
756 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
763 * Free tape ccw request
766 tape_free_request (struct tape_request * request)
768 DBF_LH(6, "Free request %p\n", request);
771 tape_put_device(request->device);
772 kfree(request->cpdata);
773 kfree(request->cpaddr);
778 __tape_start_io(struct tape_device *device, struct tape_request *request)
782 rc = ccw_device_start(
785 (unsigned long) request,
790 request->status = TAPE_REQUEST_IN_IO;
791 } else if (rc == -EBUSY) {
792 /* The common I/O subsystem is currently busy. Retry later. */
793 request->status = TAPE_REQUEST_QUEUED;
794 schedule_delayed_work(&device->tape_dnr, 0);
797 /* Start failed. Remove request and indicate failure. */
798 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
804 __tape_start_next_request(struct tape_device *device)
806 struct list_head *l, *n;
807 struct tape_request *request;
810 DBF_LH(6, "__tape_start_next_request(%p)\n", device);
812 * Try to start each request on request queue until one is
813 * started successful.
815 list_for_each_safe(l, n, &device->req_queue) {
816 request = list_entry(l, struct tape_request, list);
819 * Avoid race condition if bottom-half was triggered more than
822 if (request->status == TAPE_REQUEST_IN_IO)
825 * Request has already been stopped. We have to wait until
826 * the request is removed from the queue in the interrupt
829 if (request->status == TAPE_REQUEST_DONE)
833 * We wanted to cancel the request but the common I/O layer
834 * was busy at that time. This can only happen if this
835 * function is called by delayed_next_request.
836 * Otherwise we start the next request on the queue.
838 if (request->status == TAPE_REQUEST_CANCEL) {
839 rc = __tape_cancel_io(device, request);
841 rc = __tape_start_io(device, request);
846 /* Set ending status. */
848 request->status = TAPE_REQUEST_DONE;
850 /* Remove from request queue. */
851 list_del(&request->list);
854 if (request->callback != NULL)
855 request->callback(request, request->callback_data);
860 tape_delayed_next_request(struct work_struct *work)
862 struct tape_device *device =
863 container_of(work, struct tape_device, tape_dnr.work);
865 DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
866 spin_lock_irq(get_ccwdev_lock(device->cdev));
867 __tape_start_next_request(device);
868 spin_unlock_irq(get_ccwdev_lock(device->cdev));
871 static void tape_long_busy_timeout(unsigned long data)
873 struct tape_request *request;
874 struct tape_device *device;
876 device = (struct tape_device *) data;
877 spin_lock_irq(get_ccwdev_lock(device->cdev));
878 request = list_entry(device->req_queue.next, struct tape_request, list);
879 BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY);
880 DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
881 __tape_start_next_request(device);
882 device->lb_timeout.data = 0UL;
883 tape_put_device(device);
884 spin_unlock_irq(get_ccwdev_lock(device->cdev));
889 struct tape_device * device,
890 struct tape_request * request,
893 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
896 request->status = TAPE_REQUEST_DONE;
898 /* Remove from request queue. */
899 list_del(&request->list);
902 if (request->callback != NULL)
903 request->callback(request, request->callback_data);
906 /* Start next request. */
907 if (!list_empty(&device->req_queue))
908 __tape_start_next_request(device);
912 * Write sense data to dbf
915 tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
922 op = tape_op_verbose[request->op];
925 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
926 irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
927 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
928 sptr = (unsigned int *) irb->ecw;
929 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
930 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
931 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
932 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
936 * I/O helper function. Adds the request to the request queue
937 * and starts it if the tape is idle. Has to be called with
938 * the device lock held.
941 __tape_start_request(struct tape_device *device, struct tape_request *request)
945 switch (request->op) {
951 if (device->tape_state == TS_INIT)
953 if (device->tape_state == TS_UNUSED)
956 if (device->tape_state == TS_BLKUSE)
958 if (device->tape_state != TS_IN_USE)
962 /* Increase use count of device for the added request. */
963 request->device = tape_get_device(device);
965 if (list_empty(&device->req_queue)) {
966 /* No other requests are on the queue. Start this one. */
967 rc = __tape_start_io(device, request);
971 DBF_LH(5, "Request %p added for execution.\n", request);
972 list_add(&request->list, &device->req_queue);
974 DBF_LH(5, "Request %p add to queue.\n", request);
975 request->status = TAPE_REQUEST_QUEUED;
976 list_add_tail(&request->list, &device->req_queue);
982 * Add the request to the request queue, try to start it if the
983 * tape is idle. Return without waiting for end of i/o.
986 tape_do_io_async(struct tape_device *device, struct tape_request *request)
990 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
992 spin_lock_irq(get_ccwdev_lock(device->cdev));
993 /* Add request to request queue and try to start it. */
994 rc = __tape_start_request(device, request);
995 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1000 * tape_do_io/__tape_wake_up
1001 * Add the request to the request queue, try to start it if the
1002 * tape is idle and wait uninterruptible for its completion.
1005 __tape_wake_up(struct tape_request *request, void *data)
1007 request->callback = NULL;
1008 wake_up((wait_queue_head_t *) data);
1012 tape_do_io(struct tape_device *device, struct tape_request *request)
1016 spin_lock_irq(get_ccwdev_lock(device->cdev));
1017 /* Setup callback */
1018 request->callback = __tape_wake_up;
1019 request->callback_data = &device->wait_queue;
1020 /* Add request to request queue and try to start it. */
1021 rc = __tape_start_request(device, request);
1022 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1025 /* Request added to the queue. Wait for its completion. */
1026 wait_event(device->wait_queue, (request->callback == NULL));
1027 /* Get rc from request */
1032 * tape_do_io_interruptible/__tape_wake_up_interruptible
1033 * Add the request to the request queue, try to start it if the
1034 * tape is idle and wait uninterruptible for its completion.
1037 __tape_wake_up_interruptible(struct tape_request *request, void *data)
1039 request->callback = NULL;
1040 wake_up_interruptible((wait_queue_head_t *) data);
1044 tape_do_io_interruptible(struct tape_device *device,
1045 struct tape_request *request)
1049 spin_lock_irq(get_ccwdev_lock(device->cdev));
1050 /* Setup callback */
1051 request->callback = __tape_wake_up_interruptible;
1052 request->callback_data = &device->wait_queue;
1053 rc = __tape_start_request(device, request);
1054 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1057 /* Request added to the queue. Wait for its completion. */
1058 rc = wait_event_interruptible(device->wait_queue,
1059 (request->callback == NULL));
1060 if (rc != -ERESTARTSYS)
1061 /* Request finished normally. */
1064 /* Interrupted by a signal. We have to stop the current request. */
1065 spin_lock_irq(get_ccwdev_lock(device->cdev));
1066 rc = __tape_cancel_io(device, request);
1067 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1069 /* Wait for the interrupt that acknowledges the halt. */
1071 rc = wait_event_interruptible(
1073 (request->callback == NULL)
1075 } while (rc == -ERESTARTSYS);
1077 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
1087 tape_cancel_io(struct tape_device *device, struct tape_request *request)
1091 spin_lock_irq(get_ccwdev_lock(device->cdev));
1092 rc = __tape_cancel_io(device, request);
1093 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1098 * Tape interrupt routine, called from the ccw_device layer
1101 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1103 struct tape_device *device;
1104 struct tape_request *request;
1107 device = dev_get_drvdata(&cdev->dev);
1108 if (device == NULL) {
1111 request = (struct tape_request *) intparm;
1113 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1115 /* On special conditions irb is an error pointer */
1117 /* FIXME: What to do with the request? */
1118 switch (PTR_ERR(irb)) {
1120 DBF_LH(1, "(%08x): Request timed out\n",
1123 __tape_end_request(device, request, -EIO);
1126 DBF_LH(1, "(%08x): Unexpected i/o error %li\n",
1127 device->cdev_id, PTR_ERR(irb));
1133 * If the condition code is not zero and the start function bit is
1134 * still set, this is an deferred error and the last start I/O did
1135 * not succeed. At this point the condition that caused the deferred
1136 * error might still apply. So we just schedule the request to be
1139 if (irb->scsw.cmd.cc != 0 &&
1140 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
1141 (request->status == TAPE_REQUEST_IN_IO)) {
1142 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1143 device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
1144 request->status = TAPE_REQUEST_QUEUED;
1145 schedule_delayed_work(&device->tape_dnr, HZ);
1149 /* May be an unsolicited irq */
1151 request->rescnt = irb->scsw.cmd.count;
1152 else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
1153 !list_empty(&device->req_queue)) {
1154 /* Not Ready to Ready after long busy ? */
1155 struct tape_request *req;
1156 req = list_entry(device->req_queue.next,
1157 struct tape_request, list);
1158 if (req->status == TAPE_REQUEST_LONG_BUSY) {
1159 DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
1160 if (del_timer(&device->lb_timeout)) {
1161 device->lb_timeout.data = 0UL;
1162 tape_put_device(device);
1163 __tape_start_next_request(device);
1168 if (irb->scsw.cmd.dstat != 0x0c) {
1169 /* Set the 'ONLINE' flag depending on sense byte 1 */
1170 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1171 device->tape_generic_status |= GMT_ONLINE(~0);
1173 device->tape_generic_status &= ~GMT_ONLINE(~0);
1176 * Any request that does not come back with channel end
1177 * and device end is unusual. Log the sense data.
1179 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1180 tape_dump_sense_dbf(device, request, irb);
1182 /* Upon normal completion the device _is_ online */
1183 device->tape_generic_status |= GMT_ONLINE(~0);
1185 if (device->tape_state == TS_NOT_OPER) {
1186 DBF_EVENT(6, "tape:device is not operational\n");
1191 * Request that were canceled still come back with an interrupt.
1192 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1194 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
1195 __tape_end_request(device, request, -EIO);
1199 rc = device->discipline->irq(device, request, irb);
1201 * rc < 0 : request finished unsuccessfully.
1202 * rc == TAPE_IO_SUCCESS: request finished successfully.
1203 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1204 * rc == TAPE_IO_RETRY: request finished but needs another go.
1205 * rc == TAPE_IO_STOP: request needs to get terminated.
1208 case TAPE_IO_SUCCESS:
1209 /* Upon normal completion the device _is_ online */
1210 device->tape_generic_status |= GMT_ONLINE(~0);
1211 __tape_end_request(device, request, rc);
1213 case TAPE_IO_PENDING:
1215 case TAPE_IO_LONG_BUSY:
1216 device->lb_timeout.data =
1217 (unsigned long) tape_get_device(device);
1218 device->lb_timeout.expires = jiffies +
1219 LONG_BUSY_TIMEOUT * HZ;
1220 DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
1221 add_timer(&device->lb_timeout);
1222 request->status = TAPE_REQUEST_LONG_BUSY;
1225 rc = __tape_start_io(device, request);
1227 __tape_end_request(device, request, rc);
1230 rc = __tape_cancel_io(device, request);
1232 __tape_end_request(device, request, rc);
1236 DBF_EVENT(6, "xunknownrc\n");
1237 __tape_end_request(device, request, -EIO);
1239 __tape_end_request(device, request, rc);
1246 * Tape device open function used by tape_char frontend.
1249 tape_open(struct tape_device *device)
1253 spin_lock_irq(get_ccwdev_lock(device->cdev));
1254 if (device->tape_state == TS_NOT_OPER) {
1255 DBF_EVENT(6, "TAPE:nodev\n");
1257 } else if (device->tape_state == TS_IN_USE) {
1258 DBF_EVENT(6, "TAPE:dbusy\n");
1260 } else if (device->tape_state == TS_BLKUSE) {
1261 DBF_EVENT(6, "TAPE:dbusy\n");
1263 } else if (device->discipline != NULL &&
1264 !try_module_get(device->discipline->owner)) {
1265 DBF_EVENT(6, "TAPE:nodisc\n");
1268 tape_state_set(device, TS_IN_USE);
1271 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1276 * Tape device release function used by tape_char frontend.
1279 tape_release(struct tape_device *device)
1281 spin_lock_irq(get_ccwdev_lock(device->cdev));
1282 if (device->tape_state == TS_IN_USE)
1283 tape_state_set(device, TS_UNUSED);
1284 module_put(device->discipline->owner);
1285 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1290 * Execute a magnetic tape command a number of times.
1293 tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1298 DBF_EVENT(6, "TAPE:mtio\n");
1299 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1300 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1302 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1304 fn = device->discipline->mtop_array[mt_op];
1308 /* We assume that the backends can handle count up to 500. */
1309 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1310 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1312 for (; mt_count > 500; mt_count -= 500)
1313 if ((rc = fn(device, 500)) != 0)
1316 rc = fn(device, mt_count);
1318 rc = fn(device, mt_count);
1324 * Tape init function.
1329 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
1330 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1331 #ifdef DBF_LIKE_HELL
1332 debug_set_level(TAPE_DBF_AREA, 6);
1334 DBF_EVENT(3, "tape init\n");
1341 * Tape exit function.
1346 DBF_EVENT(6, "tape exit\n");
1348 /* Get rid of the frontends */
1350 tape_proc_cleanup();
1351 debug_unregister (TAPE_DBF_AREA);
1354 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1355 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1356 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1357 MODULE_LICENSE("GPL");
1359 module_init(tape_init);
1360 module_exit(tape_exit);
1362 EXPORT_SYMBOL(tape_generic_remove);
1363 EXPORT_SYMBOL(tape_generic_probe);
1364 EXPORT_SYMBOL(tape_generic_online);
1365 EXPORT_SYMBOL(tape_generic_offline);
1366 EXPORT_SYMBOL(tape_generic_pm_suspend);
1367 EXPORT_SYMBOL(tape_put_device);
1368 EXPORT_SYMBOL(tape_get_device);
1369 EXPORT_SYMBOL(tape_state_verbose);
1370 EXPORT_SYMBOL(tape_op_verbose);
1371 EXPORT_SYMBOL(tape_state_set);
1372 EXPORT_SYMBOL(tape_med_state_set);
1373 EXPORT_SYMBOL(tape_alloc_request);
1374 EXPORT_SYMBOL(tape_free_request);
1375 EXPORT_SYMBOL(tape_dump_sense_dbf);
1376 EXPORT_SYMBOL(tape_do_io);
1377 EXPORT_SYMBOL(tape_do_io_async);
1378 EXPORT_SYMBOL(tape_do_io_interruptible);
1379 EXPORT_SYMBOL(tape_cancel_io);
1380 EXPORT_SYMBOL(tape_mtop);