brute-forced more changes from MontaVista's tree. SCSI partition table read still...
[linux-2.4.git] / drivers / s390 / char / tape.c
1
2 /***********************************************************************
3  *  drivers/s390/char/tape.c
4  *    tape device driver for S/390 and zSeries tapes.
5  *
6  *  S390 and zSeries version
7  *    Copyright (C) 2001 IBM Corporation
8  *    Author(s): Carsten Otte <cotte@de.ibm.com>
9  *               Tuan Ngo-Anh <ngoanh@de.ibm.com>
10  *
11  ***********************************************************************
12  */
13
14 #include "tapedefs.h"
15
16 #include <linux/config.h>
17 #include <linux/stddef.h>
18 #include <linux/kernel.h>
19 #include <linux/version.h>
20 #include <linux/proc_fs.h>
21 #include <linux/init.h>
22 #include <asm/types.h>
23 #include <asm/ccwcache.h>
24 #include <asm/idals.h>
25 #include <asm/ebcdic.h>
26 #include <linux/compatmac.h>
27 #ifdef MODULE
28 #include <linux/module.h>
29 #endif   
30 #include <asm/debug.h>
31 #ifdef CONFIG_S390_TAPE_DYNAMIC
32 #include <asm/s390dyn.h>
33 #endif
34 #include "tape.h"
35 #ifdef CONFIG_S390_TAPE_3490
36 #include "tape3490.h"
37 #endif
38 #ifdef CONFIG_S390_TAPE_3480
39 #include "tape3480.h"
40 #endif
41 #ifdef CONFIG_S390_TAPE_BLOCK
42 #include "tapeblock.h"
43 #endif
44 #ifdef CONFIG_S390_TAPE_CHAR
45 #include "tapechar.h"
46 #endif
47 #ifdef CONFIG_PROC_FS
48 #include <linux/vmalloc.h>
49 #endif
50 #define PRINTK_HEADER "T390:"
51
52
53 /* state handling routines */
54 inline void tapestate_set (tape_info_t * ti, int newstate);
55 inline int tapestate_get (tape_info_t * ti);
56 void tapestate_event (tape_info_t * ti, int event);
57
58 /* our globals */
59 tape_info_t *first_tape_info = NULL;
60 tape_discipline_t *first_discipline = NULL;
61 tape_frontend_t *first_frontend = NULL;
62 devreg_t* tape_devreg[128];
63 int devregct=0;
64
65 #ifdef TAPE_DEBUG
66 debug_info_t *tape_debug_area = NULL;
67 #endif
68
69 char* state_verbose[TS_SIZE]={
70     "TS_UNUSED",  "TS_IDLE", "TS_DONE", "TS_FAILED",
71     "TS_BLOCK_INIT",
72     "TS_BSB_INIT",
73     "TS_BSF_INIT",
74     "TS_DSE_INIT",
75     "TS_EGA_INIT",
76     "TS_FSB_INIT",
77     "TS_FSF_INIT",
78     "TS_LDI_INIT",
79     "TS_LBL_INIT",
80     "TS_MSE_INIT",
81     "TS_NOP_INIT",
82     "TS_RBA_INIT",
83     "TS_RBI_INIT",
84     "TS_RBU_INIT",
85     "TS_RBL_INIT",
86     "TS_RDC_INIT",
87     "TS_RFO_INIT",
88     "TS_RSD_INIT",
89     "TS_REW_INIT",
90     "TS_REW_RELEASE_INIT",
91     "TS_RUN_INIT",
92     "TS_SEN_INIT",
93     "TS_SID_INIT",
94     "TS_SNP_INIT",
95     "TS_SPG_INIT",
96     "TS_SWI_INIT",
97     "TS_SMR_INIT",
98     "TS_SYN_INIT",
99     "TS_TIO_INIT",
100     "TS_UNA_INIT",
101     "TS_WRI_INIT",
102     "TS_WTM_INIT",
103     "TS_NOT_OPER"};
104
105 char* event_verbose[TE_SIZE]= {
106     "TE_START", "TE_DONE", "TE_FAILED", "TE_ERROR", "TE_OTHER"};
107
108 /* our root devfs handle */
109 #ifdef CONFIG_DEVFS_FS
110 devfs_handle_t tape_devfs_root_entry;
111
112 inline void
113 tape_mkdevfsroots (tape_info_t* ti) 
114 {
115     char devno [5];
116     sprintf (devno,"%04x",ti->devinfo.devno);
117     ti->devfs_dir=devfs_mk_dir (tape_devfs_root_entry, devno, ti);
118 }
119
120 inline void
121 tape_rmdevfsroots (tape_info_t* ti)
122 {
123     devfs_unregister (ti->devfs_dir);
124 }
125 #endif
126
127 #ifdef CONFIG_PROC_FS
128 /* our proc tapedevices entry */
129 static struct proc_dir_entry *tape_devices_entry;
130
131 typedef struct {
132         char *data;
133         int len;
134 } tempinfo_t;
135
136
137 static int
138 tape_devices_open (struct inode *inode, struct file *file)
139 {
140     int size=80;
141     tape_info_t* ti;
142     tempinfo_t* tempinfo;
143     char* data;
144     int pos=0;
145     tempinfo = kmalloc (sizeof(tempinfo_t),GFP_KERNEL);
146     if (!tempinfo)
147         return -ENOMEM;
148     for (ti=first_tape_info;ti!=NULL;ti=ti->next)
149         size+=80; // FIXME: Guess better!
150     data=vmalloc(size);
151     if (!data) {
152         kfree (tempinfo);
153         return -ENOMEM;
154     }
155     pos+=sprintf(data+pos,"TapeNo\tDevNo\tCuType\tCuModel\tDevType\tDevModel\tState\n");
156     for (ti=first_tape_info;ti!=NULL;ti=ti->next) {
157         pos+=sprintf(data+pos,"%d\t%04X\t%04X\t%02X\t%04X\t%02X\t\t%s\n",ti->rew_minor/2,
158                      ti->devinfo.devno,ti->devinfo.sid_data.cu_type,
159                      ti->devinfo.sid_data.cu_model,ti->devinfo.sid_data.dev_type,
160                      ti->devinfo.sid_data.dev_model,((tapestate_get(ti) >= 0) &&
161                                                        (tapestate_get(ti) < TS_SIZE)) ?
162                      state_verbose[tapestate_get (ti)] : "TS UNKNOWN");
163     }
164     tempinfo->len=pos;
165     tempinfo->data=data;
166     file->private_data= (void*) tempinfo;
167 #ifdef MODULE
168     MOD_INC_USE_COUNT;
169 #endif
170     return 0;
171 }
172
173 static ssize_t
174 tape_devices_read (struct file *file, char *user_buf, size_t user_len, loff_t * offset)
175 {
176         loff_t len;
177         tempinfo_t *p_info = (tempinfo_t *) file->private_data;
178
179         if (*offset >= p_info->len) {
180                 return 0;       /* EOF */
181         } else {
182                 len =  user_len<(p_info->len - *offset)?user_len:(p_info->len - *offset);
183                 if (copy_to_user (user_buf, &(p_info->data[*offset]), len))
184                         return -EFAULT;
185                 (*offset) += len;
186                 return len;     /* number of bytes "read" */
187         }
188 }
189
190 static int
191 tape_devices_release (struct inode *inode, struct file *file)
192 {
193         int rc = 0;
194         tempinfo_t *p_info = (tempinfo_t *) file->private_data;
195         if (p_info) {
196                 if (p_info->data)
197                         vfree (p_info->data);
198                 kfree (p_info);
199         }
200 #ifdef MODULE
201         MOD_DEC_USE_COUNT;
202 #endif
203         return rc;
204 }
205
206 static struct file_operations tape_devices_file_ops =
207 {
208         read:tape_devices_read, /* read */
209         open:tape_devices_open, /* open */
210         release:tape_devices_release,   /* close */
211 };
212
213 static struct inode_operations tape_devices_inode_ops =
214 {
215 #if !(LINUX_VERSION_CODE > KERNEL_VERSION(2,3,98))
216         default_file_ops:&tape_devices_file_ops         /* file ops */
217 #endif                          /* LINUX_IS_24 */
218 };
219 #endif /* CONFIG_PROC_FS */
220
221 /* SECTION: Parameters for tape */
222 char *tape[256] = { NULL, };
223
224 #ifndef MODULE
225 static char tape_parm_string[1024] __initdata = { 0, };
226 static void
227 tape_split_parm_string (char *str)
228 {
229         char *tmp = str;
230         int count = 0;
231         while (tmp != NULL && *tmp != '\0') {
232                 char *end;
233                 int len;
234                 end = strchr (tmp, ',');
235                 if (end == NULL) {
236                         len = strlen (tmp) + 1;
237                 } else {
238                         len = (long) end - (long) tmp + 1;
239                         *end = '\0';
240                         end++;
241                 }
242                 tape[count] = kmalloc (len * sizeof (char), GFP_ATOMIC);
243                 if (tape[count] == NULL) {
244                         printk (KERN_WARNING PRINTK_HEADER
245                                 "can't store tape= parameter no %d\n",
246                                 count + 1);
247                         break;
248                 }
249                 memset (tape[count], 0, len * sizeof (char));
250                 memcpy (tape[count], tmp, len * sizeof (char));
251                 count++;
252                 tmp = end;
253         };
254 }
255
256 void __init
257 tape_parm_setup (char *str, int *ints)
258 {
259         int len = strlen (tape_parm_string);
260         if (len != 0) {
261                 strcat (tape_parm_string, ",");
262         }
263         strcat (tape_parm_string, str);
264 }
265
266 int __init
267 tape_parm_call_setup (char *str)
268 {
269         int dummy;
270         tape_parm_setup (str, &dummy);
271         return 1;
272 }
273
274 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,2,16))
275 __setup("tape=", tape_parm_call_setup);
276 #endif   /* kernel <2.2.19 */
277 #endif   /* not defined MODULE */
278
279 static inline int
280 tape_parm_strtoul (char *str, char **stra)
281 {
282         char *temp = str;
283         int val;
284         if (*temp == '0') {
285                 temp++;         /* strip leading zero */
286                 if (*temp == 'x')
287                         temp++; /* strip leading x */
288         }
289         val = simple_strtoul (temp, &temp, 16); /* interpret anything as hex */
290         *stra = temp;
291         return val;
292 }
293
294 static inline devreg_t *
295 tape_create_devreg (int devno)
296 {
297         devreg_t *devreg = kmalloc (sizeof (devreg_t), GFP_KERNEL);
298         if (devreg != NULL) {
299                 memset (devreg, 0, sizeof (devreg_t));
300                 devreg->ci.devno = devno;
301                 devreg->flag = DEVREG_TYPE_DEVNO;
302                 devreg->oper_func = tape_oper_handler;
303         }
304         return devreg;
305 }
306
307 static inline void
308 tape_parm_parse (char **str)
309 {
310         char *temp;
311         int from, to,i,irq=0,rc,retries=0,tape_num=0;
312         s390_dev_info_t dinfo;
313         tape_info_t* ti,*tempti;
314         tape_discipline_t* disc;
315         long lockflags;
316         if (*str==NULL) {
317             /* no params present -> leave */
318             return;
319         }
320         while (*str) {
321                 temp = *str;
322                 from = 0;
323                 to = 0;
324
325                 /* turn off autodetect mode, if any range is present */
326                 from = tape_parm_strtoul (temp, &temp);
327                 to = from;
328                 if (*temp == '-') {
329                     temp++;
330                     to = tape_parm_strtoul (temp, &temp);
331                 }
332                 for (i=from;i<=to;i++) {
333                     retries=0;
334                     // register for attch/detach of a devno
335                     tape_devreg[devregct]=tape_create_devreg(i);
336                     if (tape_devreg[devregct]==NULL) {
337                         PRINT_WARN ("Could not create devreg for devno %04x, dyn. attach for this devno deactivated.\n",i);
338                     } else {
339                         s390_device_register (tape_devreg[devregct++]);
340                     }
341                     // we are activating a device if it is present
342                     for (irq = get_irq_first(); irq!=-ENODEV; irq=get_irq_next(irq)) {
343                         rc = get_dev_info_by_irq (irq, &dinfo);
344                      
345                         disc = first_discipline;
346                         while ((dinfo.devno == i) && (disc != NULL) && (disc->cu_type != dinfo.sid_data.cu_type))
347                             disc = (tape_discipline_t *) (disc->next);
348                         if ((disc == NULL) || (rc == -ENODEV) || (i!=dinfo.devno)) {
349                             continue;
350                         }
351 #ifdef TAPE_DEBUG
352                         debug_text_event (tape_debug_area,3,"det irq:  ");
353                         debug_int_event (tape_debug_area,3,irq);
354                         debug_text_event (tape_debug_area,3,"cu:       ");
355                         debug_int_event (tape_debug_area,3,disc->cu_type);
356 #endif /* TAPE_DEBUG */
357                         PRINT_INFO ("using devno %04x with discipline %04x on irq %d as tape device %d\n",dinfo.devno,dinfo.sid_data.cu_type,irq,tape_num/2);
358                         /* Allocate tape structure  */
359                         ti = kmalloc (sizeof (tape_info_t), GFP_ATOMIC);
360                         if (ti == NULL) {
361 #ifdef TAPE_DEBUG
362                             debug_text_exception (tape_debug_area,3,"ti:no mem ");
363 #endif /* TAPE_DEBUG */
364                             PRINT_INFO ("tape: can't allocate memory for "
365                                         "tape info structure\n");
366                             continue;
367                         }
368                         memset(ti,0,sizeof(tape_info_t));
369                         ti->discipline = disc;
370                         disc->tape = ti;
371                         rc = tape_setup (ti, irq, tape_num);
372                         if (rc) {
373 #ifdef TAPE_DEBUG
374                             debug_text_event (tape_debug_area,3,"tsetup err");
375                             debug_int_exception (tape_debug_area,3,rc);
376 #endif /* TAPE_DEBUG */
377                             kfree (ti);
378                         } else {
379                             s390irq_spin_lock_irqsave (irq, lockflags);
380                             if (first_tape_info == NULL) {
381                                 first_tape_info = ti;
382                             } else {
383                                 tempti = first_tape_info;
384                                 while (tempti->next != NULL)
385                                     tempti = tempti->next;
386                                 tempti->next = ti;
387                             }
388                             s390irq_spin_unlock_irqrestore (irq, lockflags);
389                         }
390                     }
391                     tape_num+=2;
392                 }
393                 str++;
394         }
395 }
396
397
398 /* SECTION: Managing wrappers for ccwcache */
399
400 #define TAPE_EMERGENCY_REQUESTS 16
401
402 static ccw_req_t *tape_emergency_req[TAPE_EMERGENCY_REQUESTS] =
403 {NULL,};
404 static spinlock_t tape_emergency_req_lock = SPIN_LOCK_UNLOCKED;
405
406 static void
407 tape_init_emergency_req (void)
408 {
409         int i;
410         for (i = 0; i < TAPE_EMERGENCY_REQUESTS; i++) {
411                 tape_emergency_req[i] = (ccw_req_t *) get_free_page (GFP_KERNEL);
412         }
413 }
414
415 #ifdef MODULE // We only cleanup the emergency requests on module unload.
416 static void
417 tape_cleanup_emergency_req (void)
418 {
419         int i;
420         for (i = 0; i < TAPE_EMERGENCY_REQUESTS; i++) {
421                 if (tape_emergency_req[i])
422                         free_page ((long) (tape_emergency_req[i]));
423                 else
424                         printk (KERN_WARNING PRINTK_HEADER "losing one page for 'in-use' emergency request\n");
425         }
426 }
427 #endif
428
429 ccw_req_t *
430 tape_alloc_request (char *magic, int cplength, int datasize)
431 {
432         ccw_req_t *rv = NULL;
433         int i;
434         if ((rv = ccw_alloc_request (magic, cplength, datasize)) != NULL) {
435                 return rv;
436         }
437         if (cplength * sizeof (ccw1_t) + datasize + sizeof (ccw_req_t) > PAGE_SIZE) {
438                 return NULL;
439         }
440         spin_lock (&tape_emergency_req_lock);
441         for (i = 0; i < TAPE_EMERGENCY_REQUESTS; i++) {
442                 if (tape_emergency_req[i] != NULL) {
443                         rv = tape_emergency_req[i];
444                         tape_emergency_req[i] = NULL;
445                 }
446         }
447         spin_unlock (&tape_emergency_req_lock);
448         if (rv) {
449                 memset (rv, 0, PAGE_SIZE);
450                 rv->cache = (kmem_cache_t *) (tape_emergency_req + i);
451                 strncpy ((char *) (&rv->magic), magic, 4);
452                 ASCEBC ((char *) (&rv->magic), 4);
453                 rv->cplength = cplength;
454                 rv->datasize = datasize;
455                 rv->data = (void *) ((long) rv + PAGE_SIZE - datasize);
456                 rv->cpaddr = (ccw1_t *) ((long) rv + sizeof (ccw_req_t));
457         }
458         return rv;
459 }
460
461 void
462 tape_free_request (ccw_req_t * request)
463 {
464         if (request->cache >= (kmem_cache_t *) tape_emergency_req &&
465             request->cache <= (kmem_cache_t *) (tape_emergency_req + TAPE_EMERGENCY_REQUESTS)) {
466                 *((ccw_req_t **) (request->cache)) = request;
467         } else {
468                 clear_normalized_cda ((ccw1_t *) (request->cpaddr));    // avoid memory leak caused by modeset_byte
469                 ccw_free_request (request);
470         }
471 }
472
473 /*
474  * Allocate a ccw request and reserve it for tape driver
475  */
476 inline
477  ccw_req_t *
478 tape_alloc_ccw_req (tape_info_t * ti, int cplength, int datasize)
479 {
480         char tape_magic_id[] = "tape";
481         ccw_req_t *cqr = NULL;
482
483         if (!ti)
484                 return NULL;
485         cqr = tape_alloc_request (tape_magic_id, cplength, datasize);
486
487         if (!cqr) {
488 #ifdef TAPE_DEBUG
489                 PRINT_WARN ("empty CQR generated\n");
490 #endif
491         }
492         cqr->magic = TAPE_MAGIC;        /* sets an identifier for tape driver   */
493         cqr->device = ti;       /* save pointer to tape info    */
494         return cqr;
495 }
496
497 /*
498  * Find the tape_info_t structure associated with irq
499  */
500 static inline tape_info_t *
501 tapedev_find_info (int irq)
502 {
503         tape_info_t *ti;
504
505         ti = first_tape_info;
506         if (ti != NULL)
507                 do {
508                         if (ti->devinfo.irq == irq)
509                                 break;
510                 } while ((ti = (tape_info_t *) ti->next) != NULL);
511         return ti;
512 }
513
514 #define QUEUE_THRESHOLD 5
515
516 /*
517  * Tape interrupt routine, called from Ingo's I/O layer
518  */
519 void
520 tape_irq (int irq, void *int_parm, struct pt_regs *regs)
521 {
522         tape_info_t *ti = tapedev_find_info (irq);
523
524         /* analyse devstat and fire event */
525         if (ti->devstat.dstat & DEV_STAT_UNIT_CHECK) {
526                 tapestate_event (ti, TE_ERROR);
527         } else if (ti->devstat.dstat & (DEV_STAT_DEV_END)) {
528                 tapestate_event (ti, TE_DONE);
529         } else
530                 tapestate_event (ti, TE_OTHER);
531 }
532
533 int 
534 tape_oper_handler ( int irq, struct _devreg *dreg) {
535     tape_info_t* ti=first_tape_info;
536     tape_info_t* newtape;
537     int rc,tape_num,retries=0,i;
538     s390_dev_info_t dinfo;
539     tape_discipline_t* disc;
540 #ifdef CONFIG_DEVFS_FS
541     tape_frontend_t* frontend;
542 #endif
543     long lockflags;
544     while ((ti!=NULL) && (ti->devinfo.irq!=irq)) 
545         ti=ti->next;
546     if (ti!=NULL) {
547         // irq is (still) used by tape. tell ingo to try again later
548         PRINT_WARN ("Oper handler for irq %d called while irq still (internaly?) used.\n",irq);
549         return -EAGAIN;
550     }
551     // irq is not used by tape
552     rc = get_dev_info_by_irq (irq, &dinfo);
553     if (rc == -ENODEV) {
554         retries++;
555         rc = get_dev_info_by_irq (irq, &dinfo);
556         if (retries > 5) {
557             PRINT_WARN ("No device information for new dev. could be retrieved.\n");
558             return -ENODEV;
559         }
560     }
561     disc = first_discipline;
562     while ((disc != NULL) && (disc->cu_type != dinfo.sid_data.cu_type))
563         disc = (tape_discipline_t *) (disc->next);
564     if (disc == NULL)
565         PRINT_WARN ("No matching discipline for cu_type %x found, ignoring device %04x.\n",dinfo.sid_data.cu_type,dinfo.devno);
566     if (rc == -ENODEV) 
567         PRINT_WARN ("No device information for new dev. could be retrieved.\n");
568     if ((disc == NULL) || (rc == -ENODEV))
569         return -ENODEV;
570     
571     /* Allocate tape structure  */
572     ti = kmalloc (sizeof (tape_info_t), GFP_ATOMIC);
573     if (ti == NULL) {
574         PRINT_INFO ( "tape: can't allocate memory for "
575                     "tape info structure\n");
576         return -ENOBUFS;
577     }
578     memset(ti,0,sizeof(tape_info_t));
579     ti->discipline = disc;
580     disc->tape = ti;
581     tape_num=0;
582     if (*tape) {
583         // we have static device ranges, so fingure out the tape_num of the attached tape
584         for (i=0;i<devregct;i++)
585             if (tape_devreg[i]->ci.devno==dinfo.devno) {
586                 tape_num=2*i;
587                 break;
588             }
589     } else {
590         // we are running in autoprobe mode, find a free tape_num
591         newtape=first_tape_info;
592         while (newtape!=NULL) {
593             if (newtape->rew_minor==tape_num) {
594                 // tape num in use. try next one
595                 tape_num+=2;
596                 newtape=first_tape_info;
597             } else {
598                 // tape num not used by newtape. look at next tape info
599                 newtape=newtape->next;
600             }
601         }
602     }
603     rc = tape_setup (ti, irq, tape_num);
604     if (rc) {
605         kfree (ti);
606         return -ENOBUFS;
607     }
608 #ifdef CONFIG_DEVFS_FS
609     for (frontend=first_frontend;frontend!=NULL;frontend=frontend->next) 
610         frontend->mkdevfstree(ti);
611 #endif
612     s390irq_spin_lock_irqsave (irq,lockflags);
613     if (first_tape_info == NULL) {
614         first_tape_info = ti;
615     } else {
616         newtape = first_tape_info;
617         while (newtape->next != NULL)
618             newtape = newtape->next;
619         newtape->next = ti;
620     }
621     s390irq_spin_unlock_irqrestore (irq, lockflags);
622     return 0;
623 }
624
625
626 static void
627 tape_noper_handler ( int irq, int status ) {
628     tape_info_t *ti=first_tape_info;
629     tape_info_t *lastti;
630 #ifdef CONFIG_DEVFS_FS
631     tape_frontend_t *frontend;
632 #endif
633     long lockflags;
634     s390irq_spin_lock_irqsave(irq,lockflags);
635     while (ti!=NULL && ti->devinfo.irq!=irq) ti=ti->next;
636     if (ti==NULL) return;
637     if (tapestate_get(ti)!=TS_UNUSED) {
638         // device is in use!
639         PRINT_WARN ("Tape #%d was detached while it was busy. Expect errors!",ti->blk_minor/2);
640         tapestate_set(ti,TS_NOT_OPER);
641         ti->rc=-ENODEV; 
642         ti->wanna_wakeup=1;
643         switch (tapestate_get(ti)) {
644         case TS_REW_RELEASE_INIT:
645             tapestate_set(ti,TS_NOT_OPER);
646             wake_up (&ti->wq);
647             break;
648 #ifdef CONFIG_S390_TAPE_BLOCK
649         case TS_BLOCK_INIT:
650             tapestate_set(ti,TS_NOT_OPER);
651             schedule_tapeblock_exec_IO(ti);
652             break;
653 #endif
654         default:
655             tapestate_set(ti,TS_NOT_OPER);
656             wake_up_interruptible (&ti->wq);
657         }
658     } else {
659         // device is unused!
660         PRINT_WARN ("Tape #%d was detached.\n",ti->blk_minor/2);
661         if (ti==first_tape_info) {
662             first_tape_info=ti->next;
663         } else {
664             lastti=first_tape_info;
665             while (lastti->next!=ti) lastti=lastti->next;
666             lastti->next=ti->next;
667         }
668 #ifdef CONFIG_DEVFS_FS
669         for (frontend=first_frontend;frontend!=NULL;frontend=frontend->next)
670             frontend->rmdevfstree(ti);
671         tape_rmdevfsroots(ti);
672 #endif
673         kfree(ti);
674     }
675     s390irq_spin_unlock_irqrestore(irq,lockflags);
676     return;
677 }
678
679
680 void
681 tape_dump_sense (devstat_t * stat)
682 {
683 #ifdef TAPE_DEBUG
684         int sl;
685 #endif
686 #if 0
687
688         PRINT_WARN ("------------I/O resulted in unit check:-----------\n");
689         for (sl = 0; sl < 4; sl++) {
690                 PRINT_WARN ("Sense:");
691                 for (sct = 0; sct < 8; sct++) {
692                         PRINT_WARN (" %2d:0x%02X", 8 * sl + sct,
693                                     stat->ii.sense.data[8 * sl + sct]);
694                 }
695                 PRINT_WARN ("\n");
696         }
697         PRINT_INFO ("Sense data: %02X%02X%02X%02X %02X%02X%02X%02X "
698                     " %02X%02X%02X%02X %02X%02X%02X%02X \n",
699                     stat->ii.sense.data[0], stat->ii.sense.data[1],
700                     stat->ii.sense.data[2], stat->ii.sense.data[3],
701                     stat->ii.sense.data[4], stat->ii.sense.data[5],
702                     stat->ii.sense.data[6], stat->ii.sense.data[7],
703                     stat->ii.sense.data[8], stat->ii.sense.data[9],
704                     stat->ii.sense.data[10], stat->ii.sense.data[11],
705                     stat->ii.sense.data[12], stat->ii.sense.data[13],
706                     stat->ii.sense.data[14], stat->ii.sense.data[15]);
707         PRINT_INFO ("Sense data: %02X%02X%02X%02X %02X%02X%02X%02X "
708                     " %02X%02X%02X%02X %02X%02X%02X%02X \n",
709                     stat->ii.sense.data[16], stat->ii.sense.data[17],
710                     stat->ii.sense.data[18], stat->ii.sense.data[19],
711                     stat->ii.sense.data[20], stat->ii.sense.data[21],
712                     stat->ii.sense.data[22], stat->ii.sense.data[23],
713                     stat->ii.sense.data[24], stat->ii.sense.data[25],
714                     stat->ii.sense.data[26], stat->ii.sense.data[27],
715                     stat->ii.sense.data[28], stat->ii.sense.data[29],
716                     stat->ii.sense.data[30], stat->ii.sense.data[31]);
717 #endif
718 #ifdef TAPE_DEBUG
719         debug_text_event (tape_debug_area,3,"SENSE:");
720         for (sl=0;sl<31;sl++) {
721             debug_int_event (tape_debug_area,3,stat->ii.sense.data[sl]);
722         }
723         debug_int_exception (tape_debug_area,3,stat->ii.sense.data[31]);
724 #endif
725 }
726
727 /*
728  * Setup tape_info_t structure of a tape device
729  */
730 int
731 tape_setup (tape_info_t * ti, int irq, int minor)
732 {
733         long lockflags;
734         int rc = 0;
735
736         if (minor>254) {
737             PRINT_WARN ("Device id %d on irq %d will not be accessible since this driver is restricted to 128 devices.\n",minor/2,irq);
738             return -EINVAL;
739         }
740         rc = get_dev_info_by_irq (irq, &(ti->devinfo));
741         if (rc == -ENODEV) {    /* end of device list */
742                 return rc;
743         }
744         ti->rew_minor = minor;
745         ti->nor_minor = minor + 1;
746         ti->blk_minor = minor;
747 #ifdef CONFIG_DEVFS_FS
748         tape_mkdevfsroots(ti);
749 #endif
750         /* Register IRQ */
751 #ifdef CONFIG_S390_TAPE_DYNAMIC
752         rc = s390_request_irq_special (irq, tape_irq, tape_noper_handler,0, "tape", &(ti->devstat));
753 #else
754         rc = s390_request_irq (irq, tape_irq, 0, "tape", &(ti->devstat));
755 #endif
756         s390irq_spin_lock_irqsave (irq, lockflags);
757         ti->next = NULL;
758         if (rc)
759             PRINT_WARN ("Cannot register irq %d, rc=%d\n", irq, rc);
760         init_waitqueue_head (&ti->wq);
761         ti->kernbuf = ti->userbuf = ti->discdata = NULL;
762         tapestate_set (ti, TS_UNUSED);
763         ti->discdata=NULL;
764         ti->discipline->setup_assist (ti);
765         ti->wanna_wakeup=0;
766         s390irq_spin_unlock_irqrestore (irq, lockflags);
767         return rc;
768 }
769
770 /*
771  *      tape_init will register the driver for each tape.
772  */
773 int
774 tape_init (void)
775 {
776         long lockflags;
777         s390_dev_info_t dinfo;
778         tape_discipline_t *disc;
779         tape_info_t *ti = NULL, *tempti = NULL;
780         char *opt_char,*opt_block,*opt_3490,*opt_3480;
781         int irq = 0, rc, retries = 0, tape_num = 0;
782         static int initialized=0;
783
784         if (initialized) // Only init the devices once
785             return 0;
786         initialized=1;
787
788 #ifdef TAPE_DEBUG
789         tape_debug_area = debug_register ( "tape", 3, 2, 10);
790         debug_register_view(tape_debug_area,&debug_hex_ascii_view);
791         debug_text_event (tape_debug_area,3,"begin init");
792 #endif /* TAPE_DEBUG */
793
794         /* print banner */        
795         PRINT_WARN ("IBM S/390 Tape Device Driver (v1.01).\n");
796         PRINT_WARN ("(C) IBM Deutschland Entwicklung GmbH, 2000\n");
797         opt_char=opt_block=opt_3480=opt_3490="not present";
798 #ifdef CONFIG_S390_TAPE_CHAR
799         opt_char="built in";
800 #endif
801 #ifdef CONFIG_S390_TAPE_BLOCK
802         opt_block="built in";
803 #endif
804 #ifdef CONFIG_S390_TAPE_3480
805         opt_3480="built in";
806 #endif
807 #ifdef CONFIG_S390_TAPE_3490
808         opt_3490="built in";
809 #endif
810         /* print feature info */
811         PRINT_WARN ("character device frontend   : %s\n",opt_char);
812         PRINT_WARN ("block device frontend       : %s\n",opt_block);
813         PRINT_WARN ("support for 3480 compatible : %s\n",opt_3480);
814         PRINT_WARN ("support for 3490 compatible : %s\n",opt_3490);
815         
816 #ifndef MODULE
817         tape_split_parm_string(tape_parm_string);
818 #endif
819         if (*tape) 
820             PRINT_INFO ("Using ranges supplied in parameters, disabling autoprobe mode.\n");
821         else
822             PRINT_INFO ("No parameters supplied, enabling autoprobe mode for all supported devices.\n");
823 #ifdef CONFIG_S390_TAPE_3490
824         if (*tape)
825             first_discipline = tape3490_init (0); // no autoprobe for devices
826         else
827             first_discipline = tape3490_init (1); // do autoprobe since no parm specified
828         first_discipline->next = NULL;
829 #endif
830
831 #ifdef CONFIG_S390_TAPE_3480
832         if (first_discipline == NULL) {
833             if (*tape)
834                 first_discipline = tape3480_init (0); // no autoprobe for devices
835             else 
836                 first_discipline = tape3480_init (1); // do autoprobe since no parm specified
837             first_discipline->next = NULL;
838         } else {
839             if (*tape)
840                 first_discipline->next = tape3480_init (0); // no autoprobe for devices
841             else
842                 first_discipline->next = tape3480_init (1); // do autoprobe since no parm specified
843             ((tape_discipline_t*) (first_discipline->next))->next=NULL;
844         }
845 #endif
846 #ifdef CONFIG_DEVFS_FS
847         tape_devfs_root_entry=devfs_mk_dir (NULL, "tape", NULL);
848 #endif CONFIG_DEVFS_FS
849
850 #ifdef TAPE_DEBUG
851         debug_text_event (tape_debug_area,3,"dev detect");
852 #endif /* TAPE_DEBUG */
853         /* Allocate the tape structures */
854         if (*tape!=NULL) {
855             // we have parameters, continue with parsing the parameters and set the devices online
856             tape_parm_parse (tape);
857         } else {
858             // we are running in autodetect mode, search all devices for compatibles
859             for (irq = get_irq_first(); irq!=-ENODEV; irq=get_irq_next(irq)) {
860                 rc = get_dev_info_by_irq (irq, &dinfo);
861                 disc = first_discipline;
862                 while ((disc != NULL) && (disc->cu_type != dinfo.sid_data.cu_type))
863                     disc = (tape_discipline_t *) (disc->next);
864                 if ((disc == NULL) || (rc == -ENODEV))
865                     continue;
866 #ifdef TAPE_DEBUG
867                 debug_text_event (tape_debug_area,3,"det irq:  ");
868                 debug_int_event (tape_debug_area,3,irq);
869                 debug_text_event (tape_debug_area,3,"cu:       ");
870                 debug_int_event (tape_debug_area,3,disc->cu_type);
871 #endif /* TAPE_DEBUG */
872                 PRINT_INFO ("using devno %04x with discipline %04x on irq %d as tape device %d\n",dinfo.devno,dinfo.sid_data.cu_type,irq,tape_num/2);
873                 /* Allocate tape structure  */
874                 ti = kmalloc (sizeof (tape_info_t), GFP_ATOMIC);
875                 if (ti == NULL) {
876 #ifdef TAPE_DEBUG
877                     debug_text_exception (tape_debug_area,3,"ti:no mem ");
878 #endif /* TAPE_DEBUG */
879                     PRINT_INFO ("tape: can't allocate memory for "
880                                     "tape info structure\n");
881                     continue;
882                 }
883                 memset(ti,0,sizeof(tape_info_t));
884                 ti->discipline = disc;
885                 disc->tape = ti;
886                 rc = tape_setup (ti, irq, tape_num);
887                 if (rc) {
888 #ifdef TAPE_DEBUG
889                     debug_text_event (tape_debug_area,3,"tsetup err");
890                     debug_int_exception (tape_debug_area,3,rc);
891 #endif /* TAPE_DEBUG */
892                     kfree (ti);
893                 } else {
894                     s390irq_spin_lock_irqsave (irq, lockflags);
895                     if (first_tape_info == NULL) {
896                         first_tape_info = ti;
897                     } else {
898                         tempti = first_tape_info;
899                         while (tempti->next != NULL)
900                             tempti = tempti->next;
901                         tempti->next = ti;
902                     }
903                     tape_num += 2;
904                     s390irq_spin_unlock_irqrestore (irq, lockflags);
905                 }
906             }
907         }
908             
909         /* Allocate local buffer for the ccwcache       */
910         tape_init_emergency_req ();
911 #ifdef CONFIG_PROC_FS
912 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,3,98))
913         tape_devices_entry = create_proc_entry ("tapedevices",
914                                                 S_IFREG | S_IRUGO | S_IWUSR,
915                                                 &proc_root);
916         tape_devices_entry->proc_fops = &tape_devices_file_ops;
917         tape_devices_entry->proc_iops = &tape_devices_inode_ops;
918 #else
919         tape_devices_entry = (struct proc_dir_entry *) kmalloc 
920             (sizeof (struct proc_dir_entry), GFP_ATOMIC);
921         if (tape_devices_entry) {
922             memset (tape_devices_entry, 0, sizeof (struct proc_dir_entry));
923             tape_devices_entry->name = "tapedevices";
924             tape_devices_entry->namelen = strlen ("tapedevices");
925             tape_devices_entry->low_ino = 0;
926             tape_devices_entry->mode = (S_IFREG | S_IRUGO | S_IWUSR);
927             tape_devices_entry->nlink = 1;
928             tape_devices_entry->uid = 0;
929             tape_devices_entry->gid = 0;
930             tape_devices_entry->size = 0;
931             tape_devices_entry->get_info = NULL;
932             tape_devices_entry->ops = &tape_devices_inode_ops;
933             proc_register (&proc_root, tape_devices_entry);
934         }
935 #endif
936 #endif /* CONFIG_PROC_FS */
937
938         return 0;
939 }
940
941 #ifdef MODULE
942 MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte (cotte@de.ibm.com)");
943 MODULE_DESCRIPTION("Linux for S/390 channel attached tape device driver");
944 MODULE_PARM (tape, "1-" __MODULE_STRING (256) "s");
945
946 int
947 init_module (void)
948 {
949 #ifdef CONFIG_S390_TAPE_CHAR
950         tapechar_init ();
951 #endif
952 #ifdef CONFIG_S390_TAPE_BLOCK
953         tapeblock_init ();
954 #endif
955         return 0;
956 }
957
958 void
959 cleanup_module (void)
960 {
961         tape_info_t *ti ,*temp;
962         tape_frontend_t* frontend, *tempfe;
963         tape_discipline_t* disc ,*tempdi;
964         int i;
965 #ifdef TAPE_DEBUG
966         debug_text_event (tape_debug_area,6,"cleaup mod");
967 #endif /* TAPE_DEBUG */
968
969         if (*tape) {
970             // we are running with parameters. we'll now deregister from our devno's
971             for (i=0;i<devregct;i++) {
972                 s390_device_unregister(tape_devreg[devregct]);
973             }
974         }
975         ti = first_tape_info;
976         while (ti != NULL) {
977                 temp = ti;
978                 ti = ti->next;
979                 //cleanup a device 
980 #ifdef TAPE_DEBUG
981                 debug_text_event (tape_debug_area,6,"free irq:");
982                 debug_int_event (tape_debug_area,6,temp->devinfo.irq);
983 #endif /* TAPE_DEBUG */
984                 free_irq (temp->devinfo.irq, &(temp->devstat));
985                 if (temp->discdata) kfree (temp->discdata);
986                 if (temp->kernbuf) kfree (temp->kernbuf);
987                 if (temp->cqr) tape_free_request(temp->cqr);
988 #ifdef CONFIG_DEVFS_FS
989                 for (frontend=first_frontend;frontend!=NULL;frontend=frontend->next)
990                     frontend->rmdevfstree(temp);
991                 tape_rmdevfsroots(temp);
992 #endif
993                 kfree (temp);
994         }
995 #ifdef CONFIG_DEVFS_FS
996         devfs_unregister (tape_devfs_root_entry);
997 #endif CONFIG_DEVFS_FS
998 #ifdef CONFIG_PROC_FS
999 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,3,98))
1000         remove_proc_entry ("tapedevices", &proc_root);
1001 #else
1002         proc_unregister (&proc_root, tape_devices_entry->low_ino);
1003         kfree (tape_devices_entry);
1004 #endif                          /* LINUX_IS_24 */
1005 #endif 
1006 #ifdef CONFIG_S390_TAPE_CHAR
1007         tapechar_uninit();
1008 #endif
1009 #ifdef CONFIG_S390_TAPE_BLOCK
1010         tapeblock_uninit();
1011 #endif
1012         frontend=first_frontend;
1013         while (frontend != NULL) {
1014                 tempfe = frontend;
1015                 frontend = frontend->next;
1016                 kfree (tempfe);
1017         }
1018         disc=first_discipline;
1019         while (disc != NULL) {
1020                 if (*tape)
1021                     disc->shutdown(0);
1022                 else
1023                     disc->shutdown(1);
1024                 tempdi = disc;
1025                 disc = disc->next;
1026                 kfree (tempdi);
1027         }
1028         /* Deallocate the local buffer for the ccwcache         */
1029         tape_cleanup_emergency_req ();
1030 #ifdef TAPE_DEBUG
1031         debug_unregister (tape_debug_area);
1032 #endif /* TAPE_DEBUG */
1033 }
1034 #endif                          /* MODULE */
1035
1036 inline void
1037 tapestate_set (tape_info_t * ti, int newstate)
1038 {
1039     if (ti->tape_state == TS_NOT_OPER) {
1040 #ifdef TAPE_DEBUG
1041         debug_text_event (tape_debug_area,3,"ts_set err");
1042         debug_text_exception (tape_debug_area,3,"dev n.oper");
1043 #endif /* TAPE_DEBUG */
1044     } else {
1045 #ifdef TAPE_DEBUG
1046         debug_text_event (tape_debug_area,4,"ts. dev:  ");
1047         debug_int_event (tape_debug_area,4,ti->blk_minor);
1048         debug_text_event (tape_debug_area,4,"old ts:   ");
1049         debug_text_event (tape_debug_area,4,(((tapestate_get (ti) < TS_SIZE) &&
1050                                              (tapestate_get (ti) >=0 )) ?
1051                                             state_verbose[tapestate_get (ti)] :
1052                                             "UNKNOWN TS"));
1053         debug_text_event (tape_debug_area,4,"new ts:   ");
1054         debug_text_event (tape_debug_area,4,(((newstate < TS_SIZE) &&
1055                                               (newstate >= 0)) ?
1056                                              state_verbose[newstate] :
1057                                              "UNKNOWN TS"));
1058 #endif /* TAPE_DEBUG */
1059         ti->tape_state = newstate;
1060     }
1061 }
1062
1063 inline int
1064 tapestate_get (tape_info_t * ti)
1065 {
1066         return (ti->tape_state);
1067 }
1068
1069 void
1070 tapestate_event (tape_info_t * ti, int event)
1071 {
1072 #ifdef TAPE_DEBUG
1073         debug_text_event (tape_debug_area,6,"te! dev:  ");
1074         debug_int_event (tape_debug_area,6,ti->blk_minor);
1075         debug_text_event (tape_debug_area,6,"event:");
1076         debug_text_event (tape_debug_area,6,((event >=0) &&
1077                                             (event < TE_SIZE)) ?
1078                          event_verbose[event] : "TE UNKNOWN");
1079         debug_text_event (tape_debug_area,6,"state:");
1080         debug_text_event (tape_debug_area,6,((tapestate_get(ti) >= 0) &&
1081                                             (tapestate_get(ti) < TS_SIZE)) ?
1082                          state_verbose[tapestate_get (ti)] :
1083                          "TS UNKNOWN");
1084 #endif /* TAPE_DEBUG */    
1085         if (event == TE_ERROR) { 
1086             ti->discipline->error_recovery(ti);
1087         } else {
1088             if ((event >= 0) &&
1089                 (event < TE_SIZE) &&
1090                 (tapestate_get (ti) >= 0) &&
1091                 (tapestate_get (ti) < TS_SIZE) &&
1092                 ((*(ti->discipline->event_table))[tapestate_get (ti)][event] != NULL))
1093                 ((*(ti->discipline->event_table))[tapestate_get (ti)][event]) (ti);
1094             else {
1095 #ifdef TAPE_DEBUG
1096                 debug_text_exception (tape_debug_area,3,"TE UNEXPEC");
1097 #endif /* TAPE_DEBUG */
1098                 ti->discipline->default_handler (ti);
1099             }
1100         }
1101 }
1102
1103 /*
1104  * Overrides for Emacs so that we follow Linus's tabbing style.
1105  * Emacs will notice this stuff at the end of the file and automatically
1106  * adjust the settings for this buffer only.  This must remain at the end
1107  * of the file.
1108  * ---------------------------------------------------------------------------
1109  * Local variables:
1110  * c-indent-level: 4
1111  * c-brace-imaginary-offset: 0
1112  * c-brace-offset: -4
1113  * c-argdecl-indent: 4
1114  * c-label-offset: -4
1115  * c-continued-statement-offset: 4
1116  * c-continued-brace-offset: 0
1117  * indent-tabs-mode: nil
1118  * tab-width: 8
1119  * End:
1120  */