ASoC: fsl: Add S/PDIF CPU DAI driver
[deliverable/linux.git] / drivers / s390 / block / dasd.c
1 /*
2 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3 * Horst Hummel <Horst.Hummel@de.ibm.com>
4 * Carsten Otte <Cotte@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
7 * Copyright IBM Corp. 1999, 2009
8 */
9
10 #define KMSG_COMPONENT "dasd"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
13 #include <linux/kmod.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/ctype.h>
17 #include <linux/major.h>
18 #include <linux/slab.h>
19 #include <linux/hdreg.h>
20 #include <linux/async.h>
21 #include <linux/mutex.h>
22 #include <linux/debugfs.h>
23 #include <linux/seq_file.h>
24 #include <linux/vmalloc.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 /*
42 * SECTION: exported variables of dasd.c
43 */
44 debug_info_t *dasd_debug_area;
45 static struct dentry *dasd_debugfs_root_entry;
46 struct dasd_discipline *dasd_diag_discipline_pointer;
47 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
48
49 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
50 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
51 " Copyright IBM Corp. 2000");
52 MODULE_SUPPORTED_DEVICE("dasd");
53 MODULE_LICENSE("GPL");
54
55 /*
56 * SECTION: prototypes for static functions of dasd.c
57 */
58 static int dasd_alloc_queue(struct dasd_block *);
59 static void dasd_setup_queue(struct dasd_block *);
60 static void dasd_free_queue(struct dasd_block *);
61 static void dasd_flush_request_queue(struct dasd_block *);
62 static int dasd_flush_block_queue(struct dasd_block *);
63 static void dasd_device_tasklet(struct dasd_device *);
64 static void dasd_block_tasklet(struct dasd_block *);
65 static void do_kick_device(struct work_struct *);
66 static void do_restore_device(struct work_struct *);
67 static void do_reload_device(struct work_struct *);
68 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
69 static void dasd_device_timeout(unsigned long);
70 static void dasd_block_timeout(unsigned long);
71 static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
72 static void dasd_profile_init(struct dasd_profile *, struct dentry *);
73 static void dasd_profile_exit(struct dasd_profile *);
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 static wait_queue_head_t shutdown_waitq;
82
83 /*
84 * Allocate memory for a new device structure.
85 */
86 struct dasd_device *dasd_alloc_device(void)
87 {
88 struct dasd_device *device;
89
90 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
91 if (!device)
92 return ERR_PTR(-ENOMEM);
93
94 /* Get two pages for normal block device operations. */
95 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
96 if (!device->ccw_mem) {
97 kfree(device);
98 return ERR_PTR(-ENOMEM);
99 }
100 /* Get one page for error recovery. */
101 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
102 if (!device->erp_mem) {
103 free_pages((unsigned long) device->ccw_mem, 1);
104 kfree(device);
105 return ERR_PTR(-ENOMEM);
106 }
107
108 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
109 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
110 spin_lock_init(&device->mem_lock);
111 atomic_set(&device->tasklet_scheduled, 0);
112 tasklet_init(&device->tasklet,
113 (void (*)(unsigned long)) dasd_device_tasklet,
114 (unsigned long) device);
115 INIT_LIST_HEAD(&device->ccw_queue);
116 init_timer(&device->timer);
117 device->timer.function = dasd_device_timeout;
118 device->timer.data = (unsigned long) device;
119 INIT_WORK(&device->kick_work, do_kick_device);
120 INIT_WORK(&device->restore_device, do_restore_device);
121 INIT_WORK(&device->reload_device, do_reload_device);
122 device->state = DASD_STATE_NEW;
123 device->target = DASD_STATE_NEW;
124 mutex_init(&device->state_mutex);
125 spin_lock_init(&device->profile.lock);
126 return device;
127 }
128
129 /*
130 * Free memory of a device structure.
131 */
132 void dasd_free_device(struct dasd_device *device)
133 {
134 kfree(device->private);
135 free_page((unsigned long) device->erp_mem);
136 free_pages((unsigned long) device->ccw_mem, 1);
137 kfree(device);
138 }
139
140 /*
141 * Allocate memory for a new device structure.
142 */
143 struct dasd_block *dasd_alloc_block(void)
144 {
145 struct dasd_block *block;
146
147 block = kzalloc(sizeof(*block), GFP_ATOMIC);
148 if (!block)
149 return ERR_PTR(-ENOMEM);
150 /* open_count = 0 means device online but not in use */
151 atomic_set(&block->open_count, -1);
152
153 spin_lock_init(&block->request_queue_lock);
154 atomic_set(&block->tasklet_scheduled, 0);
155 tasklet_init(&block->tasklet,
156 (void (*)(unsigned long)) dasd_block_tasklet,
157 (unsigned long) block);
158 INIT_LIST_HEAD(&block->ccw_queue);
159 spin_lock_init(&block->queue_lock);
160 init_timer(&block->timer);
161 block->timer.function = dasd_block_timeout;
162 block->timer.data = (unsigned long) block;
163 spin_lock_init(&block->profile.lock);
164
165 return block;
166 }
167
168 /*
169 * Free memory of a device structure.
170 */
171 void dasd_free_block(struct dasd_block *block)
172 {
173 kfree(block);
174 }
175
176 /*
177 * Make a new device known to the system.
178 */
179 static int dasd_state_new_to_known(struct dasd_device *device)
180 {
181 int rc;
182
183 /*
184 * As long as the device is not in state DASD_STATE_NEW we want to
185 * keep the reference count > 0.
186 */
187 dasd_get_device(device);
188
189 if (device->block) {
190 rc = dasd_alloc_queue(device->block);
191 if (rc) {
192 dasd_put_device(device);
193 return rc;
194 }
195 }
196 device->state = DASD_STATE_KNOWN;
197 return 0;
198 }
199
200 /*
201 * Let the system forget about a device.
202 */
203 static int dasd_state_known_to_new(struct dasd_device *device)
204 {
205 /* Disable extended error reporting for this device. */
206 dasd_eer_disable(device);
207 /* Forget the discipline information. */
208 if (device->discipline) {
209 if (device->discipline->uncheck_device)
210 device->discipline->uncheck_device(device);
211 module_put(device->discipline->owner);
212 }
213 device->discipline = NULL;
214 if (device->base_discipline)
215 module_put(device->base_discipline->owner);
216 device->base_discipline = NULL;
217 device->state = DASD_STATE_NEW;
218
219 if (device->block)
220 dasd_free_queue(device->block);
221
222 /* Give up reference we took in dasd_state_new_to_known. */
223 dasd_put_device(device);
224 return 0;
225 }
226
227 static struct dentry *dasd_debugfs_setup(const char *name,
228 struct dentry *base_dentry)
229 {
230 struct dentry *pde;
231
232 if (!base_dentry)
233 return NULL;
234 pde = debugfs_create_dir(name, base_dentry);
235 if (!pde || IS_ERR(pde))
236 return NULL;
237 return pde;
238 }
239
240 /*
241 * Request the irq line for the device.
242 */
243 static int dasd_state_known_to_basic(struct dasd_device *device)
244 {
245 struct dasd_block *block = device->block;
246 int rc = 0;
247
248 /* Allocate and register gendisk structure. */
249 if (block) {
250 rc = dasd_gendisk_alloc(block);
251 if (rc)
252 return rc;
253 block->debugfs_dentry =
254 dasd_debugfs_setup(block->gdp->disk_name,
255 dasd_debugfs_root_entry);
256 dasd_profile_init(&block->profile, block->debugfs_dentry);
257 if (dasd_global_profile_level == DASD_PROFILE_ON)
258 dasd_profile_on(&device->block->profile);
259 }
260 device->debugfs_dentry =
261 dasd_debugfs_setup(dev_name(&device->cdev->dev),
262 dasd_debugfs_root_entry);
263 dasd_profile_init(&device->profile, device->debugfs_dentry);
264
265 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
266 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
267 8 * sizeof(long));
268 debug_register_view(device->debug_area, &debug_sprintf_view);
269 debug_set_level(device->debug_area, DBF_WARNING);
270 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
271
272 device->state = DASD_STATE_BASIC;
273
274 return rc;
275 }
276
277 /*
278 * Release the irq line for the device. Terminate any running i/o.
279 */
280 static int dasd_state_basic_to_known(struct dasd_device *device)
281 {
282 int rc;
283
284 if (device->block) {
285 dasd_profile_exit(&device->block->profile);
286 if (device->block->debugfs_dentry)
287 debugfs_remove(device->block->debugfs_dentry);
288 dasd_gendisk_free(device->block);
289 dasd_block_clear_timer(device->block);
290 }
291 rc = dasd_flush_device_queue(device);
292 if (rc)
293 return rc;
294 dasd_device_clear_timer(device);
295 dasd_profile_exit(&device->profile);
296 if (device->debugfs_dentry)
297 debugfs_remove(device->debugfs_dentry);
298
299 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
300 if (device->debug_area != NULL) {
301 debug_unregister(device->debug_area);
302 device->debug_area = NULL;
303 }
304 device->state = DASD_STATE_KNOWN;
305 return 0;
306 }
307
308 /*
309 * Do the initial analysis. The do_analysis function may return
310 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
311 * until the discipline decides to continue the startup sequence
312 * by calling the function dasd_change_state. The eckd disciplines
313 * uses this to start a ccw that detects the format. The completion
314 * interrupt for this detection ccw uses the kernel event daemon to
315 * trigger the call to dasd_change_state. All this is done in the
316 * discipline code, see dasd_eckd.c.
317 * After the analysis ccw is done (do_analysis returned 0) the block
318 * device is setup.
319 * In case the analysis returns an error, the device setup is stopped
320 * (a fake disk was already added to allow formatting).
321 */
322 static int dasd_state_basic_to_ready(struct dasd_device *device)
323 {
324 int rc;
325 struct dasd_block *block;
326
327 rc = 0;
328 block = device->block;
329 /* make disk known with correct capacity */
330 if (block) {
331 if (block->base->discipline->do_analysis != NULL)
332 rc = block->base->discipline->do_analysis(block);
333 if (rc) {
334 if (rc != -EAGAIN) {
335 device->state = DASD_STATE_UNFMT;
336 goto out;
337 }
338 return rc;
339 }
340 dasd_setup_queue(block);
341 set_capacity(block->gdp,
342 block->blocks << block->s2b_shift);
343 device->state = DASD_STATE_READY;
344 rc = dasd_scan_partitions(block);
345 if (rc) {
346 device->state = DASD_STATE_BASIC;
347 return rc;
348 }
349 } else {
350 device->state = DASD_STATE_READY;
351 }
352 out:
353 if (device->discipline->basic_to_ready)
354 rc = device->discipline->basic_to_ready(device);
355 return rc;
356 }
357
358 static inline
359 int _wait_for_empty_queues(struct dasd_device *device)
360 {
361 if (device->block)
362 return list_empty(&device->ccw_queue) &&
363 list_empty(&device->block->ccw_queue);
364 else
365 return list_empty(&device->ccw_queue);
366 }
367
368 /*
369 * Remove device from block device layer. Destroy dirty buffers.
370 * Forget format information. Check if the target level is basic
371 * and if it is create fake disk for formatting.
372 */
373 static int dasd_state_ready_to_basic(struct dasd_device *device)
374 {
375 int rc;
376
377 if (device->discipline->ready_to_basic) {
378 rc = device->discipline->ready_to_basic(device);
379 if (rc)
380 return rc;
381 }
382 device->state = DASD_STATE_BASIC;
383 if (device->block) {
384 struct dasd_block *block = device->block;
385 rc = dasd_flush_block_queue(block);
386 if (rc) {
387 device->state = DASD_STATE_READY;
388 return rc;
389 }
390 dasd_flush_request_queue(block);
391 dasd_destroy_partitions(block);
392 block->blocks = 0;
393 block->bp_block = 0;
394 block->s2b_shift = 0;
395 }
396 return 0;
397 }
398
399 /*
400 * Back to basic.
401 */
402 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
403 {
404 device->state = DASD_STATE_BASIC;
405 return 0;
406 }
407
408 /*
409 * Make the device online and schedule the bottom half to start
410 * the requeueing of requests from the linux request queue to the
411 * ccw queue.
412 */
413 static int
414 dasd_state_ready_to_online(struct dasd_device * device)
415 {
416 struct gendisk *disk;
417 struct disk_part_iter piter;
418 struct hd_struct *part;
419
420 device->state = DASD_STATE_ONLINE;
421 if (device->block) {
422 dasd_schedule_block_bh(device->block);
423 if ((device->features & DASD_FEATURE_USERAW)) {
424 disk = device->block->gdp;
425 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
426 return 0;
427 }
428 disk = device->block->bdev->bd_disk;
429 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
430 while ((part = disk_part_iter_next(&piter)))
431 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
432 disk_part_iter_exit(&piter);
433 }
434 return 0;
435 }
436
437 /*
438 * Stop the requeueing of requests again.
439 */
440 static int dasd_state_online_to_ready(struct dasd_device *device)
441 {
442 int rc;
443 struct gendisk *disk;
444 struct disk_part_iter piter;
445 struct hd_struct *part;
446
447 if (device->discipline->online_to_ready) {
448 rc = device->discipline->online_to_ready(device);
449 if (rc)
450 return rc;
451 }
452
453 device->state = DASD_STATE_READY;
454 if (device->block && !(device->features & DASD_FEATURE_USERAW)) {
455 disk = device->block->bdev->bd_disk;
456 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
457 while ((part = disk_part_iter_next(&piter)))
458 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
459 disk_part_iter_exit(&piter);
460 }
461 return 0;
462 }
463
464 /*
465 * Device startup state changes.
466 */
467 static int dasd_increase_state(struct dasd_device *device)
468 {
469 int rc;
470
471 rc = 0;
472 if (device->state == DASD_STATE_NEW &&
473 device->target >= DASD_STATE_KNOWN)
474 rc = dasd_state_new_to_known(device);
475
476 if (!rc &&
477 device->state == DASD_STATE_KNOWN &&
478 device->target >= DASD_STATE_BASIC)
479 rc = dasd_state_known_to_basic(device);
480
481 if (!rc &&
482 device->state == DASD_STATE_BASIC &&
483 device->target >= DASD_STATE_READY)
484 rc = dasd_state_basic_to_ready(device);
485
486 if (!rc &&
487 device->state == DASD_STATE_UNFMT &&
488 device->target > DASD_STATE_UNFMT)
489 rc = -EPERM;
490
491 if (!rc &&
492 device->state == DASD_STATE_READY &&
493 device->target >= DASD_STATE_ONLINE)
494 rc = dasd_state_ready_to_online(device);
495
496 return rc;
497 }
498
499 /*
500 * Device shutdown state changes.
501 */
502 static int dasd_decrease_state(struct dasd_device *device)
503 {
504 int rc;
505
506 rc = 0;
507 if (device->state == DASD_STATE_ONLINE &&
508 device->target <= DASD_STATE_READY)
509 rc = dasd_state_online_to_ready(device);
510
511 if (!rc &&
512 device->state == DASD_STATE_READY &&
513 device->target <= DASD_STATE_BASIC)
514 rc = dasd_state_ready_to_basic(device);
515
516 if (!rc &&
517 device->state == DASD_STATE_UNFMT &&
518 device->target <= DASD_STATE_BASIC)
519 rc = dasd_state_unfmt_to_basic(device);
520
521 if (!rc &&
522 device->state == DASD_STATE_BASIC &&
523 device->target <= DASD_STATE_KNOWN)
524 rc = dasd_state_basic_to_known(device);
525
526 if (!rc &&
527 device->state == DASD_STATE_KNOWN &&
528 device->target <= DASD_STATE_NEW)
529 rc = dasd_state_known_to_new(device);
530
531 return rc;
532 }
533
534 /*
535 * This is the main startup/shutdown routine.
536 */
537 static void dasd_change_state(struct dasd_device *device)
538 {
539 int rc;
540
541 if (device->state == device->target)
542 /* Already where we want to go today... */
543 return;
544 if (device->state < device->target)
545 rc = dasd_increase_state(device);
546 else
547 rc = dasd_decrease_state(device);
548 if (rc == -EAGAIN)
549 return;
550 if (rc)
551 device->target = device->state;
552
553 /* let user-space know that the device status changed */
554 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
555
556 if (device->state == device->target)
557 wake_up(&dasd_init_waitq);
558 }
559
560 /*
561 * Kick starter for devices that did not complete the startup/shutdown
562 * procedure or were sleeping because of a pending state.
563 * dasd_kick_device will schedule a call do do_kick_device to the kernel
564 * event daemon.
565 */
566 static void do_kick_device(struct work_struct *work)
567 {
568 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
569 mutex_lock(&device->state_mutex);
570 dasd_change_state(device);
571 mutex_unlock(&device->state_mutex);
572 dasd_schedule_device_bh(device);
573 dasd_put_device(device);
574 }
575
576 void dasd_kick_device(struct dasd_device *device)
577 {
578 dasd_get_device(device);
579 /* queue call to dasd_kick_device to the kernel event daemon. */
580 schedule_work(&device->kick_work);
581 }
582
583 /*
584 * dasd_reload_device will schedule a call do do_reload_device to the kernel
585 * event daemon.
586 */
587 static void do_reload_device(struct work_struct *work)
588 {
589 struct dasd_device *device = container_of(work, struct dasd_device,
590 reload_device);
591 device->discipline->reload(device);
592 dasd_put_device(device);
593 }
594
595 void dasd_reload_device(struct dasd_device *device)
596 {
597 dasd_get_device(device);
598 /* queue call to dasd_reload_device to the kernel event daemon. */
599 schedule_work(&device->reload_device);
600 }
601 EXPORT_SYMBOL(dasd_reload_device);
602
603 /*
604 * dasd_restore_device will schedule a call do do_restore_device to the kernel
605 * event daemon.
606 */
607 static void do_restore_device(struct work_struct *work)
608 {
609 struct dasd_device *device = container_of(work, struct dasd_device,
610 restore_device);
611 device->cdev->drv->restore(device->cdev);
612 dasd_put_device(device);
613 }
614
615 void dasd_restore_device(struct dasd_device *device)
616 {
617 dasd_get_device(device);
618 /* queue call to dasd_restore_device to the kernel event daemon. */
619 schedule_work(&device->restore_device);
620 }
621
622 /*
623 * Set the target state for a device and starts the state change.
624 */
625 void dasd_set_target_state(struct dasd_device *device, int target)
626 {
627 dasd_get_device(device);
628 mutex_lock(&device->state_mutex);
629 /* If we are in probeonly mode stop at DASD_STATE_READY. */
630 if (dasd_probeonly && target > DASD_STATE_READY)
631 target = DASD_STATE_READY;
632 if (device->target != target) {
633 if (device->state == target)
634 wake_up(&dasd_init_waitq);
635 device->target = target;
636 }
637 if (device->state != device->target)
638 dasd_change_state(device);
639 mutex_unlock(&device->state_mutex);
640 dasd_put_device(device);
641 }
642
643 /*
644 * Enable devices with device numbers in [from..to].
645 */
646 static inline int _wait_for_device(struct dasd_device *device)
647 {
648 return (device->state == device->target);
649 }
650
651 void dasd_enable_device(struct dasd_device *device)
652 {
653 dasd_set_target_state(device, DASD_STATE_ONLINE);
654 if (device->state <= DASD_STATE_KNOWN)
655 /* No discipline for device found. */
656 dasd_set_target_state(device, DASD_STATE_NEW);
657 /* Now wait for the devices to come up. */
658 wait_event(dasd_init_waitq, _wait_for_device(device));
659
660 dasd_reload_device(device);
661 if (device->discipline->kick_validate)
662 device->discipline->kick_validate(device);
663 }
664
665 /*
666 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
667 */
668
669 unsigned int dasd_global_profile_level = DASD_PROFILE_OFF;
670
671 #ifdef CONFIG_DASD_PROFILE
672 struct dasd_profile_info dasd_global_profile_data;
673 static struct dentry *dasd_global_profile_dentry;
674 static struct dentry *dasd_debugfs_global_entry;
675
676 /*
677 * Add profiling information for cqr before execution.
678 */
679 static void dasd_profile_start(struct dasd_block *block,
680 struct dasd_ccw_req *cqr,
681 struct request *req)
682 {
683 struct list_head *l;
684 unsigned int counter;
685 struct dasd_device *device;
686
687 /* count the length of the chanq for statistics */
688 counter = 0;
689 if (dasd_global_profile_level || block->profile.data)
690 list_for_each(l, &block->ccw_queue)
691 if (++counter >= 31)
692 break;
693
694 if (dasd_global_profile_level) {
695 dasd_global_profile_data.dasd_io_nr_req[counter]++;
696 if (rq_data_dir(req) == READ)
697 dasd_global_profile_data.dasd_read_nr_req[counter]++;
698 }
699
700 spin_lock(&block->profile.lock);
701 if (block->profile.data)
702 block->profile.data->dasd_io_nr_req[counter]++;
703 if (rq_data_dir(req) == READ)
704 block->profile.data->dasd_read_nr_req[counter]++;
705 spin_unlock(&block->profile.lock);
706
707 /*
708 * We count the request for the start device, even though it may run on
709 * some other device due to error recovery. This way we make sure that
710 * we count each request only once.
711 */
712 device = cqr->startdev;
713 if (device->profile.data) {
714 counter = 1; /* request is not yet queued on the start device */
715 list_for_each(l, &device->ccw_queue)
716 if (++counter >= 31)
717 break;
718 }
719 spin_lock(&device->profile.lock);
720 if (device->profile.data) {
721 device->profile.data->dasd_io_nr_req[counter]++;
722 if (rq_data_dir(req) == READ)
723 device->profile.data->dasd_read_nr_req[counter]++;
724 }
725 spin_unlock(&device->profile.lock);
726 }
727
728 /*
729 * Add profiling information for cqr after execution.
730 */
731
732 #define dasd_profile_counter(value, index) \
733 { \
734 for (index = 0; index < 31 && value >> (2+index); index++) \
735 ; \
736 }
737
738 static void dasd_profile_end_add_data(struct dasd_profile_info *data,
739 int is_alias,
740 int is_tpm,
741 int is_read,
742 long sectors,
743 int sectors_ind,
744 int tottime_ind,
745 int tottimeps_ind,
746 int strtime_ind,
747 int irqtime_ind,
748 int irqtimeps_ind,
749 int endtime_ind)
750 {
751 /* in case of an overflow, reset the whole profile */
752 if (data->dasd_io_reqs == UINT_MAX) {
753 memset(data, 0, sizeof(*data));
754 getnstimeofday(&data->starttod);
755 }
756 data->dasd_io_reqs++;
757 data->dasd_io_sects += sectors;
758 if (is_alias)
759 data->dasd_io_alias++;
760 if (is_tpm)
761 data->dasd_io_tpm++;
762
763 data->dasd_io_secs[sectors_ind]++;
764 data->dasd_io_times[tottime_ind]++;
765 data->dasd_io_timps[tottimeps_ind]++;
766 data->dasd_io_time1[strtime_ind]++;
767 data->dasd_io_time2[irqtime_ind]++;
768 data->dasd_io_time2ps[irqtimeps_ind]++;
769 data->dasd_io_time3[endtime_ind]++;
770
771 if (is_read) {
772 data->dasd_read_reqs++;
773 data->dasd_read_sects += sectors;
774 if (is_alias)
775 data->dasd_read_alias++;
776 if (is_tpm)
777 data->dasd_read_tpm++;
778 data->dasd_read_secs[sectors_ind]++;
779 data->dasd_read_times[tottime_ind]++;
780 data->dasd_read_time1[strtime_ind]++;
781 data->dasd_read_time2[irqtime_ind]++;
782 data->dasd_read_time3[endtime_ind]++;
783 }
784 }
785
786 static void dasd_profile_end(struct dasd_block *block,
787 struct dasd_ccw_req *cqr,
788 struct request *req)
789 {
790 long strtime, irqtime, endtime, tottime; /* in microseconds */
791 long tottimeps, sectors;
792 struct dasd_device *device;
793 int sectors_ind, tottime_ind, tottimeps_ind, strtime_ind;
794 int irqtime_ind, irqtimeps_ind, endtime_ind;
795
796 device = cqr->startdev;
797 if (!(dasd_global_profile_level ||
798 block->profile.data ||
799 device->profile.data))
800 return;
801
802 sectors = blk_rq_sectors(req);
803 if (!cqr->buildclk || !cqr->startclk ||
804 !cqr->stopclk || !cqr->endclk ||
805 !sectors)
806 return;
807
808 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
809 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
810 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
811 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
812 tottimeps = tottime / sectors;
813
814 dasd_profile_counter(sectors, sectors_ind);
815 dasd_profile_counter(tottime, tottime_ind);
816 dasd_profile_counter(tottimeps, tottimeps_ind);
817 dasd_profile_counter(strtime, strtime_ind);
818 dasd_profile_counter(irqtime, irqtime_ind);
819 dasd_profile_counter(irqtime / sectors, irqtimeps_ind);
820 dasd_profile_counter(endtime, endtime_ind);
821
822 if (dasd_global_profile_level) {
823 dasd_profile_end_add_data(&dasd_global_profile_data,
824 cqr->startdev != block->base,
825 cqr->cpmode == 1,
826 rq_data_dir(req) == READ,
827 sectors, sectors_ind, tottime_ind,
828 tottimeps_ind, strtime_ind,
829 irqtime_ind, irqtimeps_ind,
830 endtime_ind);
831 }
832
833 spin_lock(&block->profile.lock);
834 if (block->profile.data)
835 dasd_profile_end_add_data(block->profile.data,
836 cqr->startdev != block->base,
837 cqr->cpmode == 1,
838 rq_data_dir(req) == READ,
839 sectors, sectors_ind, tottime_ind,
840 tottimeps_ind, strtime_ind,
841 irqtime_ind, irqtimeps_ind,
842 endtime_ind);
843 spin_unlock(&block->profile.lock);
844
845 spin_lock(&device->profile.lock);
846 if (device->profile.data)
847 dasd_profile_end_add_data(device->profile.data,
848 cqr->startdev != block->base,
849 cqr->cpmode == 1,
850 rq_data_dir(req) == READ,
851 sectors, sectors_ind, tottime_ind,
852 tottimeps_ind, strtime_ind,
853 irqtime_ind, irqtimeps_ind,
854 endtime_ind);
855 spin_unlock(&device->profile.lock);
856 }
857
858 void dasd_profile_reset(struct dasd_profile *profile)
859 {
860 struct dasd_profile_info *data;
861
862 spin_lock_bh(&profile->lock);
863 data = profile->data;
864 if (!data) {
865 spin_unlock_bh(&profile->lock);
866 return;
867 }
868 memset(data, 0, sizeof(*data));
869 getnstimeofday(&data->starttod);
870 spin_unlock_bh(&profile->lock);
871 }
872
873 void dasd_global_profile_reset(void)
874 {
875 memset(&dasd_global_profile_data, 0, sizeof(dasd_global_profile_data));
876 getnstimeofday(&dasd_global_profile_data.starttod);
877 }
878
879 int dasd_profile_on(struct dasd_profile *profile)
880 {
881 struct dasd_profile_info *data;
882
883 data = kzalloc(sizeof(*data), GFP_KERNEL);
884 if (!data)
885 return -ENOMEM;
886 spin_lock_bh(&profile->lock);
887 if (profile->data) {
888 spin_unlock_bh(&profile->lock);
889 kfree(data);
890 return 0;
891 }
892 getnstimeofday(&data->starttod);
893 profile->data = data;
894 spin_unlock_bh(&profile->lock);
895 return 0;
896 }
897
898 void dasd_profile_off(struct dasd_profile *profile)
899 {
900 spin_lock_bh(&profile->lock);
901 kfree(profile->data);
902 profile->data = NULL;
903 spin_unlock_bh(&profile->lock);
904 }
905
906 char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
907 {
908 char *buffer;
909
910 buffer = vmalloc(user_len + 1);
911 if (buffer == NULL)
912 return ERR_PTR(-ENOMEM);
913 if (copy_from_user(buffer, user_buf, user_len) != 0) {
914 vfree(buffer);
915 return ERR_PTR(-EFAULT);
916 }
917 /* got the string, now strip linefeed. */
918 if (buffer[user_len - 1] == '\n')
919 buffer[user_len - 1] = 0;
920 else
921 buffer[user_len] = 0;
922 return buffer;
923 }
924
925 static ssize_t dasd_stats_write(struct file *file,
926 const char __user *user_buf,
927 size_t user_len, loff_t *pos)
928 {
929 char *buffer, *str;
930 int rc;
931 struct seq_file *m = (struct seq_file *)file->private_data;
932 struct dasd_profile *prof = m->private;
933
934 if (user_len > 65536)
935 user_len = 65536;
936 buffer = dasd_get_user_string(user_buf, user_len);
937 if (IS_ERR(buffer))
938 return PTR_ERR(buffer);
939
940 str = skip_spaces(buffer);
941 rc = user_len;
942 if (strncmp(str, "reset", 5) == 0) {
943 dasd_profile_reset(prof);
944 } else if (strncmp(str, "on", 2) == 0) {
945 rc = dasd_profile_on(prof);
946 if (!rc)
947 rc = user_len;
948 } else if (strncmp(str, "off", 3) == 0) {
949 dasd_profile_off(prof);
950 } else
951 rc = -EINVAL;
952 vfree(buffer);
953 return rc;
954 }
955
956 static void dasd_stats_array(struct seq_file *m, unsigned int *array)
957 {
958 int i;
959
960 for (i = 0; i < 32; i++)
961 seq_printf(m, "%u ", array[i]);
962 seq_putc(m, '\n');
963 }
964
965 static void dasd_stats_seq_print(struct seq_file *m,
966 struct dasd_profile_info *data)
967 {
968 seq_printf(m, "start_time %ld.%09ld\n",
969 data->starttod.tv_sec, data->starttod.tv_nsec);
970 seq_printf(m, "total_requests %u\n", data->dasd_io_reqs);
971 seq_printf(m, "total_sectors %u\n", data->dasd_io_sects);
972 seq_printf(m, "total_pav %u\n", data->dasd_io_alias);
973 seq_printf(m, "total_hpf %u\n", data->dasd_io_tpm);
974 seq_printf(m, "histogram_sectors ");
975 dasd_stats_array(m, data->dasd_io_secs);
976 seq_printf(m, "histogram_io_times ");
977 dasd_stats_array(m, data->dasd_io_times);
978 seq_printf(m, "histogram_io_times_weighted ");
979 dasd_stats_array(m, data->dasd_io_timps);
980 seq_printf(m, "histogram_time_build_to_ssch ");
981 dasd_stats_array(m, data->dasd_io_time1);
982 seq_printf(m, "histogram_time_ssch_to_irq ");
983 dasd_stats_array(m, data->dasd_io_time2);
984 seq_printf(m, "histogram_time_ssch_to_irq_weighted ");
985 dasd_stats_array(m, data->dasd_io_time2ps);
986 seq_printf(m, "histogram_time_irq_to_end ");
987 dasd_stats_array(m, data->dasd_io_time3);
988 seq_printf(m, "histogram_ccw_queue_length ");
989 dasd_stats_array(m, data->dasd_io_nr_req);
990 seq_printf(m, "total_read_requests %u\n", data->dasd_read_reqs);
991 seq_printf(m, "total_read_sectors %u\n", data->dasd_read_sects);
992 seq_printf(m, "total_read_pav %u\n", data->dasd_read_alias);
993 seq_printf(m, "total_read_hpf %u\n", data->dasd_read_tpm);
994 seq_printf(m, "histogram_read_sectors ");
995 dasd_stats_array(m, data->dasd_read_secs);
996 seq_printf(m, "histogram_read_times ");
997 dasd_stats_array(m, data->dasd_read_times);
998 seq_printf(m, "histogram_read_time_build_to_ssch ");
999 dasd_stats_array(m, data->dasd_read_time1);
1000 seq_printf(m, "histogram_read_time_ssch_to_irq ");
1001 dasd_stats_array(m, data->dasd_read_time2);
1002 seq_printf(m, "histogram_read_time_irq_to_end ");
1003 dasd_stats_array(m, data->dasd_read_time3);
1004 seq_printf(m, "histogram_read_ccw_queue_length ");
1005 dasd_stats_array(m, data->dasd_read_nr_req);
1006 }
1007
1008 static int dasd_stats_show(struct seq_file *m, void *v)
1009 {
1010 struct dasd_profile *profile;
1011 struct dasd_profile_info *data;
1012
1013 profile = m->private;
1014 spin_lock_bh(&profile->lock);
1015 data = profile->data;
1016 if (!data) {
1017 spin_unlock_bh(&profile->lock);
1018 seq_printf(m, "disabled\n");
1019 return 0;
1020 }
1021 dasd_stats_seq_print(m, data);
1022 spin_unlock_bh(&profile->lock);
1023 return 0;
1024 }
1025
1026 static int dasd_stats_open(struct inode *inode, struct file *file)
1027 {
1028 struct dasd_profile *profile = inode->i_private;
1029 return single_open(file, dasd_stats_show, profile);
1030 }
1031
1032 static const struct file_operations dasd_stats_raw_fops = {
1033 .owner = THIS_MODULE,
1034 .open = dasd_stats_open,
1035 .read = seq_read,
1036 .llseek = seq_lseek,
1037 .release = single_release,
1038 .write = dasd_stats_write,
1039 };
1040
1041 static ssize_t dasd_stats_global_write(struct file *file,
1042 const char __user *user_buf,
1043 size_t user_len, loff_t *pos)
1044 {
1045 char *buffer, *str;
1046 ssize_t rc;
1047
1048 if (user_len > 65536)
1049 user_len = 65536;
1050 buffer = dasd_get_user_string(user_buf, user_len);
1051 if (IS_ERR(buffer))
1052 return PTR_ERR(buffer);
1053 str = skip_spaces(buffer);
1054 rc = user_len;
1055 if (strncmp(str, "reset", 5) == 0) {
1056 dasd_global_profile_reset();
1057 } else if (strncmp(str, "on", 2) == 0) {
1058 dasd_global_profile_reset();
1059 dasd_global_profile_level = DASD_PROFILE_GLOBAL_ONLY;
1060 } else if (strncmp(str, "off", 3) == 0) {
1061 dasd_global_profile_level = DASD_PROFILE_OFF;
1062 } else
1063 rc = -EINVAL;
1064 vfree(buffer);
1065 return rc;
1066 }
1067
1068 static int dasd_stats_global_show(struct seq_file *m, void *v)
1069 {
1070 if (!dasd_global_profile_level) {
1071 seq_printf(m, "disabled\n");
1072 return 0;
1073 }
1074 dasd_stats_seq_print(m, &dasd_global_profile_data);
1075 return 0;
1076 }
1077
1078 static int dasd_stats_global_open(struct inode *inode, struct file *file)
1079 {
1080 return single_open(file, dasd_stats_global_show, NULL);
1081 }
1082
1083 static const struct file_operations dasd_stats_global_fops = {
1084 .owner = THIS_MODULE,
1085 .open = dasd_stats_global_open,
1086 .read = seq_read,
1087 .llseek = seq_lseek,
1088 .release = single_release,
1089 .write = dasd_stats_global_write,
1090 };
1091
1092 static void dasd_profile_init(struct dasd_profile *profile,
1093 struct dentry *base_dentry)
1094 {
1095 umode_t mode;
1096 struct dentry *pde;
1097
1098 if (!base_dentry)
1099 return;
1100 profile->dentry = NULL;
1101 profile->data = NULL;
1102 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1103 pde = debugfs_create_file("statistics", mode, base_dentry,
1104 profile, &dasd_stats_raw_fops);
1105 if (pde && !IS_ERR(pde))
1106 profile->dentry = pde;
1107 return;
1108 }
1109
1110 static void dasd_profile_exit(struct dasd_profile *profile)
1111 {
1112 dasd_profile_off(profile);
1113 if (profile->dentry) {
1114 debugfs_remove(profile->dentry);
1115 profile->dentry = NULL;
1116 }
1117 }
1118
1119 static void dasd_statistics_removeroot(void)
1120 {
1121 dasd_global_profile_level = DASD_PROFILE_OFF;
1122 if (dasd_global_profile_dentry) {
1123 debugfs_remove(dasd_global_profile_dentry);
1124 dasd_global_profile_dentry = NULL;
1125 }
1126 if (dasd_debugfs_global_entry)
1127 debugfs_remove(dasd_debugfs_global_entry);
1128 if (dasd_debugfs_root_entry)
1129 debugfs_remove(dasd_debugfs_root_entry);
1130 }
1131
1132 static void dasd_statistics_createroot(void)
1133 {
1134 umode_t mode;
1135 struct dentry *pde;
1136
1137 dasd_debugfs_root_entry = NULL;
1138 dasd_debugfs_global_entry = NULL;
1139 dasd_global_profile_dentry = NULL;
1140 pde = debugfs_create_dir("dasd", NULL);
1141 if (!pde || IS_ERR(pde))
1142 goto error;
1143 dasd_debugfs_root_entry = pde;
1144 pde = debugfs_create_dir("global", dasd_debugfs_root_entry);
1145 if (!pde || IS_ERR(pde))
1146 goto error;
1147 dasd_debugfs_global_entry = pde;
1148
1149 mode = (S_IRUSR | S_IWUSR | S_IFREG);
1150 pde = debugfs_create_file("statistics", mode, dasd_debugfs_global_entry,
1151 NULL, &dasd_stats_global_fops);
1152 if (!pde || IS_ERR(pde))
1153 goto error;
1154 dasd_global_profile_dentry = pde;
1155 return;
1156
1157 error:
1158 DBF_EVENT(DBF_ERR, "%s",
1159 "Creation of the dasd debugfs interface failed");
1160 dasd_statistics_removeroot();
1161 return;
1162 }
1163
1164 #else
1165 #define dasd_profile_start(block, cqr, req) do {} while (0)
1166 #define dasd_profile_end(block, cqr, req) do {} while (0)
1167
1168 static void dasd_statistics_createroot(void)
1169 {
1170 return;
1171 }
1172
1173 static void dasd_statistics_removeroot(void)
1174 {
1175 return;
1176 }
1177
1178 int dasd_stats_generic_show(struct seq_file *m, void *v)
1179 {
1180 seq_printf(m, "Statistics are not activated in this kernel\n");
1181 return 0;
1182 }
1183
1184 static void dasd_profile_init(struct dasd_profile *profile,
1185 struct dentry *base_dentry)
1186 {
1187 return;
1188 }
1189
1190 static void dasd_profile_exit(struct dasd_profile *profile)
1191 {
1192 return;
1193 }
1194
1195 int dasd_profile_on(struct dasd_profile *profile)
1196 {
1197 return 0;
1198 }
1199
1200 #endif /* CONFIG_DASD_PROFILE */
1201
1202 /*
1203 * Allocate memory for a channel program with 'cplength' channel
1204 * command words and 'datasize' additional space. There are two
1205 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
1206 * memory and 2) dasd_smalloc_request uses the static ccw memory
1207 * that gets allocated for each device.
1208 */
1209 struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
1210 int datasize,
1211 struct dasd_device *device)
1212 {
1213 struct dasd_ccw_req *cqr;
1214
1215 /* Sanity checks */
1216 BUG_ON(datasize > PAGE_SIZE ||
1217 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
1218
1219 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
1220 if (cqr == NULL)
1221 return ERR_PTR(-ENOMEM);
1222 cqr->cpaddr = NULL;
1223 if (cplength > 0) {
1224 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
1225 GFP_ATOMIC | GFP_DMA);
1226 if (cqr->cpaddr == NULL) {
1227 kfree(cqr);
1228 return ERR_PTR(-ENOMEM);
1229 }
1230 }
1231 cqr->data = NULL;
1232 if (datasize > 0) {
1233 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
1234 if (cqr->data == NULL) {
1235 kfree(cqr->cpaddr);
1236 kfree(cqr);
1237 return ERR_PTR(-ENOMEM);
1238 }
1239 }
1240 cqr->magic = magic;
1241 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1242 dasd_get_device(device);
1243 return cqr;
1244 }
1245
1246 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
1247 int datasize,
1248 struct dasd_device *device)
1249 {
1250 unsigned long flags;
1251 struct dasd_ccw_req *cqr;
1252 char *data;
1253 int size;
1254
1255 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
1256 if (cplength > 0)
1257 size += cplength * sizeof(struct ccw1);
1258 if (datasize > 0)
1259 size += datasize;
1260 spin_lock_irqsave(&device->mem_lock, flags);
1261 cqr = (struct dasd_ccw_req *)
1262 dasd_alloc_chunk(&device->ccw_chunks, size);
1263 spin_unlock_irqrestore(&device->mem_lock, flags);
1264 if (cqr == NULL)
1265 return ERR_PTR(-ENOMEM);
1266 memset(cqr, 0, sizeof(struct dasd_ccw_req));
1267 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
1268 cqr->cpaddr = NULL;
1269 if (cplength > 0) {
1270 cqr->cpaddr = (struct ccw1 *) data;
1271 data += cplength*sizeof(struct ccw1);
1272 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
1273 }
1274 cqr->data = NULL;
1275 if (datasize > 0) {
1276 cqr->data = data;
1277 memset(cqr->data, 0, datasize);
1278 }
1279 cqr->magic = magic;
1280 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
1281 dasd_get_device(device);
1282 return cqr;
1283 }
1284
1285 /*
1286 * Free memory of a channel program. This function needs to free all the
1287 * idal lists that might have been created by dasd_set_cda and the
1288 * struct dasd_ccw_req itself.
1289 */
1290 void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1291 {
1292 #ifdef CONFIG_64BIT
1293 struct ccw1 *ccw;
1294
1295 /* Clear any idals used for the request. */
1296 ccw = cqr->cpaddr;
1297 do {
1298 clear_normalized_cda(ccw);
1299 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
1300 #endif
1301 kfree(cqr->cpaddr);
1302 kfree(cqr->data);
1303 kfree(cqr);
1304 dasd_put_device(device);
1305 }
1306
1307 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
1308 {
1309 unsigned long flags;
1310
1311 spin_lock_irqsave(&device->mem_lock, flags);
1312 dasd_free_chunk(&device->ccw_chunks, cqr);
1313 spin_unlock_irqrestore(&device->mem_lock, flags);
1314 dasd_put_device(device);
1315 }
1316
1317 /*
1318 * Check discipline magic in cqr.
1319 */
1320 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
1321 {
1322 struct dasd_device *device;
1323
1324 if (cqr == NULL)
1325 return -EINVAL;
1326 device = cqr->startdev;
1327 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
1328 DBF_DEV_EVENT(DBF_WARNING, device,
1329 " dasd_ccw_req 0x%08x magic doesn't match"
1330 " discipline 0x%08x",
1331 cqr->magic,
1332 *(unsigned int *) device->discipline->name);
1333 return -EINVAL;
1334 }
1335 return 0;
1336 }
1337
1338 /*
1339 * Terminate the current i/o and set the request to clear_pending.
1340 * Timer keeps device runnig.
1341 * ccw_device_clear can fail if the i/o subsystem
1342 * is in a bad mood.
1343 */
1344 int dasd_term_IO(struct dasd_ccw_req *cqr)
1345 {
1346 struct dasd_device *device;
1347 int retries, rc;
1348 char errorstring[ERRORLENGTH];
1349
1350 /* Check the cqr */
1351 rc = dasd_check_cqr(cqr);
1352 if (rc)
1353 return rc;
1354 retries = 0;
1355 device = (struct dasd_device *) cqr->startdev;
1356 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
1357 rc = ccw_device_clear(device->cdev, (long) cqr);
1358 switch (rc) {
1359 case 0: /* termination successful */
1360 cqr->status = DASD_CQR_CLEAR_PENDING;
1361 cqr->stopclk = get_tod_clock();
1362 cqr->starttime = 0;
1363 DBF_DEV_EVENT(DBF_DEBUG, device,
1364 "terminate cqr %p successful",
1365 cqr);
1366 break;
1367 case -ENODEV:
1368 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1369 "device gone, retry");
1370 break;
1371 case -EIO:
1372 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1373 "I/O error, retry");
1374 break;
1375 case -EINVAL:
1376 case -EBUSY:
1377 DBF_DEV_EVENT(DBF_ERR, device, "%s",
1378 "device busy, retry later");
1379 break;
1380 default:
1381 /* internal error 10 - unknown rc*/
1382 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
1383 dev_err(&device->cdev->dev, "An error occurred in the "
1384 "DASD device driver, reason=%s\n", errorstring);
1385 BUG();
1386 break;
1387 }
1388 retries++;
1389 }
1390 dasd_schedule_device_bh(device);
1391 return rc;
1392 }
1393
1394 /*
1395 * Start the i/o. This start_IO can fail if the channel is really busy.
1396 * In that case set up a timer to start the request later.
1397 */
1398 int dasd_start_IO(struct dasd_ccw_req *cqr)
1399 {
1400 struct dasd_device *device;
1401 int rc;
1402 char errorstring[ERRORLENGTH];
1403
1404 /* Check the cqr */
1405 rc = dasd_check_cqr(cqr);
1406 if (rc) {
1407 cqr->intrc = rc;
1408 return rc;
1409 }
1410 device = (struct dasd_device *) cqr->startdev;
1411 if (((cqr->block &&
1412 test_bit(DASD_FLAG_LOCK_STOLEN, &cqr->block->base->flags)) ||
1413 test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags)) &&
1414 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
1415 DBF_DEV_EVENT(DBF_DEBUG, device, "start_IO: return request %p "
1416 "because of stolen lock", cqr);
1417 cqr->status = DASD_CQR_ERROR;
1418 cqr->intrc = -EPERM;
1419 return -EPERM;
1420 }
1421 if (cqr->retries < 0) {
1422 /* internal error 14 - start_IO run out of retries */
1423 sprintf(errorstring, "14 %p", cqr);
1424 dev_err(&device->cdev->dev, "An error occurred in the DASD "
1425 "device driver, reason=%s\n", errorstring);
1426 cqr->status = DASD_CQR_ERROR;
1427 return -EIO;
1428 }
1429 cqr->startclk = get_tod_clock();
1430 cqr->starttime = jiffies;
1431 cqr->retries--;
1432 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1433 cqr->lpm &= device->path_data.opm;
1434 if (!cqr->lpm)
1435 cqr->lpm = device->path_data.opm;
1436 }
1437 if (cqr->cpmode == 1) {
1438 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
1439 (long) cqr, cqr->lpm);
1440 } else {
1441 rc = ccw_device_start(device->cdev, cqr->cpaddr,
1442 (long) cqr, cqr->lpm, 0);
1443 }
1444 switch (rc) {
1445 case 0:
1446 cqr->status = DASD_CQR_IN_IO;
1447 break;
1448 case -EBUSY:
1449 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1450 "start_IO: device busy, retry later");
1451 break;
1452 case -ETIMEDOUT:
1453 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1454 "start_IO: request timeout, retry later");
1455 break;
1456 case -EACCES:
1457 /* -EACCES indicates that the request used only a subset of the
1458 * available paths and all these paths are gone. If the lpm of
1459 * this request was only a subset of the opm (e.g. the ppm) then
1460 * we just do a retry with all available paths.
1461 * If we already use the full opm, something is amiss, and we
1462 * need a full path verification.
1463 */
1464 if (test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags)) {
1465 DBF_DEV_EVENT(DBF_WARNING, device,
1466 "start_IO: selected paths gone (%x)",
1467 cqr->lpm);
1468 } else if (cqr->lpm != device->path_data.opm) {
1469 cqr->lpm = device->path_data.opm;
1470 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
1471 "start_IO: selected paths gone,"
1472 " retry on all paths");
1473 } else {
1474 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1475 "start_IO: all paths in opm gone,"
1476 " do path verification");
1477 dasd_generic_last_path_gone(device);
1478 device->path_data.opm = 0;
1479 device->path_data.ppm = 0;
1480 device->path_data.npm = 0;
1481 device->path_data.tbvpm =
1482 ccw_device_get_path_mask(device->cdev);
1483 }
1484 break;
1485 case -ENODEV:
1486 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1487 "start_IO: -ENODEV device gone, retry");
1488 break;
1489 case -EIO:
1490 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1491 "start_IO: -EIO device gone, retry");
1492 break;
1493 case -EINVAL:
1494 /* most likely caused in power management context */
1495 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1496 "start_IO: -EINVAL device currently "
1497 "not accessible");
1498 break;
1499 default:
1500 /* internal error 11 - unknown rc */
1501 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
1502 dev_err(&device->cdev->dev,
1503 "An error occurred in the DASD device driver, "
1504 "reason=%s\n", errorstring);
1505 BUG();
1506 break;
1507 }
1508 cqr->intrc = rc;
1509 return rc;
1510 }
1511
1512 /*
1513 * Timeout function for dasd devices. This is used for different purposes
1514 * 1) missing interrupt handler for normal operation
1515 * 2) delayed start of request where start_IO failed with -EBUSY
1516 * 3) timeout for missing state change interrupts
1517 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
1518 * DASD_CQR_QUEUED for 2) and 3).
1519 */
1520 static void dasd_device_timeout(unsigned long ptr)
1521 {
1522 unsigned long flags;
1523 struct dasd_device *device;
1524
1525 device = (struct dasd_device *) ptr;
1526 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1527 /* re-activate request queue */
1528 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1529 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1530 dasd_schedule_device_bh(device);
1531 }
1532
1533 /*
1534 * Setup timeout for a device in jiffies.
1535 */
1536 void dasd_device_set_timer(struct dasd_device *device, int expires)
1537 {
1538 if (expires == 0)
1539 del_timer(&device->timer);
1540 else
1541 mod_timer(&device->timer, jiffies + expires);
1542 }
1543
1544 /*
1545 * Clear timeout for a device.
1546 */
1547 void dasd_device_clear_timer(struct dasd_device *device)
1548 {
1549 del_timer(&device->timer);
1550 }
1551
1552 static void dasd_handle_killed_request(struct ccw_device *cdev,
1553 unsigned long intparm)
1554 {
1555 struct dasd_ccw_req *cqr;
1556 struct dasd_device *device;
1557
1558 if (!intparm)
1559 return;
1560 cqr = (struct dasd_ccw_req *) intparm;
1561 if (cqr->status != DASD_CQR_IN_IO) {
1562 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1563 "invalid status in handle_killed_request: "
1564 "%02x", cqr->status);
1565 return;
1566 }
1567
1568 device = dasd_device_from_cdev_locked(cdev);
1569 if (IS_ERR(device)) {
1570 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1571 "unable to get device from cdev");
1572 return;
1573 }
1574
1575 if (!cqr->startdev ||
1576 device != cqr->startdev ||
1577 strncmp(cqr->startdev->discipline->ebcname,
1578 (char *) &cqr->magic, 4)) {
1579 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1580 "invalid device in request");
1581 dasd_put_device(device);
1582 return;
1583 }
1584
1585 /* Schedule request to be retried. */
1586 cqr->status = DASD_CQR_QUEUED;
1587
1588 dasd_device_clear_timer(device);
1589 dasd_schedule_device_bh(device);
1590 dasd_put_device(device);
1591 }
1592
1593 void dasd_generic_handle_state_change(struct dasd_device *device)
1594 {
1595 /* First of all start sense subsystem status request. */
1596 dasd_eer_snss(device);
1597
1598 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1599 dasd_schedule_device_bh(device);
1600 if (device->block)
1601 dasd_schedule_block_bh(device->block);
1602 }
1603
1604 /*
1605 * Interrupt handler for "normal" ssch-io based dasd devices.
1606 */
1607 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1608 struct irb *irb)
1609 {
1610 struct dasd_ccw_req *cqr, *next;
1611 struct dasd_device *device;
1612 unsigned long long now;
1613 int expires;
1614
1615 if (IS_ERR(irb)) {
1616 switch (PTR_ERR(irb)) {
1617 case -EIO:
1618 break;
1619 case -ETIMEDOUT:
1620 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1621 "request timed out\n", __func__);
1622 break;
1623 default:
1624 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1625 "unknown error %ld\n", __func__,
1626 PTR_ERR(irb));
1627 }
1628 dasd_handle_killed_request(cdev, intparm);
1629 return;
1630 }
1631
1632 now = get_tod_clock();
1633 cqr = (struct dasd_ccw_req *) intparm;
1634 /* check for conditions that should be handled immediately */
1635 if (!cqr ||
1636 !(scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1637 scsw_cstat(&irb->scsw) == 0)) {
1638 if (cqr)
1639 memcpy(&cqr->irb, irb, sizeof(*irb));
1640 device = dasd_device_from_cdev_locked(cdev);
1641 if (IS_ERR(device))
1642 return;
1643 /* ignore unsolicited interrupts for DIAG discipline */
1644 if (device->discipline == dasd_diag_discipline_pointer) {
1645 dasd_put_device(device);
1646 return;
1647 }
1648 device->discipline->dump_sense_dbf(device, irb, "int");
1649 if (device->features & DASD_FEATURE_ERPLOG)
1650 device->discipline->dump_sense(device, cqr, irb);
1651 device->discipline->check_for_device_change(device, cqr, irb);
1652 dasd_put_device(device);
1653 }
1654 if (!cqr)
1655 return;
1656
1657 device = (struct dasd_device *) cqr->startdev;
1658 if (!device ||
1659 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1660 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1661 "invalid device in request");
1662 return;
1663 }
1664
1665 /* Check for clear pending */
1666 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1667 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1668 cqr->status = DASD_CQR_CLEARED;
1669 dasd_device_clear_timer(device);
1670 wake_up(&dasd_flush_wq);
1671 dasd_schedule_device_bh(device);
1672 return;
1673 }
1674
1675 /* check status - the request might have been killed by dyn detach */
1676 if (cqr->status != DASD_CQR_IN_IO) {
1677 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1678 "status %02x", dev_name(&cdev->dev), cqr->status);
1679 return;
1680 }
1681
1682 next = NULL;
1683 expires = 0;
1684 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1685 scsw_cstat(&irb->scsw) == 0) {
1686 /* request was completed successfully */
1687 cqr->status = DASD_CQR_SUCCESS;
1688 cqr->stopclk = now;
1689 /* Start first request on queue if possible -> fast_io. */
1690 if (cqr->devlist.next != &device->ccw_queue) {
1691 next = list_entry(cqr->devlist.next,
1692 struct dasd_ccw_req, devlist);
1693 }
1694 } else { /* error */
1695 /*
1696 * If we don't want complex ERP for this request, then just
1697 * reset this and retry it in the fastpath
1698 */
1699 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1700 cqr->retries > 0) {
1701 if (cqr->lpm == device->path_data.opm)
1702 DBF_DEV_EVENT(DBF_DEBUG, device,
1703 "default ERP in fastpath "
1704 "(%i retries left)",
1705 cqr->retries);
1706 if (!test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))
1707 cqr->lpm = device->path_data.opm;
1708 cqr->status = DASD_CQR_QUEUED;
1709 next = cqr;
1710 } else
1711 cqr->status = DASD_CQR_ERROR;
1712 }
1713 if (next && (next->status == DASD_CQR_QUEUED) &&
1714 (!device->stopped)) {
1715 if (device->discipline->start_IO(next) == 0)
1716 expires = next->expires;
1717 }
1718 if (expires != 0)
1719 dasd_device_set_timer(device, expires);
1720 else
1721 dasd_device_clear_timer(device);
1722 dasd_schedule_device_bh(device);
1723 }
1724
1725 enum uc_todo dasd_generic_uc_handler(struct ccw_device *cdev, struct irb *irb)
1726 {
1727 struct dasd_device *device;
1728
1729 device = dasd_device_from_cdev_locked(cdev);
1730
1731 if (IS_ERR(device))
1732 goto out;
1733 if (test_bit(DASD_FLAG_OFFLINE, &device->flags) ||
1734 device->state != device->target ||
1735 !device->discipline->check_for_device_change){
1736 dasd_put_device(device);
1737 goto out;
1738 }
1739 if (device->discipline->dump_sense_dbf)
1740 device->discipline->dump_sense_dbf(device, irb, "uc");
1741 device->discipline->check_for_device_change(device, NULL, irb);
1742 dasd_put_device(device);
1743 out:
1744 return UC_TODO_RETRY;
1745 }
1746 EXPORT_SYMBOL_GPL(dasd_generic_uc_handler);
1747
1748 /*
1749 * If we have an error on a dasd_block layer request then we cancel
1750 * and return all further requests from the same dasd_block as well.
1751 */
1752 static void __dasd_device_recovery(struct dasd_device *device,
1753 struct dasd_ccw_req *ref_cqr)
1754 {
1755 struct list_head *l, *n;
1756 struct dasd_ccw_req *cqr;
1757
1758 /*
1759 * only requeue request that came from the dasd_block layer
1760 */
1761 if (!ref_cqr->block)
1762 return;
1763
1764 list_for_each_safe(l, n, &device->ccw_queue) {
1765 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1766 if (cqr->status == DASD_CQR_QUEUED &&
1767 ref_cqr->block == cqr->block) {
1768 cqr->status = DASD_CQR_CLEARED;
1769 }
1770 }
1771 };
1772
1773 /*
1774 * Remove those ccw requests from the queue that need to be returned
1775 * to the upper layer.
1776 */
1777 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1778 struct list_head *final_queue)
1779 {
1780 struct list_head *l, *n;
1781 struct dasd_ccw_req *cqr;
1782
1783 /* Process request with final status. */
1784 list_for_each_safe(l, n, &device->ccw_queue) {
1785 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1786
1787 /* Skip any non-final request. */
1788 if (cqr->status == DASD_CQR_QUEUED ||
1789 cqr->status == DASD_CQR_IN_IO ||
1790 cqr->status == DASD_CQR_CLEAR_PENDING)
1791 continue;
1792 if (cqr->status == DASD_CQR_ERROR) {
1793 __dasd_device_recovery(device, cqr);
1794 }
1795 /* Rechain finished requests to final queue */
1796 list_move_tail(&cqr->devlist, final_queue);
1797 }
1798 }
1799
1800 /*
1801 * the cqrs from the final queue are returned to the upper layer
1802 * by setting a dasd_block state and calling the callback function
1803 */
1804 static void __dasd_device_process_final_queue(struct dasd_device *device,
1805 struct list_head *final_queue)
1806 {
1807 struct list_head *l, *n;
1808 struct dasd_ccw_req *cqr;
1809 struct dasd_block *block;
1810 void (*callback)(struct dasd_ccw_req *, void *data);
1811 void *callback_data;
1812 char errorstring[ERRORLENGTH];
1813
1814 list_for_each_safe(l, n, final_queue) {
1815 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1816 list_del_init(&cqr->devlist);
1817 block = cqr->block;
1818 callback = cqr->callback;
1819 callback_data = cqr->callback_data;
1820 if (block)
1821 spin_lock_bh(&block->queue_lock);
1822 switch (cqr->status) {
1823 case DASD_CQR_SUCCESS:
1824 cqr->status = DASD_CQR_DONE;
1825 break;
1826 case DASD_CQR_ERROR:
1827 cqr->status = DASD_CQR_NEED_ERP;
1828 break;
1829 case DASD_CQR_CLEARED:
1830 cqr->status = DASD_CQR_TERMINATED;
1831 break;
1832 default:
1833 /* internal error 12 - wrong cqr status*/
1834 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1835 dev_err(&device->cdev->dev,
1836 "An error occurred in the DASD device driver, "
1837 "reason=%s\n", errorstring);
1838 BUG();
1839 }
1840 if (cqr->callback != NULL)
1841 (callback)(cqr, callback_data);
1842 if (block)
1843 spin_unlock_bh(&block->queue_lock);
1844 }
1845 }
1846
1847 /*
1848 * Take a look at the first request on the ccw queue and check
1849 * if it reached its expire time. If so, terminate the IO.
1850 */
1851 static void __dasd_device_check_expire(struct dasd_device *device)
1852 {
1853 struct dasd_ccw_req *cqr;
1854
1855 if (list_empty(&device->ccw_queue))
1856 return;
1857 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1858 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1859 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1860 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
1861 /*
1862 * IO in safe offline processing should not
1863 * run out of retries
1864 */
1865 cqr->retries++;
1866 }
1867 if (device->discipline->term_IO(cqr) != 0) {
1868 /* Hmpf, try again in 5 sec */
1869 dev_err(&device->cdev->dev,
1870 "cqr %p timed out (%lus) but cannot be "
1871 "ended, retrying in 5 s\n",
1872 cqr, (cqr->expires/HZ));
1873 cqr->expires += 5*HZ;
1874 dasd_device_set_timer(device, 5*HZ);
1875 } else {
1876 dev_err(&device->cdev->dev,
1877 "cqr %p timed out (%lus), %i retries "
1878 "remaining\n", cqr, (cqr->expires/HZ),
1879 cqr->retries);
1880 }
1881 }
1882 }
1883
1884 /*
1885 * Take a look at the first request on the ccw queue and check
1886 * if it needs to be started.
1887 */
1888 static void __dasd_device_start_head(struct dasd_device *device)
1889 {
1890 struct dasd_ccw_req *cqr;
1891 int rc;
1892
1893 if (list_empty(&device->ccw_queue))
1894 return;
1895 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1896 if (cqr->status != DASD_CQR_QUEUED)
1897 return;
1898 /* when device is stopped, return request to previous layer
1899 * exception: only the disconnect or unresumed bits are set and the
1900 * cqr is a path verification request
1901 */
1902 if (device->stopped &&
1903 !(!(device->stopped & ~(DASD_STOPPED_DC_WAIT | DASD_UNRESUMED_PM))
1904 && test_bit(DASD_CQR_VERIFY_PATH, &cqr->flags))) {
1905 cqr->intrc = -EAGAIN;
1906 cqr->status = DASD_CQR_CLEARED;
1907 dasd_schedule_device_bh(device);
1908 return;
1909 }
1910
1911 rc = device->discipline->start_IO(cqr);
1912 if (rc == 0)
1913 dasd_device_set_timer(device, cqr->expires);
1914 else if (rc == -EACCES) {
1915 dasd_schedule_device_bh(device);
1916 } else
1917 /* Hmpf, try again in 1/2 sec */
1918 dasd_device_set_timer(device, 50);
1919 }
1920
1921 static void __dasd_device_check_path_events(struct dasd_device *device)
1922 {
1923 int rc;
1924
1925 if (device->path_data.tbvpm) {
1926 if (device->stopped & ~(DASD_STOPPED_DC_WAIT |
1927 DASD_UNRESUMED_PM))
1928 return;
1929 rc = device->discipline->verify_path(
1930 device, device->path_data.tbvpm);
1931 if (rc)
1932 dasd_device_set_timer(device, 50);
1933 else
1934 device->path_data.tbvpm = 0;
1935 }
1936 };
1937
1938 /*
1939 * Go through all request on the dasd_device request queue,
1940 * terminate them on the cdev if necessary, and return them to the
1941 * submitting layer via callback.
1942 * Note:
1943 * Make sure that all 'submitting layers' still exist when
1944 * this function is called!. In other words, when 'device' is a base
1945 * device then all block layer requests must have been removed before
1946 * via dasd_flush_block_queue.
1947 */
1948 int dasd_flush_device_queue(struct dasd_device *device)
1949 {
1950 struct dasd_ccw_req *cqr, *n;
1951 int rc;
1952 struct list_head flush_queue;
1953
1954 INIT_LIST_HEAD(&flush_queue);
1955 spin_lock_irq(get_ccwdev_lock(device->cdev));
1956 rc = 0;
1957 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1958 /* Check status and move request to flush_queue */
1959 switch (cqr->status) {
1960 case DASD_CQR_IN_IO:
1961 rc = device->discipline->term_IO(cqr);
1962 if (rc) {
1963 /* unable to terminate requeust */
1964 dev_err(&device->cdev->dev,
1965 "Flushing the DASD request queue "
1966 "failed for request %p\n", cqr);
1967 /* stop flush processing */
1968 goto finished;
1969 }
1970 break;
1971 case DASD_CQR_QUEUED:
1972 cqr->stopclk = get_tod_clock();
1973 cqr->status = DASD_CQR_CLEARED;
1974 break;
1975 default: /* no need to modify the others */
1976 break;
1977 }
1978 list_move_tail(&cqr->devlist, &flush_queue);
1979 }
1980 finished:
1981 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1982 /*
1983 * After this point all requests must be in state CLEAR_PENDING,
1984 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1985 * one of the others.
1986 */
1987 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1988 wait_event(dasd_flush_wq,
1989 (cqr->status != DASD_CQR_CLEAR_PENDING));
1990 /*
1991 * Now set each request back to TERMINATED, DONE or NEED_ERP
1992 * and call the callback function of flushed requests
1993 */
1994 __dasd_device_process_final_queue(device, &flush_queue);
1995 return rc;
1996 }
1997
1998 /*
1999 * Acquire the device lock and process queues for the device.
2000 */
2001 static void dasd_device_tasklet(struct dasd_device *device)
2002 {
2003 struct list_head final_queue;
2004
2005 atomic_set (&device->tasklet_scheduled, 0);
2006 INIT_LIST_HEAD(&final_queue);
2007 spin_lock_irq(get_ccwdev_lock(device->cdev));
2008 /* Check expire time of first request on the ccw queue. */
2009 __dasd_device_check_expire(device);
2010 /* find final requests on ccw queue */
2011 __dasd_device_process_ccw_queue(device, &final_queue);
2012 __dasd_device_check_path_events(device);
2013 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2014 /* Now call the callback function of requests with final status */
2015 __dasd_device_process_final_queue(device, &final_queue);
2016 spin_lock_irq(get_ccwdev_lock(device->cdev));
2017 /* Now check if the head of the ccw queue needs to be started. */
2018 __dasd_device_start_head(device);
2019 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2020 if (waitqueue_active(&shutdown_waitq))
2021 wake_up(&shutdown_waitq);
2022 dasd_put_device(device);
2023 }
2024
2025 /*
2026 * Schedules a call to dasd_tasklet over the device tasklet.
2027 */
2028 void dasd_schedule_device_bh(struct dasd_device *device)
2029 {
2030 /* Protect against rescheduling. */
2031 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
2032 return;
2033 dasd_get_device(device);
2034 tasklet_hi_schedule(&device->tasklet);
2035 }
2036
2037 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
2038 {
2039 device->stopped |= bits;
2040 }
2041 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
2042
2043 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
2044 {
2045 device->stopped &= ~bits;
2046 if (!device->stopped)
2047 wake_up(&generic_waitq);
2048 }
2049 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
2050
2051 /*
2052 * Queue a request to the head of the device ccw_queue.
2053 * Start the I/O if possible.
2054 */
2055 void dasd_add_request_head(struct dasd_ccw_req *cqr)
2056 {
2057 struct dasd_device *device;
2058 unsigned long flags;
2059
2060 device = cqr->startdev;
2061 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2062 cqr->status = DASD_CQR_QUEUED;
2063 list_add(&cqr->devlist, &device->ccw_queue);
2064 /* let the bh start the request to keep them in order */
2065 dasd_schedule_device_bh(device);
2066 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2067 }
2068
2069 /*
2070 * Queue a request to the tail of the device ccw_queue.
2071 * Start the I/O if possible.
2072 */
2073 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
2074 {
2075 struct dasd_device *device;
2076 unsigned long flags;
2077
2078 device = cqr->startdev;
2079 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2080 cqr->status = DASD_CQR_QUEUED;
2081 list_add_tail(&cqr->devlist, &device->ccw_queue);
2082 /* let the bh start the request to keep them in order */
2083 dasd_schedule_device_bh(device);
2084 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2085 }
2086
2087 /*
2088 * Wakeup helper for the 'sleep_on' functions.
2089 */
2090 void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
2091 {
2092 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2093 cqr->callback_data = DASD_SLEEPON_END_TAG;
2094 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
2095 wake_up(&generic_waitq);
2096 }
2097 EXPORT_SYMBOL_GPL(dasd_wakeup_cb);
2098
2099 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
2100 {
2101 struct dasd_device *device;
2102 int rc;
2103
2104 device = cqr->startdev;
2105 spin_lock_irq(get_ccwdev_lock(device->cdev));
2106 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
2107 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2108 return rc;
2109 }
2110
2111 /*
2112 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
2113 */
2114 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
2115 {
2116 struct dasd_device *device;
2117 dasd_erp_fn_t erp_fn;
2118
2119 if (cqr->status == DASD_CQR_FILLED)
2120 return 0;
2121 device = cqr->startdev;
2122 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2123 if (cqr->status == DASD_CQR_TERMINATED) {
2124 device->discipline->handle_terminated_request(cqr);
2125 return 1;
2126 }
2127 if (cqr->status == DASD_CQR_NEED_ERP) {
2128 erp_fn = device->discipline->erp_action(cqr);
2129 erp_fn(cqr);
2130 return 1;
2131 }
2132 if (cqr->status == DASD_CQR_FAILED)
2133 dasd_log_sense(cqr, &cqr->irb);
2134 if (cqr->refers) {
2135 __dasd_process_erp(device, cqr);
2136 return 1;
2137 }
2138 }
2139 return 0;
2140 }
2141
2142 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
2143 {
2144 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
2145 if (cqr->refers) /* erp is not done yet */
2146 return 1;
2147 return ((cqr->status != DASD_CQR_DONE) &&
2148 (cqr->status != DASD_CQR_FAILED));
2149 } else
2150 return (cqr->status == DASD_CQR_FILLED);
2151 }
2152
2153 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
2154 {
2155 struct dasd_device *device;
2156 int rc;
2157 struct list_head ccw_queue;
2158 struct dasd_ccw_req *cqr;
2159
2160 INIT_LIST_HEAD(&ccw_queue);
2161 maincqr->status = DASD_CQR_FILLED;
2162 device = maincqr->startdev;
2163 list_add(&maincqr->blocklist, &ccw_queue);
2164 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
2165 cqr = list_first_entry(&ccw_queue,
2166 struct dasd_ccw_req, blocklist)) {
2167
2168 if (__dasd_sleep_on_erp(cqr))
2169 continue;
2170 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
2171 continue;
2172 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2173 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2174 cqr->status = DASD_CQR_FAILED;
2175 cqr->intrc = -EPERM;
2176 continue;
2177 }
2178 /* Non-temporary stop condition will trigger fail fast */
2179 if (device->stopped & ~DASD_STOPPED_PENDING &&
2180 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2181 (!dasd_eer_enabled(device))) {
2182 cqr->status = DASD_CQR_FAILED;
2183 cqr->intrc = -ENOLINK;
2184 continue;
2185 }
2186 /* Don't try to start requests if device is stopped */
2187 if (interruptible) {
2188 rc = wait_event_interruptible(
2189 generic_waitq, !(device->stopped));
2190 if (rc == -ERESTARTSYS) {
2191 cqr->status = DASD_CQR_FAILED;
2192 maincqr->intrc = rc;
2193 continue;
2194 }
2195 } else
2196 wait_event(generic_waitq, !(device->stopped));
2197
2198 if (!cqr->callback)
2199 cqr->callback = dasd_wakeup_cb;
2200
2201 cqr->callback_data = DASD_SLEEPON_START_TAG;
2202 dasd_add_request_tail(cqr);
2203 if (interruptible) {
2204 rc = wait_event_interruptible(
2205 generic_waitq, _wait_for_wakeup(cqr));
2206 if (rc == -ERESTARTSYS) {
2207 dasd_cancel_req(cqr);
2208 /* wait (non-interruptible) for final status */
2209 wait_event(generic_waitq,
2210 _wait_for_wakeup(cqr));
2211 cqr->status = DASD_CQR_FAILED;
2212 maincqr->intrc = rc;
2213 continue;
2214 }
2215 } else
2216 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2217 }
2218
2219 maincqr->endclk = get_tod_clock();
2220 if ((maincqr->status != DASD_CQR_DONE) &&
2221 (maincqr->intrc != -ERESTARTSYS))
2222 dasd_log_sense(maincqr, &maincqr->irb);
2223 if (maincqr->status == DASD_CQR_DONE)
2224 rc = 0;
2225 else if (maincqr->intrc)
2226 rc = maincqr->intrc;
2227 else
2228 rc = -EIO;
2229 return rc;
2230 }
2231
2232 static inline int _wait_for_wakeup_queue(struct list_head *ccw_queue)
2233 {
2234 struct dasd_ccw_req *cqr;
2235
2236 list_for_each_entry(cqr, ccw_queue, blocklist) {
2237 if (cqr->callback_data != DASD_SLEEPON_END_TAG)
2238 return 0;
2239 }
2240
2241 return 1;
2242 }
2243
2244 static int _dasd_sleep_on_queue(struct list_head *ccw_queue, int interruptible)
2245 {
2246 struct dasd_device *device;
2247 int rc;
2248 struct dasd_ccw_req *cqr, *n;
2249
2250 retry:
2251 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2252 device = cqr->startdev;
2253 if (cqr->status != DASD_CQR_FILLED) /*could be failed*/
2254 continue;
2255
2256 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2257 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2258 cqr->status = DASD_CQR_FAILED;
2259 cqr->intrc = -EPERM;
2260 continue;
2261 }
2262 /*Non-temporary stop condition will trigger fail fast*/
2263 if (device->stopped & ~DASD_STOPPED_PENDING &&
2264 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2265 !dasd_eer_enabled(device)) {
2266 cqr->status = DASD_CQR_FAILED;
2267 cqr->intrc = -EAGAIN;
2268 continue;
2269 }
2270
2271 /*Don't try to start requests if device is stopped*/
2272 if (interruptible) {
2273 rc = wait_event_interruptible(
2274 generic_waitq, !device->stopped);
2275 if (rc == -ERESTARTSYS) {
2276 cqr->status = DASD_CQR_FAILED;
2277 cqr->intrc = rc;
2278 continue;
2279 }
2280 } else
2281 wait_event(generic_waitq, !(device->stopped));
2282
2283 if (!cqr->callback)
2284 cqr->callback = dasd_wakeup_cb;
2285 cqr->callback_data = DASD_SLEEPON_START_TAG;
2286 dasd_add_request_tail(cqr);
2287 }
2288
2289 wait_event(generic_waitq, _wait_for_wakeup_queue(ccw_queue));
2290
2291 rc = 0;
2292 list_for_each_entry_safe(cqr, n, ccw_queue, blocklist) {
2293 if (__dasd_sleep_on_erp(cqr))
2294 rc = 1;
2295 }
2296 if (rc)
2297 goto retry;
2298
2299
2300 return 0;
2301 }
2302
2303 /*
2304 * Queue a request to the tail of the device ccw_queue and wait for
2305 * it's completion.
2306 */
2307 int dasd_sleep_on(struct dasd_ccw_req *cqr)
2308 {
2309 return _dasd_sleep_on(cqr, 0);
2310 }
2311
2312 /*
2313 * Start requests from a ccw_queue and wait for their completion.
2314 */
2315 int dasd_sleep_on_queue(struct list_head *ccw_queue)
2316 {
2317 return _dasd_sleep_on_queue(ccw_queue, 0);
2318 }
2319 EXPORT_SYMBOL(dasd_sleep_on_queue);
2320
2321 /*
2322 * Queue a request to the tail of the device ccw_queue and wait
2323 * interruptible for it's completion.
2324 */
2325 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
2326 {
2327 return _dasd_sleep_on(cqr, 1);
2328 }
2329
2330 /*
2331 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
2332 * for eckd devices) the currently running request has to be terminated
2333 * and be put back to status queued, before the special request is added
2334 * to the head of the queue. Then the special request is waited on normally.
2335 */
2336 static inline int _dasd_term_running_cqr(struct dasd_device *device)
2337 {
2338 struct dasd_ccw_req *cqr;
2339 int rc;
2340
2341 if (list_empty(&device->ccw_queue))
2342 return 0;
2343 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
2344 rc = device->discipline->term_IO(cqr);
2345 if (!rc)
2346 /*
2347 * CQR terminated because a more important request is pending.
2348 * Undo decreasing of retry counter because this is
2349 * not an error case.
2350 */
2351 cqr->retries++;
2352 return rc;
2353 }
2354
2355 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
2356 {
2357 struct dasd_device *device;
2358 int rc;
2359
2360 device = cqr->startdev;
2361 if (test_bit(DASD_FLAG_LOCK_STOLEN, &device->flags) &&
2362 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2363 cqr->status = DASD_CQR_FAILED;
2364 cqr->intrc = -EPERM;
2365 return -EIO;
2366 }
2367 spin_lock_irq(get_ccwdev_lock(device->cdev));
2368 rc = _dasd_term_running_cqr(device);
2369 if (rc) {
2370 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2371 return rc;
2372 }
2373 cqr->callback = dasd_wakeup_cb;
2374 cqr->callback_data = DASD_SLEEPON_START_TAG;
2375 cqr->status = DASD_CQR_QUEUED;
2376 /*
2377 * add new request as second
2378 * first the terminated cqr needs to be finished
2379 */
2380 list_add(&cqr->devlist, device->ccw_queue.next);
2381
2382 /* let the bh start the request to keep them in order */
2383 dasd_schedule_device_bh(device);
2384
2385 spin_unlock_irq(get_ccwdev_lock(device->cdev));
2386
2387 wait_event(generic_waitq, _wait_for_wakeup(cqr));
2388
2389 if (cqr->status == DASD_CQR_DONE)
2390 rc = 0;
2391 else if (cqr->intrc)
2392 rc = cqr->intrc;
2393 else
2394 rc = -EIO;
2395 return rc;
2396 }
2397
2398 /*
2399 * Cancels a request that was started with dasd_sleep_on_req.
2400 * This is useful to timeout requests. The request will be
2401 * terminated if it is currently in i/o.
2402 * Returns 0 if request termination was successful
2403 * negative error code if termination failed
2404 * Cancellation of a request is an asynchronous operation! The calling
2405 * function has to wait until the request is properly returned via callback.
2406 */
2407 int dasd_cancel_req(struct dasd_ccw_req *cqr)
2408 {
2409 struct dasd_device *device = cqr->startdev;
2410 unsigned long flags;
2411 int rc;
2412
2413 rc = 0;
2414 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
2415 switch (cqr->status) {
2416 case DASD_CQR_QUEUED:
2417 /* request was not started - just set to cleared */
2418 cqr->status = DASD_CQR_CLEARED;
2419 break;
2420 case DASD_CQR_IN_IO:
2421 /* request in IO - terminate IO and release again */
2422 rc = device->discipline->term_IO(cqr);
2423 if (rc) {
2424 dev_err(&device->cdev->dev,
2425 "Cancelling request %p failed with rc=%d\n",
2426 cqr, rc);
2427 } else {
2428 cqr->stopclk = get_tod_clock();
2429 }
2430 break;
2431 default: /* already finished or clear pending - do nothing */
2432 break;
2433 }
2434 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
2435 dasd_schedule_device_bh(device);
2436 return rc;
2437 }
2438
2439 /*
2440 * SECTION: Operations of the dasd_block layer.
2441 */
2442
2443 /*
2444 * Timeout function for dasd_block. This is used when the block layer
2445 * is waiting for something that may not come reliably, (e.g. a state
2446 * change interrupt)
2447 */
2448 static void dasd_block_timeout(unsigned long ptr)
2449 {
2450 unsigned long flags;
2451 struct dasd_block *block;
2452
2453 block = (struct dasd_block *) ptr;
2454 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
2455 /* re-activate request queue */
2456 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
2457 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
2458 dasd_schedule_block_bh(block);
2459 }
2460
2461 /*
2462 * Setup timeout for a dasd_block in jiffies.
2463 */
2464 void dasd_block_set_timer(struct dasd_block *block, int expires)
2465 {
2466 if (expires == 0)
2467 del_timer(&block->timer);
2468 else
2469 mod_timer(&block->timer, jiffies + expires);
2470 }
2471
2472 /*
2473 * Clear timeout for a dasd_block.
2474 */
2475 void dasd_block_clear_timer(struct dasd_block *block)
2476 {
2477 del_timer(&block->timer);
2478 }
2479
2480 /*
2481 * Process finished error recovery ccw.
2482 */
2483 static void __dasd_process_erp(struct dasd_device *device,
2484 struct dasd_ccw_req *cqr)
2485 {
2486 dasd_erp_fn_t erp_fn;
2487
2488 if (cqr->status == DASD_CQR_DONE)
2489 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
2490 else
2491 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
2492 erp_fn = device->discipline->erp_postaction(cqr);
2493 erp_fn(cqr);
2494 }
2495
2496 /*
2497 * Fetch requests from the block device queue.
2498 */
2499 static void __dasd_process_request_queue(struct dasd_block *block)
2500 {
2501 struct request_queue *queue;
2502 struct request *req;
2503 struct dasd_ccw_req *cqr;
2504 struct dasd_device *basedev;
2505 unsigned long flags;
2506 queue = block->request_queue;
2507 basedev = block->base;
2508 /* No queue ? Then there is nothing to do. */
2509 if (queue == NULL)
2510 return;
2511
2512 /*
2513 * We requeue request from the block device queue to the ccw
2514 * queue only in two states. In state DASD_STATE_READY the
2515 * partition detection is done and we need to requeue requests
2516 * for that. State DASD_STATE_ONLINE is normal block device
2517 * operation.
2518 */
2519 if (basedev->state < DASD_STATE_READY) {
2520 while ((req = blk_fetch_request(block->request_queue)))
2521 __blk_end_request_all(req, -EIO);
2522 return;
2523 }
2524 /* Now we try to fetch requests from the request queue */
2525 while ((req = blk_peek_request(queue))) {
2526 if (basedev->features & DASD_FEATURE_READONLY &&
2527 rq_data_dir(req) == WRITE) {
2528 DBF_DEV_EVENT(DBF_ERR, basedev,
2529 "Rejecting write request %p",
2530 req);
2531 blk_start_request(req);
2532 __blk_end_request_all(req, -EIO);
2533 continue;
2534 }
2535 if (test_bit(DASD_FLAG_ABORTALL, &basedev->flags) &&
2536 (basedev->features & DASD_FEATURE_FAILFAST ||
2537 blk_noretry_request(req))) {
2538 DBF_DEV_EVENT(DBF_ERR, basedev,
2539 "Rejecting failfast request %p",
2540 req);
2541 blk_start_request(req);
2542 __blk_end_request_all(req, -ETIMEDOUT);
2543 continue;
2544 }
2545 cqr = basedev->discipline->build_cp(basedev, block, req);
2546 if (IS_ERR(cqr)) {
2547 if (PTR_ERR(cqr) == -EBUSY)
2548 break; /* normal end condition */
2549 if (PTR_ERR(cqr) == -ENOMEM)
2550 break; /* terminate request queue loop */
2551 if (PTR_ERR(cqr) == -EAGAIN) {
2552 /*
2553 * The current request cannot be build right
2554 * now, we have to try later. If this request
2555 * is the head-of-queue we stop the device
2556 * for 1/2 second.
2557 */
2558 if (!list_empty(&block->ccw_queue))
2559 break;
2560 spin_lock_irqsave(
2561 get_ccwdev_lock(basedev->cdev), flags);
2562 dasd_device_set_stop_bits(basedev,
2563 DASD_STOPPED_PENDING);
2564 spin_unlock_irqrestore(
2565 get_ccwdev_lock(basedev->cdev), flags);
2566 dasd_block_set_timer(block, HZ/2);
2567 break;
2568 }
2569 DBF_DEV_EVENT(DBF_ERR, basedev,
2570 "CCW creation failed (rc=%ld) "
2571 "on request %p",
2572 PTR_ERR(cqr), req);
2573 blk_start_request(req);
2574 __blk_end_request_all(req, -EIO);
2575 continue;
2576 }
2577 /*
2578 * Note: callback is set to dasd_return_cqr_cb in
2579 * __dasd_block_start_head to cover erp requests as well
2580 */
2581 cqr->callback_data = (void *) req;
2582 cqr->status = DASD_CQR_FILLED;
2583 req->completion_data = cqr;
2584 blk_start_request(req);
2585 list_add_tail(&cqr->blocklist, &block->ccw_queue);
2586 INIT_LIST_HEAD(&cqr->devlist);
2587 dasd_profile_start(block, cqr, req);
2588 }
2589 }
2590
2591 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
2592 {
2593 struct request *req;
2594 int status;
2595 int error = 0;
2596
2597 req = (struct request *) cqr->callback_data;
2598 dasd_profile_end(cqr->block, cqr, req);
2599 status = cqr->block->base->discipline->free_cp(cqr, req);
2600 if (status < 0)
2601 error = status;
2602 else if (status == 0) {
2603 if (cqr->intrc == -EPERM)
2604 error = -EBADE;
2605 else if (cqr->intrc == -ENOLINK ||
2606 cqr->intrc == -ETIMEDOUT)
2607 error = cqr->intrc;
2608 else
2609 error = -EIO;
2610 }
2611 __blk_end_request_all(req, error);
2612 }
2613
2614 /*
2615 * Process ccw request queue.
2616 */
2617 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
2618 struct list_head *final_queue)
2619 {
2620 struct list_head *l, *n;
2621 struct dasd_ccw_req *cqr;
2622 dasd_erp_fn_t erp_fn;
2623 unsigned long flags;
2624 struct dasd_device *base = block->base;
2625
2626 restart:
2627 /* Process request with final status. */
2628 list_for_each_safe(l, n, &block->ccw_queue) {
2629 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2630 if (cqr->status != DASD_CQR_DONE &&
2631 cqr->status != DASD_CQR_FAILED &&
2632 cqr->status != DASD_CQR_NEED_ERP &&
2633 cqr->status != DASD_CQR_TERMINATED)
2634 continue;
2635
2636 if (cqr->status == DASD_CQR_TERMINATED) {
2637 base->discipline->handle_terminated_request(cqr);
2638 goto restart;
2639 }
2640
2641 /* Process requests that may be recovered */
2642 if (cqr->status == DASD_CQR_NEED_ERP) {
2643 erp_fn = base->discipline->erp_action(cqr);
2644 if (IS_ERR(erp_fn(cqr)))
2645 continue;
2646 goto restart;
2647 }
2648
2649 /* log sense for fatal error */
2650 if (cqr->status == DASD_CQR_FAILED) {
2651 dasd_log_sense(cqr, &cqr->irb);
2652 }
2653
2654 /* First of all call extended error reporting. */
2655 if (dasd_eer_enabled(base) &&
2656 cqr->status == DASD_CQR_FAILED) {
2657 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
2658
2659 /* restart request */
2660 cqr->status = DASD_CQR_FILLED;
2661 cqr->retries = 255;
2662 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
2663 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
2664 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
2665 flags);
2666 goto restart;
2667 }
2668
2669 /* Process finished ERP request. */
2670 if (cqr->refers) {
2671 __dasd_process_erp(base, cqr);
2672 goto restart;
2673 }
2674
2675 /* Rechain finished requests to final queue */
2676 cqr->endclk = get_tod_clock();
2677 list_move_tail(&cqr->blocklist, final_queue);
2678 }
2679 }
2680
2681 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
2682 {
2683 dasd_schedule_block_bh(cqr->block);
2684 }
2685
2686 static void __dasd_block_start_head(struct dasd_block *block)
2687 {
2688 struct dasd_ccw_req *cqr;
2689
2690 if (list_empty(&block->ccw_queue))
2691 return;
2692 /* We allways begin with the first requests on the queue, as some
2693 * of previously started requests have to be enqueued on a
2694 * dasd_device again for error recovery.
2695 */
2696 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
2697 if (cqr->status != DASD_CQR_FILLED)
2698 continue;
2699 if (test_bit(DASD_FLAG_LOCK_STOLEN, &block->base->flags) &&
2700 !test_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags)) {
2701 cqr->status = DASD_CQR_FAILED;
2702 cqr->intrc = -EPERM;
2703 dasd_schedule_block_bh(block);
2704 continue;
2705 }
2706 /* Non-temporary stop condition will trigger fail fast */
2707 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
2708 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
2709 (!dasd_eer_enabled(block->base))) {
2710 cqr->status = DASD_CQR_FAILED;
2711 cqr->intrc = -ENOLINK;
2712 dasd_schedule_block_bh(block);
2713 continue;
2714 }
2715 /* Don't try to start requests if device is stopped */
2716 if (block->base->stopped)
2717 return;
2718
2719 /* just a fail safe check, should not happen */
2720 if (!cqr->startdev)
2721 cqr->startdev = block->base;
2722
2723 /* make sure that the requests we submit find their way back */
2724 cqr->callback = dasd_return_cqr_cb;
2725
2726 dasd_add_request_tail(cqr);
2727 }
2728 }
2729
2730 /*
2731 * Central dasd_block layer routine. Takes requests from the generic
2732 * block layer request queue, creates ccw requests, enqueues them on
2733 * a dasd_device and processes ccw requests that have been returned.
2734 */
2735 static void dasd_block_tasklet(struct dasd_block *block)
2736 {
2737 struct list_head final_queue;
2738 struct list_head *l, *n;
2739 struct dasd_ccw_req *cqr;
2740
2741 atomic_set(&block->tasklet_scheduled, 0);
2742 INIT_LIST_HEAD(&final_queue);
2743 spin_lock(&block->queue_lock);
2744 /* Finish off requests on ccw queue */
2745 __dasd_process_block_ccw_queue(block, &final_queue);
2746 spin_unlock(&block->queue_lock);
2747 /* Now call the callback function of requests with final status */
2748 spin_lock_irq(&block->request_queue_lock);
2749 list_for_each_safe(l, n, &final_queue) {
2750 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2751 list_del_init(&cqr->blocklist);
2752 __dasd_cleanup_cqr(cqr);
2753 }
2754 spin_lock(&block->queue_lock);
2755 /* Get new request from the block device request queue */
2756 __dasd_process_request_queue(block);
2757 /* Now check if the head of the ccw queue needs to be started. */
2758 __dasd_block_start_head(block);
2759 spin_unlock(&block->queue_lock);
2760 spin_unlock_irq(&block->request_queue_lock);
2761 if (waitqueue_active(&shutdown_waitq))
2762 wake_up(&shutdown_waitq);
2763 dasd_put_device(block->base);
2764 }
2765
2766 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2767 {
2768 wake_up(&dasd_flush_wq);
2769 }
2770
2771 /*
2772 * Requeue a request back to the block request queue
2773 * only works for block requests
2774 */
2775 static int _dasd_requeue_request(struct dasd_ccw_req *cqr)
2776 {
2777 struct dasd_block *block = cqr->block;
2778 struct request *req;
2779 unsigned long flags;
2780
2781 if (!block)
2782 return -EINVAL;
2783 spin_lock_irqsave(&block->queue_lock, flags);
2784 req = (struct request *) cqr->callback_data;
2785 blk_requeue_request(block->request_queue, req);
2786 spin_unlock_irqrestore(&block->queue_lock, flags);
2787
2788 return 0;
2789 }
2790
2791 /*
2792 * Go through all request on the dasd_block request queue, cancel them
2793 * on the respective dasd_device, and return them to the generic
2794 * block layer.
2795 */
2796 static int dasd_flush_block_queue(struct dasd_block *block)
2797 {
2798 struct dasd_ccw_req *cqr, *n;
2799 int rc, i;
2800 struct list_head flush_queue;
2801
2802 INIT_LIST_HEAD(&flush_queue);
2803 spin_lock_bh(&block->queue_lock);
2804 rc = 0;
2805 restart:
2806 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2807 /* if this request currently owned by a dasd_device cancel it */
2808 if (cqr->status >= DASD_CQR_QUEUED)
2809 rc = dasd_cancel_req(cqr);
2810 if (rc < 0)
2811 break;
2812 /* Rechain request (including erp chain) so it won't be
2813 * touched by the dasd_block_tasklet anymore.
2814 * Replace the callback so we notice when the request
2815 * is returned from the dasd_device layer.
2816 */
2817 cqr->callback = _dasd_wake_block_flush_cb;
2818 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2819 list_move_tail(&cqr->blocklist, &flush_queue);
2820 if (i > 1)
2821 /* moved more than one request - need to restart */
2822 goto restart;
2823 }
2824 spin_unlock_bh(&block->queue_lock);
2825 /* Now call the callback function of flushed requests */
2826 restart_cb:
2827 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2828 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2829 /* Process finished ERP request. */
2830 if (cqr->refers) {
2831 spin_lock_bh(&block->queue_lock);
2832 __dasd_process_erp(block->base, cqr);
2833 spin_unlock_bh(&block->queue_lock);
2834 /* restart list_for_xx loop since dasd_process_erp
2835 * might remove multiple elements */
2836 goto restart_cb;
2837 }
2838 /* call the callback function */
2839 spin_lock_irq(&block->request_queue_lock);
2840 cqr->endclk = get_tod_clock();
2841 list_del_init(&cqr->blocklist);
2842 __dasd_cleanup_cqr(cqr);
2843 spin_unlock_irq(&block->request_queue_lock);
2844 }
2845 return rc;
2846 }
2847
2848 /*
2849 * Schedules a call to dasd_tasklet over the device tasklet.
2850 */
2851 void dasd_schedule_block_bh(struct dasd_block *block)
2852 {
2853 /* Protect against rescheduling. */
2854 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2855 return;
2856 /* life cycle of block is bound to it's base device */
2857 dasd_get_device(block->base);
2858 tasklet_hi_schedule(&block->tasklet);
2859 }
2860
2861
2862 /*
2863 * SECTION: external block device operations
2864 * (request queue handling, open, release, etc.)
2865 */
2866
2867 /*
2868 * Dasd request queue function. Called from ll_rw_blk.c
2869 */
2870 static void do_dasd_request(struct request_queue *queue)
2871 {
2872 struct dasd_block *block;
2873
2874 block = queue->queuedata;
2875 spin_lock(&block->queue_lock);
2876 /* Get new request from the block device request queue */
2877 __dasd_process_request_queue(block);
2878 /* Now check if the head of the ccw queue needs to be started. */
2879 __dasd_block_start_head(block);
2880 spin_unlock(&block->queue_lock);
2881 }
2882
2883 /*
2884 * Block timeout callback, called from the block layer
2885 *
2886 * request_queue lock is held on entry.
2887 *
2888 * Return values:
2889 * BLK_EH_RESET_TIMER if the request should be left running
2890 * BLK_EH_NOT_HANDLED if the request is handled or terminated
2891 * by the driver.
2892 */
2893 enum blk_eh_timer_return dasd_times_out(struct request *req)
2894 {
2895 struct dasd_ccw_req *cqr = req->completion_data;
2896 struct dasd_block *block = req->q->queuedata;
2897 struct dasd_device *device;
2898 int rc = 0;
2899
2900 if (!cqr)
2901 return BLK_EH_NOT_HANDLED;
2902
2903 device = cqr->startdev ? cqr->startdev : block->base;
2904 if (!device->blk_timeout)
2905 return BLK_EH_RESET_TIMER;
2906 DBF_DEV_EVENT(DBF_WARNING, device,
2907 " dasd_times_out cqr %p status %x",
2908 cqr, cqr->status);
2909
2910 spin_lock(&block->queue_lock);
2911 spin_lock(get_ccwdev_lock(device->cdev));
2912 cqr->retries = -1;
2913 cqr->intrc = -ETIMEDOUT;
2914 if (cqr->status >= DASD_CQR_QUEUED) {
2915 spin_unlock(get_ccwdev_lock(device->cdev));
2916 rc = dasd_cancel_req(cqr);
2917 } else if (cqr->status == DASD_CQR_FILLED ||
2918 cqr->status == DASD_CQR_NEED_ERP) {
2919 cqr->status = DASD_CQR_TERMINATED;
2920 spin_unlock(get_ccwdev_lock(device->cdev));
2921 } else if (cqr->status == DASD_CQR_IN_ERP) {
2922 struct dasd_ccw_req *searchcqr, *nextcqr, *tmpcqr;
2923
2924 list_for_each_entry_safe(searchcqr, nextcqr,
2925 &block->ccw_queue, blocklist) {
2926 tmpcqr = searchcqr;
2927 while (tmpcqr->refers)
2928 tmpcqr = tmpcqr->refers;
2929 if (tmpcqr != cqr)
2930 continue;
2931 /* searchcqr is an ERP request for cqr */
2932 searchcqr->retries = -1;
2933 searchcqr->intrc = -ETIMEDOUT;
2934 if (searchcqr->status >= DASD_CQR_QUEUED) {
2935 spin_unlock(get_ccwdev_lock(device->cdev));
2936 rc = dasd_cancel_req(searchcqr);
2937 spin_lock(get_ccwdev_lock(device->cdev));
2938 } else if ((searchcqr->status == DASD_CQR_FILLED) ||
2939 (searchcqr->status == DASD_CQR_NEED_ERP)) {
2940 searchcqr->status = DASD_CQR_TERMINATED;
2941 rc = 0;
2942 } else if (searchcqr->status == DASD_CQR_IN_ERP) {
2943 /*
2944 * Shouldn't happen; most recent ERP
2945 * request is at the front of queue
2946 */
2947 continue;
2948 }
2949 break;
2950 }
2951 spin_unlock(get_ccwdev_lock(device->cdev));
2952 }
2953 dasd_schedule_block_bh(block);
2954 spin_unlock(&block->queue_lock);
2955
2956 return rc ? BLK_EH_RESET_TIMER : BLK_EH_NOT_HANDLED;
2957 }
2958
2959 /*
2960 * Allocate and initialize request queue and default I/O scheduler.
2961 */
2962 static int dasd_alloc_queue(struct dasd_block *block)
2963 {
2964 int rc;
2965
2966 block->request_queue = blk_init_queue(do_dasd_request,
2967 &block->request_queue_lock);
2968 if (block->request_queue == NULL)
2969 return -ENOMEM;
2970
2971 block->request_queue->queuedata = block;
2972
2973 elevator_exit(block->request_queue->elevator);
2974 block->request_queue->elevator = NULL;
2975 rc = elevator_init(block->request_queue, "deadline");
2976 if (rc) {
2977 blk_cleanup_queue(block->request_queue);
2978 return rc;
2979 }
2980 return 0;
2981 }
2982
2983 /*
2984 * Allocate and initialize request queue.
2985 */
2986 static void dasd_setup_queue(struct dasd_block *block)
2987 {
2988 int max;
2989
2990 if (block->base->features & DASD_FEATURE_USERAW) {
2991 /*
2992 * the max_blocks value for raw_track access is 256
2993 * it is higher than the native ECKD value because we
2994 * only need one ccw per track
2995 * so the max_hw_sectors are
2996 * 2048 x 512B = 1024kB = 16 tracks
2997 */
2998 max = 2048;
2999 } else {
3000 max = block->base->discipline->max_blocks << block->s2b_shift;
3001 }
3002 blk_queue_logical_block_size(block->request_queue,
3003 block->bp_block);
3004 blk_queue_max_hw_sectors(block->request_queue, max);
3005 blk_queue_max_segments(block->request_queue, -1L);
3006 /* with page sized segments we can translate each segement into
3007 * one idaw/tidaw
3008 */
3009 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
3010 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
3011 }
3012
3013 /*
3014 * Deactivate and free request queue.
3015 */
3016 static void dasd_free_queue(struct dasd_block *block)
3017 {
3018 if (block->request_queue) {
3019 blk_cleanup_queue(block->request_queue);
3020 block->request_queue = NULL;
3021 }
3022 }
3023
3024 /*
3025 * Flush request on the request queue.
3026 */
3027 static void dasd_flush_request_queue(struct dasd_block *block)
3028 {
3029 struct request *req;
3030
3031 if (!block->request_queue)
3032 return;
3033
3034 spin_lock_irq(&block->request_queue_lock);
3035 while ((req = blk_fetch_request(block->request_queue)))
3036 __blk_end_request_all(req, -EIO);
3037 spin_unlock_irq(&block->request_queue_lock);
3038 }
3039
3040 static int dasd_open(struct block_device *bdev, fmode_t mode)
3041 {
3042 struct dasd_device *base;
3043 int rc;
3044
3045 base = dasd_device_from_gendisk(bdev->bd_disk);
3046 if (!base)
3047 return -ENODEV;
3048
3049 atomic_inc(&base->block->open_count);
3050 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
3051 rc = -ENODEV;
3052 goto unlock;
3053 }
3054
3055 if (!try_module_get(base->discipline->owner)) {
3056 rc = -EINVAL;
3057 goto unlock;
3058 }
3059
3060 if (dasd_probeonly) {
3061 dev_info(&base->cdev->dev,
3062 "Accessing the DASD failed because it is in "
3063 "probeonly mode\n");
3064 rc = -EPERM;
3065 goto out;
3066 }
3067
3068 if (base->state <= DASD_STATE_BASIC) {
3069 DBF_DEV_EVENT(DBF_ERR, base, " %s",
3070 " Cannot open unrecognized device");
3071 rc = -ENODEV;
3072 goto out;
3073 }
3074
3075 if ((mode & FMODE_WRITE) &&
3076 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
3077 (base->features & DASD_FEATURE_READONLY))) {
3078 rc = -EROFS;
3079 goto out;
3080 }
3081
3082 dasd_put_device(base);
3083 return 0;
3084
3085 out:
3086 module_put(base->discipline->owner);
3087 unlock:
3088 atomic_dec(&base->block->open_count);
3089 dasd_put_device(base);
3090 return rc;
3091 }
3092
3093 static void dasd_release(struct gendisk *disk, fmode_t mode)
3094 {
3095 struct dasd_device *base = dasd_device_from_gendisk(disk);
3096 if (base) {
3097 atomic_dec(&base->block->open_count);
3098 module_put(base->discipline->owner);
3099 dasd_put_device(base);
3100 }
3101 }
3102
3103 /*
3104 * Return disk geometry.
3105 */
3106 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3107 {
3108 struct dasd_device *base;
3109
3110 base = dasd_device_from_gendisk(bdev->bd_disk);
3111 if (!base)
3112 return -ENODEV;
3113
3114 if (!base->discipline ||
3115 !base->discipline->fill_geometry) {
3116 dasd_put_device(base);
3117 return -EINVAL;
3118 }
3119 base->discipline->fill_geometry(base->block, geo);
3120 geo->start = get_start_sect(bdev) >> base->block->s2b_shift;
3121 dasd_put_device(base);
3122 return 0;
3123 }
3124
3125 const struct block_device_operations
3126 dasd_device_operations = {
3127 .owner = THIS_MODULE,
3128 .open = dasd_open,
3129 .release = dasd_release,
3130 .ioctl = dasd_ioctl,
3131 .compat_ioctl = dasd_ioctl,
3132 .getgeo = dasd_getgeo,
3133 };
3134
3135 /*******************************************************************************
3136 * end of block device operations
3137 */
3138
3139 static void
3140 dasd_exit(void)
3141 {
3142 #ifdef CONFIG_PROC_FS
3143 dasd_proc_exit();
3144 #endif
3145 dasd_eer_exit();
3146 if (dasd_page_cache != NULL) {
3147 kmem_cache_destroy(dasd_page_cache);
3148 dasd_page_cache = NULL;
3149 }
3150 dasd_gendisk_exit();
3151 dasd_devmap_exit();
3152 if (dasd_debug_area != NULL) {
3153 debug_unregister(dasd_debug_area);
3154 dasd_debug_area = NULL;
3155 }
3156 dasd_statistics_removeroot();
3157 }
3158
3159 /*
3160 * SECTION: common functions for ccw_driver use
3161 */
3162
3163 /*
3164 * Is the device read-only?
3165 * Note that this function does not report the setting of the
3166 * readonly device attribute, but how it is configured in z/VM.
3167 */
3168 int dasd_device_is_ro(struct dasd_device *device)
3169 {
3170 struct ccw_dev_id dev_id;
3171 struct diag210 diag_data;
3172 int rc;
3173
3174 if (!MACHINE_IS_VM)
3175 return 0;
3176 ccw_device_get_id(device->cdev, &dev_id);
3177 memset(&diag_data, 0, sizeof(diag_data));
3178 diag_data.vrdcdvno = dev_id.devno;
3179 diag_data.vrdclen = sizeof(diag_data);
3180 rc = diag210(&diag_data);
3181 if (rc == 0 || rc == 2) {
3182 return diag_data.vrdcvfla & 0x80;
3183 } else {
3184 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
3185 dev_id.devno, rc);
3186 return 0;
3187 }
3188 }
3189 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
3190
3191 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
3192 {
3193 struct ccw_device *cdev = data;
3194 int ret;
3195
3196 ret = ccw_device_set_online(cdev);
3197 if (ret)
3198 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
3199 dev_name(&cdev->dev), ret);
3200 }
3201
3202 /*
3203 * Initial attempt at a probe function. this can be simplified once
3204 * the other detection code is gone.
3205 */
3206 int dasd_generic_probe(struct ccw_device *cdev,
3207 struct dasd_discipline *discipline)
3208 {
3209 int ret;
3210
3211 ret = dasd_add_sysfs_files(cdev);
3212 if (ret) {
3213 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
3214 "dasd_generic_probe: could not add "
3215 "sysfs entries");
3216 return ret;
3217 }
3218 cdev->handler = &dasd_int_handler;
3219
3220 /*
3221 * Automatically online either all dasd devices (dasd_autodetect)
3222 * or all devices specified with dasd= parameters during
3223 * initial probe.
3224 */
3225 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
3226 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
3227 async_schedule(dasd_generic_auto_online, cdev);
3228 return 0;
3229 }
3230
3231 /*
3232 * This will one day be called from a global not_oper handler.
3233 * It is also used by driver_unregister during module unload.
3234 */
3235 void dasd_generic_remove(struct ccw_device *cdev)
3236 {
3237 struct dasd_device *device;
3238 struct dasd_block *block;
3239
3240 cdev->handler = NULL;
3241
3242 device = dasd_device_from_cdev(cdev);
3243 if (IS_ERR(device)) {
3244 dasd_remove_sysfs_files(cdev);
3245 return;
3246 }
3247 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags) &&
3248 !test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3249 /* Already doing offline processing */
3250 dasd_put_device(device);
3251 dasd_remove_sysfs_files(cdev);
3252 return;
3253 }
3254 /*
3255 * This device is removed unconditionally. Set offline
3256 * flag to prevent dasd_open from opening it while it is
3257 * no quite down yet.
3258 */
3259 dasd_set_target_state(device, DASD_STATE_NEW);
3260 /* dasd_delete_device destroys the device reference. */
3261 block = device->block;
3262 dasd_delete_device(device);
3263 /*
3264 * life cycle of block is bound to device, so delete it after
3265 * device was safely removed
3266 */
3267 if (block)
3268 dasd_free_block(block);
3269
3270 dasd_remove_sysfs_files(cdev);
3271 }
3272
3273 /*
3274 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
3275 * the device is detected for the first time and is supposed to be used
3276 * or the user has started activation through sysfs.
3277 */
3278 int dasd_generic_set_online(struct ccw_device *cdev,
3279 struct dasd_discipline *base_discipline)
3280 {
3281 struct dasd_discipline *discipline;
3282 struct dasd_device *device;
3283 int rc;
3284
3285 /* first online clears initial online feature flag */
3286 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
3287 device = dasd_create_device(cdev);
3288 if (IS_ERR(device))
3289 return PTR_ERR(device);
3290
3291 discipline = base_discipline;
3292 if (device->features & DASD_FEATURE_USEDIAG) {
3293 if (!dasd_diag_discipline_pointer) {
3294 pr_warning("%s Setting the DASD online failed because "
3295 "of missing DIAG discipline\n",
3296 dev_name(&cdev->dev));
3297 dasd_delete_device(device);
3298 return -ENODEV;
3299 }
3300 discipline = dasd_diag_discipline_pointer;
3301 }
3302 if (!try_module_get(base_discipline->owner)) {
3303 dasd_delete_device(device);
3304 return -EINVAL;
3305 }
3306 if (!try_module_get(discipline->owner)) {
3307 module_put(base_discipline->owner);
3308 dasd_delete_device(device);
3309 return -EINVAL;
3310 }
3311 device->base_discipline = base_discipline;
3312 device->discipline = discipline;
3313
3314 /* check_device will allocate block device if necessary */
3315 rc = discipline->check_device(device);
3316 if (rc) {
3317 pr_warning("%s Setting the DASD online with discipline %s "
3318 "failed with rc=%i\n",
3319 dev_name(&cdev->dev), discipline->name, rc);
3320 module_put(discipline->owner);
3321 module_put(base_discipline->owner);
3322 dasd_delete_device(device);
3323 return rc;
3324 }
3325
3326 dasd_set_target_state(device, DASD_STATE_ONLINE);
3327 if (device->state <= DASD_STATE_KNOWN) {
3328 pr_warning("%s Setting the DASD online failed because of a "
3329 "missing discipline\n", dev_name(&cdev->dev));
3330 rc = -ENODEV;
3331 dasd_set_target_state(device, DASD_STATE_NEW);
3332 if (device->block)
3333 dasd_free_block(device->block);
3334 dasd_delete_device(device);
3335 } else
3336 pr_debug("dasd_generic device %s found\n",
3337 dev_name(&cdev->dev));
3338
3339 wait_event(dasd_init_waitq, _wait_for_device(device));
3340
3341 dasd_put_device(device);
3342 return rc;
3343 }
3344
3345 int dasd_generic_set_offline(struct ccw_device *cdev)
3346 {
3347 struct dasd_device *device;
3348 struct dasd_block *block;
3349 int max_count, open_count, rc;
3350
3351 rc = 0;
3352 device = dasd_device_from_cdev(cdev);
3353 if (IS_ERR(device))
3354 return PTR_ERR(device);
3355
3356 /*
3357 * We must make sure that this device is currently not in use.
3358 * The open_count is increased for every opener, that includes
3359 * the blkdev_get in dasd_scan_partitions. We are only interested
3360 * in the other openers.
3361 */
3362 if (device->block) {
3363 max_count = device->block->bdev ? 0 : -1;
3364 open_count = atomic_read(&device->block->open_count);
3365 if (open_count > max_count) {
3366 if (open_count > 0)
3367 pr_warning("%s: The DASD cannot be set offline "
3368 "with open count %i\n",
3369 dev_name(&cdev->dev), open_count);
3370 else
3371 pr_warning("%s: The DASD cannot be set offline "
3372 "while it is in use\n",
3373 dev_name(&cdev->dev));
3374 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3375 dasd_put_device(device);
3376 return -EBUSY;
3377 }
3378 }
3379
3380 if (test_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3381 /*
3382 * safe offline allready running
3383 * could only be called by normal offline so safe_offline flag
3384 * needs to be removed to run normal offline and kill all I/O
3385 */
3386 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3387 /* Already doing normal offline processing */
3388 dasd_put_device(device);
3389 return -EBUSY;
3390 } else
3391 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3392
3393 } else
3394 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
3395 /* Already doing offline processing */
3396 dasd_put_device(device);
3397 return -EBUSY;
3398 }
3399
3400 /*
3401 * if safe_offline called set safe_offline_running flag and
3402 * clear safe_offline so that a call to normal offline
3403 * can overrun safe_offline processing
3404 */
3405 if (test_and_clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags) &&
3406 !test_and_set_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags)) {
3407 /*
3408 * If we want to set the device safe offline all IO operations
3409 * should be finished before continuing the offline process
3410 * so sync bdev first and then wait for our queues to become
3411 * empty
3412 */
3413 /* sync blockdev and partitions */
3414 rc = fsync_bdev(device->block->bdev);
3415 if (rc != 0)
3416 goto interrupted;
3417
3418 /* schedule device tasklet and wait for completion */
3419 dasd_schedule_device_bh(device);
3420 rc = wait_event_interruptible(shutdown_waitq,
3421 _wait_for_empty_queues(device));
3422 if (rc != 0)
3423 goto interrupted;
3424 }
3425
3426 set_bit(DASD_FLAG_OFFLINE, &device->flags);
3427 dasd_set_target_state(device, DASD_STATE_NEW);
3428 /* dasd_delete_device destroys the device reference. */
3429 block = device->block;
3430 dasd_delete_device(device);
3431 /*
3432 * life cycle of block is bound to device, so delete it after
3433 * device was safely removed
3434 */
3435 if (block)
3436 dasd_free_block(block);
3437 return 0;
3438
3439 interrupted:
3440 /* interrupted by signal */
3441 clear_bit(DASD_FLAG_SAFE_OFFLINE, &device->flags);
3442 clear_bit(DASD_FLAG_SAFE_OFFLINE_RUNNING, &device->flags);
3443 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
3444 dasd_put_device(device);
3445 return rc;
3446 }
3447
3448 int dasd_generic_last_path_gone(struct dasd_device *device)
3449 {
3450 struct dasd_ccw_req *cqr;
3451
3452 dev_warn(&device->cdev->dev, "No operational channel path is left "
3453 "for the device\n");
3454 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "last path gone");
3455 /* First of all call extended error reporting. */
3456 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3457
3458 if (device->state < DASD_STATE_BASIC)
3459 return 0;
3460 /* Device is active. We want to keep it. */
3461 list_for_each_entry(cqr, &device->ccw_queue, devlist)
3462 if ((cqr->status == DASD_CQR_IN_IO) ||
3463 (cqr->status == DASD_CQR_CLEAR_PENDING)) {
3464 cqr->status = DASD_CQR_QUEUED;
3465 cqr->retries++;
3466 }
3467 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
3468 dasd_device_clear_timer(device);
3469 dasd_schedule_device_bh(device);
3470 return 1;
3471 }
3472 EXPORT_SYMBOL_GPL(dasd_generic_last_path_gone);
3473
3474 int dasd_generic_path_operational(struct dasd_device *device)
3475 {
3476 dev_info(&device->cdev->dev, "A channel path to the device has become "
3477 "operational\n");
3478 DBF_DEV_EVENT(DBF_WARNING, device, "%s", "path operational");
3479 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
3480 if (device->stopped & DASD_UNRESUMED_PM) {
3481 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
3482 dasd_restore_device(device);
3483 return 1;
3484 }
3485 dasd_schedule_device_bh(device);
3486 if (device->block)
3487 dasd_schedule_block_bh(device->block);
3488 return 1;
3489 }
3490 EXPORT_SYMBOL_GPL(dasd_generic_path_operational);
3491
3492 int dasd_generic_notify(struct ccw_device *cdev, int event)
3493 {
3494 struct dasd_device *device;
3495 int ret;
3496
3497 device = dasd_device_from_cdev_locked(cdev);
3498 if (IS_ERR(device))
3499 return 0;
3500 ret = 0;
3501 switch (event) {
3502 case CIO_GONE:
3503 case CIO_BOXED:
3504 case CIO_NO_PATH:
3505 device->path_data.opm = 0;
3506 device->path_data.ppm = 0;
3507 device->path_data.npm = 0;
3508 ret = dasd_generic_last_path_gone(device);
3509 break;
3510 case CIO_OPER:
3511 ret = 1;
3512 if (device->path_data.opm)
3513 ret = dasd_generic_path_operational(device);
3514 break;
3515 }
3516 dasd_put_device(device);
3517 return ret;
3518 }
3519
3520 void dasd_generic_path_event(struct ccw_device *cdev, int *path_event)
3521 {
3522 int chp;
3523 __u8 oldopm, eventlpm;
3524 struct dasd_device *device;
3525
3526 device = dasd_device_from_cdev_locked(cdev);
3527 if (IS_ERR(device))
3528 return;
3529 for (chp = 0; chp < 8; chp++) {
3530 eventlpm = 0x80 >> chp;
3531 if (path_event[chp] & PE_PATH_GONE) {
3532 oldopm = device->path_data.opm;
3533 device->path_data.opm &= ~eventlpm;
3534 device->path_data.ppm &= ~eventlpm;
3535 device->path_data.npm &= ~eventlpm;
3536 if (oldopm && !device->path_data.opm) {
3537 dev_warn(&device->cdev->dev,
3538 "No verified channel paths remain "
3539 "for the device\n");
3540 DBF_DEV_EVENT(DBF_WARNING, device,
3541 "%s", "last verified path gone");
3542 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
3543 dasd_device_set_stop_bits(device,
3544 DASD_STOPPED_DC_WAIT);
3545 }
3546 }
3547 if (path_event[chp] & PE_PATH_AVAILABLE) {
3548 device->path_data.opm &= ~eventlpm;
3549 device->path_data.ppm &= ~eventlpm;
3550 device->path_data.npm &= ~eventlpm;
3551 device->path_data.tbvpm |= eventlpm;
3552 dasd_schedule_device_bh(device);
3553 }
3554 if (path_event[chp] & PE_PATHGROUP_ESTABLISHED) {
3555 if (!(device->path_data.opm & eventlpm) &&
3556 !(device->path_data.tbvpm & eventlpm)) {
3557 /*
3558 * we can not establish a pathgroup on an
3559 * unavailable path, so trigger a path
3560 * verification first
3561 */
3562 device->path_data.tbvpm |= eventlpm;
3563 dasd_schedule_device_bh(device);
3564 }
3565 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
3566 "Pathgroup re-established\n");
3567 if (device->discipline->kick_validate)
3568 device->discipline->kick_validate(device);
3569 }
3570 }
3571 dasd_put_device(device);
3572 }
3573 EXPORT_SYMBOL_GPL(dasd_generic_path_event);
3574
3575 int dasd_generic_verify_path(struct dasd_device *device, __u8 lpm)
3576 {
3577 if (!device->path_data.opm && lpm) {
3578 device->path_data.opm = lpm;
3579 dasd_generic_path_operational(device);
3580 } else
3581 device->path_data.opm |= lpm;
3582 return 0;
3583 }
3584 EXPORT_SYMBOL_GPL(dasd_generic_verify_path);
3585
3586
3587 int dasd_generic_pm_freeze(struct ccw_device *cdev)
3588 {
3589 struct dasd_device *device = dasd_device_from_cdev(cdev);
3590 struct list_head freeze_queue;
3591 struct dasd_ccw_req *cqr, *n;
3592 struct dasd_ccw_req *refers;
3593 int rc;
3594
3595 if (IS_ERR(device))
3596 return PTR_ERR(device);
3597
3598 /* mark device as suspended */
3599 set_bit(DASD_FLAG_SUSPENDED, &device->flags);
3600
3601 if (device->discipline->freeze)
3602 rc = device->discipline->freeze(device);
3603
3604 /* disallow new I/O */
3605 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
3606
3607 /* clear active requests and requeue them to block layer if possible */
3608 INIT_LIST_HEAD(&freeze_queue);
3609 spin_lock_irq(get_ccwdev_lock(cdev));
3610 rc = 0;
3611 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
3612 /* Check status and move request to flush_queue */
3613 if (cqr->status == DASD_CQR_IN_IO) {
3614 rc = device->discipline->term_IO(cqr);
3615 if (rc) {
3616 /* unable to terminate requeust */
3617 dev_err(&device->cdev->dev,
3618 "Unable to terminate request %p "
3619 "on suspend\n", cqr);
3620 spin_unlock_irq(get_ccwdev_lock(cdev));
3621 dasd_put_device(device);
3622 return rc;
3623 }
3624 }
3625 list_move_tail(&cqr->devlist, &freeze_queue);
3626 }
3627 spin_unlock_irq(get_ccwdev_lock(cdev));
3628
3629 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
3630 wait_event(dasd_flush_wq,
3631 (cqr->status != DASD_CQR_CLEAR_PENDING));
3632 if (cqr->status == DASD_CQR_CLEARED)
3633 cqr->status = DASD_CQR_QUEUED;
3634
3635 /* requeue requests to blocklayer will only work for
3636 block device requests */
3637 if (_dasd_requeue_request(cqr))
3638 continue;
3639
3640 /* remove requests from device and block queue */
3641 list_del_init(&cqr->devlist);
3642 while (cqr->refers != NULL) {
3643 refers = cqr->refers;
3644 /* remove the request from the block queue */
3645 list_del(&cqr->blocklist);
3646 /* free the finished erp request */
3647 dasd_free_erp_request(cqr, cqr->memdev);
3648 cqr = refers;
3649 }
3650 if (cqr->block)
3651 list_del_init(&cqr->blocklist);
3652 cqr->block->base->discipline->free_cp(
3653 cqr, (struct request *) cqr->callback_data);
3654 }
3655
3656 /*
3657 * if requests remain then they are internal request
3658 * and go back to the device queue
3659 */
3660 if (!list_empty(&freeze_queue)) {
3661 /* move freeze_queue to start of the ccw_queue */
3662 spin_lock_irq(get_ccwdev_lock(cdev));
3663 list_splice_tail(&freeze_queue, &device->ccw_queue);
3664 spin_unlock_irq(get_ccwdev_lock(cdev));
3665 }
3666 dasd_put_device(device);
3667 return rc;
3668 }
3669 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
3670
3671 int dasd_generic_restore_device(struct ccw_device *cdev)
3672 {
3673 struct dasd_device *device = dasd_device_from_cdev(cdev);
3674 int rc = 0;
3675
3676 if (IS_ERR(device))
3677 return PTR_ERR(device);
3678
3679 /* allow new IO again */
3680 dasd_device_remove_stop_bits(device,
3681 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
3682
3683 dasd_schedule_device_bh(device);
3684
3685 /*
3686 * call discipline restore function
3687 * if device is stopped do nothing e.g. for disconnected devices
3688 */
3689 if (device->discipline->restore && !(device->stopped))
3690 rc = device->discipline->restore(device);
3691 if (rc || device->stopped)
3692 /*
3693 * if the resume failed for the DASD we put it in
3694 * an UNRESUMED stop state
3695 */
3696 device->stopped |= DASD_UNRESUMED_PM;
3697
3698 if (device->block)
3699 dasd_schedule_block_bh(device->block);
3700
3701 clear_bit(DASD_FLAG_SUSPENDED, &device->flags);
3702 dasd_put_device(device);
3703 return 0;
3704 }
3705 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
3706
3707 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
3708 void *rdc_buffer,
3709 int rdc_buffer_size,
3710 int magic)
3711 {
3712 struct dasd_ccw_req *cqr;
3713 struct ccw1 *ccw;
3714 unsigned long *idaw;
3715
3716 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
3717
3718 if (IS_ERR(cqr)) {
3719 /* internal error 13 - Allocating the RDC request failed*/
3720 dev_err(&device->cdev->dev,
3721 "An error occurred in the DASD device driver, "
3722 "reason=%s\n", "13");
3723 return cqr;
3724 }
3725
3726 ccw = cqr->cpaddr;
3727 ccw->cmd_code = CCW_CMD_RDC;
3728 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
3729 idaw = (unsigned long *) (cqr->data);
3730 ccw->cda = (__u32)(addr_t) idaw;
3731 ccw->flags = CCW_FLAG_IDA;
3732 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
3733 } else {
3734 ccw->cda = (__u32)(addr_t) rdc_buffer;
3735 ccw->flags = 0;
3736 }
3737
3738 ccw->count = rdc_buffer_size;
3739 cqr->startdev = device;
3740 cqr->memdev = device;
3741 cqr->expires = 10*HZ;
3742 cqr->retries = 256;
3743 cqr->buildclk = get_tod_clock();
3744 cqr->status = DASD_CQR_FILLED;
3745 return cqr;
3746 }
3747
3748
3749 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
3750 void *rdc_buffer, int rdc_buffer_size)
3751 {
3752 int ret;
3753 struct dasd_ccw_req *cqr;
3754
3755 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
3756 magic);
3757 if (IS_ERR(cqr))
3758 return PTR_ERR(cqr);
3759
3760 ret = dasd_sleep_on(cqr);
3761 dasd_sfree_request(cqr, cqr->memdev);
3762 return ret;
3763 }
3764 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
3765
3766 /*
3767 * In command mode and transport mode we need to look for sense
3768 * data in different places. The sense data itself is allways
3769 * an array of 32 bytes, so we can unify the sense data access
3770 * for both modes.
3771 */
3772 char *dasd_get_sense(struct irb *irb)
3773 {
3774 struct tsb *tsb = NULL;
3775 char *sense = NULL;
3776
3777 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
3778 if (irb->scsw.tm.tcw)
3779 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
3780 irb->scsw.tm.tcw);
3781 if (tsb && tsb->length == 64 && tsb->flags)
3782 switch (tsb->flags & 0x07) {
3783 case 1: /* tsa_iostat */
3784 sense = tsb->tsa.iostat.sense;
3785 break;
3786 case 2: /* tsa_ddpc */
3787 sense = tsb->tsa.ddpc.sense;
3788 break;
3789 default:
3790 /* currently we don't use interrogate data */
3791 break;
3792 }
3793 } else if (irb->esw.esw0.erw.cons) {
3794 sense = irb->ecw;
3795 }
3796 return sense;
3797 }
3798 EXPORT_SYMBOL_GPL(dasd_get_sense);
3799
3800 void dasd_generic_shutdown(struct ccw_device *cdev)
3801 {
3802 struct dasd_device *device;
3803
3804 device = dasd_device_from_cdev(cdev);
3805 if (IS_ERR(device))
3806 return;
3807
3808 if (device->block)
3809 dasd_schedule_block_bh(device->block);
3810
3811 dasd_schedule_device_bh(device);
3812
3813 wait_event(shutdown_waitq, _wait_for_empty_queues(device));
3814 }
3815 EXPORT_SYMBOL_GPL(dasd_generic_shutdown);
3816
3817 static int __init dasd_init(void)
3818 {
3819 int rc;
3820
3821 init_waitqueue_head(&dasd_init_waitq);
3822 init_waitqueue_head(&dasd_flush_wq);
3823 init_waitqueue_head(&generic_waitq);
3824 init_waitqueue_head(&shutdown_waitq);
3825
3826 /* register 'common' DASD debug area, used for all DBF_XXX calls */
3827 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
3828 if (dasd_debug_area == NULL) {
3829 rc = -ENOMEM;
3830 goto failed;
3831 }
3832 debug_register_view(dasd_debug_area, &debug_sprintf_view);
3833 debug_set_level(dasd_debug_area, DBF_WARNING);
3834
3835 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
3836
3837 dasd_diag_discipline_pointer = NULL;
3838
3839 dasd_statistics_createroot();
3840
3841 rc = dasd_devmap_init();
3842 if (rc)
3843 goto failed;
3844 rc = dasd_gendisk_init();
3845 if (rc)
3846 goto failed;
3847 rc = dasd_parse();
3848 if (rc)
3849 goto failed;
3850 rc = dasd_eer_init();
3851 if (rc)
3852 goto failed;
3853 #ifdef CONFIG_PROC_FS
3854 rc = dasd_proc_init();
3855 if (rc)
3856 goto failed;
3857 #endif
3858
3859 return 0;
3860 failed:
3861 pr_info("The DASD device driver could not be initialized\n");
3862 dasd_exit();
3863 return rc;
3864 }
3865
3866 module_init(dasd_init);
3867 module_exit(dasd_exit);
3868
3869 EXPORT_SYMBOL(dasd_debug_area);
3870 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
3871
3872 EXPORT_SYMBOL(dasd_add_request_head);
3873 EXPORT_SYMBOL(dasd_add_request_tail);
3874 EXPORT_SYMBOL(dasd_cancel_req);
3875 EXPORT_SYMBOL(dasd_device_clear_timer);
3876 EXPORT_SYMBOL(dasd_block_clear_timer);
3877 EXPORT_SYMBOL(dasd_enable_device);
3878 EXPORT_SYMBOL(dasd_int_handler);
3879 EXPORT_SYMBOL(dasd_kfree_request);
3880 EXPORT_SYMBOL(dasd_kick_device);
3881 EXPORT_SYMBOL(dasd_kmalloc_request);
3882 EXPORT_SYMBOL(dasd_schedule_device_bh);
3883 EXPORT_SYMBOL(dasd_schedule_block_bh);
3884 EXPORT_SYMBOL(dasd_set_target_state);
3885 EXPORT_SYMBOL(dasd_device_set_timer);
3886 EXPORT_SYMBOL(dasd_block_set_timer);
3887 EXPORT_SYMBOL(dasd_sfree_request);
3888 EXPORT_SYMBOL(dasd_sleep_on);
3889 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
3890 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
3891 EXPORT_SYMBOL(dasd_smalloc_request);
3892 EXPORT_SYMBOL(dasd_start_IO);
3893 EXPORT_SYMBOL(dasd_term_IO);
3894
3895 EXPORT_SYMBOL_GPL(dasd_generic_probe);
3896 EXPORT_SYMBOL_GPL(dasd_generic_remove);
3897 EXPORT_SYMBOL_GPL(dasd_generic_notify);
3898 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
3899 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
3900 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
3901 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
3902 EXPORT_SYMBOL_GPL(dasd_alloc_block);
3903 EXPORT_SYMBOL_GPL(dasd_free_block);
This page took 0.113716 seconds and 5 git commands to generate.