2 * drivers/s390/char/tape_core.c
3 * basic function of the tape device driver
5 * S390 and zSeries version
6 * Copyright IBM Corp. 2001,2006
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Michael Holzheu <holzheu@de.ibm.com>
9 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
10 * Martin Schwidefsky <schwidefsky@de.ibm.com>
11 * Stefan Bader <shbader@de.ibm.com>
14 #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: "
29 #define LONG_BUSY_TIMEOUT 180 /* seconds */
31 static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *);
32 static void tape_delayed_next_request(struct work_struct *);
33 static void tape_long_busy_timeout(unsigned long data);
36 * One list to contain all tape devices of all disciplines, so
37 * we can assign the devices to minor numbers of the same major
38 * The list is protected by the rwlock
40 static LIST_HEAD(tape_device_list);
41 static DEFINE_RWLOCK(tape_device_lock);
44 * Pointer to debug area.
46 debug_info_t *TAPE_DBF_AREA = NULL;
47 EXPORT_SYMBOL(TAPE_DBF_AREA);
50 * Printable strings for tape enumerations.
52 const char *tape_state_verbose[TS_SIZE] =
54 [TS_UNUSED] = "UNUSED",
55 [TS_IN_USE] = "IN_USE",
56 [TS_BLKUSE] = "BLKUSE",
58 [TS_NOT_OPER] = "NOT_OP"
61 const char *tape_op_verbose[TO_SIZE] =
63 [TO_BLOCK] = "BLK", [TO_BSB] = "BSB",
64 [TO_BSF] = "BSF", [TO_DSE] = "DSE",
65 [TO_FSB] = "FSB", [TO_FSF] = "FSF",
66 [TO_LBL] = "LBL", [TO_NOP] = "NOP",
67 [TO_RBA] = "RBA", [TO_RBI] = "RBI",
68 [TO_RFO] = "RFO", [TO_REW] = "REW",
69 [TO_RUN] = "RUN", [TO_WRI] = "WRI",
70 [TO_WTM] = "WTM", [TO_MSEN] = "MSN",
71 [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF",
72 [TO_READ_ATTMSG] = "RAT",
73 [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS",
74 [TO_UNASSIGN] = "UAS", [TO_CRYPT_ON] = "CON",
75 [TO_CRYPT_OFF] = "COF", [TO_KEKL_SET] = "KLS",
76 [TO_KEKL_QUERY] = "KLQ",[TO_RDC] = "RDC",
79 static int devid_to_int(struct ccw_dev_id *dev_id)
81 return dev_id->devno + (dev_id->ssid << 16);
85 * Some channel attached tape specific attributes.
87 * FIXME: In the future the first_minor and blocksize attribute should be
88 * replaced by a link to the cdev tree.
91 tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf)
93 struct tape_device *tdev;
95 tdev = (struct tape_device *) dev->driver_data;
96 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state);
100 DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
103 tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf)
105 struct tape_device *tdev;
107 tdev = (struct tape_device *) dev->driver_data;
108 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor);
112 DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
115 tape_state_show(struct device *dev, struct device_attribute *attr, char *buf)
117 struct tape_device *tdev;
119 tdev = (struct tape_device *) dev->driver_data;
120 return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ?
121 "OFFLINE" : tape_state_verbose[tdev->tape_state]);
125 DEVICE_ATTR(state, 0444, tape_state_show, NULL);
128 tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf)
130 struct tape_device *tdev;
133 tdev = (struct tape_device *) dev->driver_data;
134 if (tdev->first_minor < 0)
135 return scnprintf(buf, PAGE_SIZE, "N/A\n");
137 spin_lock_irq(get_ccwdev_lock(tdev->cdev));
138 if (list_empty(&tdev->req_queue))
139 rc = scnprintf(buf, PAGE_SIZE, "---\n");
141 struct tape_request *req;
143 req = list_entry(tdev->req_queue.next, struct tape_request,
145 rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
147 spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
152 DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
155 tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf)
157 struct tape_device *tdev;
159 tdev = (struct tape_device *) dev->driver_data;
161 return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size);
165 DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);
167 static struct attribute *tape_attrs[] = {
168 &dev_attr_medium_state.attr,
169 &dev_attr_first_minor.attr,
170 &dev_attr_state.attr,
171 &dev_attr_operation.attr,
172 &dev_attr_blocksize.attr,
176 static struct attribute_group tape_attr_group = {
181 * Tape state functions
184 tape_state_set(struct tape_device *device, enum tape_state newstate)
188 if (device->tape_state == TS_NOT_OPER) {
189 DBF_EVENT(3, "ts_set err: not oper\n");
192 DBF_EVENT(4, "ts. dev: %x\n", device->first_minor);
193 DBF_EVENT(4, "old ts:\t\n");
194 if (device->tape_state < TS_SIZE && device->tape_state >=0 )
195 str = tape_state_verbose[device->tape_state];
198 DBF_EVENT(4, "%s\n", str);
199 DBF_EVENT(4, "new ts:\t\n");
200 if (newstate < TS_SIZE && newstate >= 0)
201 str = tape_state_verbose[newstate];
204 DBF_EVENT(4, "%s\n", str);
205 device->tape_state = newstate;
206 wake_up(&device->state_change_wq);
210 tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
212 if (device->medium_state == newstate)
216 device->tape_generic_status |= GMT_DR_OPEN(~0);
217 PRINT_INFO("(%s): Tape is unloaded\n",
218 dev_name(&device->cdev->dev));
221 device->tape_generic_status &= ~GMT_DR_OPEN(~0);
222 PRINT_INFO("(%s): Tape has been mounted\n",
223 dev_name(&device->cdev->dev));
229 device->medium_state = newstate;
230 wake_up(&device->state_change_wq);
234 * Stop running ccw. Has to be called with the device lock held.
237 __tape_cancel_io(struct tape_device *device, struct tape_request *request)
242 /* Check if interrupt has already been processed */
243 if (request->callback == NULL)
247 for (retries = 0; retries < 5; retries++) {
248 rc = ccw_device_clear(device->cdev, (long) request);
252 request->status = TAPE_REQUEST_DONE;
255 request->status = TAPE_REQUEST_CANCEL;
256 schedule_delayed_work(&device->tape_dnr, 0);
259 DBF_EXCEPTION(2, "device gone, retry\n");
262 DBF_EXCEPTION(2, "I/O error, retry\n");
273 * Add device into the sorted list, giving it the first
274 * available minor number.
277 tape_assign_minor(struct tape_device *device)
279 struct tape_device *tmp;
283 write_lock(&tape_device_lock);
284 list_for_each_entry(tmp, &tape_device_list, node) {
285 if (minor < tmp->first_minor)
287 minor += TAPE_MINORS_PER_DEV;
290 write_unlock(&tape_device_lock);
293 device->first_minor = minor;
294 list_add_tail(&device->node, &tmp->node);
295 write_unlock(&tape_device_lock);
299 /* remove device from the list */
301 tape_remove_minor(struct tape_device *device)
303 write_lock(&tape_device_lock);
304 list_del_init(&device->node);
305 device->first_minor = -1;
306 write_unlock(&tape_device_lock);
310 * Set a device online.
312 * This function is called by the common I/O layer to move a device from the
313 * detected but offline into the online state.
314 * If we return an error (RC < 0) the device remains in the offline state. This
315 * can happen if the device is assigned somewhere else, for example.
318 tape_generic_online(struct tape_device *device,
319 struct tape_discipline *discipline)
323 DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
325 if (device->tape_state != TS_INIT) {
326 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
330 init_timer(&device->lb_timeout);
331 device->lb_timeout.function = tape_long_busy_timeout;
333 /* Let the discipline have a go at the device. */
334 device->discipline = discipline;
335 if (!try_module_get(discipline->owner)) {
336 PRINT_ERR("Cannot get module. Module gone.\n");
340 rc = discipline->setup_device(device);
343 rc = tape_assign_minor(device);
347 rc = tapechar_setup_device(device);
350 rc = tapeblock_setup_device(device);
354 tape_state_set(device, TS_UNUSED);
356 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
361 tapechar_cleanup_device(device);
363 device->discipline->cleanup_device(device);
364 device->discipline = NULL;
366 tape_remove_minor(device);
368 module_put(discipline->owner);
373 tape_cleanup_device(struct tape_device *device)
375 tapeblock_cleanup_device(device);
376 tapechar_cleanup_device(device);
377 device->discipline->cleanup_device(device);
378 module_put(device->discipline->owner);
379 tape_remove_minor(device);
380 tape_med_state_set(device, MS_UNKNOWN);
384 * Set device offline.
386 * Called by the common I/O layer if the drive should set offline on user
387 * request. We may prevent this by returning an error.
388 * Manual offline is only allowed while the drive is not in use.
391 tape_generic_offline(struct tape_device *device)
394 PRINT_ERR("tape_generic_offline: no such device\n");
398 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
399 device->cdev_id, device);
401 spin_lock_irq(get_ccwdev_lock(device->cdev));
402 switch (device->tape_state) {
405 spin_unlock_irq(get_ccwdev_lock(device->cdev));
408 tape_state_set(device, TS_INIT);
409 spin_unlock_irq(get_ccwdev_lock(device->cdev));
410 tape_cleanup_device(device);
413 DBF_EVENT(3, "(%08x): Set offline failed "
416 PRINT_WARN("(%s): Set offline failed "
418 dev_name(&device->cdev->dev));
419 spin_unlock_irq(get_ccwdev_lock(device->cdev));
423 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
428 * Allocate memory for a new device structure.
430 static struct tape_device *
431 tape_alloc_device(void)
433 struct tape_device *device;
435 device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
436 if (device == NULL) {
437 DBF_EXCEPTION(2, "ti:no mem\n");
438 PRINT_INFO ("can't allocate memory for "
439 "tape info structure\n");
440 return ERR_PTR(-ENOMEM);
442 device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
443 if (device->modeset_byte == NULL) {
444 DBF_EXCEPTION(2, "ti:no mem\n");
445 PRINT_INFO("can't allocate memory for modeset byte\n");
447 return ERR_PTR(-ENOMEM);
449 INIT_LIST_HEAD(&device->req_queue);
450 INIT_LIST_HEAD(&device->node);
451 init_waitqueue_head(&device->state_change_wq);
452 init_waitqueue_head(&device->wait_queue);
453 device->tape_state = TS_INIT;
454 device->medium_state = MS_UNKNOWN;
455 *device->modeset_byte = 0;
456 device->first_minor = -1;
457 atomic_set(&device->ref_count, 1);
458 INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request);
464 * Get a reference to an existing device structure. This will automatically
465 * increment the reference count.
468 tape_get_device_reference(struct tape_device *device)
470 DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device,
471 atomic_inc_return(&device->ref_count));
477 * Decrease the reference counter of a devices structure. If the
478 * reference counter reaches zero free the device structure.
479 * The function returns a NULL pointer to be used by the caller
480 * for clearing reference pointers.
483 tape_put_device(struct tape_device *device)
487 remain = atomic_dec_return(&device->ref_count);
489 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain);
492 DBF_EVENT(4, "put device without reference\n");
493 PRINT_ERR("put device without reference\n");
495 DBF_EVENT(4, "tape_free_device(%p)\n", device);
496 kfree(device->modeset_byte);
505 * Find tape device by a device index.
508 tape_get_device(int devindex)
510 struct tape_device *device, *tmp;
512 device = ERR_PTR(-ENODEV);
513 read_lock(&tape_device_lock);
514 list_for_each_entry(tmp, &tape_device_list, node) {
515 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
516 device = tape_get_device_reference(tmp);
520 read_unlock(&tape_device_lock);
525 * Driverfs tape probe function.
528 tape_generic_probe(struct ccw_device *cdev)
530 struct tape_device *device;
532 struct ccw_dev_id dev_id;
534 device = tape_alloc_device();
537 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
538 ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
540 tape_put_device(device);
541 PRINT_ERR("probe failed for tape device %s\n",
542 dev_name(&cdev->dev));
545 cdev->dev.driver_data = device;
546 cdev->handler = __tape_do_irq;
548 ccw_device_get_id(cdev, &dev_id);
549 device->cdev_id = devid_to_int(&dev_id);
550 PRINT_INFO("tape device %s found\n", dev_name(&cdev->dev));
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 dev_name(&device->cdev->dev));
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 = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
648 if (request == NULL) {
649 DBF_EXCEPTION(1, "cqra nomem\n");
650 return ERR_PTR(-ENOMEM);
652 /* allocate channel program */
654 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
655 GFP_ATOMIC | GFP_DMA);
656 if (request->cpaddr == NULL) {
657 DBF_EXCEPTION(1, "cqra nomem\n");
659 return ERR_PTR(-ENOMEM);
662 /* alloc small kernel buffer */
664 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
665 if (request->cpdata == NULL) {
666 DBF_EXCEPTION(1, "cqra nomem\n");
667 kfree(request->cpaddr);
669 return ERR_PTR(-ENOMEM);
672 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
679 * Free tape ccw request
682 tape_free_request (struct tape_request * request)
684 DBF_LH(6, "Free request %p\n", request);
686 if (request->device != NULL) {
687 request->device = tape_put_device(request->device);
689 kfree(request->cpdata);
690 kfree(request->cpaddr);
695 __tape_start_io(struct tape_device *device, struct tape_request *request)
699 #ifdef CONFIG_S390_TAPE_BLOCK
700 if (request->op == TO_BLOCK)
701 device->discipline->check_locate(device, request);
703 rc = ccw_device_start(
706 (unsigned long) request,
711 request->status = TAPE_REQUEST_IN_IO;
712 } else if (rc == -EBUSY) {
713 /* The common I/O subsystem is currently busy. Retry later. */
714 request->status = TAPE_REQUEST_QUEUED;
715 schedule_delayed_work(&device->tape_dnr, 0);
718 /* Start failed. Remove request and indicate failure. */
719 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
725 __tape_start_next_request(struct tape_device *device)
727 struct list_head *l, *n;
728 struct tape_request *request;
731 DBF_LH(6, "__tape_start_next_request(%p)\n", device);
733 * Try to start each request on request queue until one is
734 * started successful.
736 list_for_each_safe(l, n, &device->req_queue) {
737 request = list_entry(l, struct tape_request, list);
740 * Avoid race condition if bottom-half was triggered more than
743 if (request->status == TAPE_REQUEST_IN_IO)
746 * Request has already been stopped. We have to wait until
747 * the request is removed from the queue in the interrupt
750 if (request->status == TAPE_REQUEST_DONE)
754 * We wanted to cancel the request but the common I/O layer
755 * was busy at that time. This can only happen if this
756 * function is called by delayed_next_request.
757 * Otherwise we start the next request on the queue.
759 if (request->status == TAPE_REQUEST_CANCEL) {
760 rc = __tape_cancel_io(device, request);
762 rc = __tape_start_io(device, request);
767 /* Set ending status. */
769 request->status = TAPE_REQUEST_DONE;
771 /* Remove from request queue. */
772 list_del(&request->list);
775 if (request->callback != NULL)
776 request->callback(request, request->callback_data);
781 tape_delayed_next_request(struct work_struct *work)
783 struct tape_device *device =
784 container_of(work, struct tape_device, tape_dnr.work);
786 DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
787 spin_lock_irq(get_ccwdev_lock(device->cdev));
788 __tape_start_next_request(device);
789 spin_unlock_irq(get_ccwdev_lock(device->cdev));
792 static void tape_long_busy_timeout(unsigned long data)
794 struct tape_request *request;
795 struct tape_device *device;
797 device = (struct tape_device *) data;
798 spin_lock_irq(get_ccwdev_lock(device->cdev));
799 request = list_entry(device->req_queue.next, struct tape_request, list);
800 if (request->status != TAPE_REQUEST_LONG_BUSY)
802 DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
803 __tape_start_next_request(device);
804 device->lb_timeout.data = (unsigned long) tape_put_device(device);
805 spin_unlock_irq(get_ccwdev_lock(device->cdev));
810 struct tape_device * device,
811 struct tape_request * request,
814 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
817 request->status = TAPE_REQUEST_DONE;
819 /* Remove from request queue. */
820 list_del(&request->list);
823 if (request->callback != NULL)
824 request->callback(request, request->callback_data);
827 /* Start next request. */
828 if (!list_empty(&device->req_queue))
829 __tape_start_next_request(device);
833 * Write sense data to console/dbf
836 tape_dump_sense(struct tape_device* device, struct tape_request *request,
841 PRINT_INFO("-------------------------------------------------\n");
842 PRINT_INFO("DSTAT : %02x CSTAT: %02x CPA: %04x\n",
843 irb->scsw.cmd.dstat, irb->scsw.cmd.cstat, irb->scsw.cmd.cpa);
844 PRINT_INFO("DEVICE: %s\n", dev_name(&device->cdev->dev));
846 PRINT_INFO("OP : %s\n", tape_op_verbose[request->op]);
848 sptr = (unsigned int *) irb->ecw;
849 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
850 sptr[0], sptr[1], sptr[2], sptr[3]);
851 PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
852 sptr[4], sptr[5], sptr[6], sptr[7]);
853 PRINT_INFO("--------------------------------------------------\n");
857 * Write sense data to dbf
860 tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
867 op = tape_op_verbose[request->op];
870 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
871 irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
872 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
873 sptr = (unsigned int *) irb->ecw;
874 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
875 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
876 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
877 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
881 * I/O helper function. Adds the request to the request queue
882 * and starts it if the tape is idle. Has to be called with
883 * the device lock held.
886 __tape_start_request(struct tape_device *device, struct tape_request *request)
890 switch (request->op) {
896 if (device->tape_state == TS_INIT)
898 if (device->tape_state == TS_UNUSED)
901 if (device->tape_state == TS_BLKUSE)
903 if (device->tape_state != TS_IN_USE)
907 /* Increase use count of device for the added request. */
908 request->device = tape_get_device_reference(device);
910 if (list_empty(&device->req_queue)) {
911 /* No other requests are on the queue. Start this one. */
912 rc = __tape_start_io(device, request);
916 DBF_LH(5, "Request %p added for execution.\n", request);
917 list_add(&request->list, &device->req_queue);
919 DBF_LH(5, "Request %p add to queue.\n", request);
920 request->status = TAPE_REQUEST_QUEUED;
921 list_add_tail(&request->list, &device->req_queue);
927 * Add the request to the request queue, try to start it if the
928 * tape is idle. Return without waiting for end of i/o.
931 tape_do_io_async(struct tape_device *device, struct tape_request *request)
935 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
937 spin_lock_irq(get_ccwdev_lock(device->cdev));
938 /* Add request to request queue and try to start it. */
939 rc = __tape_start_request(device, request);
940 spin_unlock_irq(get_ccwdev_lock(device->cdev));
945 * tape_do_io/__tape_wake_up
946 * Add the request to the request queue, try to start it if the
947 * tape is idle and wait uninterruptible for its completion.
950 __tape_wake_up(struct tape_request *request, void *data)
952 request->callback = NULL;
953 wake_up((wait_queue_head_t *) data);
957 tape_do_io(struct tape_device *device, struct tape_request *request)
961 spin_lock_irq(get_ccwdev_lock(device->cdev));
963 request->callback = __tape_wake_up;
964 request->callback_data = &device->wait_queue;
965 /* Add request to request queue and try to start it. */
966 rc = __tape_start_request(device, request);
967 spin_unlock_irq(get_ccwdev_lock(device->cdev));
970 /* Request added to the queue. Wait for its completion. */
971 wait_event(device->wait_queue, (request->callback == NULL));
972 /* Get rc from request */
977 * tape_do_io_interruptible/__tape_wake_up_interruptible
978 * Add the request to the request queue, try to start it if the
979 * tape is idle and wait uninterruptible for its completion.
982 __tape_wake_up_interruptible(struct tape_request *request, void *data)
984 request->callback = NULL;
985 wake_up_interruptible((wait_queue_head_t *) data);
989 tape_do_io_interruptible(struct tape_device *device,
990 struct tape_request *request)
994 spin_lock_irq(get_ccwdev_lock(device->cdev));
996 request->callback = __tape_wake_up_interruptible;
997 request->callback_data = &device->wait_queue;
998 rc = __tape_start_request(device, request);
999 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1002 /* Request added to the queue. Wait for its completion. */
1003 rc = wait_event_interruptible(device->wait_queue,
1004 (request->callback == NULL));
1005 if (rc != -ERESTARTSYS)
1006 /* Request finished normally. */
1009 /* Interrupted by a signal. We have to stop the current request. */
1010 spin_lock_irq(get_ccwdev_lock(device->cdev));
1011 rc = __tape_cancel_io(device, request);
1012 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1014 /* Wait for the interrupt that acknowledges the halt. */
1016 rc = wait_event_interruptible(
1018 (request->callback == NULL)
1020 } while (rc == -ERESTARTSYS);
1022 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
1032 tape_cancel_io(struct tape_device *device, struct tape_request *request)
1036 spin_lock_irq(get_ccwdev_lock(device->cdev));
1037 rc = __tape_cancel_io(device, request);
1038 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1043 * Tape interrupt routine, called from the ccw_device layer
1046 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1048 struct tape_device *device;
1049 struct tape_request *request;
1052 device = (struct tape_device *) cdev->dev.driver_data;
1053 if (device == NULL) {
1054 PRINT_ERR("could not get device structure for %s "
1055 "in interrupt\n", dev_name(&cdev->dev));
1058 request = (struct tape_request *) intparm;
1060 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1062 /* On special conditions irb is an error pointer */
1064 /* FIXME: What to do with the request? */
1065 switch (PTR_ERR(irb)) {
1067 PRINT_WARN("(%s): Request timed out\n",
1068 dev_name(&cdev->dev));
1070 __tape_end_request(device, request, -EIO);
1073 PRINT_ERR("(%s): Unexpected i/o error %li\n",
1074 dev_name(&cdev->dev),
1081 * If the condition code is not zero and the start function bit is
1082 * still set, this is an deferred error and the last start I/O did
1083 * not succeed. At this point the condition that caused the deferred
1084 * error might still apply. So we just schedule the request to be
1087 if (irb->scsw.cmd.cc != 0 &&
1088 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
1089 (request->status == TAPE_REQUEST_IN_IO)) {
1090 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1091 device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
1092 request->status = TAPE_REQUEST_QUEUED;
1093 schedule_delayed_work(&device->tape_dnr, HZ);
1097 /* May be an unsolicited irq */
1099 request->rescnt = irb->scsw.cmd.count;
1100 else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
1101 !list_empty(&device->req_queue)) {
1102 /* Not Ready to Ready after long busy ? */
1103 struct tape_request *req;
1104 req = list_entry(device->req_queue.next,
1105 struct tape_request, list);
1106 if (req->status == TAPE_REQUEST_LONG_BUSY) {
1107 DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
1108 if (del_timer(&device->lb_timeout)) {
1109 device->lb_timeout.data = (unsigned long)
1110 tape_put_device(device);
1111 __tape_start_next_request(device);
1116 if (irb->scsw.cmd.dstat != 0x0c) {
1117 /* Set the 'ONLINE' flag depending on sense byte 1 */
1118 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1119 device->tape_generic_status |= GMT_ONLINE(~0);
1121 device->tape_generic_status &= ~GMT_ONLINE(~0);
1124 * Any request that does not come back with channel end
1125 * and device end is unusual. Log the sense data.
1127 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1128 tape_dump_sense_dbf(device, request, irb);
1130 /* Upon normal completion the device _is_ online */
1131 device->tape_generic_status |= GMT_ONLINE(~0);
1133 if (device->tape_state == TS_NOT_OPER) {
1134 DBF_EVENT(6, "tape:device is not operational\n");
1139 * Request that were canceled still come back with an interrupt.
1140 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1142 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
1143 __tape_end_request(device, request, -EIO);
1147 rc = device->discipline->irq(device, request, irb);
1149 * rc < 0 : request finished unsuccessfully.
1150 * rc == TAPE_IO_SUCCESS: request finished successfully.
1151 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1152 * rc == TAPE_IO_RETRY: request finished but needs another go.
1153 * rc == TAPE_IO_STOP: request needs to get terminated.
1156 case TAPE_IO_SUCCESS:
1157 /* Upon normal completion the device _is_ online */
1158 device->tape_generic_status |= GMT_ONLINE(~0);
1159 __tape_end_request(device, request, rc);
1161 case TAPE_IO_PENDING:
1163 case TAPE_IO_LONG_BUSY:
1164 device->lb_timeout.data =
1165 (unsigned long)tape_get_device_reference(device);
1166 device->lb_timeout.expires = jiffies +
1167 LONG_BUSY_TIMEOUT * HZ;
1168 DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
1169 add_timer(&device->lb_timeout);
1170 request->status = TAPE_REQUEST_LONG_BUSY;
1173 rc = __tape_start_io(device, request);
1175 __tape_end_request(device, request, rc);
1178 rc = __tape_cancel_io(device, request);
1180 __tape_end_request(device, request, rc);
1184 DBF_EVENT(6, "xunknownrc\n");
1185 PRINT_ERR("Invalid return code from discipline "
1186 "interrupt function.\n");
1187 __tape_end_request(device, request, -EIO);
1189 __tape_end_request(device, request, rc);
1196 * Tape device open function used by tape_char & tape_block frontends.
1199 tape_open(struct tape_device *device)
1203 spin_lock_irq(get_ccwdev_lock(device->cdev));
1204 if (device->tape_state == TS_NOT_OPER) {
1205 DBF_EVENT(6, "TAPE:nodev\n");
1207 } else if (device->tape_state == TS_IN_USE) {
1208 DBF_EVENT(6, "TAPE:dbusy\n");
1210 } else if (device->tape_state == TS_BLKUSE) {
1211 DBF_EVENT(6, "TAPE:dbusy\n");
1213 } else if (device->discipline != NULL &&
1214 !try_module_get(device->discipline->owner)) {
1215 DBF_EVENT(6, "TAPE:nodisc\n");
1218 tape_state_set(device, TS_IN_USE);
1221 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1226 * Tape device release function used by tape_char & tape_block frontends.
1229 tape_release(struct tape_device *device)
1231 spin_lock_irq(get_ccwdev_lock(device->cdev));
1232 if (device->tape_state == TS_IN_USE)
1233 tape_state_set(device, TS_UNUSED);
1234 module_put(device->discipline->owner);
1235 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1240 * Execute a magnetic tape command a number of times.
1243 tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1248 DBF_EVENT(6, "TAPE:mtio\n");
1249 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1250 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1252 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1254 fn = device->discipline->mtop_array[mt_op];
1258 /* We assume that the backends can handle count up to 500. */
1259 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1260 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1262 for (; mt_count > 500; mt_count -= 500)
1263 if ((rc = fn(device, 500)) != 0)
1266 rc = fn(device, mt_count);
1268 rc = fn(device, mt_count);
1274 * Tape init function.
1279 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
1280 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1281 #ifdef DBF_LIKE_HELL
1282 debug_set_level(TAPE_DBF_AREA, 6);
1284 DBF_EVENT(3, "tape init\n");
1292 * Tape exit function.
1297 DBF_EVENT(6, "tape exit\n");
1299 /* Get rid of the frontends */
1302 tape_proc_cleanup();
1303 debug_unregister (TAPE_DBF_AREA);
1306 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1307 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1308 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1309 MODULE_LICENSE("GPL");
1311 module_init(tape_init);
1312 module_exit(tape_exit);
1314 EXPORT_SYMBOL(tape_generic_remove);
1315 EXPORT_SYMBOL(tape_generic_probe);
1316 EXPORT_SYMBOL(tape_generic_online);
1317 EXPORT_SYMBOL(tape_generic_offline);
1318 EXPORT_SYMBOL(tape_put_device);
1319 EXPORT_SYMBOL(tape_get_device_reference);
1320 EXPORT_SYMBOL(tape_state_verbose);
1321 EXPORT_SYMBOL(tape_op_verbose);
1322 EXPORT_SYMBOL(tape_state_set);
1323 EXPORT_SYMBOL(tape_med_state_set);
1324 EXPORT_SYMBOL(tape_alloc_request);
1325 EXPORT_SYMBOL(tape_free_request);
1326 EXPORT_SYMBOL(tape_dump_sense);
1327 EXPORT_SYMBOL(tape_dump_sense_dbf);
1328 EXPORT_SYMBOL(tape_do_io);
1329 EXPORT_SYMBOL(tape_do_io_async);
1330 EXPORT_SYMBOL(tape_do_io_interruptible);
1331 EXPORT_SYMBOL(tape_cancel_io);
1332 EXPORT_SYMBOL(tape_mtop);