upgrade to linux 2.6.9-1.11_FC2
[linux-2.6.git] / drivers / s390 / block / dasd.c
1 /*
2  * File...........: linux/drivers/s390/block/dasd.c
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *                  Horst Hummel <Horst.Hummel@de.ibm.com>
5  *                  Carsten Otte <Cotte@de.ibm.com>
6  *                  Martin Schwidefsky <schwidefsky@de.ibm.com>
7  * Bugreports.to..: <Linux390@de.ibm.com>
8  * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
9  *
10  * $Revision: 1.149 $
11  */
12
13 #include <linux/config.h>
14 #include <linux/kmod.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/ctype.h>
18 #include <linux/major.h>
19 #include <linux/slab.h>
20 #include <linux/buffer_head.h>
21
22 #include <asm/ccwdev.h>
23 #include <asm/ebcdic.h>
24 #include <asm/idals.h>
25 #include <asm/todclk.h>
26
27 /* This is ugly... */
28 #define PRINTK_HEADER "dasd:"
29
30 #include "dasd_int.h"
31 /*
32  * SECTION: Constant definitions to be used within this file
33  */
34 #define DASD_CHANQ_MAX_SIZE 4
35
36 /*
37  * SECTION: exported variables of dasd.c
38  */
39 debug_info_t *dasd_debug_area;
40 struct dasd_discipline *dasd_diag_discipline_pointer;
41
42 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
43 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
44                    " Copyright 2000 IBM Corporation");
45 MODULE_SUPPORTED_DEVICE("dasd");
46 MODULE_PARM(dasd, "1-" __MODULE_STRING(256) "s");
47 MODULE_LICENSE("GPL");
48
49 /*
50  * SECTION: prototypes for static functions of dasd.c
51  */
52 static int  dasd_alloc_queue(struct dasd_device * device);
53 static void dasd_setup_queue(struct dasd_device * device);
54 static void dasd_free_queue(struct dasd_device * device);
55 static void dasd_flush_request_queue(struct dasd_device *);
56 static void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
57 static void dasd_flush_ccw_queue(struct dasd_device *, int);
58 static void dasd_tasklet(struct dasd_device *);
59 static void do_kick_device(void *data);
60
61 /*
62  * SECTION: Operations on the device structure.
63  */
64 static wait_queue_head_t dasd_init_waitq;
65
66 /*
67  * Allocate memory for a new device structure.
68  */
69 struct dasd_device *
70 dasd_alloc_device(void)
71 {
72         struct dasd_device *device;
73
74         device = kmalloc(sizeof (struct dasd_device), GFP_ATOMIC);
75         if (device == NULL)
76                 return ERR_PTR(-ENOMEM);
77         memset(device, 0, sizeof (struct dasd_device));
78         /* open_count = 0 means device online but not in use */
79         atomic_set(&device->open_count, -1);
80
81         /* Get two pages for normal block device operations. */
82         device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
83         if (device->ccw_mem == NULL) {
84                 kfree(device);
85                 return ERR_PTR(-ENOMEM);
86         }
87         /* Get one page for error recovery. */
88         device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
89         if (device->erp_mem == NULL) {
90                 free_pages((unsigned long) device->ccw_mem, 1);
91                 kfree(device);
92                 return ERR_PTR(-ENOMEM);
93         }
94
95         dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
96         dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
97         spin_lock_init(&device->mem_lock);
98         spin_lock_init(&device->request_queue_lock);
99         atomic_set (&device->tasklet_scheduled, 0);
100         tasklet_init(&device->tasklet, 
101                      (void (*)(unsigned long)) dasd_tasklet,
102                      (unsigned long) device);
103         INIT_LIST_HEAD(&device->ccw_queue);
104         init_timer(&device->timer);
105         INIT_WORK(&device->kick_work, do_kick_device, device);
106         device->state = DASD_STATE_NEW;
107         device->target = DASD_STATE_NEW;
108
109         return device;
110 }
111
112 /*
113  * Free memory of a device structure.
114  */
115 void
116 dasd_free_device(struct dasd_device *device)
117 {
118         if (device->private)
119                 kfree(device->private);
120         free_page((unsigned long) device->erp_mem);
121         free_pages((unsigned long) device->ccw_mem, 1);
122         kfree(device);
123 }
124
125 /*
126  * Make a new device known to the system.
127  */
128 static inline int
129 dasd_state_new_to_known(struct dasd_device *device)
130 {
131         int rc;
132
133         /*
134          * As long as the device is not in state DASD_STATE_NEW we want to 
135          * keep the reference count > 0.
136          */
137         dasd_get_device(device);
138
139         rc = dasd_alloc_queue(device);
140         if (rc) {
141                 dasd_put_device(device);
142                 return rc;
143         }
144
145         device->state = DASD_STATE_KNOWN;
146         return 0;
147 }
148
149 /*
150  * Let the system forget about a device.
151  */
152 static inline void
153 dasd_state_known_to_new(struct dasd_device * device)
154 {
155         /* Forget the discipline information. */
156         device->discipline = NULL;
157         device->state = DASD_STATE_NEW;
158
159         dasd_free_queue(device);
160
161         /* Give up reference we took in dasd_state_new_to_known. */
162         dasd_put_device(device);
163 }
164
165 /*
166  * Request the irq line for the device.
167  */
168 static inline int
169 dasd_state_known_to_basic(struct dasd_device * device)
170 {
171         int rc;
172
173         /* Allocate and register gendisk structure. */
174         rc = dasd_gendisk_alloc(device);
175         if (rc)
176                 return rc;
177
178         /* register 'device' debug area, used for all DBF_DEV_XXX calls */
179         device->debug_area = debug_register(device->cdev->dev.bus_id, 0, 2,
180                                             8 * sizeof (long));
181         debug_register_view(device->debug_area, &debug_sprintf_view);
182         debug_set_level(device->debug_area, DBF_ERR);
183         DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
184
185         device->state = DASD_STATE_BASIC;
186         return 0;
187 }
188
189 /*
190  * Release the irq line for the device. Terminate any running i/o.
191  */
192 static inline void
193 dasd_state_basic_to_known(struct dasd_device * device)
194 {
195         dasd_gendisk_free(device);
196         dasd_flush_ccw_queue(device, 1);
197         DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
198         if (device->debug_area != NULL) {
199                 debug_unregister(device->debug_area);
200                 device->debug_area = NULL;
201         }
202         device->state = DASD_STATE_KNOWN;
203 }
204
205 /*
206  * Do the initial analysis. The do_analysis function may return
207  * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
208  * until the discipline decides to continue the startup sequence
209  * by calling the function dasd_change_state. The eckd disciplines
210  * uses this to start a ccw that detects the format. The completion
211  * interrupt for this detection ccw uses the kernel event daemon to
212  * trigger the call to dasd_change_state. All this is done in the
213  * discipline code, see dasd_eckd.c.
214  * After the analysis ccw is done (do_analysis returned 0 or error)
215  * the block device is setup. Either a fake disk is added to allow
216  * formatting or a proper device request queue is created.
217  */
218 static inline int
219 dasd_state_basic_to_ready(struct dasd_device * device)
220 {
221         int rc;
222
223         rc = 0;
224         if (device->discipline->do_analysis != NULL)
225                 rc = device->discipline->do_analysis(device);
226         if (rc)
227                 return rc;
228         dasd_setup_queue(device);
229         device->state = DASD_STATE_READY;
230         if (dasd_scan_partitions(device) != 0)
231                 device->state = DASD_STATE_BASIC;
232         return 0;
233 }
234
235 /*
236  * Remove device from block device layer. Destroy dirty buffers.
237  * Forget format information. Check if the target level is basic
238  * and if it is create fake disk for formatting.
239  */
240 static inline void
241 dasd_state_ready_to_basic(struct dasd_device * device)
242 {
243         dasd_flush_ccw_queue(device, 0);
244         dasd_destroy_partitions(device);
245         dasd_flush_request_queue(device);
246         device->blocks = 0;
247         device->bp_block = 0;
248         device->s2b_shift = 0;
249         device->state = DASD_STATE_BASIC;
250 }
251
252 /*
253  * Make the device online and schedule the bottom half to start
254  * the requeueing of requests from the linux request queue to the
255  * ccw queue.
256  */
257 static inline int
258 dasd_state_ready_to_online(struct dasd_device * device)
259 {
260         device->state = DASD_STATE_ONLINE;
261         dasd_schedule_bh(device);
262         return 0;
263 }
264
265 /*
266  * Stop the requeueing of requests again.
267  */
268 static inline void
269 dasd_state_online_to_ready(struct dasd_device * device)
270 {
271         device->state = DASD_STATE_READY;
272 }
273
274 /*
275  * Device startup state changes.
276  */
277 static inline int
278 dasd_increase_state(struct dasd_device *device)
279 {
280         int rc;
281
282         rc = 0;
283         if (device->state == DASD_STATE_NEW &&
284             device->target >= DASD_STATE_KNOWN)
285                 rc = dasd_state_new_to_known(device);
286
287         if (!rc &&
288             device->state == DASD_STATE_KNOWN &&
289             device->target >= DASD_STATE_BASIC)
290                 rc = dasd_state_known_to_basic(device);
291
292         if (!rc &&
293             device->state == DASD_STATE_BASIC &&
294             device->target >= DASD_STATE_READY)
295                 rc = dasd_state_basic_to_ready(device);
296
297         if (!rc &&
298             device->state == DASD_STATE_READY &&
299             device->target >= DASD_STATE_ONLINE)
300                 rc = dasd_state_ready_to_online(device);
301
302         return rc;
303 }
304
305 /*
306  * Device shutdown state changes.
307  */
308 static inline int
309 dasd_decrease_state(struct dasd_device *device)
310 {
311         if (device->state == DASD_STATE_ONLINE &&
312             device->target <= DASD_STATE_READY)
313                 dasd_state_online_to_ready(device);
314         
315         if (device->state == DASD_STATE_READY &&
316             device->target <= DASD_STATE_BASIC)
317                 dasd_state_ready_to_basic(device);
318         
319         if (device->state == DASD_STATE_BASIC && 
320             device->target <= DASD_STATE_KNOWN)
321                 dasd_state_basic_to_known(device);
322         
323         if (device->state == DASD_STATE_KNOWN &&
324             device->target <= DASD_STATE_NEW)
325                 dasd_state_known_to_new(device);
326
327         return 0;
328 }
329
330 /*
331  * This is the main startup/shutdown routine.
332  */
333 static void
334 dasd_change_state(struct dasd_device *device)
335 {
336         int rc;
337
338         if (device->state == device->target)
339                 /* Already where we want to go today... */
340                 return;
341         if (device->state < device->target)
342                 rc = dasd_increase_state(device);
343         else
344                 rc = dasd_decrease_state(device);
345         if (rc && rc != -EAGAIN)
346                 device->target = device->state;
347
348         if (device->state == device->target)
349                 wake_up(&dasd_init_waitq);
350 }
351
352 /*
353  * Kick starter for devices that did not complete the startup/shutdown
354  * procedure or were sleeping because of a pending state.
355  * dasd_kick_device will schedule a call do do_kick_device to the kernel
356  * event daemon.
357  */
358 static void
359 do_kick_device(void *data)
360 {
361         struct dasd_device *device;
362
363         device = (struct dasd_device *) data;
364         dasd_change_state(device);
365         dasd_schedule_bh(device);
366         dasd_put_device(device);
367 }
368
369 void
370 dasd_kick_device(struct dasd_device *device)
371 {
372         dasd_get_device(device);
373         /* queue call to dasd_kick_device to the kernel event daemon. */
374         schedule_work(&device->kick_work);
375 }
376
377 /*
378  * Set the target state for a device and starts the state change.
379  */
380 void
381 dasd_set_target_state(struct dasd_device *device, int target)
382 {
383         /* If we are in probeonly mode stop at DASD_STATE_READY. */
384         if (dasd_probeonly && target > DASD_STATE_READY)
385                 target = DASD_STATE_READY;
386         if (device->target != target) {
387                 if (device->state == target)
388                         wake_up(&dasd_init_waitq);
389                 device->target = target;
390         }
391         if (device->state != device->target)
392                 dasd_change_state(device);
393 }
394
395 /*
396  * Enable devices with device numbers in [from..to].
397  */
398 static inline int
399 _wait_for_device(struct dasd_device *device)
400 {
401         return (device->state == device->target);
402 }
403
404 void
405 dasd_enable_device(struct dasd_device *device)
406 {
407         dasd_set_target_state(device, DASD_STATE_ONLINE);
408         if (device->state <= DASD_STATE_KNOWN)
409                 /* No discipline for device found. */
410                 dasd_set_target_state(device, DASD_STATE_NEW);
411         /* Now wait for the devices to come up. */
412         wait_event(dasd_init_waitq, _wait_for_device(device));
413 }
414
415 /*
416  * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
417  */
418 #ifdef CONFIG_DASD_PROFILE
419
420 struct dasd_profile_info_t dasd_global_profile;
421 unsigned int dasd_profile_level = DASD_PROFILE_OFF;
422
423 /*
424  * Increments counter in global and local profiling structures.
425  */
426 #define dasd_profile_counter(value, counter, device) \
427 { \
428         int index; \
429         for (index = 0; index < 31 && value >> (2+index); index++); \
430         dasd_global_profile.counter[index]++; \
431         device->profile.counter[index]++; \
432 }
433
434 /*
435  * Add profiling information for cqr before execution.
436  */
437 static inline void
438 dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
439                    struct request *req)
440 {
441         struct list_head *l;
442         unsigned int counter;
443
444         if (dasd_profile_level != DASD_PROFILE_ON)
445                 return;
446
447         /* count the length of the chanq for statistics */
448         counter = 0;
449         list_for_each(l, &device->ccw_queue)
450                 if (++counter >= 31)
451                         break;
452         dasd_global_profile.dasd_io_nr_req[counter]++;
453         device->profile.dasd_io_nr_req[counter]++;
454 }
455
456 /*
457  * Add profiling information for cqr after execution.
458  */
459 static inline void
460 dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
461                  struct request *req)
462 {
463         long strtime, irqtime, endtime, tottime;        /* in microseconds */
464         long tottimeps, sectors;
465
466         if (dasd_profile_level != DASD_PROFILE_ON)
467                 return;
468
469         sectors = req->nr_sectors;
470         if (!cqr->buildclk || !cqr->startclk ||
471             !cqr->stopclk || !cqr->endclk ||
472             !sectors)
473                 return;
474
475         strtime = ((cqr->startclk - cqr->buildclk) >> 12);
476         irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
477         endtime = ((cqr->endclk - cqr->stopclk) >> 12);
478         tottime = ((cqr->endclk - cqr->buildclk) >> 12);
479         tottimeps = tottime / sectors;
480
481         if (!dasd_global_profile.dasd_io_reqs)
482                 memset(&dasd_global_profile, 0,
483                        sizeof (struct dasd_profile_info_t));
484         dasd_global_profile.dasd_io_reqs++;
485         dasd_global_profile.dasd_io_sects += sectors;
486
487         if (!device->profile.dasd_io_reqs)
488                 memset(&device->profile, 0,
489                        sizeof (struct dasd_profile_info_t));
490         device->profile.dasd_io_reqs++;
491         device->profile.dasd_io_sects += sectors;
492
493         dasd_profile_counter(sectors, dasd_io_secs, device);
494         dasd_profile_counter(tottime, dasd_io_times, device);
495         dasd_profile_counter(tottimeps, dasd_io_timps, device);
496         dasd_profile_counter(strtime, dasd_io_time1, device);
497         dasd_profile_counter(irqtime, dasd_io_time2, device);
498         dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
499         dasd_profile_counter(endtime, dasd_io_time3, device);
500 }
501 #else
502 #define dasd_profile_start(device, cqr, req) do {} while (0)
503 #define dasd_profile_end(device, cqr, req) do {} while (0)
504 #endif                          /* CONFIG_DASD_PROFILE */
505
506 /*
507  * Allocate memory for a channel program with 'cplength' channel
508  * command words and 'datasize' additional space. There are two
509  * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
510  * memory and 2) dasd_smalloc_request uses the static ccw memory
511  * that gets allocated for each device.
512  */
513 struct dasd_ccw_req *
514 dasd_kmalloc_request(char *magic, int cplength, int datasize,
515                    struct dasd_device * device)
516 {
517         struct dasd_ccw_req *cqr;
518
519         /* Sanity checks */
520         if ( magic == NULL || datasize > PAGE_SIZE ||
521              (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
522                 BUG();
523         debug_text_event ( dasd_debug_area, 1, "ALLC");
524         debug_text_event ( dasd_debug_area, 1, magic);
525         debug_int_event ( dasd_debug_area, 1, cplength);
526         debug_int_event ( dasd_debug_area, 1, datasize);
527
528         cqr = kmalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
529         if (cqr == NULL)
530                 return ERR_PTR(-ENOMEM);
531         memset(cqr, 0, sizeof(struct dasd_ccw_req));
532         cqr->cpaddr = NULL;
533         if (cplength > 0) {
534                 cqr->cpaddr = kmalloc(cplength*sizeof(struct ccw1),
535                                       GFP_ATOMIC | GFP_DMA);
536                 if (cqr->cpaddr == NULL) {
537                         kfree(cqr);
538                         return ERR_PTR(-ENOMEM);
539                 }
540                 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
541         }
542         cqr->data = NULL;
543         if (datasize > 0) {
544                 cqr->data = kmalloc(datasize, GFP_ATOMIC | GFP_DMA);
545                 if (cqr->data == NULL) {
546                         if (cqr->cpaddr != NULL)
547                                 kfree(cqr->cpaddr);
548                         kfree(cqr);
549                         return ERR_PTR(-ENOMEM);
550                 }
551                 memset(cqr->data, 0, datasize);
552         }
553         strncpy((char *) &cqr->magic, magic, 4);
554         ASCEBC((char *) &cqr->magic, 4);
555         set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
556         dasd_get_device(device);
557         return cqr;
558 }
559
560 struct dasd_ccw_req *
561 dasd_smalloc_request(char *magic, int cplength, int datasize,
562                    struct dasd_device * device)
563 {
564         unsigned long flags;
565         struct dasd_ccw_req *cqr;
566         char *data;
567         int size;
568
569         /* Sanity checks */
570         if ( magic == NULL || datasize > PAGE_SIZE ||
571              (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
572                 BUG();
573         debug_text_event ( dasd_debug_area, 1, "ALLC");
574         debug_text_event ( dasd_debug_area, 1, magic);
575         debug_int_event ( dasd_debug_area, 1, cplength);
576         debug_int_event ( dasd_debug_area, 1, datasize);
577
578         size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
579         if (cplength > 0)
580                 size += cplength * sizeof(struct ccw1);
581         if (datasize > 0)
582                 size += datasize;
583         spin_lock_irqsave(&device->mem_lock, flags);
584         cqr = (struct dasd_ccw_req *)
585                 dasd_alloc_chunk(&device->ccw_chunks, size);
586         spin_unlock_irqrestore(&device->mem_lock, flags);
587         if (cqr == NULL)
588                 return ERR_PTR(-ENOMEM);
589         memset(cqr, 0, sizeof(struct dasd_ccw_req));
590         data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
591         cqr->cpaddr = NULL;
592         if (cplength > 0) {
593                 cqr->cpaddr = (struct ccw1 *) data;
594                 data += cplength*sizeof(struct ccw1);
595                 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
596         }
597         cqr->data = NULL;
598         if (datasize > 0) {
599                 cqr->data = data;
600                 memset(cqr->data, 0, datasize);
601         }
602         strncpy((char *) &cqr->magic, magic, 4);
603         ASCEBC((char *) &cqr->magic, 4);
604         set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
605         dasd_get_device(device);
606         return cqr;
607 }
608
609 /*
610  * Free memory of a channel program. This function needs to free all the
611  * idal lists that might have been created by dasd_set_cda and the
612  * struct dasd_ccw_req itself.
613  */
614 void
615 dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
616 {
617 #ifdef CONFIG_ARCH_S390X
618         struct ccw1 *ccw;
619
620         /* Clear any idals used for the request. */
621         ccw = cqr->cpaddr;
622         do {
623                 clear_normalized_cda(ccw);
624         } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
625 #endif
626         debug_text_event ( dasd_debug_area, 1, "FREE");
627         debug_int_event ( dasd_debug_area, 1, (long) cqr);
628         if (cqr->cpaddr != NULL)
629                 kfree(cqr->cpaddr);
630         if (cqr->data != NULL)
631                 kfree(cqr->data);
632         kfree(cqr);
633         dasd_put_device(device);
634 }
635
636 void
637 dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
638 {
639         unsigned long flags;
640
641         debug_text_event(dasd_debug_area, 1, "FREE");
642         debug_int_event(dasd_debug_area, 1, (long) cqr);
643         spin_lock_irqsave(&device->mem_lock, flags);
644         dasd_free_chunk(&device->ccw_chunks, cqr);
645         spin_unlock_irqrestore(&device->mem_lock, flags);
646         dasd_put_device(device);
647 }
648
649 /*
650  * Check discipline magic in cqr.
651  */
652 static inline int
653 dasd_check_cqr(struct dasd_ccw_req *cqr)
654 {
655         struct dasd_device *device;
656
657         if (cqr == NULL)
658                 return -EINVAL;
659         device = cqr->device;
660         if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
661                 DEV_MESSAGE(KERN_WARNING, device,
662                             " dasd_ccw_req 0x%08x magic doesn't match"
663                             " discipline 0x%08x",
664                             cqr->magic,
665                             *(unsigned int *) device->discipline->name);
666                 return -EINVAL;
667         }
668         return 0;
669 }
670
671 /*
672  * Terminate the current i/o and set the request to clear_pending.
673  * Timer keeps device runnig.
674  * ccw_device_clear can fail if the i/o subsystem
675  * is in a bad mood.
676  */
677 int
678 dasd_term_IO(struct dasd_ccw_req * cqr)
679 {
680         struct dasd_device *device;
681         int retries, rc;
682
683         /* Check the cqr */
684         rc = dasd_check_cqr(cqr);
685         if (rc)
686                 return rc;
687         retries = 0;
688         device = (struct dasd_device *) cqr->device;
689         while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
690                 rc = ccw_device_clear(device->cdev, (long) cqr);
691                 switch (rc) {
692                 case 0: /* termination successful */
693                         if (cqr->retries > 0) {
694                                 cqr->retries--;
695                                 cqr->status = DASD_CQR_CLEAR;
696                         } else
697                                 cqr->status = DASD_CQR_FAILED;
698                         cqr->stopclk = get_clock();
699                         break;
700                 case -ENODEV:
701                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
702                                       "device gone, retry");
703                         break;
704                 case -EIO:
705                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
706                                       "I/O error, retry");
707                         break;
708                 case -EINVAL:
709                 case -EBUSY:
710                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
711                                       "device busy, retry later");
712                         break;
713                 default:
714                         DEV_MESSAGE(KERN_ERR, device,
715                                     "line %d unknown RC=%d, please "
716                                     "report to linux390@de.ibm.com",
717                                     __LINE__, rc);
718                         BUG();
719                         break;
720                 }
721                 retries++;
722         }
723         dasd_schedule_bh(device);
724         return rc;
725 }
726
727 /*
728  * Start the i/o. This start_IO can fail if the channel is really busy.
729  * In that case set up a timer to start the request later.
730  */
731 int
732 dasd_start_IO(struct dasd_ccw_req * cqr)
733 {
734         struct dasd_device *device;
735         int rc;
736
737         /* Check the cqr */
738         rc = dasd_check_cqr(cqr);
739         if (rc)
740                 return rc;
741         device = (struct dasd_device *) cqr->device;
742         if (cqr->retries < 0) {
743                 DEV_MESSAGE(KERN_DEBUG, device,
744                             "start_IO: request %p (%02x/%i) - no retry left.",
745                             cqr, cqr->status, cqr->retries);
746                 cqr->status = DASD_CQR_FAILED;
747                 return -EIO;
748         }
749         cqr->startclk = get_clock();
750         cqr->starttime = jiffies;
751         cqr->retries--;
752         rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
753                               cqr->lpm, 0);
754         switch (rc) {
755         case 0:
756                 cqr->status = DASD_CQR_IN_IO;
757                 break;
758         case -EBUSY:
759                 DBF_DEV_EVENT(DBF_ERR, device, "%s",
760                               "start_IO: device busy, retry later");
761                 break;
762         case -ETIMEDOUT:
763                 DBF_DEV_EVENT(DBF_ERR, device, "%s",
764                               "start_IO: request timeout, retry later");
765                 break;
766         case -ENODEV:
767         case -EIO:
768                 DBF_DEV_EVENT(DBF_ERR, device, "%s",
769                               "start_IO: device gone, retry");
770                 break;
771         default:
772                 DEV_MESSAGE(KERN_ERR, device,
773                             "line %d unknown RC=%d, please report"
774                             " to linux390@de.ibm.com", __LINE__, rc);
775                 BUG();
776                 break;
777         }
778         return rc;
779 }
780
781 /*
782  * Timeout function for dasd devices. This is used for different purposes
783  *  1) missing interrupt handler for normal operation
784  *  2) delayed start of request where start_IO failed with -EBUSY
785  *  3) timeout for missing state change interrupts
786  * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
787  * DASD_CQR_QUEUED for 2) and 3).
788  */
789 static void
790 dasd_timeout_device(unsigned long ptr)
791 {
792         unsigned long flags;
793         struct dasd_device *device;
794
795         device = (struct dasd_device *) ptr;
796         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
797         /* re-activate request queue */
798         device->stopped &= ~DASD_STOPPED_PENDING;
799         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
800         dasd_schedule_bh(device);
801 }
802
803 /*
804  * Setup timeout for a device in jiffies.
805  */
806 void
807 dasd_set_timer(struct dasd_device *device, int expires)
808 {
809         if (expires == 0) {
810                 if (timer_pending(&device->timer))
811                         del_timer(&device->timer);
812                 return;
813         }
814         if (timer_pending(&device->timer)) {
815                 if (mod_timer(&device->timer, jiffies + expires))
816                         return;
817         }
818         device->timer.function = dasd_timeout_device;
819         device->timer.data = (unsigned long) device;
820         device->timer.expires = jiffies + expires;
821         add_timer(&device->timer);
822 }
823
824 /*
825  * Clear timeout for a device.
826  */
827 void
828 dasd_clear_timer(struct dasd_device *device)
829 {
830         if (timer_pending(&device->timer))
831                 del_timer(&device->timer);
832 }
833
834 static void
835 dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
836 {
837         struct dasd_ccw_req *cqr;
838         struct dasd_device *device;
839
840         cqr = (struct dasd_ccw_req *) intparm;
841         if (cqr->status != DASD_CQR_IN_IO) {
842                 MESSAGE(KERN_DEBUG,
843                         "invalid status in handle_killed_request: "
844                         "bus_id %s, status %02x",
845                         cdev->dev.bus_id, cqr->status);
846                 return;
847         }
848
849         device = (struct dasd_device *) cqr->device;
850         if (device == NULL ||
851             device != dasd_device_from_cdev(cdev) ||
852             strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
853                 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
854                         cdev->dev.bus_id);
855                 return;
856         }
857
858         /* Schedule request to be retried. */
859         cqr->status = DASD_CQR_QUEUED;
860
861         dasd_clear_timer(device);
862         dasd_schedule_bh(device);
863         dasd_put_device(device);
864 }
865
866 static void
867 dasd_handle_state_change_pending(struct dasd_device *device)
868 {
869         struct dasd_ccw_req *cqr;
870         struct list_head *l, *n;
871
872         device->stopped &= ~DASD_STOPPED_PENDING;
873
874         /* restart all 'running' IO on queue */
875         list_for_each_safe(l, n, &device->ccw_queue) {
876                 cqr = list_entry(l, struct dasd_ccw_req, list);
877                 if (cqr->status == DASD_CQR_IN_IO) {
878                         cqr->status = DASD_CQR_QUEUED;
879                 }
880         }
881         dasd_clear_timer(device);
882         dasd_schedule_bh(device);
883 }
884
885 /*
886  * Interrupt handler for "normal" ssch-io based dasd devices.
887  */
888 void
889 dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
890                  struct irb *irb)
891 {
892         struct dasd_ccw_req *cqr, *next;
893         struct dasd_device *device;
894         unsigned long long now;
895         int expires;
896         dasd_era_t era;
897         char mask;
898
899         if (IS_ERR(irb)) {
900                 switch (PTR_ERR(irb)) {
901                 case -EIO:
902                         dasd_handle_killed_request(cdev, intparm);
903                         break;
904                 case -ETIMEDOUT:
905                         printk(KERN_WARNING"%s(%s): request timed out\n",
906                                __FUNCTION__, cdev->dev.bus_id);
907                         //FIXME - dasd uses own timeout interface...
908                         break;
909                 default:
910                         printk(KERN_WARNING"%s(%s): unknown error %ld\n",
911                                __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
912                 }
913                 return;
914         }
915
916         now = get_clock();
917
918         DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
919                   cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
920                   (unsigned int) intparm);
921
922         /* first of all check for state change pending interrupt */
923         mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
924         if ((irb->scsw.dstat & mask) == mask) {
925                 device = dasd_device_from_cdev(cdev);
926                 if (!IS_ERR(device)) {
927                         dasd_handle_state_change_pending(device);
928                         dasd_put_device(device);
929                 }
930                 return;
931         }
932
933         cqr = (struct dasd_ccw_req *) intparm;
934
935         /* check for unsolicited interrupts */
936         if (cqr == NULL) {
937                 MESSAGE(KERN_DEBUG,
938                         "unsolicited interrupt received: bus_id %s",
939                         cdev->dev.bus_id);
940                 return;
941         }
942
943         device = (struct dasd_device *) cqr->device;
944         if (device == NULL ||
945             strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
946                 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
947                         cdev->dev.bus_id);
948                 return;
949         }
950
951         /* Check for clear pending */
952         if (cqr->status == DASD_CQR_CLEAR &&
953             irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
954                 cqr->status = DASD_CQR_QUEUED;
955                 dasd_clear_timer(device);
956                 dasd_schedule_bh(device);
957                 return;
958         }
959
960         /* check status - the request might have been killed by dyn detach */
961         if (cqr->status != DASD_CQR_IN_IO) {
962                 MESSAGE(KERN_DEBUG,
963                         "invalid status: bus_id %s, status %02x",
964                         cdev->dev.bus_id, cqr->status);
965                 return;
966         }
967         DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x",
968                       ((irb->scsw.cstat << 8) | irb->scsw.dstat));
969
970         /* Find out the appropriate era_action. */
971         if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) 
972                 era = dasd_era_fatal;
973         else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
974                  irb->scsw.cstat == 0 &&
975                  !irb->esw.esw0.erw.cons)
976                 era = dasd_era_none;
977         else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
978                 era = dasd_era_fatal; /* don't recover this request */
979         else if (irb->esw.esw0.erw.cons)
980                 era = device->discipline->examine_error(cqr, irb);
981         else 
982                 era = dasd_era_recover;
983
984         DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
985         expires = 0;
986         if (era == dasd_era_none) {
987                 cqr->status = DASD_CQR_DONE;
988                 cqr->stopclk = now;
989                 /* Start first request on queue if possible -> fast_io. */
990                 if (cqr->list.next != &device->ccw_queue) {
991                         next = list_entry(cqr->list.next,
992                                           struct dasd_ccw_req, list);
993                         if ((next->status == DASD_CQR_QUEUED) &&
994                             (!device->stopped)) {
995                                 if (device->discipline->start_IO(next) == 0)
996                                         expires = next->expires;
997                                 else
998                                         DEV_MESSAGE(KERN_DEBUG, device, "%s",
999                                                     "Interrupt fastpath "
1000                                                     "failed!");
1001                         }
1002                 }
1003         } else {                /* error */
1004                 memcpy(&cqr->irb, irb, sizeof (struct irb));
1005 #ifdef ERP_DEBUG
1006                 /* dump sense data */
1007                 dasd_log_sense(cqr, irb);
1008 #endif
1009                 switch (era) {
1010                 case dasd_era_fatal:
1011                         cqr->status = DASD_CQR_FAILED;
1012                         cqr->stopclk = now;
1013                         break;
1014                 case dasd_era_recover:
1015                         cqr->status = DASD_CQR_ERROR;
1016                         break;
1017                 default:
1018                         BUG();
1019                 }
1020         }
1021         if (expires != 0)
1022                 dasd_set_timer(device, expires);
1023         else
1024                 dasd_clear_timer(device);
1025         dasd_schedule_bh(device);
1026 }
1027
1028 /*
1029  * posts the buffer_cache about a finalized request
1030  */
1031 static inline void
1032 dasd_end_request(struct request *req, int uptodate)
1033 {
1034         if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
1035                 BUG();
1036         add_disk_randomness(req->rq_disk);
1037         end_that_request_last(req);
1038 }
1039
1040 /*
1041  * Process finished error recovery ccw.
1042  */
1043 static inline void
1044 __dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
1045 {
1046         dasd_erp_fn_t erp_fn;
1047
1048         if (cqr->status == DASD_CQR_DONE)
1049                 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1050         else
1051                 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1052         erp_fn = device->discipline->erp_postaction(cqr);
1053         erp_fn(cqr);
1054 }
1055
1056 /*
1057  * Process ccw request queue.
1058  */
1059 static inline void
1060 __dasd_process_ccw_queue(struct dasd_device * device,
1061                          struct list_head *final_queue)
1062 {
1063         struct list_head *l, *n;
1064         struct dasd_ccw_req *cqr;
1065         dasd_erp_fn_t erp_fn;
1066
1067 restart:
1068         /* Process request with final status. */
1069         list_for_each_safe(l, n, &device->ccw_queue) {
1070                 cqr = list_entry(l, struct dasd_ccw_req, list);
1071                 /* Stop list processing at the first non-final request. */
1072                 if (cqr->status != DASD_CQR_DONE &&
1073                     cqr->status != DASD_CQR_FAILED &&
1074                     cqr->status != DASD_CQR_ERROR)
1075                         break;
1076                 /*  Process requests with DASD_CQR_ERROR */
1077                 if (cqr->status == DASD_CQR_ERROR) {
1078                         if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1079                                 cqr->status = DASD_CQR_FAILED;
1080                                 cqr->stopclk = get_clock();
1081                         } else {
1082                                 if (cqr->irb.esw.esw0.erw.cons) {
1083                                         erp_fn = device->discipline->erp_action(cqr);
1084                                         erp_fn(cqr);
1085                                 } else
1086                                         dasd_default_erp_action(cqr);
1087                         }
1088                         goto restart;
1089                 }
1090                 /* Process finished ERP request. */
1091                 if (cqr->refers) {
1092                         __dasd_process_erp(device, cqr);
1093                         goto restart;
1094                 }
1095
1096                 /* Rechain finished requests to final queue */
1097                 cqr->endclk = get_clock();
1098                 list_move_tail(&cqr->list, final_queue);
1099         }
1100 }
1101
1102 static void
1103 dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
1104 {
1105         struct request *req;
1106         struct dasd_device *device;
1107         int status;
1108
1109         req = (struct request *) data;
1110         device = cqr->device; 
1111         dasd_profile_end(device, cqr, req);
1112         status = cqr->device->discipline->free_cp(cqr,req);
1113         spin_lock_irq(&device->request_queue_lock);
1114         dasd_end_request(req, status);
1115         spin_unlock_irq(&device->request_queue_lock);
1116 }
1117
1118
1119 /*
1120  * Fetch requests from the block device queue.
1121  */
1122 static inline void
1123 __dasd_process_blk_queue(struct dasd_device * device)
1124 {
1125         request_queue_t *queue;
1126         struct request *req;
1127         struct dasd_ccw_req *cqr;
1128         int nr_queued;
1129
1130         queue = device->request_queue;
1131         /* No queue ? Then there is nothing to do. */
1132         if (queue == NULL)
1133                 return;
1134
1135         /*
1136          * We requeue request from the block device queue to the ccw
1137          * queue only in two states. In state DASD_STATE_READY the
1138          * partition detection is done and we need to requeue requests
1139          * for that. State DASD_STATE_ONLINE is normal block device
1140          * operation.
1141          */
1142         if (device->state != DASD_STATE_READY &&
1143             device->state != DASD_STATE_ONLINE)
1144                 return;
1145         nr_queued = 0;
1146         /* Now we try to fetch requests from the request queue */
1147         list_for_each_entry(cqr, &device->ccw_queue, list)
1148                 if (cqr->status == DASD_CQR_QUEUED)
1149                         nr_queued++;
1150         while (!blk_queue_plugged(queue) &&
1151                elv_next_request(queue) &&
1152                 nr_queued < DASD_CHANQ_MAX_SIZE) {
1153                 req = elv_next_request(queue);
1154                 if (test_bit(DASD_FLAG_RO, &device->flags) &&
1155                     rq_data_dir(req) == WRITE) {
1156                         DBF_EVENT(DBF_ERR,
1157                                   "(%s) Rejecting write request %p",
1158                                   device->cdev->dev.bus_id,
1159                                   req);
1160                         blkdev_dequeue_request(req);
1161                         dasd_end_request(req, 0);
1162                         continue;
1163                 }
1164                 if (device->stopped & DASD_STOPPED_DC_EIO) {
1165                         blkdev_dequeue_request(req);
1166                         dasd_end_request(req, 0);
1167                         continue;
1168                 }
1169                 cqr = device->discipline->build_cp(device, req);
1170                 if (IS_ERR(cqr)) {
1171                         if (PTR_ERR(cqr) == -ENOMEM)
1172                                 break;  /* terminate request queue loop */
1173                         DBF_EVENT(DBF_ERR,
1174                                   "(%s) CCW creation failed on request %p",
1175                                   device->cdev->dev.bus_id,
1176                                   req);
1177                         blkdev_dequeue_request(req);
1178                         dasd_end_request(req, 0);
1179                         continue;
1180                 }
1181                 cqr->callback = dasd_end_request_cb;
1182                 cqr->callback_data = (void *) req;
1183                 cqr->status = DASD_CQR_QUEUED;
1184                 blkdev_dequeue_request(req);
1185                 list_add_tail(&cqr->list, &device->ccw_queue);
1186                 dasd_profile_start(device, cqr, req);
1187                 nr_queued++;
1188         }
1189 }
1190
1191 /*
1192  * Take a look at the first request on the ccw queue and check
1193  * if it reached its expire time. If so, terminate the IO.
1194  */
1195 static inline void
1196 __dasd_check_expire(struct dasd_device * device)
1197 {
1198         struct dasd_ccw_req *cqr;
1199
1200         if (list_empty(&device->ccw_queue))
1201                 return;
1202         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1203         if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
1204                 if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
1205                         if (device->discipline->term_IO(cqr) != 0)
1206                                 /* Hmpf, try again in 1/10 sec */
1207                                 dasd_set_timer(device, 10);
1208                 }
1209         }
1210 }
1211
1212 /*
1213  * Take a look at the first request on the ccw queue and check
1214  * if it needs to be started.
1215  */
1216 static inline void
1217 __dasd_start_head(struct dasd_device * device)
1218 {
1219         struct dasd_ccw_req *cqr;
1220         int rc;
1221
1222         if (list_empty(&device->ccw_queue))
1223                 return;
1224         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1225         if ((cqr->status == DASD_CQR_QUEUED) &&
1226             (!device->stopped)) {
1227                 /* try to start the first I/O that can be started */
1228                 rc = device->discipline->start_IO(cqr);
1229                 if (rc == 0)
1230                         dasd_set_timer(device, cqr->expires);
1231                 else
1232                         /* Hmpf, try again in 1/2 sec */
1233                         dasd_set_timer(device, 50);
1234         }
1235 }
1236
1237 /*
1238  * Remove requests from the ccw queue. 
1239  */
1240 static void
1241 dasd_flush_ccw_queue(struct dasd_device * device, int all)
1242 {
1243         struct list_head flush_queue;
1244         struct list_head *l, *n;
1245         struct dasd_ccw_req *cqr;
1246
1247         INIT_LIST_HEAD(&flush_queue);
1248         spin_lock_irq(get_ccwdev_lock(device->cdev));
1249         list_for_each_safe(l, n, &device->ccw_queue) {
1250                 cqr = list_entry(l, struct dasd_ccw_req, list);
1251                 /* Flush all request or only block device requests? */
1252                 if (all == 0 && cqr->callback == dasd_end_request_cb)
1253                         continue;
1254                 if (cqr->status == DASD_CQR_IN_IO)
1255                         device->discipline->term_IO(cqr);
1256                 if (cqr->status != DASD_CQR_DONE ||
1257                     cqr->status != DASD_CQR_FAILED) {
1258                         cqr->status = DASD_CQR_FAILED;
1259                         cqr->stopclk = get_clock();
1260                 }
1261                 /* Process finished ERP request. */
1262                 if (cqr->refers) {
1263                         __dasd_process_erp(device, cqr);
1264                         continue;
1265                 }
1266                 /* Rechain request on device request queue */
1267                 cqr->endclk = get_clock();
1268                 list_move_tail(&cqr->list, &flush_queue);
1269         }
1270         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1271         /* Now call the callback function of flushed requests */
1272         list_for_each_safe(l, n, &flush_queue) {
1273                 cqr = list_entry(l, struct dasd_ccw_req, list);
1274                 if (cqr->callback != NULL)
1275                         (cqr->callback)(cqr, cqr->callback_data);
1276         }
1277 }
1278
1279 /*
1280  * Acquire the device lock and process queues for the device.
1281  */
1282 static void
1283 dasd_tasklet(struct dasd_device * device)
1284 {
1285         struct list_head final_queue;
1286         struct list_head *l, *n;
1287         struct dasd_ccw_req *cqr;
1288
1289         atomic_set (&device->tasklet_scheduled, 0);
1290         INIT_LIST_HEAD(&final_queue);
1291         spin_lock_irq(get_ccwdev_lock(device->cdev));
1292         /* Check expire time of first request on the ccw queue. */
1293         __dasd_check_expire(device);
1294         /* Finish off requests on ccw queue */
1295         __dasd_process_ccw_queue(device, &final_queue);
1296         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1297         /* Now call the callback function of requests with final status */
1298         list_for_each_safe(l, n, &final_queue) {
1299                 cqr = list_entry(l, struct dasd_ccw_req, list);
1300                 list_del(&cqr->list);
1301                 if (cqr->callback != NULL)
1302                         (cqr->callback)(cqr, cqr->callback_data);
1303         }
1304         spin_lock_irq(&device->request_queue_lock);
1305         spin_lock(get_ccwdev_lock(device->cdev));
1306         /* Get new request from the block device request queue */
1307         __dasd_process_blk_queue(device);
1308         /* Now check if the head of the ccw queue needs to be started. */
1309         __dasd_start_head(device);
1310         spin_unlock(get_ccwdev_lock(device->cdev));
1311         spin_unlock_irq(&device->request_queue_lock);
1312         dasd_put_device(device);
1313 }
1314
1315 /*
1316  * Schedules a call to dasd_tasklet over the device tasklet.
1317  */
1318 void
1319 dasd_schedule_bh(struct dasd_device * device)
1320 {
1321         /* Protect against rescheduling. */
1322         if (atomic_compare_and_swap (0, 1, &device->tasklet_scheduled))
1323                 return;
1324         dasd_get_device(device);
1325         tasklet_hi_schedule(&device->tasklet);
1326 }
1327
1328 /*
1329  * Queue a request to the head of the ccw_queue. Start the I/O if
1330  * possible.
1331  */
1332 void
1333 dasd_add_request_head(struct dasd_ccw_req *req)
1334 {
1335         struct dasd_device *device;
1336         unsigned long flags;
1337
1338         device = req->device;
1339         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1340         req->status = DASD_CQR_QUEUED;
1341         req->device = device;
1342         list_add(&req->list, &device->ccw_queue);
1343         /* let the bh start the request to keep them in order */
1344         dasd_schedule_bh(device);
1345         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1346 }
1347
1348 /*
1349  * Queue a request to the tail of the ccw_queue. Start the I/O if
1350  * possible.
1351  */
1352 void
1353 dasd_add_request_tail(struct dasd_ccw_req *req)
1354 {
1355         struct dasd_device *device;
1356         unsigned long flags;
1357
1358         device = req->device;
1359         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1360         req->status = DASD_CQR_QUEUED;
1361         req->device = device;
1362         list_add_tail(&req->list, &device->ccw_queue);
1363         /* let the bh start the request to keep them in order */
1364         dasd_schedule_bh(device);
1365         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1366 }
1367
1368 /*
1369  * Wakeup callback.
1370  */
1371 static void
1372 dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1373 {
1374         wake_up((wait_queue_head_t *) data);
1375 }
1376
1377 static inline int
1378 _wait_for_wakeup(struct dasd_ccw_req *cqr)
1379 {
1380         struct dasd_device *device;
1381         int rc;
1382
1383         device = cqr->device;
1384         spin_lock_irq(get_ccwdev_lock(device->cdev));
1385         rc = cqr->status == DASD_CQR_DONE || cqr->status == DASD_CQR_FAILED;
1386         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1387         return rc;
1388 }
1389
1390 /*
1391  * Attempts to start a special ccw queue and waits for its completion.
1392  */
1393 int
1394 dasd_sleep_on(struct dasd_ccw_req * cqr)
1395 {
1396         wait_queue_head_t wait_q;
1397         struct dasd_device *device;
1398         int rc;
1399         
1400         device = cqr->device;
1401         spin_lock_irq(get_ccwdev_lock(device->cdev));
1402         
1403         init_waitqueue_head (&wait_q);
1404         cqr->callback = dasd_wakeup_cb;
1405         cqr->callback_data = (void *) &wait_q;
1406         cqr->status = DASD_CQR_QUEUED;
1407         list_add_tail(&cqr->list, &device->ccw_queue);
1408         
1409         /* let the bh start the request to keep them in order */
1410         dasd_schedule_bh(device);
1411         
1412         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1413
1414         wait_event(wait_q, _wait_for_wakeup(cqr));
1415         
1416         /* Request status is either done or failed. */
1417         rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1418         return rc;
1419 }
1420
1421 /*
1422  * Attempts to start a special ccw queue and wait interruptible
1423  * for its completion.
1424  */
1425 int
1426 dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
1427 {
1428         wait_queue_head_t wait_q;
1429         struct dasd_device *device;
1430         int rc, finished;
1431
1432         device = cqr->device;
1433         spin_lock_irq(get_ccwdev_lock(device->cdev));
1434
1435         init_waitqueue_head (&wait_q);
1436         cqr->callback = dasd_wakeup_cb;
1437         cqr->callback_data = (void *) &wait_q;
1438         cqr->status = DASD_CQR_QUEUED;
1439         list_add_tail(&cqr->list, &device->ccw_queue);
1440
1441         /* let the bh start the request to keep them in order */
1442         dasd_schedule_bh(device);
1443         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1444
1445         finished = 0;
1446         while (!finished) {
1447                 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1448                 if (rc != -ERESTARTSYS) {
1449                         /* Request status is either done or failed. */
1450                         rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1451                         break;
1452                 }
1453                 spin_lock_irq(get_ccwdev_lock(device->cdev));
1454                 if (cqr->status == DASD_CQR_IN_IO &&
1455                     device->discipline->term_IO(cqr) == 0) {
1456                         list_del(&cqr->list);
1457                         finished = 1;
1458                 }
1459                 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1460         }
1461         return rc;
1462 }
1463
1464 /*
1465  * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1466  * for eckd devices) the currently running request has to be terminated
1467  * and be put back to status queued, before the special request is added
1468  * to the head of the queue. Then the special request is waited on normally.
1469  */
1470 static inline int
1471 _dasd_term_running_cqr(struct dasd_device *device)
1472 {
1473         struct dasd_ccw_req *cqr;
1474         int rc;
1475
1476         if (list_empty(&device->ccw_queue))
1477                 return 0;
1478         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1479         rc = device->discipline->term_IO(cqr);
1480         if (rc == 0) {
1481                 /* termination successful */
1482                 cqr->status = DASD_CQR_QUEUED;
1483                 cqr->startclk = cqr->stopclk = 0;
1484                 cqr->starttime = 0;
1485         }
1486         return rc;
1487 }
1488
1489 int
1490 dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
1491 {
1492         wait_queue_head_t wait_q;
1493         struct dasd_device *device;
1494         int rc;
1495         
1496         device = cqr->device;
1497         spin_lock_irq(get_ccwdev_lock(device->cdev));
1498         rc = _dasd_term_running_cqr(device);
1499         if (rc) {
1500                 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1501                 return rc;
1502         }
1503         
1504         init_waitqueue_head (&wait_q);
1505         cqr->callback = dasd_wakeup_cb;
1506         cqr->callback_data = (void *) &wait_q;
1507         cqr->status = DASD_CQR_QUEUED;
1508         list_add(&cqr->list, &device->ccw_queue);
1509         
1510         /* let the bh start the request to keep them in order */
1511         dasd_schedule_bh(device);
1512         
1513         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1514
1515         wait_event(wait_q, _wait_for_wakeup(cqr));
1516         
1517         /* Request status is either done or failed. */
1518         rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1519         return rc;
1520 }
1521
1522 /*
1523  * Cancels a request that was started with dasd_sleep_on_req.
1524  * This is useful to timeout requests. The request will be
1525  * terminated if it is currently in i/o.
1526  * Returns 1 if the request has been terminated.
1527  */
1528 int
1529 dasd_cancel_req(struct dasd_ccw_req *cqr)
1530 {
1531         struct dasd_device *device = cqr->device;
1532         unsigned long flags;
1533         int rc;
1534
1535         rc = 0;
1536         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1537         switch (cqr->status) {
1538         case DASD_CQR_QUEUED:
1539                 /* request was not started - just set to failed */
1540                 cqr->status = DASD_CQR_FAILED;
1541                 break;
1542         case DASD_CQR_IN_IO:
1543                 /* request in IO - terminate IO and release again */
1544                 if (device->discipline->term_IO(cqr) != 0)
1545                         /* what to do if unable to terminate ??????
1546                            e.g. not _IN_IO */
1547                         cqr->status = DASD_CQR_FAILED;
1548                 cqr->stopclk = get_clock();
1549                 rc = 1;
1550                 break;
1551         case DASD_CQR_DONE:
1552         case DASD_CQR_FAILED:
1553                 /* already finished - do nothing */
1554                 break;
1555         default:
1556                 DEV_MESSAGE(KERN_ALERT, device,
1557                             "invalid status %02x in request",
1558                             cqr->status);
1559                 BUG();
1560
1561         }
1562         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1563         dasd_schedule_bh(device);
1564         return rc;
1565 }
1566
1567 /*
1568  * SECTION: Block device operations (request queue, partitions, open, release).
1569  */
1570
1571 /*
1572  * Dasd request queue function. Called from ll_rw_blk.c
1573  */
1574 static void
1575 do_dasd_request(request_queue_t * queue)
1576 {
1577         struct dasd_device *device;
1578
1579         device = (struct dasd_device *) queue->queuedata;
1580         spin_lock(get_ccwdev_lock(device->cdev));
1581         /* Get new request from the block device request queue */
1582         __dasd_process_blk_queue(device);
1583         /* Now check if the head of the ccw queue needs to be started. */
1584         __dasd_start_head(device);
1585         spin_unlock(get_ccwdev_lock(device->cdev));
1586 }
1587
1588 /*
1589  * Allocate and initialize request queue.
1590  */
1591 static int
1592 dasd_alloc_queue(struct dasd_device * device)
1593 {
1594         device->request_queue = blk_init_queue(do_dasd_request,
1595                                                &device->request_queue_lock);
1596         if (device->request_queue == NULL)
1597                 return -ENOMEM;
1598
1599         device->request_queue->queuedata = device;
1600 #if 0
1601         elevator_exit(device->request_queue);
1602         rc = elevator_init(device->request_queue, &elevator_noop);
1603         if (rc) {
1604                 blk_cleanup_queue(device->request_queue);
1605                 return rc;
1606         }
1607 #endif
1608         return 0;
1609 }
1610
1611 /*
1612  * Allocate and initialize request queue.
1613  */
1614 static void
1615 dasd_setup_queue(struct dasd_device * device)
1616 {
1617         int max;
1618
1619         blk_queue_hardsect_size(device->request_queue, device->bp_block);
1620         max = device->discipline->max_blocks << device->s2b_shift;
1621         blk_queue_max_sectors(device->request_queue, max);
1622         blk_queue_max_phys_segments(device->request_queue, -1L);
1623         blk_queue_max_hw_segments(device->request_queue, -1L);
1624         blk_queue_max_segment_size(device->request_queue, -1L);
1625         blk_queue_segment_boundary(device->request_queue, -1L);
1626 }
1627
1628 /*
1629  * Deactivate and free request queue.
1630  */
1631 static void
1632 dasd_free_queue(struct dasd_device * device)
1633 {
1634         if (device->request_queue) {
1635                 blk_cleanup_queue(device->request_queue);
1636                 device->request_queue = NULL;
1637         }
1638 }
1639
1640 /*
1641  * Flush request on the request queue.
1642  */
1643 static void
1644 dasd_flush_request_queue(struct dasd_device * device)
1645 {
1646         struct request *req;
1647
1648         if (!device->request_queue)
1649                 return;
1650         
1651         spin_lock_irq(&device->request_queue_lock);
1652         while (!list_empty(&device->request_queue->queue_head)) {
1653                 req = elv_next_request(device->request_queue);
1654                 if (req == NULL)
1655                         break;
1656                 dasd_end_request(req, 0);
1657                 blkdev_dequeue_request(req);
1658         }
1659         spin_unlock_irq(&device->request_queue_lock);
1660 }
1661
1662 static int
1663 dasd_open(struct inode *inp, struct file *filp)
1664 {
1665         struct gendisk *disk = inp->i_bdev->bd_disk;
1666         struct dasd_device *device = disk->private_data;
1667         int rc;
1668
1669         atomic_inc(&device->open_count);
1670         if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1671                 rc = -ENODEV;
1672                 goto unlock;
1673         }
1674
1675         if (!try_module_get(device->discipline->owner)) {
1676                 rc = -EINVAL;
1677                 goto unlock;
1678         }
1679
1680         if (dasd_probeonly) {
1681                 MESSAGE(KERN_INFO,
1682                         "No access to device %s due to probeonly mode",
1683                         disk->disk_name);
1684                 rc = -EPERM;
1685                 goto out;
1686         }
1687
1688         if (device->state < DASD_STATE_BASIC) {
1689                 DBF_DEV_EVENT(DBF_ERR, device, " %s",
1690                               " Cannot open unrecognized device");
1691                 rc = -ENODEV;
1692                 goto out;
1693         }
1694
1695         return 0;
1696
1697 out:
1698         module_put(device->discipline->owner);
1699 unlock:
1700         atomic_dec(&device->open_count);
1701         return rc;
1702 }
1703
1704 static int
1705 dasd_release(struct inode *inp, struct file *filp)
1706 {
1707         struct gendisk *disk = inp->i_bdev->bd_disk;
1708         struct dasd_device *device = disk->private_data;
1709
1710         atomic_dec(&device->open_count);
1711         module_put(device->discipline->owner);
1712         return 0;
1713 }
1714
1715 struct block_device_operations
1716 dasd_device_operations = {
1717         .owner          = THIS_MODULE,
1718         .open           = dasd_open,
1719         .release        = dasd_release,
1720         .ioctl          = dasd_ioctl,
1721 };
1722
1723
1724 static void
1725 dasd_exit(void)
1726 {
1727 #ifdef CONFIG_PROC_FS
1728         dasd_proc_exit();
1729 #endif
1730         dasd_ioctl_exit();
1731         dasd_gendisk_exit();
1732         dasd_devmap_exit();
1733         devfs_remove("dasd");
1734         if (dasd_debug_area != NULL) {
1735                 debug_unregister(dasd_debug_area);
1736                 dasd_debug_area = NULL;
1737         }
1738 }
1739
1740 /*
1741  * SECTION: common functions for ccw_driver use
1742  */
1743
1744 /* initial attempt at a probe function. this can be simplified once
1745  * the other detection code is gone */
1746 int
1747 dasd_generic_probe (struct ccw_device *cdev,
1748                     struct dasd_discipline *discipline)
1749 {
1750         int ret;
1751
1752         ret = dasd_add_sysfs_files(cdev);
1753         if (ret) {
1754                 printk(KERN_WARNING
1755                        "dasd_generic_probe: could not add sysfs entries "
1756                        "for %s\n", cdev->dev.bus_id);
1757         }
1758
1759         cdev->handler = &dasd_int_handler;
1760
1761         return ret;
1762 }
1763
1764 /* this will one day be called from a global not_oper handler.
1765  * It is also used by driver_unregister during module unload */
1766 void
1767 dasd_generic_remove (struct ccw_device *cdev)
1768 {
1769         struct dasd_device *device;
1770
1771         dasd_remove_sysfs_files(cdev);
1772         device = dasd_device_from_cdev(cdev);
1773         if (IS_ERR(device))
1774                 return;
1775         if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1776                 /* Already doing offline processing */
1777                 dasd_put_device(device);
1778                 return;
1779         }
1780         /*
1781          * This device is removed unconditionally. Set offline
1782          * flag to prevent dasd_open from opening it while it is
1783          * no quite down yet.
1784          */
1785         dasd_set_target_state(device, DASD_STATE_NEW);
1786         /* dasd_delete_device destroys the device reference. */
1787         dasd_delete_device(device);
1788 }
1789
1790 /* activate a device. This is called from dasd_{eckd,fba}_probe() when either
1791  * the device is detected for the first time and is supposed to be used
1792  * or the user has started activation through sysfs */
1793 int
1794 dasd_generic_set_online (struct ccw_device *cdev,
1795                          struct dasd_discipline *discipline)
1796
1797 {
1798         struct dasd_device *device;
1799         int rc;
1800
1801         device = dasd_create_device(cdev);
1802         if (IS_ERR(device))
1803                 return PTR_ERR(device);
1804
1805         if (test_bit(DASD_FLAG_USE_DIAG, &device->flags)) {
1806                 if (!dasd_diag_discipline_pointer) {
1807                         printk (KERN_WARNING
1808                                 "dasd_generic couldn't online device %s "
1809                                 "- discipline DIAG not available\n",
1810                                 cdev->dev.bus_id);
1811                         dasd_delete_device(device);
1812                         return -ENODEV;
1813                 }
1814                 discipline = dasd_diag_discipline_pointer;
1815         }
1816         device->discipline = discipline;
1817
1818         rc = discipline->check_device(device);
1819         if (rc) {
1820                 printk (KERN_WARNING
1821                         "dasd_generic couldn't online device %s "
1822                         "with discipline %s\n", 
1823                         cdev->dev.bus_id, discipline->name);
1824                 dasd_delete_device(device);
1825                 return rc;
1826         }
1827
1828         dasd_set_target_state(device, DASD_STATE_ONLINE);
1829         if (device->state <= DASD_STATE_KNOWN) {
1830                 printk (KERN_WARNING
1831                         "dasd_generic discipline not found for %s\n",
1832                         cdev->dev.bus_id);
1833                 rc = -ENODEV;
1834                 dasd_set_target_state(device, DASD_STATE_NEW);
1835                 dasd_delete_device(device);
1836         } else
1837                 pr_debug("dasd_generic device %s found\n",
1838                                 cdev->dev.bus_id);
1839
1840         /* FIXME: we have to wait for the root device but we don't want
1841          * to wait for each single device but for all at once. */
1842         wait_event(dasd_init_waitq, _wait_for_device(device));
1843
1844         dasd_put_device(device);
1845
1846         return rc;
1847 }
1848
1849 int
1850 dasd_generic_set_offline (struct ccw_device *cdev)
1851 {
1852         struct dasd_device *device;
1853         int max_count;
1854
1855         device = dasd_device_from_cdev(cdev);
1856         if (IS_ERR(device))
1857                 return PTR_ERR(device);
1858         if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1859                 /* Already doing offline processing */
1860                 dasd_put_device(device);
1861                 return 0;
1862         }
1863         /*
1864          * We must make sure that this device is currently not in use.
1865          * The open_count is increased for every opener, that includes
1866          * the blkdev_get in dasd_scan_partitions. We are only interested
1867          * in the other openers.
1868          */
1869         max_count = device->bdev ? 0 : -1;
1870         if (atomic_read(&device->open_count) > max_count) {
1871                 printk (KERN_WARNING "Can't offline dasd device with open"
1872                         " count = %i.\n",
1873                         atomic_read(&device->open_count));
1874                 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
1875                 dasd_put_device(device);
1876                 return -EBUSY;
1877         }
1878         dasd_set_target_state(device, DASD_STATE_NEW);
1879         /* dasd_delete_device destroys the device reference. */
1880         dasd_delete_device(device);
1881
1882         return 0;
1883 }
1884
1885 int
1886 dasd_generic_notify(struct ccw_device *cdev, int event)
1887 {
1888         struct dasd_device *device;
1889         struct dasd_ccw_req *cqr;
1890         unsigned long flags;
1891         int ret;
1892
1893         device = dasd_device_from_cdev(cdev);
1894         if (IS_ERR(device))
1895                 return 0;
1896         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1897         ret = 0;
1898         switch (event) {
1899         case CIO_GONE:
1900         case CIO_NO_PATH:
1901                 if (device->state < DASD_STATE_BASIC)
1902                         break;
1903                 /* Device is active. We want to keep it. */
1904                 if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
1905                         list_for_each_entry(cqr, &device->ccw_queue, list)
1906                                 if (cqr->status == DASD_CQR_IN_IO)
1907                                         cqr->status = DASD_CQR_FAILED;
1908                         device->stopped |= DASD_STOPPED_DC_EIO;
1909                         dasd_schedule_bh(device);
1910                 } else {
1911                         list_for_each_entry(cqr, &device->ccw_queue, list)
1912                                 if (cqr->status == DASD_CQR_IN_IO)
1913                                         cqr->status = DASD_CQR_QUEUED;
1914                         device->stopped |= DASD_STOPPED_DC_WAIT;
1915                         dasd_set_timer(device, 0);
1916                 }
1917                 ret = 1;
1918                 break;
1919         case CIO_OPER:
1920                 /* FIXME: add a sanity check. */
1921                 device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
1922                 dasd_schedule_bh(device);
1923                 ret = 1;
1924                 break;
1925         }
1926         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1927         dasd_put_device(device);
1928         return ret;
1929 }
1930
1931 /*
1932  * Automatically online either all dasd devices (dasd_autodetect) or
1933  * all devices specified with dasd= parameters.
1934  */
1935 void
1936 dasd_generic_auto_online (struct ccw_driver *dasd_discipline_driver)
1937 {
1938         struct device_driver *drv;
1939         struct device *d, *dev;
1940         struct ccw_device *cdev;
1941
1942         drv = get_driver(&dasd_discipline_driver->driver);
1943         down_read(&drv->bus->subsys.rwsem);
1944         dev = NULL;
1945         list_for_each_entry(d, &drv->devices, driver_list) {
1946                 dev = get_device(d);
1947                 if (!dev)
1948                         continue;
1949                 cdev = to_ccwdev(dev);
1950                 if (dasd_autodetect || dasd_busid_known(cdev->dev.bus_id) == 0)
1951                         ccw_device_set_online(cdev);
1952                 put_device(dev);
1953         }
1954         up_read(&drv->bus->subsys.rwsem);
1955         put_driver(drv);
1956 }
1957
1958 static int __init
1959 dasd_init(void)
1960 {
1961         int rc;
1962
1963         init_waitqueue_head(&dasd_init_waitq);
1964
1965         /* register 'common' DASD debug area, used for all DBF_XXX calls */
1966         dasd_debug_area = debug_register("dasd", 0, 2, 8 * sizeof (long));
1967         if (dasd_debug_area == NULL) {
1968                 rc = -ENOMEM;
1969                 goto failed;
1970         }
1971         debug_register_view(dasd_debug_area, &debug_hex_ascii_view);
1972         debug_set_level(dasd_debug_area, DBF_ERR);
1973
1974         DBF_EVENT(DBF_EMERG, "%s", "debug area created");
1975
1976         dasd_diag_discipline_pointer = NULL;
1977
1978         rc = devfs_mk_dir("dasd");
1979         if (rc)
1980                 goto failed;
1981         rc = dasd_devmap_init();
1982         if (rc)
1983                 goto failed;
1984         rc = dasd_gendisk_init();
1985         if (rc)
1986                 goto failed;
1987         rc = dasd_parse();
1988         if (rc)
1989                 goto failed;
1990         rc = dasd_ioctl_init();
1991         if (rc)
1992                 goto failed;
1993 #ifdef CONFIG_PROC_FS
1994         rc = dasd_proc_init();
1995         if (rc)
1996                 goto failed;
1997 #endif
1998
1999         return 0;
2000 failed:
2001         MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2002         dasd_exit();
2003         return rc;
2004 }
2005
2006 module_init(dasd_init);
2007 module_exit(dasd_exit);
2008
2009 EXPORT_SYMBOL(dasd_debug_area);
2010 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2011
2012 EXPORT_SYMBOL(dasd_add_request_head);
2013 EXPORT_SYMBOL(dasd_add_request_tail);
2014 EXPORT_SYMBOL(dasd_cancel_req);
2015 EXPORT_SYMBOL(dasd_clear_timer);
2016 EXPORT_SYMBOL(dasd_enable_device);
2017 EXPORT_SYMBOL(dasd_int_handler);
2018 EXPORT_SYMBOL(dasd_kfree_request);
2019 EXPORT_SYMBOL(dasd_kick_device);
2020 EXPORT_SYMBOL(dasd_kmalloc_request);
2021 EXPORT_SYMBOL(dasd_schedule_bh);
2022 EXPORT_SYMBOL(dasd_set_target_state);
2023 EXPORT_SYMBOL(dasd_set_timer);
2024 EXPORT_SYMBOL(dasd_sfree_request);
2025 EXPORT_SYMBOL(dasd_sleep_on);
2026 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2027 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2028 EXPORT_SYMBOL(dasd_smalloc_request);
2029 EXPORT_SYMBOL(dasd_start_IO);
2030 EXPORT_SYMBOL(dasd_term_IO);
2031
2032 EXPORT_SYMBOL_GPL(dasd_generic_probe);
2033 EXPORT_SYMBOL_GPL(dasd_generic_remove);
2034 EXPORT_SYMBOL_GPL(dasd_generic_notify);
2035 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2036 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
2037 EXPORT_SYMBOL_GPL(dasd_generic_auto_online);
2038
2039 /*
2040  * Overrides for Emacs so that we follow Linus's tabbing style.
2041  * Emacs will notice this stuff at the end of the file and automatically
2042  * adjust the settings for this buffer only.  This must remain at the end
2043  * of the file.
2044  * ---------------------------------------------------------------------------
2045  * Local variables:
2046  * c-indent-level: 4
2047  * c-brace-imaginary-offset: 0
2048  * c-brace-offset: -4
2049  * c-argdecl-indent: 4
2050  * c-label-offset: -4
2051  * c-continued-statement-offset: 4
2052  * c-continued-brace-offset: 0
2053  * indent-tabs-mode: 1
2054  * tab-width: 8
2055  * End:
2056  */