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