OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / drivers / s390 / char / tape_core.c
blobb3a3e8e8656e6db1be5e3a50b63056c43f0897f6
1 /*
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
29 #include "tape.h"
30 #include "tape_std.h"
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",
60 [TS_INIT] = "INIT ",
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.
93 static ssize_t
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);
102 static
103 DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
105 static ssize_t
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);
114 static
115 DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
117 static ssize_t
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]);
127 static
128 DEVICE_ATTR(state, 0444, tape_state_show, NULL);
130 static ssize_t
131 tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf)
133 struct tape_device *tdev;
134 ssize_t rc;
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");
143 else {
144 struct tape_request *req;
146 req = list_entry(tdev->req_queue.next, struct tape_request,
147 list);
148 rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
150 spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
151 return rc;
154 static
155 DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
157 static ssize_t
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);
167 static
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,
176 NULL
179 static struct attribute_group tape_attr_group = {
180 .attrs = tape_attrs,
184 * Tape state functions
186 void
187 tape_state_set(struct tape_device *device, enum tape_state newstate)
189 const char *str;
191 if (device->tape_state == TS_NOT_OPER) {
192 DBF_EVENT(3, "ts_set err: not oper\n");
193 return;
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];
199 else
200 str = "UNKNOWN TS";
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];
205 else
206 str = "UNKNOWN TS";
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;
218 static void
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 };
228 switch (p->state) {
229 case MS_UNLOADED:
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);
234 break;
235 case MS_LOADED:
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);
240 break;
241 default:
242 break;
244 tape_put_device(device);
245 kfree(p);
248 static void
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);
254 if (p) {
255 INIT_WORK(&p->work, tape_med_state_work_handler);
256 p->device = tape_get_device(device);
257 p->state = state;
258 schedule_work(&p->work);
262 void
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)
269 return;
270 device->medium_state = newstate;
271 switch(newstate){
272 case MS_UNLOADED:
273 device->tape_generic_status |= GMT_DR_OPEN(~0);
274 if (oldstate == MS_LOADED)
275 tape_med_state_work(device, MS_UNLOADED);
276 break;
277 case MS_LOADED:
278 device->tape_generic_status &= ~GMT_DR_OPEN(~0);
279 if (oldstate == MS_UNLOADED)
280 tape_med_state_work(device, MS_LOADED);
281 break;
282 default:
283 break;
285 wake_up(&device->state_change_wq);
289 * Stop running ccw. Has to be called with the device lock held.
291 static int
292 __tape_cancel_io(struct tape_device *device, struct tape_request *request)
294 int retries;
295 int rc;
297 /* Check if interrupt has already been processed */
298 if (request->callback == NULL)
299 return 0;
301 rc = 0;
302 for (retries = 0; retries < 5; retries++) {
303 rc = ccw_device_clear(device->cdev, (long) request);
305 switch (rc) {
306 case 0:
307 request->status = TAPE_REQUEST_DONE;
308 return 0;
309 case -EBUSY:
310 request->status = TAPE_REQUEST_CANCEL;
311 schedule_delayed_work(&device->tape_dnr, 0);
312 return 0;
313 case -ENODEV:
314 DBF_EXCEPTION(2, "device gone, retry\n");
315 break;
316 case -EIO:
317 DBF_EXCEPTION(2, "I/O error, retry\n");
318 break;
319 default:
320 BUG();
324 return rc;
328 * Add device into the sorted list, giving it the first
329 * available minor number.
331 static int
332 tape_assign_minor(struct tape_device *device)
334 struct tape_device *tmp;
335 int minor;
337 minor = 0;
338 write_lock(&tape_device_lock);
339 list_for_each_entry(tmp, &tape_device_list, node) {
340 if (minor < tmp->first_minor)
341 break;
342 minor += TAPE_MINORS_PER_DEV;
344 if (minor >= 256) {
345 write_unlock(&tape_device_lock);
346 return -ENODEV;
348 device->first_minor = minor;
349 list_add_tail(&device->node, &tmp->node);
350 write_unlock(&tape_device_lock);
351 return 0;
354 /* remove device from the list */
355 static void
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)
376 int rc;
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);
382 return -EINVAL;
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)) {
391 return -EINVAL;
394 rc = discipline->setup_device(device);
395 if (rc)
396 goto out;
397 rc = tape_assign_minor(device);
398 if (rc)
399 goto out_discipline;
401 rc = tapechar_setup_device(device);
402 if (rc)
403 goto out_minor;
404 rc = tapeblock_setup_device(device);
405 if (rc)
406 goto out_char;
408 tape_state_set(device, TS_UNUSED);
410 DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
412 return 0;
414 out_char:
415 tapechar_cleanup_device(device);
416 out_minor:
417 tape_remove_minor(device);
418 out_discipline:
419 device->discipline->cleanup_device(device);
420 device->discipline = NULL;
421 out:
422 module_put(discipline->owner);
423 return rc;
426 static void
427 tape_cleanup_device(struct tape_device *device)
429 tapeblock_cleanup_device(device);
430 tapechar_cleanup_device(device);
431 device->discipline->cleanup_device(device);
432 module_put(device->discipline->owner);
433 tape_remove_minor(device);
434 tape_med_state_set(device, MS_UNKNOWN);
438 * Suspend device.
440 * Called by the common I/O layer if the drive should be suspended on user
441 * request. We refuse to suspend if the device is loaded or in use for the
442 * following reason:
443 * While the Linux guest is suspended, it might be logged off which causes
444 * devices to be detached. Tape devices are automatically rewound and unloaded
445 * during DETACH processing (unless the tape device was attached with the
446 * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to
447 * resume the original state of the tape device, since we would need to
448 * manually re-load the cartridge which was active at suspend time.
450 int tape_generic_pm_suspend(struct ccw_device *cdev)
452 struct tape_device *device;
454 device = dev_get_drvdata(&cdev->dev);
455 if (!device) {
456 return -ENODEV;
459 DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n",
460 device->cdev_id, device);
462 if (device->medium_state != MS_UNLOADED) {
463 pr_err("A cartridge is loaded in tape device %s, "
464 "refusing to suspend\n", dev_name(&cdev->dev));
465 return -EBUSY;
468 spin_lock_irq(get_ccwdev_lock(device->cdev));
469 switch (device->tape_state) {
470 case TS_INIT:
471 case TS_NOT_OPER:
472 case TS_UNUSED:
473 spin_unlock_irq(get_ccwdev_lock(device->cdev));
474 break;
475 default:
476 pr_err("Tape device %s is busy, refusing to "
477 "suspend\n", dev_name(&cdev->dev));
478 spin_unlock_irq(get_ccwdev_lock(device->cdev));
479 return -EBUSY;
482 DBF_LH(3, "(%08x): Drive suspended.\n", device->cdev_id);
483 return 0;
487 * Set device offline.
489 * Called by the common I/O layer if the drive should set offline on user
490 * request. We may prevent this by returning an error.
491 * Manual offline is only allowed while the drive is not in use.
494 tape_generic_offline(struct ccw_device *cdev)
496 struct tape_device *device;
498 device = dev_get_drvdata(&cdev->dev);
499 if (!device) {
500 return -ENODEV;
503 DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
504 device->cdev_id, device);
506 spin_lock_irq(get_ccwdev_lock(device->cdev));
507 switch (device->tape_state) {
508 case TS_INIT:
509 case TS_NOT_OPER:
510 spin_unlock_irq(get_ccwdev_lock(device->cdev));
511 break;
512 case TS_UNUSED:
513 tape_state_set(device, TS_INIT);
514 spin_unlock_irq(get_ccwdev_lock(device->cdev));
515 tape_cleanup_device(device);
516 break;
517 default:
518 DBF_EVENT(3, "(%08x): Set offline failed "
519 "- drive in use.\n",
520 device->cdev_id);
521 spin_unlock_irq(get_ccwdev_lock(device->cdev));
522 return -EBUSY;
525 DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
526 return 0;
530 * Allocate memory for a new device structure.
532 static struct tape_device *
533 tape_alloc_device(void)
535 struct tape_device *device;
537 device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
538 if (device == NULL) {
539 DBF_EXCEPTION(2, "ti:no mem\n");
540 return ERR_PTR(-ENOMEM);
542 device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
543 if (device->modeset_byte == NULL) {
544 DBF_EXCEPTION(2, "ti:no mem\n");
545 kfree(device);
546 return ERR_PTR(-ENOMEM);
548 mutex_init(&device->mutex);
549 INIT_LIST_HEAD(&device->req_queue);
550 INIT_LIST_HEAD(&device->node);
551 init_waitqueue_head(&device->state_change_wq);
552 init_waitqueue_head(&device->wait_queue);
553 device->tape_state = TS_INIT;
554 device->medium_state = MS_UNKNOWN;
555 *device->modeset_byte = 0;
556 device->first_minor = -1;
557 atomic_set(&device->ref_count, 1);
558 INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request);
560 return device;
564 * Get a reference to an existing device structure. This will automatically
565 * increment the reference count.
567 struct tape_device *
568 tape_get_device(struct tape_device *device)
570 int count;
572 count = atomic_inc_return(&device->ref_count);
573 DBF_EVENT(4, "tape_get_device(%p) = %i\n", device, count);
574 return device;
578 * Decrease the reference counter of a devices structure. If the
579 * reference counter reaches zero free the device structure.
580 * The function returns a NULL pointer to be used by the caller
581 * for clearing reference pointers.
583 void
584 tape_put_device(struct tape_device *device)
586 int count;
588 count = atomic_dec_return(&device->ref_count);
589 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, count);
590 BUG_ON(count < 0);
591 if (count == 0) {
592 kfree(device->modeset_byte);
593 kfree(device);
598 * Find tape device by a device index.
600 struct tape_device *
601 tape_find_device(int devindex)
603 struct tape_device *device, *tmp;
605 device = ERR_PTR(-ENODEV);
606 read_lock(&tape_device_lock);
607 list_for_each_entry(tmp, &tape_device_list, node) {
608 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
609 device = tape_get_device(tmp);
610 break;
613 read_unlock(&tape_device_lock);
614 return device;
618 * Driverfs tape probe function.
621 tape_generic_probe(struct ccw_device *cdev)
623 struct tape_device *device;
624 int ret;
625 struct ccw_dev_id dev_id;
627 device = tape_alloc_device();
628 if (IS_ERR(device))
629 return -ENODEV;
630 ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP |
631 CCWDEV_DO_MULTIPATH);
632 ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
633 if (ret) {
634 tape_put_device(device);
635 return ret;
637 dev_set_drvdata(&cdev->dev, device);
638 cdev->handler = __tape_do_irq;
639 device->cdev = cdev;
640 ccw_device_get_id(cdev, &dev_id);
641 device->cdev_id = devid_to_int(&dev_id);
642 return ret;
645 static void
646 __tape_discard_requests(struct tape_device *device)
648 struct tape_request * request;
649 struct list_head * l, *n;
651 list_for_each_safe(l, n, &device->req_queue) {
652 request = list_entry(l, struct tape_request, list);
653 if (request->status == TAPE_REQUEST_IN_IO)
654 request->status = TAPE_REQUEST_DONE;
655 list_del(&request->list);
657 /* Decrease ref_count for removed request. */
658 request->device = NULL;
659 tape_put_device(device);
660 request->rc = -EIO;
661 if (request->callback != NULL)
662 request->callback(request, request->callback_data);
667 * Driverfs tape remove function.
669 * This function is called whenever the common I/O layer detects the device
670 * gone. This can happen at any time and we cannot refuse.
672 void
673 tape_generic_remove(struct ccw_device *cdev)
675 struct tape_device * device;
677 device = dev_get_drvdata(&cdev->dev);
678 if (!device) {
679 return;
681 DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
683 spin_lock_irq(get_ccwdev_lock(device->cdev));
684 switch (device->tape_state) {
685 case TS_INIT:
686 tape_state_set(device, TS_NOT_OPER);
687 case TS_NOT_OPER:
689 * Nothing to do.
691 spin_unlock_irq(get_ccwdev_lock(device->cdev));
692 break;
693 case TS_UNUSED:
695 * Need only to release the device.
697 tape_state_set(device, TS_NOT_OPER);
698 spin_unlock_irq(get_ccwdev_lock(device->cdev));
699 tape_cleanup_device(device);
700 break;
701 default:
703 * There may be requests on the queue. We will not get
704 * an interrupt for a request that was running. So we
705 * just post them all as I/O errors.
707 DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
708 device->cdev_id);
709 pr_warning("%s: A tape unit was detached while in "
710 "use\n", dev_name(&device->cdev->dev));
711 tape_state_set(device, TS_NOT_OPER);
712 __tape_discard_requests(device);
713 spin_unlock_irq(get_ccwdev_lock(device->cdev));
714 tape_cleanup_device(device);
717 device = dev_get_drvdata(&cdev->dev);
718 if (device) {
719 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
720 dev_set_drvdata(&cdev->dev, NULL);
721 tape_put_device(device);
726 * Allocate a new tape ccw request
728 struct tape_request *
729 tape_alloc_request(int cplength, int datasize)
731 struct tape_request *request;
733 BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
735 DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
737 request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
738 if (request == NULL) {
739 DBF_EXCEPTION(1, "cqra nomem\n");
740 return ERR_PTR(-ENOMEM);
742 /* allocate channel program */
743 if (cplength > 0) {
744 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
745 GFP_ATOMIC | GFP_DMA);
746 if (request->cpaddr == NULL) {
747 DBF_EXCEPTION(1, "cqra nomem\n");
748 kfree(request);
749 return ERR_PTR(-ENOMEM);
752 /* alloc small kernel buffer */
753 if (datasize > 0) {
754 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
755 if (request->cpdata == NULL) {
756 DBF_EXCEPTION(1, "cqra nomem\n");
757 kfree(request->cpaddr);
758 kfree(request);
759 return ERR_PTR(-ENOMEM);
762 DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
763 request->cpdata);
765 return request;
769 * Free tape ccw request
771 void
772 tape_free_request (struct tape_request * request)
774 DBF_LH(6, "Free request %p\n", request);
776 if (request->device)
777 tape_put_device(request->device);
778 kfree(request->cpdata);
779 kfree(request->cpaddr);
780 kfree(request);
783 static int
784 __tape_start_io(struct tape_device *device, struct tape_request *request)
786 int rc;
788 #ifdef CONFIG_S390_TAPE_BLOCK
789 if (request->op == TO_BLOCK)
790 device->discipline->check_locate(device, request);
791 #endif
792 rc = ccw_device_start(
793 device->cdev,
794 request->cpaddr,
795 (unsigned long) request,
796 0x00,
797 request->options
799 if (rc == 0) {
800 request->status = TAPE_REQUEST_IN_IO;
801 } else if (rc == -EBUSY) {
802 /* The common I/O subsystem is currently busy. Retry later. */
803 request->status = TAPE_REQUEST_QUEUED;
804 schedule_delayed_work(&device->tape_dnr, 0);
805 rc = 0;
806 } else {
807 /* Start failed. Remove request and indicate failure. */
808 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
810 return rc;
813 static void
814 __tape_start_next_request(struct tape_device *device)
816 struct list_head *l, *n;
817 struct tape_request *request;
818 int rc;
820 DBF_LH(6, "__tape_start_next_request(%p)\n", device);
822 * Try to start each request on request queue until one is
823 * started successful.
825 list_for_each_safe(l, n, &device->req_queue) {
826 request = list_entry(l, struct tape_request, list);
829 * Avoid race condition if bottom-half was triggered more than
830 * once.
832 if (request->status == TAPE_REQUEST_IN_IO)
833 return;
835 * Request has already been stopped. We have to wait until
836 * the request is removed from the queue in the interrupt
837 * handling.
839 if (request->status == TAPE_REQUEST_DONE)
840 return;
843 * We wanted to cancel the request but the common I/O layer
844 * was busy at that time. This can only happen if this
845 * function is called by delayed_next_request.
846 * Otherwise we start the next request on the queue.
848 if (request->status == TAPE_REQUEST_CANCEL) {
849 rc = __tape_cancel_io(device, request);
850 } else {
851 rc = __tape_start_io(device, request);
853 if (rc == 0)
854 return;
856 /* Set ending status. */
857 request->rc = rc;
858 request->status = TAPE_REQUEST_DONE;
860 /* Remove from request queue. */
861 list_del(&request->list);
863 /* Do callback. */
864 if (request->callback != NULL)
865 request->callback(request, request->callback_data);
869 static void
870 tape_delayed_next_request(struct work_struct *work)
872 struct tape_device *device =
873 container_of(work, struct tape_device, tape_dnr.work);
875 DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
876 spin_lock_irq(get_ccwdev_lock(device->cdev));
877 __tape_start_next_request(device);
878 spin_unlock_irq(get_ccwdev_lock(device->cdev));
881 static void tape_long_busy_timeout(unsigned long data)
883 struct tape_request *request;
884 struct tape_device *device;
886 device = (struct tape_device *) data;
887 spin_lock_irq(get_ccwdev_lock(device->cdev));
888 request = list_entry(device->req_queue.next, struct tape_request, list);
889 BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY);
890 DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id);
891 __tape_start_next_request(device);
892 device->lb_timeout.data = 0UL;
893 tape_put_device(device);
894 spin_unlock_irq(get_ccwdev_lock(device->cdev));
897 static void
898 __tape_end_request(
899 struct tape_device * device,
900 struct tape_request * request,
901 int rc)
903 DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
904 if (request) {
905 request->rc = rc;
906 request->status = TAPE_REQUEST_DONE;
908 /* Remove from request queue. */
909 list_del(&request->list);
911 /* Do callback. */
912 if (request->callback != NULL)
913 request->callback(request, request->callback_data);
916 /* Start next request. */
917 if (!list_empty(&device->req_queue))
918 __tape_start_next_request(device);
922 * Write sense data to dbf
924 void
925 tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
926 struct irb *irb)
928 unsigned int *sptr;
929 const char* op;
931 if (request != NULL)
932 op = tape_op_verbose[request->op];
933 else
934 op = "---";
935 DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n",
936 irb->scsw.cmd.dstat, irb->scsw.cmd.cstat);
937 DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
938 sptr = (unsigned int *) irb->ecw;
939 DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
940 DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
941 DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
942 DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
946 * I/O helper function. Adds the request to the request queue
947 * and starts it if the tape is idle. Has to be called with
948 * the device lock held.
950 static int
951 __tape_start_request(struct tape_device *device, struct tape_request *request)
953 int rc;
955 switch (request->op) {
956 case TO_MSEN:
957 case TO_ASSIGN:
958 case TO_UNASSIGN:
959 case TO_READ_ATTMSG:
960 case TO_RDC:
961 if (device->tape_state == TS_INIT)
962 break;
963 if (device->tape_state == TS_UNUSED)
964 break;
965 default:
966 if (device->tape_state == TS_BLKUSE)
967 break;
968 if (device->tape_state != TS_IN_USE)
969 return -ENODEV;
972 /* Increase use count of device for the added request. */
973 request->device = tape_get_device(device);
975 if (list_empty(&device->req_queue)) {
976 /* No other requests are on the queue. Start this one. */
977 rc = __tape_start_io(device, request);
978 if (rc)
979 return rc;
981 DBF_LH(5, "Request %p added for execution.\n", request);
982 list_add(&request->list, &device->req_queue);
983 } else {
984 DBF_LH(5, "Request %p add to queue.\n", request);
985 request->status = TAPE_REQUEST_QUEUED;
986 list_add_tail(&request->list, &device->req_queue);
988 return 0;
992 * Add the request to the request queue, try to start it if the
993 * tape is idle. Return without waiting for end of i/o.
996 tape_do_io_async(struct tape_device *device, struct tape_request *request)
998 int rc;
1000 DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
1002 spin_lock_irq(get_ccwdev_lock(device->cdev));
1003 /* Add request to request queue and try to start it. */
1004 rc = __tape_start_request(device, request);
1005 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1006 return rc;
1010 * tape_do_io/__tape_wake_up
1011 * Add the request to the request queue, try to start it if the
1012 * tape is idle and wait uninterruptible for its completion.
1014 static void
1015 __tape_wake_up(struct tape_request *request, void *data)
1017 request->callback = NULL;
1018 wake_up((wait_queue_head_t *) data);
1022 tape_do_io(struct tape_device *device, struct tape_request *request)
1024 int rc;
1026 spin_lock_irq(get_ccwdev_lock(device->cdev));
1027 /* Setup callback */
1028 request->callback = __tape_wake_up;
1029 request->callback_data = &device->wait_queue;
1030 /* Add request to request queue and try to start it. */
1031 rc = __tape_start_request(device, request);
1032 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1033 if (rc)
1034 return rc;
1035 /* Request added to the queue. Wait for its completion. */
1036 wait_event(device->wait_queue, (request->callback == NULL));
1037 /* Get rc from request */
1038 return request->rc;
1042 * tape_do_io_interruptible/__tape_wake_up_interruptible
1043 * Add the request to the request queue, try to start it if the
1044 * tape is idle and wait uninterruptible for its completion.
1046 static void
1047 __tape_wake_up_interruptible(struct tape_request *request, void *data)
1049 request->callback = NULL;
1050 wake_up_interruptible((wait_queue_head_t *) data);
1054 tape_do_io_interruptible(struct tape_device *device,
1055 struct tape_request *request)
1057 int rc;
1059 spin_lock_irq(get_ccwdev_lock(device->cdev));
1060 /* Setup callback */
1061 request->callback = __tape_wake_up_interruptible;
1062 request->callback_data = &device->wait_queue;
1063 rc = __tape_start_request(device, request);
1064 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1065 if (rc)
1066 return rc;
1067 /* Request added to the queue. Wait for its completion. */
1068 rc = wait_event_interruptible(device->wait_queue,
1069 (request->callback == NULL));
1070 if (rc != -ERESTARTSYS)
1071 /* Request finished normally. */
1072 return request->rc;
1074 /* Interrupted by a signal. We have to stop the current request. */
1075 spin_lock_irq(get_ccwdev_lock(device->cdev));
1076 rc = __tape_cancel_io(device, request);
1077 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1078 if (rc == 0) {
1079 /* Wait for the interrupt that acknowledges the halt. */
1080 do {
1081 rc = wait_event_interruptible(
1082 device->wait_queue,
1083 (request->callback == NULL)
1085 } while (rc == -ERESTARTSYS);
1087 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
1088 rc = -ERESTARTSYS;
1090 return rc;
1094 * Stop running ccw.
1097 tape_cancel_io(struct tape_device *device, struct tape_request *request)
1099 int rc;
1101 spin_lock_irq(get_ccwdev_lock(device->cdev));
1102 rc = __tape_cancel_io(device, request);
1103 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1104 return rc;
1108 * Tape interrupt routine, called from the ccw_device layer
1110 static void
1111 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1113 struct tape_device *device;
1114 struct tape_request *request;
1115 int rc;
1117 device = dev_get_drvdata(&cdev->dev);
1118 if (device == NULL) {
1119 return;
1121 request = (struct tape_request *) intparm;
1123 DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1125 /* On special conditions irb is an error pointer */
1126 if (IS_ERR(irb)) {
1127 /* FIXME: What to do with the request? */
1128 switch (PTR_ERR(irb)) {
1129 case -ETIMEDOUT:
1130 DBF_LH(1, "(%08x): Request timed out\n",
1131 device->cdev_id);
1132 case -EIO:
1133 __tape_end_request(device, request, -EIO);
1134 break;
1135 default:
1136 DBF_LH(1, "(%08x): Unexpected i/o error %li\n",
1137 device->cdev_id, PTR_ERR(irb));
1139 return;
1143 * If the condition code is not zero and the start function bit is
1144 * still set, this is an deferred error and the last start I/O did
1145 * not succeed. At this point the condition that caused the deferred
1146 * error might still apply. So we just schedule the request to be
1147 * started later.
1149 if (irb->scsw.cmd.cc != 0 &&
1150 (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
1151 (request->status == TAPE_REQUEST_IN_IO)) {
1152 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1153 device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl);
1154 request->status = TAPE_REQUEST_QUEUED;
1155 schedule_delayed_work(&device->tape_dnr, HZ);
1156 return;
1159 /* May be an unsolicited irq */
1160 if(request != NULL)
1161 request->rescnt = irb->scsw.cmd.count;
1162 else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) &&
1163 !list_empty(&device->req_queue)) {
1164 /* Not Ready to Ready after long busy ? */
1165 struct tape_request *req;
1166 req = list_entry(device->req_queue.next,
1167 struct tape_request, list);
1168 if (req->status == TAPE_REQUEST_LONG_BUSY) {
1169 DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id);
1170 if (del_timer(&device->lb_timeout)) {
1171 device->lb_timeout.data = 0UL;
1172 tape_put_device(device);
1173 __tape_start_next_request(device);
1175 return;
1178 if (irb->scsw.cmd.dstat != 0x0c) {
1179 /* Set the 'ONLINE' flag depending on sense byte 1 */
1180 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1181 device->tape_generic_status |= GMT_ONLINE(~0);
1182 else
1183 device->tape_generic_status &= ~GMT_ONLINE(~0);
1186 * Any request that does not come back with channel end
1187 * and device end is unusual. Log the sense data.
1189 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1190 tape_dump_sense_dbf(device, request, irb);
1191 } else {
1192 /* Upon normal completion the device _is_ online */
1193 device->tape_generic_status |= GMT_ONLINE(~0);
1195 if (device->tape_state == TS_NOT_OPER) {
1196 DBF_EVENT(6, "tape:device is not operational\n");
1197 return;
1201 * Request that were canceled still come back with an interrupt.
1202 * To detect these request the state will be set to TAPE_REQUEST_DONE.
1204 if(request != NULL && request->status == TAPE_REQUEST_DONE) {
1205 __tape_end_request(device, request, -EIO);
1206 return;
1209 rc = device->discipline->irq(device, request, irb);
1211 * rc < 0 : request finished unsuccessfully.
1212 * rc == TAPE_IO_SUCCESS: request finished successfully.
1213 * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1214 * rc == TAPE_IO_RETRY: request finished but needs another go.
1215 * rc == TAPE_IO_STOP: request needs to get terminated.
1217 switch (rc) {
1218 case TAPE_IO_SUCCESS:
1219 /* Upon normal completion the device _is_ online */
1220 device->tape_generic_status |= GMT_ONLINE(~0);
1221 __tape_end_request(device, request, rc);
1222 break;
1223 case TAPE_IO_PENDING:
1224 break;
1225 case TAPE_IO_LONG_BUSY:
1226 device->lb_timeout.data =
1227 (unsigned long) tape_get_device(device);
1228 device->lb_timeout.expires = jiffies +
1229 LONG_BUSY_TIMEOUT * HZ;
1230 DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id);
1231 add_timer(&device->lb_timeout);
1232 request->status = TAPE_REQUEST_LONG_BUSY;
1233 break;
1234 case TAPE_IO_RETRY:
1235 rc = __tape_start_io(device, request);
1236 if (rc)
1237 __tape_end_request(device, request, rc);
1238 break;
1239 case TAPE_IO_STOP:
1240 rc = __tape_cancel_io(device, request);
1241 if (rc)
1242 __tape_end_request(device, request, rc);
1243 break;
1244 default:
1245 if (rc > 0) {
1246 DBF_EVENT(6, "xunknownrc\n");
1247 __tape_end_request(device, request, -EIO);
1248 } else {
1249 __tape_end_request(device, request, rc);
1251 break;
1256 * Tape device open function used by tape_char & tape_block frontends.
1259 tape_open(struct tape_device *device)
1261 int rc;
1263 spin_lock_irq(get_ccwdev_lock(device->cdev));
1264 if (device->tape_state == TS_NOT_OPER) {
1265 DBF_EVENT(6, "TAPE:nodev\n");
1266 rc = -ENODEV;
1267 } else if (device->tape_state == TS_IN_USE) {
1268 DBF_EVENT(6, "TAPE:dbusy\n");
1269 rc = -EBUSY;
1270 } else if (device->tape_state == TS_BLKUSE) {
1271 DBF_EVENT(6, "TAPE:dbusy\n");
1272 rc = -EBUSY;
1273 } else if (device->discipline != NULL &&
1274 !try_module_get(device->discipline->owner)) {
1275 DBF_EVENT(6, "TAPE:nodisc\n");
1276 rc = -ENODEV;
1277 } else {
1278 tape_state_set(device, TS_IN_USE);
1279 rc = 0;
1281 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1282 return rc;
1286 * Tape device release function used by tape_char & tape_block frontends.
1289 tape_release(struct tape_device *device)
1291 spin_lock_irq(get_ccwdev_lock(device->cdev));
1292 if (device->tape_state == TS_IN_USE)
1293 tape_state_set(device, TS_UNUSED);
1294 module_put(device->discipline->owner);
1295 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1296 return 0;
1300 * Execute a magnetic tape command a number of times.
1303 tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1305 tape_mtop_fn fn;
1306 int rc;
1308 DBF_EVENT(6, "TAPE:mtio\n");
1309 DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1310 DBF_EVENT(6, "TAPE:arg: %x\n", mt_count);
1312 if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1313 return -EINVAL;
1314 fn = device->discipline->mtop_array[mt_op];
1315 if (fn == NULL)
1316 return -EINVAL;
1318 /* We assume that the backends can handle count up to 500. */
1319 if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF ||
1320 mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) {
1321 rc = 0;
1322 for (; mt_count > 500; mt_count -= 500)
1323 if ((rc = fn(device, 500)) != 0)
1324 break;
1325 if (rc == 0)
1326 rc = fn(device, mt_count);
1327 } else
1328 rc = fn(device, mt_count);
1329 return rc;
1334 * Tape init function.
1336 static int
1337 tape_init (void)
1339 TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
1340 debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1341 #ifdef DBF_LIKE_HELL
1342 debug_set_level(TAPE_DBF_AREA, 6);
1343 #endif
1344 DBF_EVENT(3, "tape init\n");
1345 tape_proc_init();
1346 tapechar_init ();
1347 tapeblock_init ();
1348 return 0;
1352 * Tape exit function.
1354 static void
1355 tape_exit(void)
1357 DBF_EVENT(6, "tape exit\n");
1359 /* Get rid of the frontends */
1360 tapechar_exit();
1361 tapeblock_exit();
1362 tape_proc_cleanup();
1363 debug_unregister (TAPE_DBF_AREA);
1366 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1367 "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1368 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1369 MODULE_LICENSE("GPL");
1371 module_init(tape_init);
1372 module_exit(tape_exit);
1374 EXPORT_SYMBOL(tape_generic_remove);
1375 EXPORT_SYMBOL(tape_generic_probe);
1376 EXPORT_SYMBOL(tape_generic_online);
1377 EXPORT_SYMBOL(tape_generic_offline);
1378 EXPORT_SYMBOL(tape_generic_pm_suspend);
1379 EXPORT_SYMBOL(tape_put_device);
1380 EXPORT_SYMBOL(tape_get_device);
1381 EXPORT_SYMBOL(tape_state_verbose);
1382 EXPORT_SYMBOL(tape_op_verbose);
1383 EXPORT_SYMBOL(tape_state_set);
1384 EXPORT_SYMBOL(tape_med_state_set);
1385 EXPORT_SYMBOL(tape_alloc_request);
1386 EXPORT_SYMBOL(tape_free_request);
1387 EXPORT_SYMBOL(tape_dump_sense_dbf);
1388 EXPORT_SYMBOL(tape_do_io);
1389 EXPORT_SYMBOL(tape_do_io_async);
1390 EXPORT_SYMBOL(tape_do_io_interruptible);
1391 EXPORT_SYMBOL(tape_cancel_io);
1392 EXPORT_SYMBOL(tape_mtop);