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