Merge branch 'upstream-fixes' into upstream
[linux-2.6] / drivers / s390 / char / tape_core.c
1 /*
2  *  drivers/s390/char/tape_core.c
3  *    basic function of the tape device driver
4  *
5  *  S390 and zSeries version
6  *    Copyright (C) 2001,2005 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *    Author(s): Carsten Otte <cotte@de.ibm.com>
8  *               Michael Holzheu <holzheu@de.ibm.com>
9  *               Tuan Ngo-Anh <ngoanh@de.ibm.com>
10  *               Martin Schwidefsky <schwidefsky@de.ibm.com>
11  *               Stefan Bader <shbader@de.ibm.com>
12  */
13
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>
20
21 #include <asm/types.h>       // for variable types
22
23 #define TAPE_DBF_AREA   tape_core_dbf
24
25 #include "tape.h"
26 #include "tape_std.h"
27
28 #define PRINTK_HEADER "TAPE_CORE: "
29
30 static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *);
31 static void tape_delayed_next_request(void * data);
32
33 /*
34  * One list to contain all tape devices of all disciplines, so
35  * we can assign the devices to minor numbers of the same major
36  * The list is protected by the rwlock
37  */
38 static struct list_head tape_device_list = LIST_HEAD_INIT(tape_device_list);
39 static DEFINE_RWLOCK(tape_device_lock);
40
41 /*
42  * Pointer to debug area.
43  */
44 debug_info_t *TAPE_DBF_AREA = NULL;
45 EXPORT_SYMBOL(TAPE_DBF_AREA);
46
47 /*
48  * Printable strings for tape enumerations.
49  */
50 const char *tape_state_verbose[TS_SIZE] =
51 {
52         [TS_UNUSED]   = "UNUSED",
53         [TS_IN_USE]   = "IN_USE",
54         [TS_BLKUSE]   = "BLKUSE",
55         [TS_INIT]     = "INIT  ",
56         [TS_NOT_OPER] = "NOT_OP"
57 };
58
59 const char *tape_op_verbose[TO_SIZE] =
60 {
61         [TO_BLOCK] = "BLK",     [TO_BSB] = "BSB",
62         [TO_BSF] = "BSF",       [TO_DSE] = "DSE",
63         [TO_FSB] = "FSB",       [TO_FSF] = "FSF",
64         [TO_LBL] = "LBL",       [TO_NOP] = "NOP",
65         [TO_RBA] = "RBA",       [TO_RBI] = "RBI",
66         [TO_RFO] = "RFO",       [TO_REW] = "REW",
67         [TO_RUN] = "RUN",       [TO_WRI] = "WRI",
68         [TO_WTM] = "WTM",       [TO_MSEN] = "MSN",
69         [TO_LOAD] = "LOA",      [TO_READ_CONFIG] = "RCF",
70         [TO_READ_ATTMSG] = "RAT",
71         [TO_DIS] = "DIS",       [TO_ASSIGN] = "ASS",
72         [TO_UNASSIGN] = "UAS"
73 };
74
75 static inline int
76 busid_to_int(char *bus_id)
77 {
78         int     dec;
79         int     d;
80         char *  s;
81
82         for(s = bus_id, d = 0; *s != '\0' && *s != '.'; s++)
83                 d = (d * 10) + (*s - '0');
84         dec = d;
85         for(s++, d = 0; *s != '\0' && *s != '.'; s++)
86                 d = (d * 10) + (*s - '0');
87         dec = (dec << 8) + d;
88
89         for(s++; *s != '\0'; s++) {
90                 if (*s >= '0' && *s <= '9') {
91                         d = *s - '0';
92                 } else if (*s >= 'a' && *s <= 'f') {
93                         d = *s - 'a' + 10;
94                 } else {
95                         d = *s - 'A' + 10;
96                 }
97                 dec = (dec << 4) + d;
98         }
99
100         return dec;
101 }
102
103 /*
104  * Some channel attached tape specific attributes.
105  *
106  * FIXME: In the future the first_minor and blocksize attribute should be
107  *        replaced by a link to the cdev tree.
108  */
109 static ssize_t
110 tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf)
111 {
112         struct tape_device *tdev;
113
114         tdev = (struct tape_device *) dev->driver_data;
115         return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state);
116 }
117
118 static
119 DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL);
120
121 static ssize_t
122 tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf)
123 {
124         struct tape_device *tdev;
125
126         tdev = (struct tape_device *) dev->driver_data;
127         return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor);
128 }
129
130 static
131 DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL);
132
133 static ssize_t
134 tape_state_show(struct device *dev, struct device_attribute *attr, char *buf)
135 {
136         struct tape_device *tdev;
137
138         tdev = (struct tape_device *) dev->driver_data;
139         return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ?
140                 "OFFLINE" : tape_state_verbose[tdev->tape_state]);
141 }
142
143 static
144 DEVICE_ATTR(state, 0444, tape_state_show, NULL);
145
146 static ssize_t
147 tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf)
148 {
149         struct tape_device *tdev;
150         ssize_t rc;
151
152         tdev = (struct tape_device *) dev->driver_data;
153         if (tdev->first_minor < 0)
154                 return scnprintf(buf, PAGE_SIZE, "N/A\n");
155
156         spin_lock_irq(get_ccwdev_lock(tdev->cdev));
157         if (list_empty(&tdev->req_queue))
158                 rc = scnprintf(buf, PAGE_SIZE, "---\n");
159         else {
160                 struct tape_request *req;
161
162                 req = list_entry(tdev->req_queue.next, struct tape_request,
163                         list);
164                 rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]);
165         }
166         spin_unlock_irq(get_ccwdev_lock(tdev->cdev));
167         return rc;
168 }
169
170 static
171 DEVICE_ATTR(operation, 0444, tape_operation_show, NULL);
172
173 static ssize_t
174 tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf)
175 {
176         struct tape_device *tdev;
177
178         tdev = (struct tape_device *) dev->driver_data;
179
180         return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size);
181 }
182
183 static
184 DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL);
185
186 static struct attribute *tape_attrs[] = {
187         &dev_attr_medium_state.attr,
188         &dev_attr_first_minor.attr,
189         &dev_attr_state.attr,
190         &dev_attr_operation.attr,
191         &dev_attr_blocksize.attr,
192         NULL
193 };
194
195 static struct attribute_group tape_attr_group = {
196         .attrs = tape_attrs,
197 };
198
199 /*
200  * Tape state functions
201  */
202 void
203 tape_state_set(struct tape_device *device, enum tape_state newstate)
204 {
205         const char *str;
206
207         if (device->tape_state == TS_NOT_OPER) {
208                 DBF_EVENT(3, "ts_set err: not oper\n");
209                 return;
210         }
211         DBF_EVENT(4, "ts. dev:  %x\n", device->first_minor);
212         DBF_EVENT(4, "old ts:\t\n");
213         if (device->tape_state < TS_SIZE && device->tape_state >=0 )
214                 str = tape_state_verbose[device->tape_state];
215         else
216                 str = "UNKNOWN TS";
217         DBF_EVENT(4, "%s\n", str);
218         DBF_EVENT(4, "new ts:\t\n");
219         if (newstate < TS_SIZE && newstate >= 0)
220                 str = tape_state_verbose[newstate];
221         else
222                 str = "UNKNOWN TS";
223         DBF_EVENT(4, "%s\n", str);
224         device->tape_state = newstate;
225         wake_up(&device->state_change_wq);
226 }
227
228 void
229 tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
230 {
231         if (device->medium_state == newstate)
232                 return;
233         switch(newstate){
234         case MS_UNLOADED:
235                 device->tape_generic_status |= GMT_DR_OPEN(~0);
236                 PRINT_INFO("(%s): Tape is unloaded\n",
237                            device->cdev->dev.bus_id);
238                 break;
239         case MS_LOADED:
240                 device->tape_generic_status &= ~GMT_DR_OPEN(~0);
241                 PRINT_INFO("(%s): Tape has been mounted\n",
242                            device->cdev->dev.bus_id);
243                 break;
244         default:
245                 // print nothing
246                 break;
247         }
248         device->medium_state = newstate;
249         wake_up(&device->state_change_wq);
250 }
251
252 /*
253  * Stop running ccw. Has to be called with the device lock held.
254  */
255 static inline int
256 __tape_cancel_io(struct tape_device *device, struct tape_request *request)
257 {
258         int retries;
259         int rc;
260
261         /* Check if interrupt has already been processed */
262         if (request->callback == NULL)
263                 return 0;
264
265         rc = 0;
266         for (retries = 0; retries < 5; retries++) {
267                 rc = ccw_device_clear(device->cdev, (long) request);
268
269                 switch (rc) {
270                         case 0:
271                                 request->status = TAPE_REQUEST_DONE;
272                                 return 0;
273                         case -EBUSY:
274                                 request->status = TAPE_REQUEST_CANCEL;
275                                 schedule_work(&device->tape_dnr);
276                                 return 0;
277                         case -ENODEV:
278                                 DBF_EXCEPTION(2, "device gone, retry\n");
279                                 break;
280                         case -EIO:
281                                 DBF_EXCEPTION(2, "I/O error, retry\n");
282                                 break;
283                         default:
284                                 BUG();
285                 }
286         }
287
288         return rc;
289 }
290
291 /*
292  * Add device into the sorted list, giving it the first
293  * available minor number.
294  */
295 static int
296 tape_assign_minor(struct tape_device *device)
297 {
298         struct tape_device *tmp;
299         int minor;
300
301         minor = 0;
302         write_lock(&tape_device_lock);
303         list_for_each_entry(tmp, &tape_device_list, node) {
304                 if (minor < tmp->first_minor)
305                         break;
306                 minor += TAPE_MINORS_PER_DEV;
307         }
308         if (minor >= 256) {
309                 write_unlock(&tape_device_lock);
310                 return -ENODEV;
311         }
312         device->first_minor = minor;
313         list_add_tail(&device->node, &tmp->node);
314         write_unlock(&tape_device_lock);
315         return 0;
316 }
317
318 /* remove device from the list */
319 static void
320 tape_remove_minor(struct tape_device *device)
321 {
322         write_lock(&tape_device_lock);
323         list_del_init(&device->node);
324         device->first_minor = -1;
325         write_unlock(&tape_device_lock);
326 }
327
328 /*
329  * Set a device online.
330  *
331  * This function is called by the common I/O layer to move a device from the
332  * detected but offline into the online state.
333  * If we return an error (RC < 0) the device remains in the offline state. This
334  * can happen if the device is assigned somewhere else, for example.
335  */
336 int
337 tape_generic_online(struct tape_device *device,
338                    struct tape_discipline *discipline)
339 {
340         int rc;
341
342         DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
343
344         if (device->tape_state != TS_INIT) {
345                 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
346                 return -EINVAL;
347         }
348
349         /* Let the discipline have a go at the device. */
350         device->discipline = discipline;
351         if (!try_module_get(discipline->owner)) {
352                 PRINT_ERR("Cannot get module. Module gone.\n");
353                 return -EINVAL;
354         }
355
356         rc = discipline->setup_device(device);
357         if (rc)
358                 goto out;
359         rc = tape_assign_minor(device);
360         if (rc)
361                 goto out_discipline;
362
363         rc = tapechar_setup_device(device);
364         if (rc)
365                 goto out_minor;
366         rc = tapeblock_setup_device(device);
367         if (rc)
368                 goto out_char;
369
370         tape_state_set(device, TS_UNUSED);
371
372         DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
373
374         return 0;
375
376 out_char:
377         tapechar_cleanup_device(device);
378 out_discipline:
379         device->discipline->cleanup_device(device);
380         device->discipline = NULL;
381 out_minor:
382         tape_remove_minor(device);
383 out:
384         module_put(discipline->owner);
385         return rc;
386 }
387
388 static inline void
389 tape_cleanup_device(struct tape_device *device)
390 {
391         tapeblock_cleanup_device(device);
392         tapechar_cleanup_device(device);
393         device->discipline->cleanup_device(device);
394         module_put(device->discipline->owner);
395         tape_remove_minor(device);
396         tape_med_state_set(device, MS_UNKNOWN);
397 }
398
399 /*
400  * Set device offline.
401  *
402  * Called by the common I/O layer if the drive should set offline on user
403  * request. We may prevent this by returning an error.
404  * Manual offline is only allowed while the drive is not in use.
405  */
406 int
407 tape_generic_offline(struct tape_device *device)
408 {
409         if (!device) {
410                 PRINT_ERR("tape_generic_offline: no such device\n");
411                 return -ENODEV;
412         }
413
414         DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
415                 device->cdev_id, device);
416
417         spin_lock_irq(get_ccwdev_lock(device->cdev));
418         switch (device->tape_state) {
419                 case TS_INIT:
420                 case TS_NOT_OPER:
421                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
422                         break;
423                 case TS_UNUSED:
424                         tape_state_set(device, TS_INIT);
425                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
426                         tape_cleanup_device(device);
427                         break;
428                 default:
429                         DBF_EVENT(3, "(%08x): Set offline failed "
430                                 "- drive in use.\n",
431                                 device->cdev_id);
432                         PRINT_WARN("(%s): Set offline failed "
433                                 "- drive in use.\n",
434                                 device->cdev->dev.bus_id);
435                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
436                         return -EBUSY;
437         }
438
439         DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
440         return 0;
441 }
442
443 /*
444  * Allocate memory for a new device structure.
445  */
446 static struct tape_device *
447 tape_alloc_device(void)
448 {
449         struct tape_device *device;
450
451         device = kzalloc(sizeof(struct tape_device), GFP_KERNEL);
452         if (device == NULL) {
453                 DBF_EXCEPTION(2, "ti:no mem\n");
454                 PRINT_INFO ("can't allocate memory for "
455                             "tape info structure\n");
456                 return ERR_PTR(-ENOMEM);
457         }
458         device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA);
459         if (device->modeset_byte == NULL) {
460                 DBF_EXCEPTION(2, "ti:no mem\n");
461                 PRINT_INFO("can't allocate memory for modeset byte\n");
462                 kfree(device);
463                 return ERR_PTR(-ENOMEM);
464         }
465         INIT_LIST_HEAD(&device->req_queue);
466         INIT_LIST_HEAD(&device->node);
467         init_waitqueue_head(&device->state_change_wq);
468         device->tape_state = TS_INIT;
469         device->medium_state = MS_UNKNOWN;
470         *device->modeset_byte = 0;
471         device->first_minor = -1;
472         atomic_set(&device->ref_count, 1);
473         INIT_WORK(&device->tape_dnr, tape_delayed_next_request, device);
474
475         return device;
476 }
477
478 /*
479  * Get a reference to an existing device structure. This will automatically
480  * increment the reference count.
481  */
482 struct tape_device *
483 tape_get_device_reference(struct tape_device *device)
484 {
485         DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device,
486                 atomic_inc_return(&device->ref_count));
487
488         return device;
489 }
490
491 /*
492  * Decrease the reference counter of a devices structure. If the
493  * reference counter reaches zero free the device structure.
494  * The function returns a NULL pointer to be used by the caller
495  * for clearing reference pointers.
496  */
497 struct tape_device *
498 tape_put_device(struct tape_device *device)
499 {
500         int remain;
501
502         remain = atomic_dec_return(&device->ref_count);
503         if (remain > 0) {
504                 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain);
505         } else {
506                 if (remain < 0) {
507                         DBF_EVENT(4, "put device without reference\n");
508                         PRINT_ERR("put device without reference\n");
509                 } else {
510                         DBF_EVENT(4, "tape_free_device(%p)\n", device);
511                         kfree(device->modeset_byte);
512                         kfree(device);
513                 }
514         }
515
516         return NULL;                    
517 }
518
519 /*
520  * Find tape device by a device index.
521  */
522 struct tape_device *
523 tape_get_device(int devindex)
524 {
525         struct tape_device *device, *tmp;
526
527         device = ERR_PTR(-ENODEV);
528         read_lock(&tape_device_lock);
529         list_for_each_entry(tmp, &tape_device_list, node) {
530                 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
531                         device = tape_get_device_reference(tmp);
532                         break;
533                 }
534         }
535         read_unlock(&tape_device_lock);
536         return device;
537 }
538
539 /*
540  * Driverfs tape probe function.
541  */
542 int
543 tape_generic_probe(struct ccw_device *cdev)
544 {
545         struct tape_device *device;
546         int ret;
547
548         device = tape_alloc_device();
549         if (IS_ERR(device))
550                 return -ENODEV;
551         ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
552         ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
553         if (ret) {
554                 tape_put_device(device);
555                 PRINT_ERR("probe failed for tape device %s\n", cdev->dev.bus_id);
556                 return ret;
557         }
558         cdev->dev.driver_data = device;
559         cdev->handler = __tape_do_irq;
560         device->cdev = cdev;
561         device->cdev_id = busid_to_int(cdev->dev.bus_id);
562         PRINT_INFO("tape device %s found\n", cdev->dev.bus_id);
563         return ret;
564 }
565
566 static inline void
567 __tape_discard_requests(struct tape_device *device)
568 {
569         struct tape_request *   request;
570         struct list_head *      l, *n;
571
572         list_for_each_safe(l, n, &device->req_queue) {
573                 request = list_entry(l, struct tape_request, list);
574                 if (request->status == TAPE_REQUEST_IN_IO)
575                         request->status = TAPE_REQUEST_DONE;
576                 list_del(&request->list);
577
578                 /* Decrease ref_count for removed request. */
579                 request->device = tape_put_device(device);
580                 request->rc = -EIO;
581                 if (request->callback != NULL)
582                         request->callback(request, request->callback_data);
583         }
584 }
585
586 /*
587  * Driverfs tape remove function.
588  *
589  * This function is called whenever the common I/O layer detects the device
590  * gone. This can happen at any time and we cannot refuse.
591  */
592 void
593 tape_generic_remove(struct ccw_device *cdev)
594 {
595         struct tape_device *    device;
596
597         device = cdev->dev.driver_data;
598         if (!device) {
599                 PRINT_ERR("No device pointer in tape_generic_remove!\n");
600                 return;
601         }
602         DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
603
604         spin_lock_irq(get_ccwdev_lock(device->cdev));
605         switch (device->tape_state) {
606                 case TS_INIT:
607                         tape_state_set(device, TS_NOT_OPER);
608                 case TS_NOT_OPER:
609                         /*
610                          * Nothing to do.
611                          */
612                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
613                         break;
614                 case TS_UNUSED:
615                         /*
616                          * Need only to release the device.
617                          */
618                         tape_state_set(device, TS_NOT_OPER);
619                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
620                         tape_cleanup_device(device);
621                         break;
622                 default:
623                         /*
624                          * There may be requests on the queue. We will not get
625                          * an interrupt for a request that was running. So we
626                          * just post them all as I/O errors.
627                          */
628                         DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
629                                 device->cdev_id);
630                         PRINT_WARN("(%s): Drive in use vanished - "
631                                 "expect trouble!\n",
632                                 device->cdev->dev.bus_id);
633                         PRINT_WARN("State was %i\n", device->tape_state);
634                         tape_state_set(device, TS_NOT_OPER);
635                         __tape_discard_requests(device);
636                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
637                         tape_cleanup_device(device);
638         }
639
640         if (cdev->dev.driver_data != NULL) {
641                 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
642                 cdev->dev.driver_data = tape_put_device(cdev->dev.driver_data);
643         }
644 }
645
646 /*
647  * Allocate a new tape ccw request
648  */
649 struct tape_request *
650 tape_alloc_request(int cplength, int datasize)
651 {
652         struct tape_request *request;
653
654         if (datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
655                 BUG();
656
657         DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
658
659         request = kzalloc(sizeof(struct tape_request), GFP_KERNEL);
660         if (request == NULL) {
661                 DBF_EXCEPTION(1, "cqra nomem\n");
662                 return ERR_PTR(-ENOMEM);
663         }
664         /* allocate channel program */
665         if (cplength > 0) {
666                 request->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
667                                           GFP_ATOMIC | GFP_DMA);
668                 if (request->cpaddr == NULL) {
669                         DBF_EXCEPTION(1, "cqra nomem\n");
670                         kfree(request);
671                         return ERR_PTR(-ENOMEM);
672                 }
673         }
674         /* alloc small kernel buffer */
675         if (datasize > 0) {
676                 request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA);
677                 if (request->cpdata == NULL) {
678                         DBF_EXCEPTION(1, "cqra nomem\n");
679                         kfree(request->cpaddr);
680                         kfree(request);
681                         return ERR_PTR(-ENOMEM);
682                 }
683         }
684         DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
685                 request->cpdata);
686
687         return request;
688 }
689
690 /*
691  * Free tape ccw request
692  */
693 void
694 tape_free_request (struct tape_request * request)
695 {
696         DBF_LH(6, "Free request %p\n", request);
697
698         if (request->device != NULL) {
699                 request->device = tape_put_device(request->device);
700         }
701         kfree(request->cpdata);
702         kfree(request->cpaddr);
703         kfree(request);
704 }
705
706 static inline int
707 __tape_start_io(struct tape_device *device, struct tape_request *request)
708 {
709         int rc;
710
711 #ifdef CONFIG_S390_TAPE_BLOCK
712         if (request->op == TO_BLOCK)
713                 device->discipline->check_locate(device, request);
714 #endif
715         rc = ccw_device_start(
716                 device->cdev,
717                 request->cpaddr,
718                 (unsigned long) request,
719                 0x00,
720                 request->options
721         );
722         if (rc == 0) {
723                 request->status = TAPE_REQUEST_IN_IO;
724         } else if (rc == -EBUSY) {
725                 /* The common I/O subsystem is currently busy. Retry later. */
726                 request->status = TAPE_REQUEST_QUEUED;
727                 schedule_work(&device->tape_dnr);
728                 rc = 0;
729         } else {
730                 /* Start failed. Remove request and indicate failure. */
731                 DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc);
732         }
733         return rc;
734 }
735
736 static inline void
737 __tape_start_next_request(struct tape_device *device)
738 {
739         struct list_head *l, *n;
740         struct tape_request *request;
741         int rc;
742
743         DBF_LH(6, "__tape_start_next_request(%p)\n", device);
744         /*
745          * Try to start each request on request queue until one is
746          * started successful.
747          */
748         list_for_each_safe(l, n, &device->req_queue) {
749                 request = list_entry(l, struct tape_request, list);
750
751                 /*
752                  * Avoid race condition if bottom-half was triggered more than
753                  * once.
754                  */
755                 if (request->status == TAPE_REQUEST_IN_IO)
756                         return;
757                 /*
758                  * Request has already been stopped. We have to wait until
759                  * the request is removed from the queue in the interrupt
760                  * handling.
761                  */
762                 if (request->status == TAPE_REQUEST_DONE)
763                         return;
764
765                 /*
766                  * We wanted to cancel the request but the common I/O layer
767                  * was busy at that time. This can only happen if this
768                  * function is called by delayed_next_request.
769                  * Otherwise we start the next request on the queue.
770                  */
771                 if (request->status == TAPE_REQUEST_CANCEL) {
772                         rc = __tape_cancel_io(device, request);
773                 } else {
774                         rc = __tape_start_io(device, request);
775                 }
776                 if (rc == 0)
777                         return;
778
779                 /* Set ending status. */
780                 request->rc = rc;
781                 request->status = TAPE_REQUEST_DONE;
782
783                 /* Remove from request queue. */
784                 list_del(&request->list);
785
786                 /* Do callback. */
787                 if (request->callback != NULL)
788                         request->callback(request, request->callback_data);
789         }
790 }
791
792 static void
793 tape_delayed_next_request(void *data)
794 {
795         struct tape_device *    device;
796
797         device = (struct tape_device *) data;
798         DBF_LH(6, "tape_delayed_next_request(%p)\n", device);
799         spin_lock_irq(get_ccwdev_lock(device->cdev));
800         __tape_start_next_request(device);
801         spin_unlock_irq(get_ccwdev_lock(device->cdev));
802 }
803
804 static inline void
805 __tape_end_request(
806         struct tape_device *    device,
807         struct tape_request *   request,
808         int                     rc)
809 {
810         DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc);
811         if (request) {
812                 request->rc = rc;
813                 request->status = TAPE_REQUEST_DONE;
814
815                 /* Remove from request queue. */
816                 list_del(&request->list);
817
818                 /* Do callback. */
819                 if (request->callback != NULL)
820                         request->callback(request, request->callback_data);
821         }
822
823         /* Start next request. */
824         if (!list_empty(&device->req_queue))
825                 __tape_start_next_request(device);
826 }
827
828 /*
829  * Write sense data to console/dbf
830  */
831 void
832 tape_dump_sense(struct tape_device* device, struct tape_request *request,
833                 struct irb *irb)
834 {
835         unsigned int *sptr;
836
837         PRINT_INFO("-------------------------------------------------\n");
838         PRINT_INFO("DSTAT : %02x  CSTAT: %02x   CPA: %04x\n",
839                    irb->scsw.dstat, irb->scsw.cstat, irb->scsw.cpa);
840         PRINT_INFO("DEVICE: %s\n", device->cdev->dev.bus_id);
841         if (request != NULL)
842                 PRINT_INFO("OP    : %s\n", tape_op_verbose[request->op]);
843
844         sptr = (unsigned int *) irb->ecw;
845         PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
846                    sptr[0], sptr[1], sptr[2], sptr[3]);
847         PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
848                    sptr[4], sptr[5], sptr[6], sptr[7]);
849         PRINT_INFO("--------------------------------------------------\n");
850 }
851
852 /*
853  * Write sense data to dbf
854  */
855 void
856 tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
857                     struct irb *irb)
858 {
859         unsigned int *sptr;
860         const char* op;
861
862         if (request != NULL)
863                 op = tape_op_verbose[request->op];
864         else
865                 op = "---";
866         DBF_EVENT(3, "DSTAT : %02x   CSTAT: %02x\n",
867                   irb->scsw.dstat,irb->scsw.cstat);
868         DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
869         sptr = (unsigned int *) irb->ecw;
870         DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
871         DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
872         DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
873         DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
874 }
875
876 /*
877  * I/O helper function. Adds the request to the request queue
878  * and starts it if the tape is idle. Has to be called with
879  * the device lock held.
880  */
881 static inline int
882 __tape_start_request(struct tape_device *device, struct tape_request *request)
883 {
884         int rc;
885
886         switch (request->op) {
887                 case TO_MSEN:
888                 case TO_ASSIGN:
889                 case TO_UNASSIGN:
890                 case TO_READ_ATTMSG:
891                         if (device->tape_state == TS_INIT)
892                                 break;
893                         if (device->tape_state == TS_UNUSED)
894                                 break;
895                 default:
896                         if (device->tape_state == TS_BLKUSE)
897                                 break;
898                         if (device->tape_state != TS_IN_USE)
899                                 return -ENODEV;
900         }
901
902         /* Increase use count of device for the added request. */
903         request->device = tape_get_device_reference(device);
904
905         if (list_empty(&device->req_queue)) {
906                 /* No other requests are on the queue. Start this one. */
907                 rc = __tape_start_io(device, request);
908                 if (rc)
909                         return rc;
910
911                 DBF_LH(5, "Request %p added for execution.\n", request);
912                 list_add(&request->list, &device->req_queue);
913         } else {
914                 DBF_LH(5, "Request %p add to queue.\n", request);
915                 request->status = TAPE_REQUEST_QUEUED;
916                 list_add_tail(&request->list, &device->req_queue);
917         }
918         return 0;
919 }
920
921 /*
922  * Add the request to the request queue, try to start it if the
923  * tape is idle. Return without waiting for end of i/o.
924  */
925 int
926 tape_do_io_async(struct tape_device *device, struct tape_request *request)
927 {
928         int rc;
929
930         DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
931
932         spin_lock_irq(get_ccwdev_lock(device->cdev));
933         /* Add request to request queue and try to start it. */
934         rc = __tape_start_request(device, request);
935         spin_unlock_irq(get_ccwdev_lock(device->cdev));
936         return rc;
937 }
938
939 /*
940  * tape_do_io/__tape_wake_up
941  * Add the request to the request queue, try to start it if the
942  * tape is idle and wait uninterruptible for its completion.
943  */
944 static void
945 __tape_wake_up(struct tape_request *request, void *data)
946 {
947         request->callback = NULL;
948         wake_up((wait_queue_head_t *) data);
949 }
950
951 int
952 tape_do_io(struct tape_device *device, struct tape_request *request)
953 {
954         wait_queue_head_t wq;
955         int rc;
956
957         init_waitqueue_head(&wq);
958         spin_lock_irq(get_ccwdev_lock(device->cdev));
959         /* Setup callback */
960         request->callback = __tape_wake_up;
961         request->callback_data = &wq;
962         /* Add request to request queue and try to start it. */
963         rc = __tape_start_request(device, request);
964         spin_unlock_irq(get_ccwdev_lock(device->cdev));
965         if (rc)
966                 return rc;
967         /* Request added to the queue. Wait for its completion. */
968         wait_event(wq, (request->callback == NULL));
969         /* Get rc from request */
970         return request->rc;
971 }
972
973 /*
974  * tape_do_io_interruptible/__tape_wake_up_interruptible
975  * Add the request to the request queue, try to start it if the
976  * tape is idle and wait uninterruptible for its completion.
977  */
978 static void
979 __tape_wake_up_interruptible(struct tape_request *request, void *data)
980 {
981         request->callback = NULL;
982         wake_up_interruptible((wait_queue_head_t *) data);
983 }
984
985 int
986 tape_do_io_interruptible(struct tape_device *device,
987                          struct tape_request *request)
988 {
989         wait_queue_head_t wq;
990         int rc;
991
992         init_waitqueue_head(&wq);
993         spin_lock_irq(get_ccwdev_lock(device->cdev));
994         /* Setup callback */
995         request->callback = __tape_wake_up_interruptible;
996         request->callback_data = &wq;
997         rc = __tape_start_request(device, request);
998         spin_unlock_irq(get_ccwdev_lock(device->cdev));
999         if (rc)
1000                 return rc;
1001         /* Request added to the queue. Wait for its completion. */
1002         rc = wait_event_interruptible(wq, (request->callback == NULL));
1003         if (rc != -ERESTARTSYS)
1004                 /* Request finished normally. */
1005                 return request->rc;
1006
1007         /* Interrupted by a signal. We have to stop the current request. */
1008         spin_lock_irq(get_ccwdev_lock(device->cdev));
1009         rc = __tape_cancel_io(device, request);
1010         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1011         if (rc == 0) {
1012                 /* Wait for the interrupt that acknowledges the halt. */
1013                 do {
1014                         rc = wait_event_interruptible(
1015                                 wq,
1016                                 (request->callback == NULL)
1017                         );
1018                 } while (rc == -ERESTARTSYS);
1019
1020                 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
1021                 rc = -ERESTARTSYS;
1022         }
1023         return rc;
1024 }
1025
1026 /*
1027  * Stop running ccw.
1028  */
1029 int
1030 tape_cancel_io(struct tape_device *device, struct tape_request *request)
1031 {
1032         int rc;
1033
1034         spin_lock_irq(get_ccwdev_lock(device->cdev));
1035         rc = __tape_cancel_io(device, request);
1036         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1037         return rc;
1038 }
1039
1040 /*
1041  * Tape interrupt routine, called from the ccw_device layer
1042  */
1043 static void
1044 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1045 {
1046         struct tape_device *device;
1047         struct tape_request *request;
1048         int rc;
1049
1050         device = (struct tape_device *) cdev->dev.driver_data;
1051         if (device == NULL) {
1052                 PRINT_ERR("could not get device structure for %s "
1053                           "in interrupt\n", cdev->dev.bus_id);
1054                 return;
1055         }
1056         request = (struct tape_request *) intparm;
1057
1058         DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
1059
1060         /* On special conditions irb is an error pointer */
1061         if (IS_ERR(irb)) {
1062                 /* FIXME: What to do with the request? */
1063                 switch (PTR_ERR(irb)) {
1064                         case -ETIMEDOUT:
1065                                 PRINT_WARN("(%s): Request timed out\n",
1066                                         cdev->dev.bus_id);
1067                         case -EIO:
1068                                 __tape_end_request(device, request, -EIO);
1069                                 break;
1070                         default:
1071                                 PRINT_ERR("(%s): Unexpected i/o error %li\n",
1072                                         cdev->dev.bus_id,
1073                                         PTR_ERR(irb));
1074                 }
1075                 return;
1076         }
1077
1078         /*
1079          * If the condition code is not zero and the start function bit is
1080          * still set, this is an deferred error and the last start I/O did
1081          * not succeed. At this point the condition that caused the deferred
1082          * error might still apply. So we just schedule the request to be
1083          * started later.
1084          */
1085         if (irb->scsw.cc != 0 && (irb->scsw.fctl & SCSW_FCTL_START_FUNC) &&
1086             (request->status == TAPE_REQUEST_IN_IO)) {
1087                 DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n",
1088                         device->cdev_id, irb->scsw.cc, irb->scsw.fctl);
1089                 request->status = TAPE_REQUEST_QUEUED;
1090                 schedule_delayed_work(&device->tape_dnr, HZ);
1091                 return;
1092         }
1093
1094         /* May be an unsolicited irq */
1095         if(request != NULL)
1096                 request->rescnt = irb->scsw.count;
1097
1098         if (irb->scsw.dstat != 0x0c) {
1099                 /* Set the 'ONLINE' flag depending on sense byte 1 */
1100                 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1101                         device->tape_generic_status |= GMT_ONLINE(~0);
1102                 else
1103                         device->tape_generic_status &= ~GMT_ONLINE(~0);
1104
1105                 /*
1106                  * Any request that does not come back with channel end
1107                  * and device end is unusual. Log the sense data.
1108                  */
1109                 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1110                 tape_dump_sense_dbf(device, request, irb);
1111         } else {
1112                 /* Upon normal completion the device _is_ online */
1113                 device->tape_generic_status |= GMT_ONLINE(~0);
1114         }
1115         if (device->tape_state == TS_NOT_OPER) {
1116                 DBF_EVENT(6, "tape:device is not operational\n");
1117                 return;
1118         }
1119
1120         /*
1121          * Request that were canceled still come back with an interrupt.
1122          * To detect these request the state will be set to TAPE_REQUEST_DONE.
1123          */
1124         if(request != NULL && request->status == TAPE_REQUEST_DONE) {
1125                 __tape_end_request(device, request, -EIO);
1126                 return;
1127         }
1128
1129         rc = device->discipline->irq(device, request, irb);
1130         /*
1131          * rc < 0 : request finished unsuccessfully.
1132          * rc == TAPE_IO_SUCCESS: request finished successfully.
1133          * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1134          * rc == TAPE_IO_RETRY: request finished but needs another go.
1135          * rc == TAPE_IO_STOP: request needs to get terminated.
1136          */
1137         switch (rc) {
1138                 case TAPE_IO_SUCCESS:
1139                         /* Upon normal completion the device _is_ online */
1140                         device->tape_generic_status |= GMT_ONLINE(~0);
1141                         __tape_end_request(device, request, rc);
1142                         break;
1143                 case TAPE_IO_PENDING:
1144                         break;
1145                 case TAPE_IO_RETRY:
1146                         rc = __tape_start_io(device, request);
1147                         if (rc)
1148                                 __tape_end_request(device, request, rc);
1149                         break;
1150                 case TAPE_IO_STOP:
1151                         rc = __tape_cancel_io(device, request);
1152                         if (rc)
1153                                 __tape_end_request(device, request, rc);
1154                         break;
1155                 default:
1156                         if (rc > 0) {
1157                                 DBF_EVENT(6, "xunknownrc\n");
1158                                 PRINT_ERR("Invalid return code from discipline "
1159                                         "interrupt function.\n");
1160                                 __tape_end_request(device, request, -EIO);
1161                         } else {
1162                                 __tape_end_request(device, request, rc);
1163                         }
1164                         break;
1165         }
1166 }
1167
1168 /*
1169  * Tape device open function used by tape_char & tape_block frontends.
1170  */
1171 int
1172 tape_open(struct tape_device *device)
1173 {
1174         int rc;
1175
1176         spin_lock(get_ccwdev_lock(device->cdev));
1177         if (device->tape_state == TS_NOT_OPER) {
1178                 DBF_EVENT(6, "TAPE:nodev\n");
1179                 rc = -ENODEV;
1180         } else if (device->tape_state == TS_IN_USE) {
1181                 DBF_EVENT(6, "TAPE:dbusy\n");
1182                 rc = -EBUSY;
1183         } else if (device->tape_state == TS_BLKUSE) {
1184                 DBF_EVENT(6, "TAPE:dbusy\n");
1185                 rc = -EBUSY;
1186         } else if (device->discipline != NULL &&
1187                    !try_module_get(device->discipline->owner)) {
1188                 DBF_EVENT(6, "TAPE:nodisc\n");
1189                 rc = -ENODEV;
1190         } else {
1191                 tape_state_set(device, TS_IN_USE);
1192                 rc = 0;
1193         }
1194         spin_unlock(get_ccwdev_lock(device->cdev));
1195         return rc;
1196 }
1197
1198 /*
1199  * Tape device release function used by tape_char & tape_block frontends.
1200  */
1201 int
1202 tape_release(struct tape_device *device)
1203 {
1204         spin_lock(get_ccwdev_lock(device->cdev));
1205         if (device->tape_state == TS_IN_USE)
1206                 tape_state_set(device, TS_UNUSED);
1207         module_put(device->discipline->owner);
1208         spin_unlock(get_ccwdev_lock(device->cdev));
1209         return 0;
1210 }
1211
1212 /*
1213  * Execute a magnetic tape command a number of times.
1214  */
1215 int
1216 tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1217 {
1218         tape_mtop_fn fn;
1219         int rc;
1220
1221         DBF_EVENT(6, "TAPE:mtio\n");
1222         DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1223         DBF_EVENT(6, "TAPE:arg:  %x\n", mt_count);
1224
1225         if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1226                 return -EINVAL;
1227         fn = device->discipline->mtop_array[mt_op];
1228         if (fn == NULL)
1229                 return -EINVAL;
1230
1231         /* We assume that the backends can handle count up to 500. */
1232         if (mt_op == MTBSR  || mt_op == MTFSR  || mt_op == MTFSF  ||
1233             mt_op == MTBSF  || mt_op == MTFSFM || mt_op == MTBSFM) {
1234                 rc = 0;
1235                 for (; mt_count > 500; mt_count -= 500)
1236                         if ((rc = fn(device, 500)) != 0)
1237                                 break;
1238                 if (rc == 0)
1239                         rc = fn(device, mt_count);
1240         } else
1241                 rc = fn(device, mt_count);
1242         return rc;
1243
1244 }
1245
1246 /*
1247  * Tape init function.
1248  */
1249 static int
1250 tape_init (void)
1251 {
1252         TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long));
1253         debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1254 #ifdef DBF_LIKE_HELL
1255         debug_set_level(TAPE_DBF_AREA, 6);
1256 #endif
1257         DBF_EVENT(3, "tape init\n");
1258         tape_proc_init();
1259         tapechar_init ();
1260         tapeblock_init ();
1261         return 0;
1262 }
1263
1264 /*
1265  * Tape exit function.
1266  */
1267 static void
1268 tape_exit(void)
1269 {
1270         DBF_EVENT(6, "tape exit\n");
1271
1272         /* Get rid of the frontends */
1273         tapechar_exit();
1274         tapeblock_exit();
1275         tape_proc_cleanup();
1276         debug_unregister (TAPE_DBF_AREA);
1277 }
1278
1279 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1280               "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1281 MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver");
1282 MODULE_LICENSE("GPL");
1283
1284 module_init(tape_init);
1285 module_exit(tape_exit);
1286
1287 EXPORT_SYMBOL(tape_generic_remove);
1288 EXPORT_SYMBOL(tape_generic_probe);
1289 EXPORT_SYMBOL(tape_generic_online);
1290 EXPORT_SYMBOL(tape_generic_offline);
1291 EXPORT_SYMBOL(tape_put_device);
1292 EXPORT_SYMBOL(tape_get_device_reference);
1293 EXPORT_SYMBOL(tape_state_verbose);
1294 EXPORT_SYMBOL(tape_op_verbose);
1295 EXPORT_SYMBOL(tape_state_set);
1296 EXPORT_SYMBOL(tape_med_state_set);
1297 EXPORT_SYMBOL(tape_alloc_request);
1298 EXPORT_SYMBOL(tape_free_request);
1299 EXPORT_SYMBOL(tape_dump_sense);
1300 EXPORT_SYMBOL(tape_dump_sense_dbf);
1301 EXPORT_SYMBOL(tape_do_io);
1302 EXPORT_SYMBOL(tape_do_io_async);
1303 EXPORT_SYMBOL(tape_do_io_interruptible);
1304 EXPORT_SYMBOL(tape_cancel_io);
1305 EXPORT_SYMBOL(tape_mtop);