patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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,2002 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  */
12
13 #include <linux/config.h>
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_remove_request(struct tape_device *, struct tape_request *);
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 rwlock_t tape_device_lock = RW_LOCK_UNLOCKED;
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, 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, 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, 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, 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, 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         if (device->tape_state < TO_SIZE && device->tape_state >= 0)
213                 str = tape_state_verbose[device->tape_state];
214         else
215                 str = "UNKNOWN TS";
216         DBF_EVENT(4, "old ts:   %s\n", str);
217         if (device->tape_state < TO_SIZE && device->tape_state >=0 )
218                 str = tape_state_verbose[device->tape_state];
219         else
220                 str = "UNKNOWN TS";
221         DBF_EVENT(4, "%s\n", str);
222         DBF_EVENT(4, "new ts:\t\n");
223         if (newstate < TO_SIZE && newstate >= 0)
224                 str = tape_state_verbose[newstate];
225         else
226                 str = "UNKNOWN TS";
227         DBF_EVENT(4, "%s\n", str);
228         device->tape_state = newstate;
229         wake_up(&device->state_change_wq);
230 }
231
232 void
233 tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate)
234 {
235         if (device->medium_state == newstate)
236                 return;
237         switch(newstate){
238         case MS_UNLOADED:
239                 device->tape_generic_status |= GMT_DR_OPEN(~0);
240                 PRINT_INFO("(%s): Tape is unloaded\n",
241                            device->cdev->dev.bus_id);
242                 break;
243         case MS_LOADED:
244                 device->tape_generic_status &= ~GMT_DR_OPEN(~0);
245                 PRINT_INFO("(%s): Tape has been mounted\n",
246                            device->cdev->dev.bus_id);
247                 break;
248         default:
249                 // print nothing
250                 break;
251         }
252         device->medium_state = newstate;
253         wake_up(&device->state_change_wq);
254 }
255
256 /*
257  * Stop running ccw. Has to be called with the device lock held.
258  */
259 static inline int
260 __tape_halt_io(struct tape_device *device, struct tape_request *request)
261 {
262         int retries;
263         int rc;
264
265         /* Check if interrupt has already been processed */
266         if (request->callback == NULL)
267                 return 0;
268
269         rc = 0;
270         for (retries = 0; retries < 5; retries++) {
271                 rc = ccw_device_clear(device->cdev, (long) request);
272
273                 if (rc == 0) {                     /* Termination successful */
274                         request->rc     = -EIO;
275                         request->status = TAPE_REQUEST_DONE;
276                         return 0;
277                 }
278
279                 if (rc == -ENODEV)
280                         DBF_EXCEPTION(2, "device gone, retry\n");
281                 else if (rc == -EIO)
282                         DBF_EXCEPTION(2, "I/O error, retry\n");
283                 else if (rc == -EBUSY)
284                         DBF_EXCEPTION(2, "device busy, retry late\n");
285                 else
286                         BUG();
287         }
288
289         return rc;
290 }
291
292 /*
293  * Add device into the sorted list, giving it the first
294  * available minor number.
295  */
296 static int
297 tape_assign_minor(struct tape_device *device)
298 {
299         struct tape_device *tmp;
300         int minor;
301
302         minor = 0;
303         write_lock(&tape_device_lock);
304         list_for_each_entry(tmp, &tape_device_list, node) {
305                 if (minor < tmp->first_minor)
306                         break;
307                 minor += TAPE_MINORS_PER_DEV;
308         }
309         if (minor >= 256) {
310                 write_unlock(&tape_device_lock);
311                 return -ENODEV;
312         }
313         device->first_minor = minor;
314         list_add_tail(&device->node, &tmp->node);
315         write_unlock(&tape_device_lock);
316         return 0;
317 }
318
319 /* remove device from the list */
320 static void
321 tape_remove_minor(struct tape_device *device)
322 {
323         write_lock(&tape_device_lock);
324         list_del_init(&device->node);
325         device->first_minor = -1;
326         write_unlock(&tape_device_lock);
327 }
328
329 /*
330  * Set a device online.
331  *
332  * This function is called by the common I/O layer to move a device from the
333  * detected but offline into the online state.
334  * If we return an error (RC < 0) the device remains in the offline state. This
335  * can happen if the device is assigned somewhere else, for example.
336  */
337 int
338 tape_generic_online(struct tape_device *device,
339                    struct tape_discipline *discipline)
340 {
341         int rc;
342
343         DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline);
344
345         if (device->tape_state != TS_INIT) {
346                 DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state);
347                 return -EINVAL;
348         }
349
350         /* Let the discipline have a go at the device. */
351         device->discipline = discipline;
352         rc = discipline->setup_device(device);
353         if (rc)
354                 goto out;
355         rc = tape_assign_minor(device);
356         if (rc)
357                 goto out_discipline;
358
359         rc = tapechar_setup_device(device);
360         if (rc)
361                 goto out_minor;
362         rc = tapeblock_setup_device(device);
363         if (rc)
364                 goto out_char;
365
366         tape_state_set(device, TS_UNUSED);
367
368         DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id);
369
370         return 0;
371
372 out_char:
373         tapechar_cleanup_device(device);
374 out_discipline:
375         device->discipline->cleanup_device(device);
376         device->discipline = NULL;
377 out_minor:
378         tape_remove_minor(device);
379 out:
380         return rc;
381 }
382
383 static inline void
384 tape_cleanup_device(struct tape_device *device)
385 {
386         tapeblock_cleanup_device(device);
387         tapechar_cleanup_device(device);
388         device->discipline->cleanup_device(device);
389         tape_remove_minor(device);
390         tape_med_state_set(device, MS_UNKNOWN);
391 }
392
393 /*
394  * Set device offline.
395  *
396  * Called by the common I/O layer if the drive should set offline on user
397  * request. We may prevent this by returning an error.
398  * Manual offline is only allowed while the drive is not in use.
399  */
400 int
401 tape_generic_offline(struct tape_device *device)
402 {
403         if (!device) {
404                 PRINT_ERR("tape_generic_offline: no such device\n");
405                 return -ENODEV;
406         }
407
408         DBF_LH(3, "(%08x): tape_generic_offline(%p)\n",
409                 device->cdev_id, device);
410
411         spin_lock_irq(get_ccwdev_lock(device->cdev));
412         switch (device->tape_state) {
413                 case TS_INIT:
414                 case TS_NOT_OPER:
415                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
416                         break;
417                 case TS_UNUSED:
418                         tape_state_set(device, TS_INIT);
419                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
420                         tape_cleanup_device(device);
421                         break;
422                 default:
423                         DBF_EVENT(3, "(%08x): Set offline failed "
424                                 "- drive in use.\n",
425                                 device->cdev_id);
426                         PRINT_WARN("(%s): Set offline failed "
427                                 "- drive in use.\n",
428                                 device->cdev->dev.bus_id);
429                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
430                         return -EBUSY;
431         }
432
433         DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id);
434         return 0;
435 }
436
437 /*
438  * Allocate memory for a new device structure.
439  */
440 static struct tape_device *
441 tape_alloc_device(void)
442 {
443         struct tape_device *device;
444
445         device = (struct tape_device *)
446                 kmalloc(sizeof(struct tape_device), GFP_KERNEL);
447         if (device == NULL) {
448                 DBF_EXCEPTION(2, "ti:no mem\n");
449                 PRINT_INFO ("can't allocate memory for "
450                             "tape info structure\n");
451                 return ERR_PTR(-ENOMEM);
452         }
453         memset(device, 0, sizeof(struct tape_device));
454         device->modeset_byte = (char *) kmalloc(1, GFP_KERNEL | GFP_DMA);
455         if (device->modeset_byte == NULL) {
456                 DBF_EXCEPTION(2, "ti:no mem\n");
457                 PRINT_INFO("can't allocate memory for modeset byte\n");
458                 kfree(device);
459                 return ERR_PTR(-ENOMEM);
460         }
461         INIT_LIST_HEAD(&device->req_queue);
462         INIT_LIST_HEAD(&device->node);
463         init_waitqueue_head(&device->state_change_wq);
464         device->tape_state = TS_INIT;
465         device->medium_state = MS_UNKNOWN;
466         *device->modeset_byte = 0;
467         device->first_minor = -1;
468         atomic_set(&device->ref_count, 1);
469
470         return device;
471 }
472
473 /*
474  * Get a reference to an existing device structure. This will automatically
475  * increment the reference count.
476  */
477 struct tape_device *
478 tape_get_device_reference(struct tape_device *device)
479 {
480         DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device,
481                 atomic_inc_return(&device->ref_count));
482
483         return device;
484 }
485
486 /*
487  * Decrease the reference counter of a devices structure. If the
488  * reference counter reaches zero free the device structure.
489  * The function returns a NULL pointer to be used by the caller
490  * for clearing reference pointers.
491  */
492 struct tape_device *
493 tape_put_device(struct tape_device *device)
494 {
495         int remain;
496
497         remain = atomic_dec_return(&device->ref_count);
498         if (remain > 0) {
499                 DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain);
500         } else {
501                 if (remain < 0) {
502                         DBF_EVENT(4, "put device without reference\n");
503                         PRINT_ERR("put device without reference\n");
504                 } else {
505                         DBF_EVENT(4, "tape_free_device(%p)\n", device);
506                         kfree(device->modeset_byte);
507                         kfree(device);
508                 }
509         }
510
511         return NULL;                    
512 }
513
514 /*
515  * Find tape device by a device index.
516  */
517 struct tape_device *
518 tape_get_device(int devindex)
519 {
520         struct tape_device *device, *tmp;
521
522         device = ERR_PTR(-ENODEV);
523         read_lock(&tape_device_lock);
524         list_for_each_entry(tmp, &tape_device_list, node) {
525                 if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) {
526                         device = tape_get_device_reference(tmp);
527                         break;
528                 }
529         }
530         read_unlock(&tape_device_lock);
531         return device;
532 }
533
534 /*
535  * Driverfs tape probe function.
536  */
537 int
538 tape_generic_probe(struct ccw_device *cdev)
539 {
540         struct tape_device *device;
541
542         device = tape_alloc_device();
543         if (IS_ERR(device))
544                 return -ENODEV;
545         PRINT_INFO("tape device %s found\n", cdev->dev.bus_id);
546         cdev->dev.driver_data = device;
547         device->cdev = cdev;
548         device->cdev_id = busid_to_int(cdev->dev.bus_id);
549         cdev->handler = __tape_do_irq;
550
551         ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
552         sysfs_create_group(&cdev->dev.kobj, &tape_attr_group);
553
554         return 0;
555 }
556
557 static inline void
558 __tape_discard_requests(struct tape_device *device)
559 {
560         struct tape_request *   request;
561         struct list_head *      l, *n;
562
563         list_for_each_safe(l, n, &device->req_queue) {
564                 request = list_entry(l, struct tape_request, list);
565                 if (request->status == TAPE_REQUEST_IN_IO)
566                         request->status = TAPE_REQUEST_DONE;
567                 list_del(&request->list);
568
569                 /* Decrease ref_count for removed request. */
570                 request->device = tape_put_device(device);
571                 request->rc = -EIO;
572                 if (request->callback != NULL)
573                         request->callback(request, request->callback_data);
574         }
575 }
576
577 /*
578  * Driverfs tape remove function.
579  *
580  * This function is called whenever the common I/O layer detects the device
581  * gone. This can happen at any time and we cannot refuse.
582  */
583 void
584 tape_generic_remove(struct ccw_device *cdev)
585 {
586         struct tape_device *    device;
587
588         device = cdev->dev.driver_data;
589         if (!device) {
590                 PRINT_ERR("No device pointer in tape_generic_remove!\n");
591                 return;
592         }
593         DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
594
595         spin_lock_irq(get_ccwdev_lock(device->cdev));
596         switch (device->tape_state) {
597                 case TS_INIT:
598                         tape_state_set(device, TS_NOT_OPER);
599                 case TS_NOT_OPER:
600                         /*
601                          * Nothing to do.
602                          */
603                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
604                         break;
605                 case TS_UNUSED:
606                         /*
607                          * Need only to release the device.
608                          */
609                         tape_state_set(device, TS_NOT_OPER);
610                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
611                         tape_cleanup_device(device);
612                         break;
613                 default:
614                         /*
615                          * There may be requests on the queue. We will not get
616                          * an interrupt for a request that was running. So we
617                          * just post them all as I/O errors.
618                          */
619                         DBF_EVENT(3, "(%08x): Drive in use vanished!\n",
620                                 device->cdev_id);
621                         PRINT_WARN("(%s): Drive in use vanished - "
622                                 "expect trouble!\n",
623                                 device->cdev->dev.bus_id);
624                         PRINT_WARN("State was %i\n", device->tape_state);
625                         tape_state_set(device, TS_NOT_OPER);
626                         __tape_discard_requests(device);
627                         spin_unlock_irq(get_ccwdev_lock(device->cdev));
628                         tape_cleanup_device(device);
629         }
630
631         if (cdev->dev.driver_data != NULL) {
632                 sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group);
633                 cdev->dev.driver_data = tape_put_device(cdev->dev.driver_data);
634         }
635 }
636
637 /*
638  * Allocate a new tape ccw request
639  */
640 struct tape_request *
641 tape_alloc_request(int cplength, int datasize)
642 {
643         struct tape_request *request;
644
645         if (datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
646                 BUG();
647
648         DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize);
649
650         request = (struct tape_request *) kmalloc(sizeof(struct tape_request),
651                                                   GFP_KERNEL);
652         if (request == NULL) {
653                 DBF_EXCEPTION(1, "cqra nomem\n");
654                 return ERR_PTR(-ENOMEM);
655         }
656         memset(request, 0, sizeof(struct tape_request));
657         /* allocate channel program */
658         if (cplength > 0) {
659                 request->cpaddr = kmalloc(cplength*sizeof(struct ccw1),
660                                           GFP_ATOMIC | GFP_DMA);
661                 if (request->cpaddr == NULL) {
662                         DBF_EXCEPTION(1, "cqra nomem\n");
663                         kfree(request);
664                         return ERR_PTR(-ENOMEM);
665                 }
666                 memset(request->cpaddr, 0, cplength*sizeof(struct ccw1));
667         }
668         /* alloc small kernel buffer */
669         if (datasize > 0) {
670                 request->cpdata = kmalloc(datasize, GFP_KERNEL | GFP_DMA);
671                 if (request->cpdata == NULL) {
672                         DBF_EXCEPTION(1, "cqra nomem\n");
673                         if (request->cpaddr != NULL)
674                                 kfree(request->cpaddr);
675                         kfree(request);
676                         return ERR_PTR(-ENOMEM);
677                 }
678                 memset(request->cpdata, 0, datasize);
679         }
680         DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr,
681                 request->cpdata);
682
683         return request;
684 }
685
686 /*
687  * Free tape ccw request
688  */
689 void
690 tape_free_request (struct tape_request * request)
691 {
692         DBF_LH(6, "Free request %p\n", request);
693
694         if (request->device != NULL) {
695                 request->device = tape_put_device(request->device);
696         }
697         if (request->cpdata != NULL)
698                 kfree(request->cpdata);
699         if (request->cpaddr != NULL)
700                 kfree(request->cpaddr);
701         kfree(request);
702 }
703
704 static inline void
705 __tape_do_io_list(struct tape_device *device)
706 {
707         struct list_head *l, *n;
708         struct tape_request *request;
709         int rc;
710
711         DBF_LH(6, "__tape_do_io_list(%p)\n", device);
712         /*
713          * Try to start each request on request queue until one is
714          * started successful.
715          */
716         list_for_each_safe(l, n, &device->req_queue) {
717                 request = list_entry(l, struct tape_request, list);
718 #ifdef CONFIG_S390_TAPE_BLOCK
719                 if (request->op == TO_BLOCK)
720                         device->discipline->check_locate(device, request);
721 #endif
722                 rc = ccw_device_start(device->cdev, request->cpaddr,
723                                       (unsigned long) request, 0x00,
724                                       request->options);
725                 if (rc == 0) {
726                         request->status = TAPE_REQUEST_IN_IO;
727                         break;
728                 }
729                 /* Start failed. Remove request and indicate failure. */
730                 DBF_EVENT(1, "tape: DOIO failed with er = %i\n", rc);
731
732                 /* Set ending status and do callback. */
733                 request->rc = rc;
734                 request->status = TAPE_REQUEST_DONE;
735                 __tape_remove_request(device, request);
736         }
737 }
738
739 static void
740 __tape_remove_request(struct tape_device *device, struct tape_request *request)
741 {
742         /* Remove from request queue. */
743         list_del(&request->list);
744
745         /* Do callback. */
746         if (request->callback != NULL)
747                 request->callback(request, request->callback_data);
748
749         /* Start next request. */
750         if (!list_empty(&device->req_queue))
751                 __tape_do_io_list(device);
752 }
753
754 /*
755  * Write sense data to console/dbf
756  */
757 void
758 tape_dump_sense(struct tape_device* device, struct tape_request *request,
759                 struct irb *irb)
760 {
761         unsigned int *sptr;
762
763         PRINT_INFO("-------------------------------------------------\n");
764         PRINT_INFO("DSTAT : %02x  CSTAT: %02x   CPA: %04x\n",
765                    irb->scsw.dstat, irb->scsw.cstat, irb->scsw.cpa);
766         PRINT_INFO("DEVICE: %s\n", device->cdev->dev.bus_id);
767         if (request != NULL)
768                 PRINT_INFO("OP    : %s\n", tape_op_verbose[request->op]);
769
770         sptr = (unsigned int *) irb->ecw;
771         PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
772                    sptr[0], sptr[1], sptr[2], sptr[3]);
773         PRINT_INFO("Sense data: %08X %08X %08X %08X \n",
774                    sptr[4], sptr[5], sptr[6], sptr[7]);
775         PRINT_INFO("--------------------------------------------------\n");
776 }
777
778 /*
779  * Write sense data to dbf
780  */
781 void
782 tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request,
783                     struct irb *irb)
784 {
785         unsigned int *sptr;
786         const char* op;
787
788         if (request != NULL)
789                 op = tape_op_verbose[request->op];
790         else
791                 op = "---";
792         DBF_EVENT(3, "DSTAT : %02x   CSTAT: %02x\n",
793                   irb->scsw.dstat,irb->scsw.cstat);
794         DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op);
795         sptr = (unsigned int *) irb->ecw;
796         DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]);
797         DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]);
798         DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]);
799         DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]);
800 }
801
802 /*
803  * I/O helper function. Adds the request to the request queue
804  * and starts it if the tape is idle. Has to be called with
805  * the device lock held.
806  */
807 static inline int
808 __tape_do_io(struct tape_device *device, struct tape_request *request)
809 {
810         int rc;
811
812         switch (request->op) {
813                 case TO_MSEN:
814                 case TO_ASSIGN:
815                 case TO_UNASSIGN:
816                 case TO_READ_ATTMSG:
817                         if (device->tape_state == TS_INIT)
818                                 break;
819                         if (device->tape_state == TS_UNUSED)
820                                 break;
821                 default:
822                         if (device->tape_state == TS_BLKUSE)
823                                 break;
824                         if (device->tape_state != TS_IN_USE)
825                                 return -ENODEV;
826         }
827
828         /* Increase use count of device for the added request. */
829         request->device = tape_get_device_reference(device);
830
831         if (list_empty(&device->req_queue)) {
832                 /* No other requests are on the queue. Start this one. */
833 #ifdef CONFIG_S390_TAPE_BLOCK
834                 if (request->op == TO_BLOCK)
835                         device->discipline->check_locate(device, request);
836 #endif
837                 rc = ccw_device_start(device->cdev, request->cpaddr,
838                                       (unsigned long) request, 0x00,
839                                       request->options);
840                 if (rc) {
841                         DBF_EVENT(1, "tape: DOIO failed with rc = %i\n", rc);
842                         return rc;
843                 }
844                 DBF_LH(5, "Request %p added for execution.\n", request);
845                 list_add(&request->list, &device->req_queue);
846                 request->status = TAPE_REQUEST_IN_IO;
847         } else {
848                 DBF_LH(5, "Request %p add to queue.\n", request);
849                 list_add_tail(&request->list, &device->req_queue);
850                 request->status = TAPE_REQUEST_QUEUED;
851         }
852         return 0;
853 }
854
855 /*
856  * Add the request to the request queue, try to start it if the
857  * tape is idle. Return without waiting for end of i/o.
858  */
859 int
860 tape_do_io_async(struct tape_device *device, struct tape_request *request)
861 {
862         int rc;
863
864         DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request);
865
866         spin_lock_irq(get_ccwdev_lock(device->cdev));
867         /* Add request to request queue and try to start it. */
868         rc = __tape_do_io(device, request);
869         spin_unlock_irq(get_ccwdev_lock(device->cdev));
870         return rc;
871 }
872
873 /*
874  * tape_do_io/__tape_wake_up
875  * Add the request to the request queue, try to start it if the
876  * tape is idle and wait uninterruptible for its completion.
877  */
878 static void
879 __tape_wake_up(struct tape_request *request, void *data)
880 {
881         request->callback = NULL;
882         wake_up((wait_queue_head_t *) data);
883 }
884
885 int
886 tape_do_io(struct tape_device *device, struct tape_request *request)
887 {
888         wait_queue_head_t wq;
889         int rc;
890
891         init_waitqueue_head(&wq);
892         spin_lock_irq(get_ccwdev_lock(device->cdev));
893         /* Setup callback */
894         request->callback = __tape_wake_up;
895         request->callback_data = &wq;
896         /* Add request to request queue and try to start it. */
897         rc = __tape_do_io(device, request);
898         spin_unlock_irq(get_ccwdev_lock(device->cdev));
899         if (rc)
900                 return rc;
901         /* Request added to the queue. Wait for its completion. */
902         wait_event(wq, (request->callback == NULL));
903         /* Get rc from request */
904         return request->rc;
905 }
906
907 /*
908  * tape_do_io_interruptible/__tape_wake_up_interruptible
909  * Add the request to the request queue, try to start it if the
910  * tape is idle and wait uninterruptible for its completion.
911  */
912 static void
913 __tape_wake_up_interruptible(struct tape_request *request, void *data)
914 {
915         request->callback = NULL;
916         wake_up_interruptible((wait_queue_head_t *) data);
917 }
918
919 int
920 tape_do_io_interruptible(struct tape_device *device,
921                          struct tape_request *request)
922 {
923         wait_queue_head_t wq;
924         int rc;
925
926         init_waitqueue_head(&wq);
927         spin_lock_irq(get_ccwdev_lock(device->cdev));
928         /* Setup callback */
929         request->callback = __tape_wake_up_interruptible;
930         request->callback_data = &wq;
931         rc = __tape_do_io(device, request);
932         spin_unlock_irq(get_ccwdev_lock(device->cdev));
933         if (rc)
934                 return rc;
935         /* Request added to the queue. Wait for its completion. */
936         rc = wait_event_interruptible(wq, (request->callback == NULL));
937         if (rc != -ERESTARTSYS)
938                 /* Request finished normally. */
939                 return request->rc;
940         /* Interrupted by a signal. We have to stop the current request. */
941         spin_lock_irq(get_ccwdev_lock(device->cdev));
942         rc = __tape_halt_io(device, request);
943         if (rc == 0) {
944                 DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id);
945                 rc = -ERESTARTSYS;
946         }
947         spin_unlock_irq(get_ccwdev_lock(device->cdev));
948         return rc;
949 }
950
951 /*
952  * Handle requests that return an i/o error in the irb.
953  */
954 static inline void
955 tape_handle_killed_request(
956         struct tape_device *device,
957         struct tape_request *request)
958 {
959         if(request != NULL) {
960                 /* Set ending status. FIXME: Should the request be retried? */
961                 request->rc = -EIO;
962                 request->status = TAPE_REQUEST_DONE;
963                 __tape_remove_request(device, request);
964         } else {
965                 __tape_do_io_list(device);
966         }
967 }
968
969 /*
970  * Tape interrupt routine, called from the ccw_device layer
971  */
972 static void
973 __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
974 {
975         struct tape_device *device;
976         struct tape_request *request;
977         int final;
978         int rc;
979
980         device = (struct tape_device *) cdev->dev.driver_data;
981         if (device == NULL) {
982                 PRINT_ERR("could not get device structure for %s "
983                           "in interrupt\n", cdev->dev.bus_id);
984                 return;
985         }
986         request = (struct tape_request *) intparm;
987
988         DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request);
989
990         /* On special conditions irb is an error pointer */
991         if (IS_ERR(irb)) {
992                 switch (PTR_ERR(irb)) {
993                         case -ETIMEDOUT:
994                                 PRINT_WARN("(%s): Request timed out\n",
995                                         cdev->dev.bus_id);
996                         case -EIO:
997                                 tape_handle_killed_request(device, request);
998                                 break;
999                         default:
1000                                 PRINT_ERR("(%s): Unexpected i/o error %li\n",
1001                                         cdev->dev.bus_id,
1002                                         PTR_ERR(irb));
1003                 }
1004                 return;
1005         }
1006
1007         /* May be an unsolicited irq */
1008         if(request != NULL)
1009                 request->rescnt = irb->scsw.count;
1010
1011         if (irb->scsw.dstat != 0x0c) {
1012                 /* Set the 'ONLINE' flag depending on sense byte 1 */
1013                 if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE)
1014                         device->tape_generic_status |= GMT_ONLINE(~0);
1015                 else
1016                         device->tape_generic_status &= ~GMT_ONLINE(~0);
1017
1018                 /*
1019                  * Any request that does not come back with channel end
1020                  * and device end is unusual. Log the sense data.
1021                  */
1022                 DBF_EVENT(3,"-- Tape Interrupthandler --\n");
1023                 tape_dump_sense_dbf(device, request, irb);
1024         } else {
1025                 /* Upon normal completion the device _is_ online */
1026                 device->tape_generic_status |= GMT_ONLINE(~0);
1027         }
1028         if (device->tape_state == TS_NOT_OPER) {
1029                 DBF_EVENT(6, "tape:device is not operational\n");
1030                 return;
1031         }
1032
1033         /*
1034          * Request that were canceled still come back with an interrupt.
1035          * To detect these request the state will be set to TAPE_REQUEST_DONE.
1036          */
1037         if(request != NULL && request->status == TAPE_REQUEST_DONE) {
1038                 __tape_remove_request(device, request);
1039                 return;
1040         }
1041
1042         rc = device->discipline->irq(device, request, irb);
1043         /*
1044          * rc < 0 : request finished unsuccessfully.
1045          * rc == TAPE_IO_SUCCESS: request finished successfully.
1046          * rc == TAPE_IO_PENDING: request is still running. Ignore rc.
1047          * rc == TAPE_IO_RETRY: request finished but needs another go.
1048          * rc == TAPE_IO_STOP: request needs to get terminated.
1049          */
1050         final = 0;
1051         switch (rc) {
1052         case TAPE_IO_SUCCESS:
1053                 /* Upon normal completion the device _is_ online */
1054                 device->tape_generic_status |= GMT_ONLINE(~0);
1055                 final = 1;
1056                 break;
1057         case TAPE_IO_PENDING:
1058                 break;
1059         case TAPE_IO_RETRY:
1060 #ifdef CONFIG_S390_TAPE_BLOCK
1061                 if (request->op == TO_BLOCK)
1062                         device->discipline->check_locate(device, request);
1063 #endif
1064                 rc = ccw_device_start(cdev, request->cpaddr,
1065                                       (unsigned long) request, 0x00,
1066                                       request->options);
1067                 if (rc) {
1068                         DBF_EVENT(1, "tape: DOIO failed with er = %i\n", rc);
1069                         final = 1;
1070                 }
1071                 break;
1072         case TAPE_IO_STOP:
1073                 __tape_halt_io(device, request);
1074                 break;
1075         default:
1076                 if (rc > 0) {
1077                         DBF_EVENT(6, "xunknownrc\n");
1078                         PRINT_ERR("Invalid return code from discipline "
1079                                   "interrupt function.\n");
1080                         rc = -EIO;
1081                 }
1082                 final = 1;
1083                 break;
1084         }
1085         if (final) {
1086                 /* May be an unsolicited irq */
1087                 if(request != NULL) {
1088                         /* Set ending status. */
1089                         request->rc = rc;
1090                         request->status = TAPE_REQUEST_DONE;
1091                         __tape_remove_request(device, request);
1092                 } else {
1093                         __tape_do_io_list(device);
1094                 }
1095         }
1096 }
1097
1098 /*
1099  * Tape device open function used by tape_char & tape_block frontends.
1100  */
1101 int
1102 tape_open(struct tape_device *device)
1103 {
1104         int rc;
1105
1106         spin_lock(get_ccwdev_lock(device->cdev));
1107         if (device->tape_state == TS_NOT_OPER) {
1108                 DBF_EVENT(6, "TAPE:nodev\n");
1109                 rc = -ENODEV;
1110         } else if (device->tape_state == TS_IN_USE) {
1111                 DBF_EVENT(6, "TAPE:dbusy\n");
1112                 rc = -EBUSY;
1113         } else if (device->tape_state == TS_BLKUSE) {
1114                 DBF_EVENT(6, "TAPE:dbusy\n");
1115                 rc = -EBUSY;
1116         } else if (device->discipline != NULL &&
1117                    !try_module_get(device->discipline->owner)) {
1118                 DBF_EVENT(6, "TAPE:nodisc\n");
1119                 rc = -ENODEV;
1120         } else {
1121                 tape_state_set(device, TS_IN_USE);
1122                 rc = 0;
1123         }
1124         spin_unlock(get_ccwdev_lock(device->cdev));
1125         return rc;
1126 }
1127
1128 /*
1129  * Tape device release function used by tape_char & tape_block frontends.
1130  */
1131 int
1132 tape_release(struct tape_device *device)
1133 {
1134         spin_lock(get_ccwdev_lock(device->cdev));
1135         if (device->tape_state == TS_IN_USE)
1136                 tape_state_set(device, TS_UNUSED);
1137         module_put(device->discipline->owner);
1138         spin_unlock(get_ccwdev_lock(device->cdev));
1139         return 0;
1140 }
1141
1142 /*
1143  * Execute a magnetic tape command a number of times.
1144  */
1145 int
1146 tape_mtop(struct tape_device *device, int mt_op, int mt_count)
1147 {
1148         tape_mtop_fn fn;
1149         int rc;
1150
1151         DBF_EVENT(6, "TAPE:mtio\n");
1152         DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op);
1153         DBF_EVENT(6, "TAPE:arg:  %x\n", mt_count);
1154
1155         if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS)
1156                 return -EINVAL;
1157         fn = device->discipline->mtop_array[mt_op];
1158         if (fn == NULL)
1159                 return -EINVAL;
1160
1161         /* We assume that the backends can handle count up to 500. */
1162         if (mt_op == MTBSR  || mt_op == MTFSR  || mt_op == MTFSF  ||
1163             mt_op == MTBSF  || mt_op == MTFSFM || mt_op == MTBSFM) {
1164                 rc = 0;
1165                 for (; mt_count > 500; mt_count -= 500)
1166                         if ((rc = fn(device, 500)) != 0)
1167                                 break;
1168                 if (rc == 0)
1169                         rc = fn(device, mt_count);
1170         } else
1171                 rc = fn(device, mt_count);
1172         return rc;
1173
1174 }
1175
1176 /*
1177  * Tape init function.
1178  */
1179 static int
1180 tape_init (void)
1181 {
1182         TAPE_DBF_AREA = debug_register ( "tape", 1, 2, 4*sizeof(long));
1183         debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view);
1184 #ifdef DBF_LIKE_HELL
1185         debug_set_level(TAPE_DBF_AREA, 6);
1186 #endif
1187         DBF_EVENT(3, "tape init: ($Revision: 1.50 $)\n");
1188         tape_proc_init();
1189         tapechar_init ();
1190         tapeblock_init ();
1191         return 0;
1192 }
1193
1194 /*
1195  * Tape exit function.
1196  */
1197 static void
1198 tape_exit(void)
1199 {
1200         DBF_EVENT(6, "tape exit\n");
1201
1202         /* Get rid of the frontends */
1203         tapechar_exit();
1204         tapeblock_exit();
1205         tape_proc_cleanup();
1206         debug_unregister (TAPE_DBF_AREA);
1207 }
1208
1209 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and "
1210               "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)");
1211 MODULE_DESCRIPTION("Linux on zSeries channel attached "
1212                    "tape device driver ($Revision: 1.50 $)");
1213 MODULE_LICENSE("GPL");
1214
1215 module_init(tape_init);
1216 module_exit(tape_exit);
1217
1218 EXPORT_SYMBOL(tape_generic_remove);
1219 EXPORT_SYMBOL(tape_generic_probe);
1220 EXPORT_SYMBOL(tape_generic_online);
1221 EXPORT_SYMBOL(tape_generic_offline);
1222 EXPORT_SYMBOL(tape_put_device);
1223 EXPORT_SYMBOL(tape_get_device_reference);
1224 EXPORT_SYMBOL(tape_state_verbose);
1225 EXPORT_SYMBOL(tape_op_verbose);
1226 EXPORT_SYMBOL(tape_state_set);
1227 EXPORT_SYMBOL(tape_med_state_set);
1228 EXPORT_SYMBOL(tape_alloc_request);
1229 EXPORT_SYMBOL(tape_free_request);
1230 EXPORT_SYMBOL(tape_dump_sense);
1231 EXPORT_SYMBOL(tape_dump_sense_dbf);
1232 EXPORT_SYMBOL(tape_do_io);
1233 EXPORT_SYMBOL(tape_do_io_async);
1234 EXPORT_SYMBOL(tape_do_io_interruptible);
1235 EXPORT_SYMBOL(tape_mtop);