]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/s390/block/dasd.c
[S390] dasd: Add support for raw ECKD access.
[mv-sheeva.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  * Copyright IBM Corp. 1999, 2009
9  */
10
11 #define KMSG_COMPONENT "dasd"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
14 #include <linux/kernel_stat.h>
15 #include <linux/kmod.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/ctype.h>
19 #include <linux/major.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/hdreg.h>
23 #include <linux/async.h>
24 #include <linux/mutex.h>
25
26 #include <asm/ccwdev.h>
27 #include <asm/ebcdic.h>
28 #include <asm/idals.h>
29 #include <asm/itcw.h>
30 #include <asm/diag.h>
31
32 /* This is ugly... */
33 #define PRINTK_HEADER "dasd:"
34
35 #include "dasd_int.h"
36 /*
37  * SECTION: Constant definitions to be used within this file
38  */
39 #define DASD_CHANQ_MAX_SIZE 4
40
41 #define DASD_SLEEPON_START_TAG  (void *) 1
42 #define DASD_SLEEPON_END_TAG    (void *) 2
43
44 /*
45  * SECTION: exported variables of dasd.c
46  */
47 debug_info_t *dasd_debug_area;
48 struct dasd_discipline *dasd_diag_discipline_pointer;
49 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
50
51 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
52 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
53                    " Copyright 2000 IBM Corporation");
54 MODULE_SUPPORTED_DEVICE("dasd");
55 MODULE_LICENSE("GPL");
56
57 /*
58  * SECTION: prototypes for static functions of dasd.c
59  */
60 static int  dasd_alloc_queue(struct dasd_block *);
61 static void dasd_setup_queue(struct dasd_block *);
62 static void dasd_free_queue(struct dasd_block *);
63 static void dasd_flush_request_queue(struct dasd_block *);
64 static int dasd_flush_block_queue(struct dasd_block *);
65 static void dasd_device_tasklet(struct dasd_device *);
66 static void dasd_block_tasklet(struct dasd_block *);
67 static void do_kick_device(struct work_struct *);
68 static void do_restore_device(struct work_struct *);
69 static void do_reload_device(struct work_struct *);
70 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
71 static void dasd_device_timeout(unsigned long);
72 static void dasd_block_timeout(unsigned long);
73 static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
74
75 /*
76  * SECTION: Operations on the device structure.
77  */
78 static wait_queue_head_t dasd_init_waitq;
79 static wait_queue_head_t dasd_flush_wq;
80 static wait_queue_head_t generic_waitq;
81
82 /*
83  * Allocate memory for a new device structure.
84  */
85 struct dasd_device *dasd_alloc_device(void)
86 {
87         struct dasd_device *device;
88
89         device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
90         if (!device)
91                 return ERR_PTR(-ENOMEM);
92
93         /* Get two pages for normal block device operations. */
94         device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
95         if (!device->ccw_mem) {
96                 kfree(device);
97                 return ERR_PTR(-ENOMEM);
98         }
99         /* Get one page for error recovery. */
100         device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
101         if (!device->erp_mem) {
102                 free_pages((unsigned long) device->ccw_mem, 1);
103                 kfree(device);
104                 return ERR_PTR(-ENOMEM);
105         }
106
107         dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
108         dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
109         spin_lock_init(&device->mem_lock);
110         atomic_set(&device->tasklet_scheduled, 0);
111         tasklet_init(&device->tasklet,
112                      (void (*)(unsigned long)) dasd_device_tasklet,
113                      (unsigned long) device);
114         INIT_LIST_HEAD(&device->ccw_queue);
115         init_timer(&device->timer);
116         device->timer.function = dasd_device_timeout;
117         device->timer.data = (unsigned long) device;
118         INIT_WORK(&device->kick_work, do_kick_device);
119         INIT_WORK(&device->restore_device, do_restore_device);
120         INIT_WORK(&device->reload_device, do_reload_device);
121         device->state = DASD_STATE_NEW;
122         device->target = DASD_STATE_NEW;
123         mutex_init(&device->state_mutex);
124
125         return device;
126 }
127
128 /*
129  * Free memory of a device structure.
130  */
131 void dasd_free_device(struct dasd_device *device)
132 {
133         kfree(device->private);
134         free_page((unsigned long) device->erp_mem);
135         free_pages((unsigned long) device->ccw_mem, 1);
136         kfree(device);
137 }
138
139 /*
140  * Allocate memory for a new device structure.
141  */
142 struct dasd_block *dasd_alloc_block(void)
143 {
144         struct dasd_block *block;
145
146         block = kzalloc(sizeof(*block), GFP_ATOMIC);
147         if (!block)
148                 return ERR_PTR(-ENOMEM);
149         /* open_count = 0 means device online but not in use */
150         atomic_set(&block->open_count, -1);
151
152         spin_lock_init(&block->request_queue_lock);
153         atomic_set(&block->tasklet_scheduled, 0);
154         tasklet_init(&block->tasklet,
155                      (void (*)(unsigned long)) dasd_block_tasklet,
156                      (unsigned long) block);
157         INIT_LIST_HEAD(&block->ccw_queue);
158         spin_lock_init(&block->queue_lock);
159         init_timer(&block->timer);
160         block->timer.function = dasd_block_timeout;
161         block->timer.data = (unsigned long) block;
162
163         return block;
164 }
165
166 /*
167  * Free memory of a device structure.
168  */
169 void dasd_free_block(struct dasd_block *block)
170 {
171         kfree(block);
172 }
173
174 /*
175  * Make a new device known to the system.
176  */
177 static int dasd_state_new_to_known(struct dasd_device *device)
178 {
179         int rc;
180
181         /*
182          * As long as the device is not in state DASD_STATE_NEW we want to
183          * keep the reference count > 0.
184          */
185         dasd_get_device(device);
186
187         if (device->block) {
188                 rc = dasd_alloc_queue(device->block);
189                 if (rc) {
190                         dasd_put_device(device);
191                         return rc;
192                 }
193         }
194         device->state = DASD_STATE_KNOWN;
195         return 0;
196 }
197
198 /*
199  * Let the system forget about a device.
200  */
201 static int dasd_state_known_to_new(struct dasd_device *device)
202 {
203         /* Disable extended error reporting for this device. */
204         dasd_eer_disable(device);
205         /* Forget the discipline information. */
206         if (device->discipline) {
207                 if (device->discipline->uncheck_device)
208                         device->discipline->uncheck_device(device);
209                 module_put(device->discipline->owner);
210         }
211         device->discipline = NULL;
212         if (device->base_discipline)
213                 module_put(device->base_discipline->owner);
214         device->base_discipline = NULL;
215         device->state = DASD_STATE_NEW;
216
217         if (device->block)
218                 dasd_free_queue(device->block);
219
220         /* Give up reference we took in dasd_state_new_to_known. */
221         dasd_put_device(device);
222         return 0;
223 }
224
225 /*
226  * Request the irq line for the device.
227  */
228 static int dasd_state_known_to_basic(struct dasd_device *device)
229 {
230         int rc;
231
232         /* Allocate and register gendisk structure. */
233         if (device->block) {
234                 rc = dasd_gendisk_alloc(device->block);
235                 if (rc)
236                         return rc;
237         }
238         /* register 'device' debug area, used for all DBF_DEV_XXX calls */
239         device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
240                                             8 * sizeof(long));
241         debug_register_view(device->debug_area, &debug_sprintf_view);
242         debug_set_level(device->debug_area, DBF_WARNING);
243         DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
244
245         device->state = DASD_STATE_BASIC;
246         return 0;
247 }
248
249 /*
250  * Release the irq line for the device. Terminate any running i/o.
251  */
252 static int dasd_state_basic_to_known(struct dasd_device *device)
253 {
254         int rc;
255         if (device->block) {
256                 dasd_gendisk_free(device->block);
257                 dasd_block_clear_timer(device->block);
258         }
259         rc = dasd_flush_device_queue(device);
260         if (rc)
261                 return rc;
262         dasd_device_clear_timer(device);
263
264         DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
265         if (device->debug_area != NULL) {
266                 debug_unregister(device->debug_area);
267                 device->debug_area = NULL;
268         }
269         device->state = DASD_STATE_KNOWN;
270         return 0;
271 }
272
273 /*
274  * Do the initial analysis. The do_analysis function may return
275  * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
276  * until the discipline decides to continue the startup sequence
277  * by calling the function dasd_change_state. The eckd disciplines
278  * uses this to start a ccw that detects the format. The completion
279  * interrupt for this detection ccw uses the kernel event daemon to
280  * trigger the call to dasd_change_state. All this is done in the
281  * discipline code, see dasd_eckd.c.
282  * After the analysis ccw is done (do_analysis returned 0) the block
283  * device is setup.
284  * In case the analysis returns an error, the device setup is stopped
285  * (a fake disk was already added to allow formatting).
286  */
287 static int dasd_state_basic_to_ready(struct dasd_device *device)
288 {
289         int rc;
290         struct dasd_block *block;
291
292         rc = 0;
293         block = device->block;
294         /* make disk known with correct capacity */
295         if (block) {
296                 if (block->base->discipline->do_analysis != NULL)
297                         rc = block->base->discipline->do_analysis(block);
298                 if (rc) {
299                         if (rc != -EAGAIN)
300                                 device->state = DASD_STATE_UNFMT;
301                         return rc;
302                 }
303                 dasd_setup_queue(block);
304                 set_capacity(block->gdp,
305                              block->blocks << block->s2b_shift);
306                 device->state = DASD_STATE_READY;
307                 rc = dasd_scan_partitions(block);
308                 if (rc)
309                         device->state = DASD_STATE_BASIC;
310         } else {
311                 device->state = DASD_STATE_READY;
312         }
313         return rc;
314 }
315
316 /*
317  * Remove device from block device layer. Destroy dirty buffers.
318  * Forget format information. Check if the target level is basic
319  * and if it is create fake disk for formatting.
320  */
321 static int dasd_state_ready_to_basic(struct dasd_device *device)
322 {
323         int rc;
324
325         device->state = DASD_STATE_BASIC;
326         if (device->block) {
327                 struct dasd_block *block = device->block;
328                 rc = dasd_flush_block_queue(block);
329                 if (rc) {
330                         device->state = DASD_STATE_READY;
331                         return rc;
332                 }
333                 dasd_flush_request_queue(block);
334                 dasd_destroy_partitions(block);
335                 block->blocks = 0;
336                 block->bp_block = 0;
337                 block->s2b_shift = 0;
338         }
339         return 0;
340 }
341
342 /*
343  * Back to basic.
344  */
345 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
346 {
347         device->state = DASD_STATE_BASIC;
348         return 0;
349 }
350
351 /*
352  * Make the device online and schedule the bottom half to start
353  * the requeueing of requests from the linux request queue to the
354  * ccw queue.
355  */
356 static int
357 dasd_state_ready_to_online(struct dasd_device * device)
358 {
359         int rc;
360         struct gendisk *disk;
361         struct disk_part_iter piter;
362         struct hd_struct *part;
363
364         if (device->discipline->ready_to_online) {
365                 rc = device->discipline->ready_to_online(device);
366                 if (rc)
367                         return rc;
368         }
369         device->state = DASD_STATE_ONLINE;
370         if (device->block) {
371                 dasd_schedule_block_bh(device->block);
372                 if ((device->features & DASD_FEATURE_USERAW)) {
373                         disk = device->block->gdp;
374                         kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
375                         return 0;
376                 }
377                 disk = device->block->bdev->bd_disk;
378                 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
379                 while ((part = disk_part_iter_next(&piter)))
380                         kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
381                 disk_part_iter_exit(&piter);
382         }
383         return 0;
384 }
385
386 /*
387  * Stop the requeueing of requests again.
388  */
389 static int dasd_state_online_to_ready(struct dasd_device *device)
390 {
391         int rc;
392         struct gendisk *disk;
393         struct disk_part_iter piter;
394         struct hd_struct *part;
395
396         if (device->discipline->online_to_ready) {
397                 rc = device->discipline->online_to_ready(device);
398                 if (rc)
399                         return rc;
400         }
401         device->state = DASD_STATE_READY;
402         if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
403                 disk = device->block->bdev->bd_disk;
404                 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
405                 while ((part = disk_part_iter_next(&piter)))
406                         kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
407                 disk_part_iter_exit(&piter);
408         }
409         return 0;
410 }
411
412 /*
413  * Device startup state changes.
414  */
415 static int dasd_increase_state(struct dasd_device *device)
416 {
417         int rc;
418
419         rc = 0;
420         if (device->state == DASD_STATE_NEW &&
421             device->target >= DASD_STATE_KNOWN)
422                 rc = dasd_state_new_to_known(device);
423
424         if (!rc &&
425             device->state == DASD_STATE_KNOWN &&
426             device->target >= DASD_STATE_BASIC)
427                 rc = dasd_state_known_to_basic(device);
428
429         if (!rc &&
430             device->state == DASD_STATE_BASIC &&
431             device->target >= DASD_STATE_READY)
432                 rc = dasd_state_basic_to_ready(device);
433
434         if (!rc &&
435             device->state == DASD_STATE_UNFMT &&
436             device->target > DASD_STATE_UNFMT)
437                 rc = -EPERM;
438
439         if (!rc &&
440             device->state == DASD_STATE_READY &&
441             device->target >= DASD_STATE_ONLINE)
442                 rc = dasd_state_ready_to_online(device);
443
444         return rc;
445 }
446
447 /*
448  * Device shutdown state changes.
449  */
450 static int dasd_decrease_state(struct dasd_device *device)
451 {
452         int rc;
453
454         rc = 0;
455         if (device->state == DASD_STATE_ONLINE &&
456             device->target <= DASD_STATE_READY)
457                 rc = dasd_state_online_to_ready(device);
458
459         if (!rc &&
460             device->state == DASD_STATE_READY &&
461             device->target <= DASD_STATE_BASIC)
462                 rc = dasd_state_ready_to_basic(device);
463
464         if (!rc &&
465             device->state == DASD_STATE_UNFMT &&
466             device->target <= DASD_STATE_BASIC)
467                 rc = dasd_state_unfmt_to_basic(device);
468
469         if (!rc &&
470             device->state == DASD_STATE_BASIC &&
471             device->target <= DASD_STATE_KNOWN)
472                 rc = dasd_state_basic_to_known(device);
473
474         if (!rc &&
475             device->state == DASD_STATE_KNOWN &&
476             device->target <= DASD_STATE_NEW)
477                 rc = dasd_state_known_to_new(device);
478
479         return rc;
480 }
481
482 /*
483  * This is the main startup/shutdown routine.
484  */
485 static void dasd_change_state(struct dasd_device *device)
486 {
487         int rc;
488
489         if (device->state == device->target)
490                 /* Already where we want to go today... */
491                 return;
492         if (device->state < device->target)
493                 rc = dasd_increase_state(device);
494         else
495                 rc = dasd_decrease_state(device);
496         if (rc == -EAGAIN)
497                 return;
498         if (rc)
499                 device->target = device->state;
500
501         if (device->state == device->target)
502                 wake_up(&dasd_init_waitq);
503
504         /* let user-space know that the device status changed */
505         kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
506 }
507
508 /*
509  * Kick starter for devices that did not complete the startup/shutdown
510  * procedure or were sleeping because of a pending state.
511  * dasd_kick_device will schedule a call do do_kick_device to the kernel
512  * event daemon.
513  */
514 static void do_kick_device(struct work_struct *work)
515 {
516         struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
517         mutex_lock(&device->state_mutex);
518         dasd_change_state(device);
519         mutex_unlock(&device->state_mutex);
520         dasd_schedule_device_bh(device);
521         dasd_put_device(device);
522 }
523
524 void dasd_kick_device(struct dasd_device *device)
525 {
526         dasd_get_device(device);
527         /* queue call to dasd_kick_device to the kernel event daemon. */
528         schedule_work(&device->kick_work);
529 }
530
531 /*
532  * dasd_reload_device will schedule a call do do_reload_device to the kernel
533  * event daemon.
534  */
535 static void do_reload_device(struct work_struct *work)
536 {
537         struct dasd_device *device = container_of(work, struct dasd_device,
538                                                   reload_device);
539         device->discipline->reload(device);
540         dasd_put_device(device);
541 }
542
543 void dasd_reload_device(struct dasd_device *device)
544 {
545         dasd_get_device(device);
546         /* queue call to dasd_reload_device to the kernel event daemon. */
547         schedule_work(&device->reload_device);
548 }
549 EXPORT_SYMBOL(dasd_reload_device);
550
551 /*
552  * dasd_restore_device will schedule a call do do_restore_device to the kernel
553  * event daemon.
554  */
555 static void do_restore_device(struct work_struct *work)
556 {
557         struct dasd_device *device = container_of(work, struct dasd_device,
558                                                   restore_device);
559         device->cdev->drv->restore(device->cdev);
560         dasd_put_device(device);
561 }
562
563 void dasd_restore_device(struct dasd_device *device)
564 {
565         dasd_get_device(device);
566         /* queue call to dasd_restore_device to the kernel event daemon. */
567         schedule_work(&device->restore_device);
568 }
569
570 /*
571  * Set the target state for a device and starts the state change.
572  */
573 void dasd_set_target_state(struct dasd_device *device, int target)
574 {
575         dasd_get_device(device);
576         mutex_lock(&device->state_mutex);
577         /* If we are in probeonly mode stop at DASD_STATE_READY. */
578         if (dasd_probeonly && target > DASD_STATE_READY)
579                 target = DASD_STATE_READY;
580         if (device->target != target) {
581                 if (device->state == target)
582                         wake_up(&dasd_init_waitq);
583                 device->target = target;
584         }
585         if (device->state != device->target)
586                 dasd_change_state(device);
587         mutex_unlock(&device->state_mutex);
588         dasd_put_device(device);
589 }
590
591 /*
592  * Enable devices with device numbers in [from..to].
593  */
594 static inline int _wait_for_device(struct dasd_device *device)
595 {
596         return (device->state == device->target);
597 }
598
599 void dasd_enable_device(struct dasd_device *device)
600 {
601         dasd_set_target_state(device, DASD_STATE_ONLINE);
602         if (device->state <= DASD_STATE_KNOWN)
603                 /* No discipline for device found. */
604                 dasd_set_target_state(device, DASD_STATE_NEW);
605         /* Now wait for the devices to come up. */
606         wait_event(dasd_init_waitq, _wait_for_device(device));
607 }
608
609 /*
610  * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
611  */
612 #ifdef CONFIG_DASD_PROFILE
613
614 struct dasd_profile_info_t dasd_global_profile;
615 unsigned int dasd_profile_level = DASD_PROFILE_OFF;
616
617 /*
618  * Increments counter in global and local profiling structures.
619  */
620 #define dasd_profile_counter(value, counter, block) \
621 { \
622         int index; \
623         for (index = 0; index < 31 && value >> (2+index); index++); \
624         dasd_global_profile.counter[index]++; \
625         block->profile.counter[index]++; \
626 }
627
628 /*
629  * Add profiling information for cqr before execution.
630  */
631 static void dasd_profile_start(struct dasd_block *block,
632                                struct dasd_ccw_req *cqr,
633                                struct request *req)
634 {
635         struct list_head *l;
636         unsigned int counter;
637
638         if (dasd_profile_level != DASD_PROFILE_ON)
639                 return;
640
641         /* count the length of the chanq for statistics */
642         counter = 0;
643         list_for_each(l, &block->ccw_queue)
644                 if (++counter >= 31)
645                         break;
646         dasd_global_profile.dasd_io_nr_req[counter]++;
647         block->profile.dasd_io_nr_req[counter]++;
648 }
649
650 /*
651  * Add profiling information for cqr after execution.
652  */
653 static void dasd_profile_end(struct dasd_block *block,
654                              struct dasd_ccw_req *cqr,
655                              struct request *req)
656 {
657         long strtime, irqtime, endtime, tottime;        /* in microseconds */
658         long tottimeps, sectors;
659
660         if (dasd_profile_level != DASD_PROFILE_ON)
661                 return;
662
663         sectors = blk_rq_sectors(req);
664         if (!cqr->buildclk || !cqr->startclk ||
665             !cqr->stopclk || !cqr->endclk ||
666             !sectors)
667                 return;
668
669         strtime = ((cqr->startclk - cqr->buildclk) >> 12);
670         irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
671         endtime = ((cqr->endclk - cqr->stopclk) >> 12);
672         tottime = ((cqr->endclk - cqr->buildclk) >> 12);
673         tottimeps = tottime / sectors;
674
675         if (!dasd_global_profile.dasd_io_reqs)
676                 memset(&dasd_global_profile, 0,
677                        sizeof(struct dasd_profile_info_t));
678         dasd_global_profile.dasd_io_reqs++;
679         dasd_global_profile.dasd_io_sects += sectors;
680
681         if (!block->profile.dasd_io_reqs)
682                 memset(&block->profile, 0,
683                        sizeof(struct dasd_profile_info_t));
684         block->profile.dasd_io_reqs++;
685         block->profile.dasd_io_sects += sectors;
686
687         dasd_profile_counter(sectors, dasd_io_secs, block);
688         dasd_profile_counter(tottime, dasd_io_times, block);
689         dasd_profile_counter(tottimeps, dasd_io_timps, block);
690         dasd_profile_counter(strtime, dasd_io_time1, block);
691         dasd_profile_counter(irqtime, dasd_io_time2, block);
692         dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
693         dasd_profile_counter(endtime, dasd_io_time3, block);
694 }
695 #else
696 #define dasd_profile_start(block, cqr, req) do {} while (0)
697 #define dasd_profile_end(block, cqr, req) do {} while (0)
698 #endif                          /* CONFIG_DASD_PROFILE */
699
700 /*
701  * Allocate memory for a channel program with 'cplength' channel
702  * command words and 'datasize' additional space. There are two
703  * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
704  * memory and 2) dasd_smalloc_request uses the static ccw memory
705  * that gets allocated for each device.
706  */
707 struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
708                                           int datasize,
709                                           struct dasd_device *device)
710 {
711         struct dasd_ccw_req *cqr;
712
713         /* Sanity checks */
714         BUG_ON(datasize > PAGE_SIZE ||
715              (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
716
717         cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
718         if (cqr == NULL)
719                 return ERR_PTR(-ENOMEM);
720         cqr->cpaddr = NULL;
721         if (cplength > 0) {
722                 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
723                                       GFP_ATOMIC | GFP_DMA);
724                 if (cqr->cpaddr == NULL) {
725                         kfree(cqr);
726                         return ERR_PTR(-ENOMEM);
727                 }
728         }
729         cqr->data = NULL;
730         if (datasize > 0) {
731                 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
732                 if (cqr->data == NULL) {
733                         kfree(cqr->cpaddr);
734                         kfree(cqr);
735                         return ERR_PTR(-ENOMEM);
736                 }
737         }
738         cqr->magic =  magic;
739         set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
740         dasd_get_device(device);
741         return cqr;
742 }
743
744 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
745                                           int datasize,
746                                           struct dasd_device *device)
747 {
748         unsigned long flags;
749         struct dasd_ccw_req *cqr;
750         char *data;
751         int size;
752
753         size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
754         if (cplength > 0)
755                 size += cplength * sizeof(struct ccw1);
756         if (datasize > 0)
757                 size += datasize;
758         spin_lock_irqsave(&device->mem_lock, flags);
759         cqr = (struct dasd_ccw_req *)
760                 dasd_alloc_chunk(&device->ccw_chunks, size);
761         spin_unlock_irqrestore(&device->mem_lock, flags);
762         if (cqr == NULL)
763                 return ERR_PTR(-ENOMEM);
764         memset(cqr, 0, sizeof(struct dasd_ccw_req));
765         data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
766         cqr->cpaddr = NULL;
767         if (cplength > 0) {
768                 cqr->cpaddr = (struct ccw1 *) data;
769                 data += cplength*sizeof(struct ccw1);
770                 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
771         }
772         cqr->data = NULL;
773         if (datasize > 0) {
774                 cqr->data = data;
775                 memset(cqr->data, 0, datasize);
776         }
777         cqr->magic = magic;
778         set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
779         dasd_get_device(device);
780         return cqr;
781 }
782
783 /*
784  * Free memory of a channel program. This function needs to free all the
785  * idal lists that might have been created by dasd_set_cda and the
786  * struct dasd_ccw_req itself.
787  */
788 void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
789 {
790 #ifdef CONFIG_64BIT
791         struct ccw1 *ccw;
792
793         /* Clear any idals used for the request. */
794         ccw = cqr->cpaddr;
795         do {
796                 clear_normalized_cda(ccw);
797         } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
798 #endif
799         kfree(cqr->cpaddr);
800         kfree(cqr->data);
801         kfree(cqr);
802         dasd_put_device(device);
803 }
804
805 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
806 {
807         unsigned long flags;
808
809         spin_lock_irqsave(&device->mem_lock, flags);
810         dasd_free_chunk(&device->ccw_chunks, cqr);
811         spin_unlock_irqrestore(&device->mem_lock, flags);
812         dasd_put_device(device);
813 }
814
815 /*
816  * Check discipline magic in cqr.
817  */
818 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
819 {
820         struct dasd_device *device;
821
822         if (cqr == NULL)
823                 return -EINVAL;
824         device = cqr->startdev;
825         if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
826                 DBF_DEV_EVENT(DBF_WARNING, device,
827                             " dasd_ccw_req 0x%08x magic doesn't match"
828                             " discipline 0x%08x",
829                             cqr->magic,
830                             *(unsigned int *) device->discipline->name);
831                 return -EINVAL;
832         }
833         return 0;
834 }
835
836 /*
837  * Terminate the current i/o and set the request to clear_pending.
838  * Timer keeps device runnig.
839  * ccw_device_clear can fail if the i/o subsystem
840  * is in a bad mood.
841  */
842 int dasd_term_IO(struct dasd_ccw_req *cqr)
843 {
844         struct dasd_device *device;
845         int retries, rc;
846         char errorstring[ERRORLENGTH];
847
848         /* Check the cqr */
849         rc = dasd_check_cqr(cqr);
850         if (rc)
851                 return rc;
852         retries = 0;
853         device = (struct dasd_device *) cqr->startdev;
854         while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
855                 rc = ccw_device_clear(device->cdev, (long) cqr);
856                 switch (rc) {
857                 case 0: /* termination successful */
858                         cqr->retries--;
859                         cqr->status = DASD_CQR_CLEAR_PENDING;
860                         cqr->stopclk = get_clock();
861                         cqr->starttime = 0;
862                         DBF_DEV_EVENT(DBF_DEBUG, device,
863                                       "terminate cqr %p successful",
864                                       cqr);
865                         break;
866                 case -ENODEV:
867                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
868                                       "device gone, retry");
869                         break;
870                 case -EIO:
871                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
872                                       "I/O error, retry");
873                         break;
874                 case -EINVAL:
875                 case -EBUSY:
876                         DBF_DEV_EVENT(DBF_ERR, device, "%s",
877                                       "device busy, retry later");
878                         break;
879                 default:
880                         /* internal error 10 - unknown rc*/
881                         snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
882                         dev_err(&device->cdev->dev, "An error occurred in the "
883                                 "DASD device driver, reason=%s\n", errorstring);
884                         BUG();
885                         break;
886                 }
887                 retries++;
888         }
889         dasd_schedule_device_bh(device);
890         return rc;
891 }
892
893 /*
894  * Start the i/o. This start_IO can fail if the channel is really busy.
895  * In that case set up a timer to start the request later.
896  */
897 int dasd_start_IO(struct dasd_ccw_req *cqr)
898 {
899         struct dasd_device *device;
900         int rc;
901         char errorstring[ERRORLENGTH];
902
903         /* Check the cqr */
904         rc = dasd_check_cqr(cqr);
905         if (rc) {
906                 cqr->intrc = rc;
907                 return rc;
908         }
909         device = (struct dasd_device *) cqr->startdev;
910         if (((cqr->block &&
911               test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
912              test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
913             !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
914                 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
915                               "because of stolen lock", cqr);
916                 cqr->status = DASD_CQR_ERROR;
917                 cqr->intrc = -EPERM;
918                 return -EPERM;
919         }
920         if (cqr->retries < 0) {
921                 /* internal error 14 - start_IO run out of retries */
922                 sprintf(errorstring, "14 %p", cqr);
923                 dev_err(&device->cdev->dev, "An error occurred in the DASD "
924                         "device driver, reason=%s\n", errorstring);
925                 cqr->status = DASD_CQR_ERROR;
926                 return -EIO;
927         }
928         cqr->startclk = get_clock();
929         cqr->starttime = jiffies;
930         cqr->retries--;
931         if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
932                 cqr->lpm &= device->path_data.opm;
933                 if (!cqr->lpm)
934                         cqr->lpm = device->path_data.opm;
935         }
936         if (cqr->cpmode == 1) {
937                 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
938                                          (long) cqr, cqr->lpm);
939         } else {
940                 rc = ccw_device_start(device->cdev, cqr->cpaddr,
941                                       (long) cqr, cqr->lpm, 0);
942         }
943         switch (rc) {
944         case 0:
945                 cqr->status = DASD_CQR_IN_IO;
946                 break;
947         case -EBUSY:
948                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
949                               "start_IO: device busy, retry later");
950                 break;
951         case -ETIMEDOUT:
952                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
953                               "start_IO: request timeout, retry later");
954                 break;
955         case -EACCES:
956                 /* -EACCES indicates that the request used only a subset of the
957                  * available paths and all these paths are gone. If the lpm of
958                  * this request was only a subset of the opm (e.g. the ppm) then
959                  * we just do a retry with all available paths.
960                  * If we already use the full opm, something is amiss, and we
961                  * need a full path verification.
962                  */
963                 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
964                         DBF_DEV_EVENT(DBF_WARNING, device,
965                                       "start_IO: selected paths gone (%x)",
966                                       cqr->lpm);
967                 } else if (cqr->lpm != device->path_data.opm) {
968                         cqr->lpm = device->path_data.opm;
969                         DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
970                                       "start_IO: selected paths gone,"
971                                       " retry on all paths");
972                 } else {
973                         DBF_DEV_EVENT(DBF_WARNING, device, "%s",
974                                       "start_IO: all paths in opm gone,"
975                                       " do path verification");
976                         dasd_generic_last_path_gone(device);
977                         device->path_data.opm = 0;
978                         device->path_data.ppm = 0;
979                         device->path_data.npm = 0;
980                         device->path_data.tbvpm =
981                                 ccw_device_get_path_mask(device->cdev);
982                 }
983                 break;
984         case -ENODEV:
985                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
986                               "start_IO: -ENODEV device gone, retry");
987                 break;
988         case -EIO:
989                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
990                               "start_IO: -EIO device gone, retry");
991                 break;
992         case -EINVAL:
993                 /* most likely caused in power management context */
994                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
995                               "start_IO: -EINVAL device currently "
996                               "not accessible");
997                 break;
998         default:
999                 /* internal error 11 - unknown rc */
1000                 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1001                 dev_err(&device->cdev->dev,
1002                         "An error occurred in the DASD device driver, "
1003                         "reason=%s\n", errorstring);
1004                 BUG();
1005                 break;
1006         }
1007         cqr->intrc = rc;
1008         return rc;
1009 }
1010
1011 /*
1012  * Timeout function for dasd devices. This is used for different purposes
1013  *  1) missing interrupt handler for normal operation
1014  *  2) delayed start of request where start_IO failed with -EBUSY
1015  *  3) timeout for missing state change interrupts
1016  * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1017  * DASD_CQR_QUEUED for 2) and 3).
1018  */
1019 static void dasd_device_timeout(unsigned long ptr)
1020 {
1021         unsigned long flags;
1022         struct dasd_device *device;
1023
1024         device = (struct dasd_device *) ptr;
1025         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1026         /* re-activate request queue */
1027         dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1028         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1029         dasd_schedule_device_bh(device);
1030 }
1031
1032 /*
1033  * Setup timeout for a device in jiffies.
1034  */
1035 void dasd_device_set_timer(struct dasd_device *device, int expires)
1036 {
1037         if (expires == 0)
1038                 del_timer(&device->timer);
1039         else
1040                 mod_timer(&device->timer, jiffies + expires);
1041 }
1042
1043 /*
1044  * Clear timeout for a device.
1045  */
1046 void dasd_device_clear_timer(struct dasd_device *device)
1047 {
1048         del_timer(&device->timer);
1049 }
1050
1051 static void dasd_handle_killed_request(struct ccw_device *cdev,
1052                                        unsigned long intparm)
1053 {
1054         struct dasd_ccw_req *cqr;
1055         struct dasd_device *device;
1056
1057         if (!intparm)
1058                 return;
1059         cqr = (struct dasd_ccw_req *) intparm;
1060         if (cqr->status != DASD_CQR_IN_IO) {
1061                 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1062                                 "invalid status in handle_killed_request: "
1063                                 "%02x", cqr->status);
1064                 return;
1065         }
1066
1067         device = dasd_device_from_cdev_locked(cdev);
1068         if (IS_ERR(device)) {
1069                 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1070                                 "unable to get device from cdev");
1071                 return;
1072         }
1073
1074         if (!cqr->startdev ||
1075             device != cqr->startdev ||
1076             strncmp(cqr->startdev->discipline->ebcname,
1077                     (char *) &cqr->magic, 4)) {
1078                 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1079                                 "invalid device in request");
1080                 dasd_put_device(device);
1081                 return;
1082         }
1083
1084         /* Schedule request to be retried. */
1085         cqr->status = DASD_CQR_QUEUED;
1086
1087         dasd_device_clear_timer(device);
1088         dasd_schedule_device_bh(device);
1089         dasd_put_device(device);
1090 }
1091
1092 void dasd_generic_handle_state_change(struct dasd_device *device)
1093 {
1094         /* First of all start sense subsystem status request. */
1095         dasd_eer_snss(device);
1096
1097         dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1098         dasd_schedule_device_bh(device);
1099         if (device->block)
1100                 dasd_schedule_block_bh(device->block);
1101 }
1102
1103 /*
1104  * Interrupt handler for "normal" ssch-io based dasd devices.
1105  */
1106 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1107                       struct irb *irb)
1108 {
1109         struct dasd_ccw_req *cqr, *next;
1110         struct dasd_device *device;
1111         unsigned long long now;
1112         int expires;
1113
1114         kstat_cpu(smp_processor_id()).irqs[IOINT_DAS]++;
1115         if (IS_ERR(irb)) {
1116                 switch (PTR_ERR(irb)) {
1117                 case -EIO:
1118                         break;
1119                 case -ETIMEDOUT:
1120                         DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1121                                         "request timed out\n", __func__);
1122                         break;
1123                 default:
1124                         DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1125                                         "unknown error %ld\n", __func__,
1126                                         PTR_ERR(irb));
1127                 }
1128                 dasd_handle_killed_request(cdev, intparm);
1129                 return;
1130         }
1131
1132         now = get_clock();
1133         cqr = (struct dasd_ccw_req *) intparm;
1134         /* check for conditions that should be handled immediately */
1135         if (!cqr ||
1136             !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1137               scsw_cstat(&irb->scsw) == 0)) {
1138                 if (cqr)
1139                         memcpy(&cqr->irb, irb, sizeof(*irb));
1140                 device = dasd_device_from_cdev_locked(cdev);
1141                 if (IS_ERR(device))
1142                         return;
1143                 /* ignore unsolicited interrupts for DIAG discipline */
1144                 if (device->discipline == dasd_diag_discipline_pointer) {
1145                         dasd_put_device(device);
1146                         return;
1147                 }
1148                 device->discipline->dump_sense_dbf(device, irb, "int");
1149                 if (device->features & DASD_FEATURE_ERPLOG)
1150                         device->discipline->dump_sense(device, cqr, irb);
1151                 device->discipline->check_for_device_change(device, cqr, irb);
1152                 dasd_put_device(device);
1153         }
1154         if (!cqr)
1155                 return;
1156
1157         device = (struct dasd_device *) cqr->startdev;
1158         if (!device ||
1159             strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1160                 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1161                                 "invalid device in request");
1162                 return;
1163         }
1164
1165         /* Check for clear pending */
1166         if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1167             scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1168                 cqr->status = DASD_CQR_CLEARED;
1169                 dasd_device_clear_timer(device);
1170                 wake_up(&dasd_flush_wq);
1171                 dasd_schedule_device_bh(device);
1172                 return;
1173         }
1174
1175         /* check status - the request might have been killed by dyn detach */
1176         if (cqr->status != DASD_CQR_IN_IO) {
1177                 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1178                               "status %02x", dev_name(&cdev->dev), cqr->status);
1179                 return;
1180         }
1181
1182         next = NULL;
1183         expires = 0;
1184         if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1185             scsw_cstat(&irb->scsw) == 0) {
1186                 /* request was completed successfully */
1187                 cqr->status = DASD_CQR_SUCCESS;
1188                 cqr->stopclk = now;
1189                 /* Start first request on queue if possible -> fast_io. */
1190                 if (cqr->devlist.next != &device->ccw_queue) {
1191                         next = list_entry(cqr->devlist.next,
1192                                           struct dasd_ccw_req, devlist);
1193                 }
1194         } else {  /* error */
1195                 /*
1196                  * If we don't want complex ERP for this request, then just
1197                  * reset this and retry it in the fastpath
1198                  */
1199                 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1200                     cqr->retries > 0) {
1201                         if (cqr->lpm == device->path_data.opm)
1202                                 DBF_DEV_EVENT(DBF_DEBUG, device,
1203                                               "default ERP in fastpath "
1204                                               "(%i retries left)",
1205                                               cqr->retries);
1206                         if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1207                                 cqr->lpm = device->path_data.opm;
1208                         cqr->status = DASD_CQR_QUEUED;
1209                         next = cqr;
1210                 } else
1211                         cqr->status = DASD_CQR_ERROR;
1212         }
1213         if (next && (next->status == DASD_CQR_QUEUED) &&
1214             (!device->stopped)) {
1215                 if (device->discipline->start_IO(next) == 0)
1216                         expires = next->expires;
1217         }
1218         if (expires != 0)
1219                 dasd_device_set_timer(device, expires);
1220         else
1221                 dasd_device_clear_timer(device);
1222         dasd_schedule_device_bh(device);
1223 }
1224
1225 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1226 {
1227         struct dasd_device *device;
1228
1229         device = dasd_device_from_cdev_locked(cdev);
1230
1231         if (IS_ERR(device))
1232                 goto out;
1233         if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1234            device->state != device->target ||
1235            !device->discipline->check_for_device_change){
1236                 dasd_put_device(device);
1237                 goto out;
1238         }
1239         if (device->discipline->dump_sense_dbf)
1240                 device->discipline->dump_sense_dbf(device, irb, "uc");
1241         device->discipline->check_for_device_change(device, NULL, irb);
1242         dasd_put_device(device);
1243 out:
1244         return UC_TODO_RETRY;
1245 }
1246 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1247
1248 /*
1249  * If we have an error on a dasd_block layer request then we cancel
1250  * and return all further requests from the same dasd_block as well.
1251  */
1252 static void __dasd_device_recovery(struct dasd_device *device,
1253                                    struct dasd_ccw_req *ref_cqr)
1254 {
1255         struct list_head *l, *n;
1256         struct dasd_ccw_req *cqr;
1257
1258         /*
1259          * only requeue request that came from the dasd_block layer
1260          */
1261         if (!ref_cqr->block)
1262                 return;
1263
1264         list_for_each_safe(l, n, &device->ccw_queue) {
1265                 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1266                 if (cqr->status == DASD_CQR_QUEUED &&
1267                     ref_cqr->block == cqr->block) {
1268                         cqr->status = DASD_CQR_CLEARED;
1269                 }
1270         }
1271 };
1272
1273 /*
1274  * Remove those ccw requests from the queue that need to be returned
1275  * to the upper layer.
1276  */
1277 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1278                                             struct list_head *final_queue)
1279 {
1280         struct list_head *l, *n;
1281         struct dasd_ccw_req *cqr;
1282
1283         /* Process request with final status. */
1284         list_for_each_safe(l, n, &device->ccw_queue) {
1285                 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1286
1287                 /* Stop list processing at the first non-final request. */
1288                 if (cqr->status == DASD_CQR_QUEUED ||
1289                     cqr->status == DASD_CQR_IN_IO ||
1290                     cqr->status == DASD_CQR_CLEAR_PENDING)
1291                         break;
1292                 if (cqr->status == DASD_CQR_ERROR) {
1293                         __dasd_device_recovery(device, cqr);
1294                 }
1295                 /* Rechain finished requests to final queue */
1296                 list_move_tail(&cqr->devlist, final_queue);
1297         }
1298 }
1299
1300 /*
1301  * the cqrs from the final queue are returned to the upper layer
1302  * by setting a dasd_block state and calling the callback function
1303  */
1304 static void __dasd_device_process_final_queue(struct dasd_device *device,
1305                                               struct list_head *final_queue)
1306 {
1307         struct list_head *l, *n;
1308         struct dasd_ccw_req *cqr;
1309         struct dasd_block *block;
1310         void (*callback)(struct dasd_ccw_req *, void *data);
1311         void *callback_data;
1312         char errorstring[ERRORLENGTH];
1313
1314         list_for_each_safe(l, n, final_queue) {
1315                 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1316                 list_del_init(&cqr->devlist);
1317                 block = cqr->block;
1318                 callback = cqr->callback;
1319                 callback_data = cqr->callback_data;
1320                 if (block)
1321                         spin_lock_bh(&block->queue_lock);
1322                 switch (cqr->status) {
1323                 case DASD_CQR_SUCCESS:
1324                         cqr->status = DASD_CQR_DONE;
1325                         break;
1326                 case DASD_CQR_ERROR:
1327                         cqr->status = DASD_CQR_NEED_ERP;
1328                         break;
1329                 case DASD_CQR_CLEARED:
1330                         cqr->status = DASD_CQR_TERMINATED;
1331                         break;
1332                 default:
1333                         /* internal error 12 - wrong cqr status*/
1334                         snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1335                         dev_err(&device->cdev->dev,
1336                                 "An error occurred in the DASD device driver, "
1337                                 "reason=%s\n", errorstring);
1338                         BUG();
1339                 }
1340                 if (cqr->callback != NULL)
1341                         (callback)(cqr, callback_data);
1342                 if (block)
1343                         spin_unlock_bh(&block->queue_lock);
1344         }
1345 }
1346
1347 /*
1348  * Take a look at the first request on the ccw queue and check
1349  * if it reached its expire time. If so, terminate the IO.
1350  */
1351 static void __dasd_device_check_expire(struct dasd_device *device)
1352 {
1353         struct dasd_ccw_req *cqr;
1354
1355         if (list_empty(&device->ccw_queue))
1356                 return;
1357         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1358         if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1359             (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1360                 if (device->discipline->term_IO(cqr) != 0) {
1361                         /* Hmpf, try again in 5 sec */
1362                         dev_err(&device->cdev->dev,
1363                                 "cqr %p timed out (%lus) but cannot be "
1364                                 "ended, retrying in 5 s\n",
1365                                 cqr, (cqr->expires/HZ));
1366                         cqr->expires += 5*HZ;
1367                         dasd_device_set_timer(device, 5*HZ);
1368                 } else {
1369                         dev_err(&device->cdev->dev,
1370                                 "cqr %p timed out (%lus), %i retries "
1371                                 "remaining\n", cqr, (cqr->expires/HZ),
1372                                 cqr->retries);
1373                 }
1374         }
1375 }
1376
1377 /*
1378  * Take a look at the first request on the ccw queue and check
1379  * if it needs to be started.
1380  */
1381 static void __dasd_device_start_head(struct dasd_device *device)
1382 {
1383         struct dasd_ccw_req *cqr;
1384         int rc;
1385
1386         if (list_empty(&device->ccw_queue))
1387                 return;
1388         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1389         if (cqr->status != DASD_CQR_QUEUED)
1390                 return;
1391         /* when device is stopped, return request to previous layer
1392          * exception: only the disconnect or unresumed bits are set and the
1393          * cqr is a path verification request
1394          */
1395         if (device->stopped &&
1396             !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1397               && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1398                 cqr->intrc = -EAGAIN;
1399                 cqr->status = DASD_CQR_CLEARED;
1400                 dasd_schedule_device_bh(device);
1401                 return;
1402         }
1403
1404         rc = device->discipline->start_IO(cqr);
1405         if (rc == 0)
1406                 dasd_device_set_timer(device, cqr->expires);
1407         else if (rc == -EACCES) {
1408                 dasd_schedule_device_bh(device);
1409         } else
1410                 /* Hmpf, try again in 1/2 sec */
1411                 dasd_device_set_timer(device, 50);
1412 }
1413
1414 static void __dasd_device_check_path_events(struct dasd_device *device)
1415 {
1416         int rc;
1417
1418         if (device->path_data.tbvpm) {
1419                 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1420                                         DASD_UNRESUMED_PM))
1421                         return;
1422                 rc = device->discipline->verify_path(
1423                         device, device->path_data.tbvpm);
1424                 if (rc)
1425                         dasd_device_set_timer(device, 50);
1426                 else
1427                         device->path_data.tbvpm = 0;
1428         }
1429 };
1430
1431 /*
1432  * Go through all request on the dasd_device request queue,
1433  * terminate them on the cdev if necessary, and return them to the
1434  * submitting layer via callback.
1435  * Note:
1436  * Make sure that all 'submitting layers' still exist when
1437  * this function is called!. In other words, when 'device' is a base
1438  * device then all block layer requests must have been removed before
1439  * via dasd_flush_block_queue.
1440  */
1441 int dasd_flush_device_queue(struct dasd_device *device)
1442 {
1443         struct dasd_ccw_req *cqr, *n;
1444         int rc;
1445         struct list_head flush_queue;
1446
1447         INIT_LIST_HEAD(&flush_queue);
1448         spin_lock_irq(get_ccwdev_lock(device->cdev));
1449         rc = 0;
1450         list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1451                 /* Check status and move request to flush_queue */
1452                 switch (cqr->status) {
1453                 case DASD_CQR_IN_IO:
1454                         rc = device->discipline->term_IO(cqr);
1455                         if (rc) {
1456                                 /* unable to terminate requeust */
1457                                 dev_err(&device->cdev->dev,
1458                                         "Flushing the DASD request queue "
1459                                         "failed for request %p\n", cqr);
1460                                 /* stop flush processing */
1461                                 goto finished;
1462                         }
1463                         break;
1464                 case DASD_CQR_QUEUED:
1465                         cqr->stopclk = get_clock();
1466                         cqr->status = DASD_CQR_CLEARED;
1467                         break;
1468                 default: /* no need to modify the others */
1469                         break;
1470                 }
1471                 list_move_tail(&cqr->devlist, &flush_queue);
1472         }
1473 finished:
1474         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1475         /*
1476          * After this point all requests must be in state CLEAR_PENDING,
1477          * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1478          * one of the others.
1479          */
1480         list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1481                 wait_event(dasd_flush_wq,
1482                            (cqr->status != DASD_CQR_CLEAR_PENDING));
1483         /*
1484          * Now set each request back to TERMINATED, DONE or NEED_ERP
1485          * and call the callback function of flushed requests
1486          */
1487         __dasd_device_process_final_queue(device, &flush_queue);
1488         return rc;
1489 }
1490
1491 /*
1492  * Acquire the device lock and process queues for the device.
1493  */
1494 static void dasd_device_tasklet(struct dasd_device *device)
1495 {
1496         struct list_head final_queue;
1497
1498         atomic_set (&device->tasklet_scheduled, 0);
1499         INIT_LIST_HEAD(&final_queue);
1500         spin_lock_irq(get_ccwdev_lock(device->cdev));
1501         /* Check expire time of first request on the ccw queue. */
1502         __dasd_device_check_expire(device);
1503         /* find final requests on ccw queue */
1504         __dasd_device_process_ccw_queue(device, &final_queue);
1505         __dasd_device_check_path_events(device);
1506         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1507         /* Now call the callback function of requests with final status */
1508         __dasd_device_process_final_queue(device, &final_queue);
1509         spin_lock_irq(get_ccwdev_lock(device->cdev));
1510         /* Now check if the head of the ccw queue needs to be started. */
1511         __dasd_device_start_head(device);
1512         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1513         dasd_put_device(device);
1514 }
1515
1516 /*
1517  * Schedules a call to dasd_tasklet over the device tasklet.
1518  */
1519 void dasd_schedule_device_bh(struct dasd_device *device)
1520 {
1521         /* Protect against rescheduling. */
1522         if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1523                 return;
1524         dasd_get_device(device);
1525         tasklet_hi_schedule(&device->tasklet);
1526 }
1527
1528 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
1529 {
1530         device->stopped |= bits;
1531 }
1532 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
1533
1534 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
1535 {
1536         device->stopped &= ~bits;
1537         if (!device->stopped)
1538                 wake_up(&generic_waitq);
1539 }
1540 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
1541
1542 /*
1543  * Queue a request to the head of the device ccw_queue.
1544  * Start the I/O if possible.
1545  */
1546 void dasd_add_request_head(struct dasd_ccw_req *cqr)
1547 {
1548         struct dasd_device *device;
1549         unsigned long flags;
1550
1551         device = cqr->startdev;
1552         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1553         cqr->status = DASD_CQR_QUEUED;
1554         list_add(&cqr->devlist, &device->ccw_queue);
1555         /* let the bh start the request to keep them in order */
1556         dasd_schedule_device_bh(device);
1557         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1558 }
1559
1560 /*
1561  * Queue a request to the tail of the device ccw_queue.
1562  * Start the I/O if possible.
1563  */
1564 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
1565 {
1566         struct dasd_device *device;
1567         unsigned long flags;
1568
1569         device = cqr->startdev;
1570         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1571         cqr->status = DASD_CQR_QUEUED;
1572         list_add_tail(&cqr->devlist, &device->ccw_queue);
1573         /* let the bh start the request to keep them in order */
1574         dasd_schedule_device_bh(device);
1575         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1576 }
1577
1578 /*
1579  * Wakeup helper for the 'sleep_on' functions.
1580  */
1581 static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1582 {
1583         spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
1584         cqr->callback_data = DASD_SLEEPON_END_TAG;
1585         spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
1586         wake_up(&generic_waitq);
1587 }
1588
1589 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
1590 {
1591         struct dasd_device *device;
1592         int rc;
1593
1594         device = cqr->startdev;
1595         spin_lock_irq(get_ccwdev_lock(device->cdev));
1596         rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
1597         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1598         return rc;
1599 }
1600
1601 /*
1602  * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
1603  */
1604 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
1605 {
1606         struct dasd_device *device;
1607         dasd_erp_fn_t erp_fn;
1608
1609         if (cqr->status == DASD_CQR_FILLED)
1610                 return 0;
1611         device = cqr->startdev;
1612         if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1613                 if (cqr->status == DASD_CQR_TERMINATED) {
1614                         device->discipline->handle_terminated_request(cqr);
1615                         return 1;
1616                 }
1617                 if (cqr->status == DASD_CQR_NEED_ERP) {
1618                         erp_fn = device->discipline->erp_action(cqr);
1619                         erp_fn(cqr);
1620                         return 1;
1621                 }
1622                 if (cqr->status == DASD_CQR_FAILED)
1623                         dasd_log_sense(cqr, &cqr->irb);
1624                 if (cqr->refers) {
1625                         __dasd_process_erp(device, cqr);
1626                         return 1;
1627                 }
1628         }
1629         return 0;
1630 }
1631
1632 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
1633 {
1634         if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1635                 if (cqr->refers) /* erp is not done yet */
1636                         return 1;
1637                 return ((cqr->status != DASD_CQR_DONE) &&
1638                         (cqr->status != DASD_CQR_FAILED));
1639         } else
1640                 return (cqr->status == DASD_CQR_FILLED);
1641 }
1642
1643 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
1644 {
1645         struct dasd_device *device;
1646         int rc;
1647         struct list_head ccw_queue;
1648         struct dasd_ccw_req *cqr;
1649
1650         INIT_LIST_HEAD(&ccw_queue);
1651         maincqr->status = DASD_CQR_FILLED;
1652         device = maincqr->startdev;
1653         list_add(&maincqr->blocklist, &ccw_queue);
1654         for (cqr = maincqr;  __dasd_sleep_on_loop_condition(cqr);
1655              cqr = list_first_entry(&ccw_queue,
1656                                     struct dasd_ccw_req, blocklist)) {
1657
1658                 if (__dasd_sleep_on_erp(cqr))
1659                         continue;
1660                 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
1661                         continue;
1662                 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
1663                     !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1664                         cqr->status = DASD_CQR_FAILED;
1665                         cqr->intrc = -EPERM;
1666                         continue;
1667                 }
1668                 /* Non-temporary stop condition will trigger fail fast */
1669                 if (device->stopped & ~DASD_STOPPED_PENDING &&
1670                     test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1671                     (!dasd_eer_enabled(device))) {
1672                         cqr->status = DASD_CQR_FAILED;
1673                         continue;
1674                 }
1675                 /* Don't try to start requests if device is stopped */
1676                 if (interruptible) {
1677                         rc = wait_event_interruptible(
1678                                 generic_waitq, !(device->stopped));
1679                         if (rc == -ERESTARTSYS) {
1680                                 cqr->status = DASD_CQR_FAILED;
1681                                 maincqr->intrc = rc;
1682                                 continue;
1683                         }
1684                 } else
1685                         wait_event(generic_waitq, !(device->stopped));
1686
1687                 cqr->callback = dasd_wakeup_cb;
1688                 cqr->callback_data = DASD_SLEEPON_START_TAG;
1689                 dasd_add_request_tail(cqr);
1690                 if (interruptible) {
1691                         rc = wait_event_interruptible(
1692                                 generic_waitq, _wait_for_wakeup(cqr));
1693                         if (rc == -ERESTARTSYS) {
1694                                 dasd_cancel_req(cqr);
1695                                 /* wait (non-interruptible) for final status */
1696                                 wait_event(generic_waitq,
1697                                            _wait_for_wakeup(cqr));
1698                                 cqr->status = DASD_CQR_FAILED;
1699                                 maincqr->intrc = rc;
1700                                 continue;
1701                         }
1702                 } else
1703                         wait_event(generic_waitq, _wait_for_wakeup(cqr));
1704         }
1705
1706         maincqr->endclk = get_clock();
1707         if ((maincqr->status != DASD_CQR_DONE) &&
1708             (maincqr->intrc != -ERESTARTSYS))
1709                 dasd_log_sense(maincqr, &maincqr->irb);
1710         if (maincqr->status == DASD_CQR_DONE)
1711                 rc = 0;
1712         else if (maincqr->intrc)
1713                 rc = maincqr->intrc;
1714         else
1715                 rc = -EIO;
1716         return rc;
1717 }
1718
1719 /*
1720  * Queue a request to the tail of the device ccw_queue and wait for
1721  * it's completion.
1722  */
1723 int dasd_sleep_on(struct dasd_ccw_req *cqr)
1724 {
1725         return _dasd_sleep_on(cqr, 0);
1726 }
1727
1728 /*
1729  * Queue a request to the tail of the device ccw_queue and wait
1730  * interruptible for it's completion.
1731  */
1732 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
1733 {
1734         return _dasd_sleep_on(cqr, 1);
1735 }
1736
1737 /*
1738  * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1739  * for eckd devices) the currently running request has to be terminated
1740  * and be put back to status queued, before the special request is added
1741  * to the head of the queue. Then the special request is waited on normally.
1742  */
1743 static inline int _dasd_term_running_cqr(struct dasd_device *device)
1744 {
1745         struct dasd_ccw_req *cqr;
1746
1747         if (list_empty(&device->ccw_queue))
1748                 return 0;
1749         cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1750         return device->discipline->term_IO(cqr);
1751 }
1752
1753 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
1754 {
1755         struct dasd_device *device;
1756         int rc;
1757
1758         device = cqr->startdev;
1759         if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
1760             !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1761                 cqr->status = DASD_CQR_FAILED;
1762                 cqr->intrc = -EPERM;
1763                 return -EIO;
1764         }
1765         spin_lock_irq(get_ccwdev_lock(device->cdev));
1766         rc = _dasd_term_running_cqr(device);
1767         if (rc) {
1768                 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1769                 return rc;
1770         }
1771         cqr->callback = dasd_wakeup_cb;
1772         cqr->callback_data = DASD_SLEEPON_START_TAG;
1773         cqr->status = DASD_CQR_QUEUED;
1774         list_add(&cqr->devlist, &device->ccw_queue);
1775
1776         /* let the bh start the request to keep them in order */
1777         dasd_schedule_device_bh(device);
1778
1779         spin_unlock_irq(get_ccwdev_lock(device->cdev));
1780
1781         wait_event(generic_waitq, _wait_for_wakeup(cqr));
1782
1783         if (cqr->status == DASD_CQR_DONE)
1784                 rc = 0;
1785         else if (cqr->intrc)
1786                 rc = cqr->intrc;
1787         else
1788                 rc = -EIO;
1789         return rc;
1790 }
1791
1792 /*
1793  * Cancels a request that was started with dasd_sleep_on_req.
1794  * This is useful to timeout requests. The request will be
1795  * terminated if it is currently in i/o.
1796  * Returns 1 if the request has been terminated.
1797  *         0 if there was no need to terminate the request (not started yet)
1798  *         negative error code if termination failed
1799  * Cancellation of a request is an asynchronous operation! The calling
1800  * function has to wait until the request is properly returned via callback.
1801  */
1802 int dasd_cancel_req(struct dasd_ccw_req *cqr)
1803 {
1804         struct dasd_device *device = cqr->startdev;
1805         unsigned long flags;
1806         int rc;
1807
1808         rc = 0;
1809         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1810         switch (cqr->status) {
1811         case DASD_CQR_QUEUED:
1812                 /* request was not started - just set to cleared */
1813                 cqr->status = DASD_CQR_CLEARED;
1814                 break;
1815         case DASD_CQR_IN_IO:
1816                 /* request in IO - terminate IO and release again */
1817                 rc = device->discipline->term_IO(cqr);
1818                 if (rc) {
1819                         dev_err(&device->cdev->dev,
1820                                 "Cancelling request %p failed with rc=%d\n",
1821                                 cqr, rc);
1822                 } else {
1823                         cqr->stopclk = get_clock();
1824                 }
1825                 break;
1826         default: /* already finished or clear pending - do nothing */
1827                 break;
1828         }
1829         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1830         dasd_schedule_device_bh(device);
1831         return rc;
1832 }
1833
1834
1835 /*
1836  * SECTION: Operations of the dasd_block layer.
1837  */
1838
1839 /*
1840  * Timeout function for dasd_block. This is used when the block layer
1841  * is waiting for something that may not come reliably, (e.g. a state
1842  * change interrupt)
1843  */
1844 static void dasd_block_timeout(unsigned long ptr)
1845 {
1846         unsigned long flags;
1847         struct dasd_block *block;
1848
1849         block = (struct dasd_block *) ptr;
1850         spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1851         /* re-activate request queue */
1852         dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
1853         spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1854         dasd_schedule_block_bh(block);
1855 }
1856
1857 /*
1858  * Setup timeout for a dasd_block in jiffies.
1859  */
1860 void dasd_block_set_timer(struct dasd_block *block, int expires)
1861 {
1862         if (expires == 0)
1863                 del_timer(&block->timer);
1864         else
1865                 mod_timer(&block->timer, jiffies + expires);
1866 }
1867
1868 /*
1869  * Clear timeout for a dasd_block.
1870  */
1871 void dasd_block_clear_timer(struct dasd_block *block)
1872 {
1873         del_timer(&block->timer);
1874 }
1875
1876 /*
1877  * Process finished error recovery ccw.
1878  */
1879 static void __dasd_process_erp(struct dasd_device *device,
1880                                struct dasd_ccw_req *cqr)
1881 {
1882         dasd_erp_fn_t erp_fn;
1883
1884         if (cqr->status == DASD_CQR_DONE)
1885                 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1886         else
1887                 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
1888         erp_fn = device->discipline->erp_postaction(cqr);
1889         erp_fn(cqr);
1890 }
1891
1892 /*
1893  * Fetch requests from the block device queue.
1894  */
1895 static void __dasd_process_request_queue(struct dasd_block *block)
1896 {
1897         struct request_queue *queue;
1898         struct request *req;
1899         struct dasd_ccw_req *cqr;
1900         struct dasd_device *basedev;
1901         unsigned long flags;
1902         queue = block->request_queue;
1903         basedev = block->base;
1904         /* No queue ? Then there is nothing to do. */
1905         if (queue == NULL)
1906                 return;
1907
1908         /*
1909          * We requeue request from the block device queue to the ccw
1910          * queue only in two states. In state DASD_STATE_READY the
1911          * partition detection is done and we need to requeue requests
1912          * for that. State DASD_STATE_ONLINE is normal block device
1913          * operation.
1914          */
1915         if (basedev->state < DASD_STATE_READY) {
1916                 while ((req = blk_fetch_request(block->request_queue)))
1917                         __blk_end_request_all(req, -EIO);
1918                 return;
1919         }
1920         /* Now we try to fetch requests from the request queue */
1921         while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
1922                 if (basedev->features & DASD_FEATURE_READONLY &&
1923                     rq_data_dir(req) == WRITE) {
1924                         DBF_DEV_EVENT(DBF_ERR, basedev,
1925                                       "Rejecting write request %p",
1926                                       req);
1927                         blk_start_request(req);
1928                         __blk_end_request_all(req, -EIO);
1929                         continue;
1930                 }
1931                 cqr = basedev->discipline->build_cp(basedev, block, req);
1932                 if (IS_ERR(cqr)) {
1933                         if (PTR_ERR(cqr) == -EBUSY)
1934                                 break;  /* normal end condition */
1935                         if (PTR_ERR(cqr) == -ENOMEM)
1936                                 break;  /* terminate request queue loop */
1937                         if (PTR_ERR(cqr) == -EAGAIN) {
1938                                 /*
1939                                  * The current request cannot be build right
1940                                  * now, we have to try later. If this request
1941                                  * is the head-of-queue we stop the device
1942                                  * for 1/2 second.
1943                                  */
1944                                 if (!list_empty(&block->ccw_queue))
1945                                         break;
1946                                 spin_lock_irqsave(
1947                                         get_ccwdev_lock(basedev->cdev), flags);
1948                                 dasd_device_set_stop_bits(basedev,
1949                                                           DASD_STOPPED_PENDING);
1950                                 spin_unlock_irqrestore(
1951                                         get_ccwdev_lock(basedev->cdev), flags);
1952                                 dasd_block_set_timer(block, HZ/2);
1953                                 break;
1954                         }
1955                         DBF_DEV_EVENT(DBF_ERR, basedev,
1956                                       "CCW creation failed (rc=%ld) "
1957                                       "on request %p",
1958                                       PTR_ERR(cqr), req);
1959                         blk_start_request(req);
1960                         __blk_end_request_all(req, -EIO);
1961                         continue;
1962                 }
1963                 /*
1964                  *  Note: callback is set to dasd_return_cqr_cb in
1965                  * __dasd_block_start_head to cover erp requests as well
1966                  */
1967                 cqr->callback_data = (void *) req;
1968                 cqr->status = DASD_CQR_FILLED;
1969                 blk_start_request(req);
1970                 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1971                 dasd_profile_start(block, cqr, req);
1972         }
1973 }
1974
1975 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1976 {
1977         struct request *req;
1978         int status;
1979         int error = 0;
1980
1981         req = (struct request *) cqr->callback_data;
1982         dasd_profile_end(cqr->block, cqr, req);
1983         status = cqr->block->base->discipline->free_cp(cqr, req);
1984         if (status <= 0)
1985                 error = status ? status : -EIO;
1986         __blk_end_request_all(req, error);
1987 }
1988
1989 /*
1990  * Process ccw request queue.
1991  */
1992 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1993                                            struct list_head *final_queue)
1994 {
1995         struct list_head *l, *n;
1996         struct dasd_ccw_req *cqr;
1997         dasd_erp_fn_t erp_fn;
1998         unsigned long flags;
1999         struct dasd_device *base = block->base;
2000
2001 restart:
2002         /* Process request with final status. */
2003         list_for_each_safe(l, n, &block->ccw_queue) {
2004                 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2005                 if (cqr->status != DASD_CQR_DONE &&
2006                     cqr->status != DASD_CQR_FAILED &&
2007                     cqr->status != DASD_CQR_NEED_ERP &&
2008                     cqr->status != DASD_CQR_TERMINATED)
2009                         continue;
2010
2011                 if (cqr->status == DASD_CQR_TERMINATED) {
2012                         base->discipline->handle_terminated_request(cqr);
2013                         goto restart;
2014                 }
2015
2016                 /*  Process requests that may be recovered */
2017                 if (cqr->status == DASD_CQR_NEED_ERP) {
2018                         erp_fn = base->discipline->erp_action(cqr);
2019                         if (IS_ERR(erp_fn(cqr)))
2020                                 continue;
2021                         goto restart;
2022                 }
2023
2024                 /* log sense for fatal error */
2025                 if (cqr->status == DASD_CQR_FAILED) {
2026                         dasd_log_sense(cqr, &cqr->irb);
2027                 }
2028
2029                 /* First of all call extended error reporting. */
2030                 if (dasd_eer_enabled(base) &&
2031                     cqr->status == DASD_CQR_FAILED) {
2032                         dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2033
2034                         /* restart request  */
2035                         cqr->status = DASD_CQR_FILLED;
2036                         cqr->retries = 255;
2037                         spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2038                         dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2039                         spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2040                                                flags);
2041                         goto restart;
2042                 }
2043
2044                 /* Process finished ERP request. */
2045                 if (cqr->refers) {
2046                         __dasd_process_erp(base, cqr);
2047                         goto restart;
2048                 }
2049
2050                 /* Rechain finished requests to final queue */
2051                 cqr->endclk = get_clock();
2052                 list_move_tail(&cqr->blocklist, final_queue);
2053         }
2054 }
2055
2056 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2057 {
2058         dasd_schedule_block_bh(cqr->block);
2059 }
2060
2061 static void __dasd_block_start_head(struct dasd_block *block)
2062 {
2063         struct dasd_ccw_req *cqr;
2064
2065         if (list_empty(&block->ccw_queue))
2066                 return;
2067         /* We allways begin with the first requests on the queue, as some
2068          * of previously started requests have to be enqueued on a
2069          * dasd_device again for error recovery.
2070          */
2071         list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2072                 if (cqr->status != DASD_CQR_FILLED)
2073                         continue;
2074                 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2075                     !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2076                         cqr->status = DASD_CQR_FAILED;
2077                         cqr->intrc = -EPERM;
2078                         dasd_schedule_block_bh(block);
2079                         continue;
2080                 }
2081                 /* Non-temporary stop condition will trigger fail fast */
2082                 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2083                     test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2084                     (!dasd_eer_enabled(block->base))) {
2085                         cqr->status = DASD_CQR_FAILED;
2086                         dasd_schedule_block_bh(block);
2087                         continue;
2088                 }
2089                 /* Don't try to start requests if device is stopped */
2090                 if (block->base->stopped)
2091                         return;
2092
2093                 /* just a fail safe check, should not happen */
2094                 if (!cqr->startdev)
2095                         cqr->startdev = block->base;
2096
2097                 /* make sure that the requests we submit find their way back */
2098                 cqr->callback = dasd_return_cqr_cb;
2099
2100                 dasd_add_request_tail(cqr);
2101         }
2102 }
2103
2104 /*
2105  * Central dasd_block layer routine. Takes requests from the generic
2106  * block layer request queue, creates ccw requests, enqueues them on
2107  * a dasd_device and processes ccw requests that have been returned.
2108  */
2109 static void dasd_block_tasklet(struct dasd_block *block)
2110 {
2111         struct list_head final_queue;
2112         struct list_head *l, *n;
2113         struct dasd_ccw_req *cqr;
2114
2115         atomic_set(&block->tasklet_scheduled, 0);
2116         INIT_LIST_HEAD(&final_queue);
2117         spin_lock(&block->queue_lock);
2118         /* Finish off requests on ccw queue */
2119         __dasd_process_block_ccw_queue(block, &final_queue);
2120         spin_unlock(&block->queue_lock);
2121         /* Now call the callback function of requests with final status */
2122         spin_lock_irq(&block->request_queue_lock);
2123         list_for_each_safe(l, n, &final_queue) {
2124                 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2125                 list_del_init(&cqr->blocklist);
2126                 __dasd_cleanup_cqr(cqr);
2127         }
2128         spin_lock(&block->queue_lock);
2129         /* Get new request from the block device request queue */
2130         __dasd_process_request_queue(block);
2131         /* Now check if the head of the ccw queue needs to be started. */
2132         __dasd_block_start_head(block);
2133         spin_unlock(&block->queue_lock);
2134         spin_unlock_irq(&block->request_queue_lock);
2135         dasd_put_device(block->base);
2136 }
2137
2138 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2139 {
2140         wake_up(&dasd_flush_wq);
2141 }
2142
2143 /*
2144  * Go through all request on the dasd_block request queue, cancel them
2145  * on the respective dasd_device, and return them to the generic
2146  * block layer.
2147  */
2148 static int dasd_flush_block_queue(struct dasd_block *block)
2149 {
2150         struct dasd_ccw_req *cqr, *n;
2151         int rc, i;
2152         struct list_head flush_queue;
2153
2154         INIT_LIST_HEAD(&flush_queue);
2155         spin_lock_bh(&block->queue_lock);
2156         rc = 0;
2157 restart:
2158         list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2159                 /* if this request currently owned by a dasd_device cancel it */
2160                 if (cqr->status >= DASD_CQR_QUEUED)
2161                         rc = dasd_cancel_req(cqr);
2162                 if (rc < 0)
2163                         break;
2164                 /* Rechain request (including erp chain) so it won't be
2165                  * touched by the dasd_block_tasklet anymore.
2166                  * Replace the callback so we notice when the request
2167                  * is returned from the dasd_device layer.
2168                  */
2169                 cqr->callback = _dasd_wake_block_flush_cb;
2170                 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2171                         list_move_tail(&cqr->blocklist, &flush_queue);
2172                 if (i > 1)
2173                         /* moved more than one request - need to restart */
2174                         goto restart;
2175         }
2176         spin_unlock_bh(&block->queue_lock);
2177         /* Now call the callback function of flushed requests */
2178 restart_cb:
2179         list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2180                 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2181                 /* Process finished ERP request. */
2182                 if (cqr->refers) {
2183                         spin_lock_bh(&block->queue_lock);
2184                         __dasd_process_erp(block->base, cqr);
2185                         spin_unlock_bh(&block->queue_lock);
2186                         /* restart list_for_xx loop since dasd_process_erp
2187                          * might remove multiple elements */
2188                         goto restart_cb;
2189                 }
2190                 /* call the callback function */
2191                 spin_lock_irq(&block->request_queue_lock);
2192                 cqr->endclk = get_clock();
2193                 list_del_init(&cqr->blocklist);
2194                 __dasd_cleanup_cqr(cqr);
2195                 spin_unlock_irq(&block->request_queue_lock);
2196         }
2197         return rc;
2198 }
2199
2200 /*
2201  * Schedules a call to dasd_tasklet over the device tasklet.
2202  */
2203 void dasd_schedule_block_bh(struct dasd_block *block)
2204 {
2205         /* Protect against rescheduling. */
2206         if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2207                 return;
2208         /* life cycle of block is bound to it's base device */
2209         dasd_get_device(block->base);
2210         tasklet_hi_schedule(&block->tasklet);
2211 }
2212
2213
2214 /*
2215  * SECTION: external block device operations
2216  * (request queue handling, open, release, etc.)
2217  */
2218
2219 /*
2220  * Dasd request queue function. Called from ll_rw_blk.c
2221  */
2222 static void do_dasd_request(struct request_queue *queue)
2223 {
2224         struct dasd_block *block;
2225
2226         block = queue->queuedata;
2227         spin_lock(&block->queue_lock);
2228         /* Get new request from the block device request queue */
2229         __dasd_process_request_queue(block);
2230         /* Now check if the head of the ccw queue needs to be started. */
2231         __dasd_block_start_head(block);
2232         spin_unlock(&block->queue_lock);
2233 }
2234
2235 /*
2236  * Allocate and initialize request queue and default I/O scheduler.
2237  */
2238 static int dasd_alloc_queue(struct dasd_block *block)
2239 {
2240         int rc;
2241
2242         block->request_queue = blk_init_queue(do_dasd_request,
2243                                                &block->request_queue_lock);
2244         if (block->request_queue == NULL)
2245                 return -ENOMEM;
2246
2247         block->request_queue->queuedata = block;
2248
2249         elevator_exit(block->request_queue->elevator);
2250         block->request_queue->elevator = NULL;
2251         rc = elevator_init(block->request_queue, "deadline");
2252         if (rc) {
2253                 blk_cleanup_queue(block->request_queue);
2254                 return rc;
2255         }
2256         return 0;
2257 }
2258
2259 /*
2260  * Allocate and initialize request queue.
2261  */
2262 static void dasd_setup_queue(struct dasd_block *block)
2263 {
2264         int max;
2265
2266         if (block->base->features & DASD_FEATURE_USERAW) {
2267                 /*
2268                  * the max_blocks value for raw_track access is 256
2269                  * it is higher than the native ECKD value because we
2270                  * only need one ccw per track
2271                  * so the max_hw_sectors are
2272                  * 2048 x 512B = 1024kB = 16 tracks
2273                  */
2274                 max = 2048;
2275         } else {
2276                 max = block->base->discipline->max_blocks << block->s2b_shift;
2277         }
2278         blk_queue_logical_block_size(block->request_queue,
2279                                      block->bp_block);
2280         blk_queue_max_hw_sectors(block->request_queue, max);
2281         blk_queue_max_segments(block->request_queue, -1L);
2282         /* with page sized segments we can translate each segement into
2283          * one idaw/tidaw
2284          */
2285         blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2286         blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
2287 }
2288
2289 /*
2290  * Deactivate and free request queue.
2291  */
2292 static void dasd_free_queue(struct dasd_block *block)
2293 {
2294         if (block->request_queue) {
2295                 blk_cleanup_queue(block->request_queue);
2296                 block->request_queue = NULL;
2297         }
2298 }
2299
2300 /*
2301  * Flush request on the request queue.
2302  */
2303 static void dasd_flush_request_queue(struct dasd_block *block)
2304 {
2305         struct request *req;
2306
2307         if (!block->request_queue)
2308                 return;
2309
2310         spin_lock_irq(&block->request_queue_lock);
2311         while ((req = blk_fetch_request(block->request_queue)))
2312                 __blk_end_request_all(req, -EIO);
2313         spin_unlock_irq(&block->request_queue_lock);
2314 }
2315
2316 static int dasd_open(struct block_device *bdev, fmode_t mode)
2317 {
2318         struct dasd_block *block = bdev->bd_disk->private_data;
2319         struct dasd_device *base;
2320         int rc;
2321
2322         if (!block)
2323                 return -ENODEV;
2324
2325         base = block->base;
2326         atomic_inc(&block->open_count);
2327         if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
2328                 rc = -ENODEV;
2329                 goto unlock;
2330         }
2331
2332         if (!try_module_get(base->discipline->owner)) {
2333                 rc = -EINVAL;
2334                 goto unlock;
2335         }
2336
2337         if (dasd_probeonly) {
2338                 dev_info(&base->cdev->dev,
2339                          "Accessing the DASD failed because it is in "
2340                          "probeonly mode\n");
2341                 rc = -EPERM;
2342                 goto out;
2343         }
2344
2345         if (base->state <= DASD_STATE_BASIC) {
2346                 DBF_DEV_EVENT(DBF_ERR, base, " %s",
2347                               " Cannot open unrecognized device");
2348                 rc = -ENODEV;
2349                 goto out;
2350         }
2351
2352         if ((mode & FMODE_WRITE) &&
2353             (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2354              (base->features & DASD_FEATURE_READONLY))) {
2355                 rc = -EROFS;
2356                 goto out;
2357         }
2358
2359         return 0;
2360
2361 out:
2362         module_put(base->discipline->owner);
2363 unlock:
2364         atomic_dec(&block->open_count);
2365         return rc;
2366 }
2367
2368 static int dasd_release(struct gendisk *disk, fmode_t mode)
2369 {
2370         struct dasd_block *block = disk->private_data;
2371
2372         atomic_dec(&block->open_count);
2373         module_put(block->base->discipline->owner);
2374         return 0;
2375 }
2376
2377 /*
2378  * Return disk geometry.
2379  */
2380 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
2381 {
2382         struct dasd_block *block;
2383         struct dasd_device *base;
2384
2385         block = bdev->bd_disk->private_data;
2386         if (!block)
2387                 return -ENODEV;
2388         base = block->base;
2389
2390         if (!base->discipline ||
2391             !base->discipline->fill_geometry)
2392                 return -EINVAL;
2393
2394         base->discipline->fill_geometry(block, geo);
2395         geo->start = get_start_sect(bdev) >> block->s2b_shift;
2396         return 0;
2397 }
2398
2399 const struct block_device_operations
2400 dasd_device_operations = {
2401         .owner          = THIS_MODULE,
2402         .open           = dasd_open,
2403         .release        = dasd_release,
2404         .ioctl          = dasd_ioctl,
2405         .compat_ioctl   = dasd_ioctl,
2406         .getgeo         = dasd_getgeo,
2407 };
2408
2409 /*******************************************************************************
2410  * end of block device operations
2411  */
2412
2413 static void
2414 dasd_exit(void)
2415 {
2416 #ifdef CONFIG_PROC_FS
2417         dasd_proc_exit();
2418 #endif
2419         dasd_eer_exit();
2420         if (dasd_page_cache != NULL) {
2421                 kmem_cache_destroy(dasd_page_cache);
2422                 dasd_page_cache = NULL;
2423         }
2424         dasd_gendisk_exit();
2425         dasd_devmap_exit();
2426         if (dasd_debug_area != NULL) {
2427                 debug_unregister(dasd_debug_area);
2428                 dasd_debug_area = NULL;
2429         }
2430 }
2431
2432 /*
2433  * SECTION: common functions for ccw_driver use
2434  */
2435
2436 /*
2437  * Is the device read-only?
2438  * Note that this function does not report the setting of the
2439  * readonly device attribute, but how it is configured in z/VM.
2440  */
2441 int dasd_device_is_ro(struct dasd_device *device)
2442 {
2443         struct ccw_dev_id dev_id;
2444         struct diag210 diag_data;
2445         int rc;
2446
2447         if (!MACHINE_IS_VM)
2448                 return 0;
2449         ccw_device_get_id(device->cdev, &dev_id);
2450         memset(&diag_data, 0, sizeof(diag_data));
2451         diag_data.vrdcdvno = dev_id.devno;
2452         diag_data.vrdclen = sizeof(diag_data);
2453         rc = diag210(&diag_data);
2454         if (rc == 0 || rc == 2) {
2455                 return diag_data.vrdcvfla & 0x80;
2456         } else {
2457                 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2458                           dev_id.devno, rc);
2459                 return 0;
2460         }
2461 }
2462 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2463
2464 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2465 {
2466         struct ccw_device *cdev = data;
2467         int ret;
2468
2469         ret = ccw_device_set_online(cdev);
2470         if (ret)
2471                 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2472                            dev_name(&cdev->dev), ret);
2473 }
2474
2475 /*
2476  * Initial attempt at a probe function. this can be simplified once
2477  * the other detection code is gone.
2478  */
2479 int dasd_generic_probe(struct ccw_device *cdev,
2480                        struct dasd_discipline *discipline)
2481 {
2482         int ret;
2483
2484         ret = dasd_add_sysfs_files(cdev);
2485         if (ret) {
2486                 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2487                                 "dasd_generic_probe: could not add "
2488                                 "sysfs entries");
2489                 return ret;
2490         }
2491         cdev->handler = &dasd_int_handler;
2492
2493         /*
2494          * Automatically online either all dasd devices (dasd_autodetect)
2495          * or all devices specified with dasd= parameters during
2496          * initial probe.
2497          */
2498         if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
2499             (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
2500                 async_schedule(dasd_generic_auto_online, cdev);
2501         return 0;
2502 }
2503
2504 /*
2505  * This will one day be called from a global not_oper handler.
2506  * It is also used by driver_unregister during module unload.
2507  */
2508 void dasd_generic_remove(struct ccw_device *cdev)
2509 {
2510         struct dasd_device *device;
2511         struct dasd_block *block;
2512
2513         cdev->handler = NULL;
2514
2515         dasd_remove_sysfs_files(cdev);
2516         device = dasd_device_from_cdev(cdev);
2517         if (IS_ERR(device))
2518                 return;
2519         if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2520                 /* Already doing offline processing */
2521                 dasd_put_device(device);
2522                 return;
2523         }
2524         /*
2525          * This device is removed unconditionally. Set offline
2526          * flag to prevent dasd_open from opening it while it is
2527          * no quite down yet.
2528          */
2529         dasd_set_target_state(device, DASD_STATE_NEW);
2530         /* dasd_delete_device destroys the device reference. */
2531         block = device->block;
2532         device->block = NULL;
2533         dasd_delete_device(device);
2534         /*
2535          * life cycle of block is bound to device, so delete it after
2536          * device was safely removed
2537          */
2538         if (block)
2539                 dasd_free_block(block);
2540 }
2541
2542 /*
2543  * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
2544  * the device is detected for the first time and is supposed to be used
2545  * or the user has started activation through sysfs.
2546  */
2547 int dasd_generic_set_online(struct ccw_device *cdev,
2548                             struct dasd_discipline *base_discipline)
2549 {
2550         struct dasd_discipline *discipline;
2551         struct dasd_device *device;
2552         int rc;
2553
2554         /* first online clears initial online feature flag */
2555         dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
2556         device = dasd_create_device(cdev);
2557         if (IS_ERR(device))
2558                 return PTR_ERR(device);
2559
2560         discipline = base_discipline;
2561         if (device->features & DASD_FEATURE_USEDIAG) {
2562                 if (!dasd_diag_discipline_pointer) {
2563                         pr_warning("%s Setting the DASD online failed because "
2564                                    "of missing DIAG discipline\n",
2565                                    dev_name(&cdev->dev));
2566                         dasd_delete_device(device);
2567                         return -ENODEV;
2568                 }
2569                 discipline = dasd_diag_discipline_pointer;
2570         }
2571         if (!try_module_get(base_discipline->owner)) {
2572                 dasd_delete_device(device);
2573                 return -EINVAL;
2574         }
2575         if (!try_module_get(discipline->owner)) {
2576                 module_put(base_discipline->owner);
2577                 dasd_delete_device(device);
2578                 return -EINVAL;
2579         }
2580         device->base_discipline = base_discipline;
2581         device->discipline = discipline;
2582
2583         /* check_device will allocate block device if necessary */
2584         rc = discipline->check_device(device);
2585         if (rc) {
2586                 pr_warning("%s Setting the DASD online with discipline %s "
2587                            "failed with rc=%i\n",
2588                            dev_name(&cdev->dev), discipline->name, rc);
2589                 module_put(discipline->owner);
2590                 module_put(base_discipline->owner);
2591                 dasd_delete_device(device);
2592                 return rc;
2593         }
2594
2595         dasd_set_target_state(device, DASD_STATE_ONLINE);
2596         if (device->state <= DASD_STATE_KNOWN) {
2597                 pr_warning("%s Setting the DASD online failed because of a "
2598                            "missing discipline\n", dev_name(&cdev->dev));
2599                 rc = -ENODEV;
2600                 dasd_set_target_state(device, DASD_STATE_NEW);
2601                 if (device->block)
2602                         dasd_free_block(device->block);
2603                 dasd_delete_device(device);
2604         } else
2605                 pr_debug("dasd_generic device %s found\n",
2606                                 dev_name(&cdev->dev));
2607
2608         wait_event(dasd_init_waitq, _wait_for_device(device));
2609
2610         dasd_put_device(device);
2611         return rc;
2612 }
2613
2614 int dasd_generic_set_offline(struct ccw_device *cdev)
2615 {
2616         struct dasd_device *device;
2617         struct dasd_block *block;
2618         int max_count, open_count;
2619
2620         device = dasd_device_from_cdev(cdev);
2621         if (IS_ERR(device))
2622                 return PTR_ERR(device);
2623         if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2624                 /* Already doing offline processing */
2625                 dasd_put_device(device);
2626                 return 0;
2627         }
2628         /*
2629          * We must make sure that this device is currently not in use.
2630          * The open_count is increased for every opener, that includes
2631          * the blkdev_get in dasd_scan_partitions. We are only interested
2632          * in the other openers.
2633          */
2634         if (device->block) {
2635                 max_count = device->block->bdev ? 0 : -1;
2636                 open_count = atomic_read(&device->block->open_count);
2637                 if (open_count > max_count) {
2638                         if (open_count > 0)
2639                                 pr_warning("%s: The DASD cannot be set offline "
2640                                            "with open count %i\n",
2641                                            dev_name(&cdev->dev), open_count);
2642                         else
2643                                 pr_warning("%s: The DASD cannot be set offline "
2644                                            "while it is in use\n",
2645                                            dev_name(&cdev->dev));
2646                         clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2647                         dasd_put_device(device);
2648                         return -EBUSY;
2649                 }
2650         }
2651         dasd_set_target_state(device, DASD_STATE_NEW);
2652         /* dasd_delete_device destroys the device reference. */
2653         block = device->block;
2654         device->block = NULL;
2655         dasd_delete_device(device);
2656         /*
2657          * life cycle of block is bound to device, so delete it after
2658          * device was safely removed
2659          */
2660         if (block)
2661                 dasd_free_block(block);
2662         return 0;
2663 }
2664
2665 int dasd_generic_last_path_gone(struct dasd_device *device)
2666 {
2667         struct dasd_ccw_req *cqr;
2668
2669         dev_warn(&device->cdev->dev, "No operational channel path is left "
2670                  "for the device\n");
2671         DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
2672         /* First of all call extended error reporting. */
2673         dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2674
2675         if (device->state < DASD_STATE_BASIC)
2676                 return 0;
2677         /* Device is active. We want to keep it. */
2678         list_for_each_entry(cqr, &device->ccw_queue, devlist)
2679                 if ((cqr->status == DASD_CQR_IN_IO) ||
2680                     (cqr->status == DASD_CQR_CLEAR_PENDING)) {
2681                         cqr->status = DASD_CQR_QUEUED;
2682                         cqr->retries++;
2683                 }
2684         dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
2685         dasd_device_clear_timer(device);
2686         dasd_schedule_device_bh(device);
2687         return 1;
2688 }
2689 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
2690
2691 int dasd_generic_path_operational(struct dasd_device *device)
2692 {
2693         dev_info(&device->cdev->dev, "A channel path to the device has become "
2694                  "operational\n");
2695         DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
2696         dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
2697         if (device->stopped & DASD_UNRESUMED_PM) {
2698                 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
2699                 dasd_restore_device(device);
2700                 return 1;
2701         }
2702         dasd_schedule_device_bh(device);
2703         if (device->block)
2704                 dasd_schedule_block_bh(device->block);
2705         return 1;
2706 }
2707 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
2708
2709 int dasd_generic_notify(struct ccw_device *cdev, int event)
2710 {
2711         struct dasd_device *device;
2712         int ret;
2713
2714         device = dasd_device_from_cdev_locked(cdev);
2715         if (IS_ERR(device))
2716                 return 0;
2717         ret = 0;
2718         switch (event) {
2719         case CIO_GONE:
2720         case CIO_BOXED:
2721         case CIO_NO_PATH:
2722                 device->path_data.opm = 0;
2723                 device->path_data.ppm = 0;
2724                 device->path_data.npm = 0;
2725                 ret = dasd_generic_last_path_gone(device);
2726                 break;
2727         case CIO_OPER:
2728                 ret = 1;
2729                 if (device->path_data.opm)
2730                         ret = dasd_generic_path_operational(device);
2731                 break;
2732         }
2733         dasd_put_device(device);
2734         return ret;
2735 }
2736
2737 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
2738 {
2739         int chp;
2740         __u8 oldopm, eventlpm;
2741         struct dasd_device *device;
2742
2743         device = dasd_device_from_cdev_locked(cdev);
2744         if (IS_ERR(device))
2745                 return;
2746         for (chp = 0; chp < 8; chp++) {
2747                 eventlpm = 0x80 >> chp;
2748                 if (path_event[chp] & PE_PATH_GONE) {
2749                         oldopm = device->path_data.opm;
2750                         device->path_data.opm &= ~eventlpm;
2751                         device->path_data.ppm &= ~eventlpm;
2752                         device->path_data.npm &= ~eventlpm;
2753                         if (oldopm && !device->path_data.opm)
2754                                 dasd_generic_last_path_gone(device);
2755                 }
2756                 if (path_event[chp] & PE_PATH_AVAILABLE) {
2757                         device->path_data.opm &= ~eventlpm;
2758                         device->path_data.ppm &= ~eventlpm;
2759                         device->path_data.npm &= ~eventlpm;
2760                         device->path_data.tbvpm |= eventlpm;
2761                         dasd_schedule_device_bh(device);
2762                 }
2763         }
2764         dasd_put_device(device);
2765 }
2766 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
2767
2768 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
2769 {
2770         if (!device->path_data.opm && lpm) {
2771                 device->path_data.opm = lpm;
2772                 dasd_generic_path_operational(device);
2773         } else
2774                 device->path_data.opm |= lpm;
2775         return 0;
2776 }
2777 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
2778
2779
2780 int dasd_generic_pm_freeze(struct ccw_device *cdev)
2781 {
2782         struct dasd_ccw_req *cqr, *n;
2783         int rc;
2784         struct list_head freeze_queue;
2785         struct dasd_device *device = dasd_device_from_cdev(cdev);
2786
2787         if (IS_ERR(device))
2788                 return PTR_ERR(device);
2789
2790         if (device->discipline->freeze)
2791                 rc = device->discipline->freeze(device);
2792
2793         /* disallow new I/O  */
2794         dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
2795         /* clear active requests */
2796         INIT_LIST_HEAD(&freeze_queue);
2797         spin_lock_irq(get_ccwdev_lock(cdev));
2798         rc = 0;
2799         list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2800                 /* Check status and move request to flush_queue */
2801                 if (cqr->status == DASD_CQR_IN_IO) {
2802                         rc = device->discipline->term_IO(cqr);
2803                         if (rc) {
2804                                 /* unable to terminate requeust */
2805                                 dev_err(&device->cdev->dev,
2806                                         "Unable to terminate request %p "
2807                                         "on suspend\n", cqr);
2808                                 spin_unlock_irq(get_ccwdev_lock(cdev));
2809                                 dasd_put_device(device);
2810                                 return rc;
2811                         }
2812                 }
2813                 list_move_tail(&cqr->devlist, &freeze_queue);
2814         }
2815
2816         spin_unlock_irq(get_ccwdev_lock(cdev));
2817
2818         list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
2819                 wait_event(dasd_flush_wq,
2820                            (cqr->status != DASD_CQR_CLEAR_PENDING));
2821                 if (cqr->status == DASD_CQR_CLEARED)
2822                         cqr->status = DASD_CQR_QUEUED;
2823         }
2824         /* move freeze_queue to start of the ccw_queue */
2825         spin_lock_irq(get_ccwdev_lock(cdev));
2826         list_splice_tail(&freeze_queue, &device->ccw_queue);
2827         spin_unlock_irq(get_ccwdev_lock(cdev));
2828
2829         dasd_put_device(device);
2830         return rc;
2831 }
2832 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
2833
2834 int dasd_generic_restore_device(struct ccw_device *cdev)
2835 {
2836         struct dasd_device *device = dasd_device_from_cdev(cdev);
2837         int rc = 0;
2838
2839         if (IS_ERR(device))
2840                 return PTR_ERR(device);
2841
2842         /* allow new IO again */
2843         dasd_device_remove_stop_bits(device,
2844                                      (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
2845
2846         dasd_schedule_device_bh(device);
2847
2848         /*
2849          * call discipline restore function
2850          * if device is stopped do nothing e.g. for disconnected devices
2851          */
2852         if (device->discipline->restore && !(device->stopped))
2853                 rc = device->discipline->restore(device);
2854         if (rc || device->stopped)
2855                 /*
2856                  * if the resume failed for the DASD we put it in
2857                  * an UNRESUMED stop state
2858                  */
2859                 device->stopped |= DASD_UNRESUMED_PM;
2860
2861         if (device->block)
2862                 dasd_schedule_block_bh(device->block);
2863
2864         dasd_put_device(device);
2865         return 0;
2866 }
2867 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
2868
2869 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2870                                                    void *rdc_buffer,
2871                                                    int rdc_buffer_size,
2872                                                    int magic)
2873 {
2874         struct dasd_ccw_req *cqr;
2875         struct ccw1 *ccw;
2876         unsigned long *idaw;
2877
2878         cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2879
2880         if (IS_ERR(cqr)) {
2881                 /* internal error 13 - Allocating the RDC request failed*/
2882                 dev_err(&device->cdev->dev,
2883                          "An error occurred in the DASD device driver, "
2884                          "reason=%s\n", "13");
2885                 return cqr;
2886         }
2887
2888         ccw = cqr->cpaddr;
2889         ccw->cmd_code = CCW_CMD_RDC;
2890         if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
2891                 idaw = (unsigned long *) (cqr->data);
2892                 ccw->cda = (__u32)(addr_t) idaw;
2893                 ccw->flags = CCW_FLAG_IDA;
2894                 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
2895         } else {
2896                 ccw->cda = (__u32)(addr_t) rdc_buffer;
2897                 ccw->flags = 0;
2898         }
2899
2900         ccw->count = rdc_buffer_size;
2901         cqr->startdev = device;
2902         cqr->memdev = device;
2903         cqr->expires = 10*HZ;
2904         cqr->retries = 256;
2905         cqr->buildclk = get_clock();
2906         cqr->status = DASD_CQR_FILLED;
2907         return cqr;
2908 }
2909
2910
2911 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
2912                                 void *rdc_buffer, int rdc_buffer_size)
2913 {
2914         int ret;
2915         struct dasd_ccw_req *cqr;
2916
2917         cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
2918                                      magic);
2919         if (IS_ERR(cqr))
2920                 return PTR_ERR(cqr);
2921
2922         ret = dasd_sleep_on(cqr);
2923         dasd_sfree_request(cqr, cqr->memdev);
2924         return ret;
2925 }
2926 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
2927
2928 /*
2929  *   In command mode and transport mode we need to look for sense
2930  *   data in different places. The sense data itself is allways
2931  *   an array of 32 bytes, so we can unify the sense data access
2932  *   for both modes.
2933  */
2934 char *dasd_get_sense(struct irb *irb)
2935 {
2936         struct tsb *tsb = NULL;
2937         char *sense = NULL;
2938
2939         if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2940                 if (irb->scsw.tm.tcw)
2941                         tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2942                                           irb->scsw.tm.tcw);
2943                 if (tsb && tsb->length == 64 && tsb->flags)
2944                         switch (tsb->flags & 0x07) {
2945                         case 1: /* tsa_iostat */
2946                                 sense = tsb->tsa.iostat.sense;
2947                                 break;
2948                         case 2: /* tsa_ddpc */
2949                                 sense = tsb->tsa.ddpc.sense;
2950                                 break;
2951                         default:
2952                                 /* currently we don't use interrogate data */
2953                                 break;
2954                         }
2955         } else if (irb->esw.esw0.erw.cons) {
2956                 sense = irb->ecw;
2957         }
2958         return sense;
2959 }
2960 EXPORT_SYMBOL_GPL(dasd_get_sense);
2961
2962 static int __init dasd_init(void)
2963 {
2964         int rc;
2965
2966         init_waitqueue_head(&dasd_init_waitq);
2967         init_waitqueue_head(&dasd_flush_wq);
2968         init_waitqueue_head(&generic_waitq);
2969
2970         /* register 'common' DASD debug area, used for all DBF_XXX calls */
2971         dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
2972         if (dasd_debug_area == NULL) {
2973                 rc = -ENOMEM;
2974                 goto failed;
2975         }
2976         debug_register_view(dasd_debug_area, &debug_sprintf_view);
2977         debug_set_level(dasd_debug_area, DBF_WARNING);
2978
2979         DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2980
2981         dasd_diag_discipline_pointer = NULL;
2982
2983         rc = dasd_devmap_init();
2984         if (rc)
2985                 goto failed;
2986         rc = dasd_gendisk_init();
2987         if (rc)
2988                 goto failed;
2989         rc = dasd_parse();
2990         if (rc)
2991                 goto failed;
2992         rc = dasd_eer_init();
2993         if (rc)
2994                 goto failed;
2995 #ifdef CONFIG_PROC_FS
2996         rc = dasd_proc_init();
2997         if (rc)
2998                 goto failed;
2999 #endif
3000
3001         return 0;
3002 failed:
3003         pr_info("The DASD device driver could not be initialized\n");
3004         dasd_exit();
3005         return rc;
3006 }
3007
3008 module_init(dasd_init);
3009 module_exit(dasd_exit);
3010
3011 EXPORT_SYMBOL(dasd_debug_area);
3012 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
3013
3014 EXPORT_SYMBOL(dasd_add_request_head);
3015 EXPORT_SYMBOL(dasd_add_request_tail);
3016 EXPORT_SYMBOL(dasd_cancel_req);
3017 EXPORT_SYMBOL(dasd_device_clear_timer);
3018 EXPORT_SYMBOL(dasd_block_clear_timer);
3019 EXPORT_SYMBOL(dasd_enable_device);
3020 EXPORT_SYMBOL(dasd_int_handler);
3021 EXPORT_SYMBOL(dasd_kfree_request);
3022 EXPORT_SYMBOL(dasd_kick_device);
3023 EXPORT_SYMBOL(dasd_kmalloc_request);
3024 EXPORT_SYMBOL(dasd_schedule_device_bh);
3025 EXPORT_SYMBOL(dasd_schedule_block_bh);
3026 EXPORT_SYMBOL(dasd_set_target_state);
3027 EXPORT_SYMBOL(dasd_device_set_timer);
3028 EXPORT_SYMBOL(dasd_block_set_timer);
3029 EXPORT_SYMBOL(dasd_sfree_request);
3030 EXPORT_SYMBOL(dasd_sleep_on);
3031 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
3032 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
3033 EXPORT_SYMBOL(dasd_smalloc_request);
3034 EXPORT_SYMBOL(dasd_start_IO);
3035 EXPORT_SYMBOL(dasd_term_IO);
3036
3037 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3038 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3039 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3040 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3041 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3042 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
3043 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
3044 EXPORT_SYMBOL_GPL(dasd_alloc_block);
3045 EXPORT_SYMBOL_GPL(dasd_free_block);