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