2 * File...........: linux/drivers/s390/block/dasd.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Horst Hummel <Horst.Hummel@de.ibm.com>
5 * Carsten Otte <Cotte@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Bugreports.to..: <Linux390@de.ibm.com>
8 * Copyright IBM Corp. 1999, 2009
11 #define KMSG_COMPONENT "dasd"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14 #include <linux/kmod.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/ctype.h>
18 #include <linux/major.h>
19 #include <linux/slab.h>
20 #include <linux/buffer_head.h>
21 #include <linux/hdreg.h>
22 #include <linux/async.h>
23 #include <linux/mutex.h>
25 #include <asm/ccwdev.h>
26 #include <asm/ebcdic.h>
27 #include <asm/idals.h>
32 #define PRINTK_HEADER "dasd:"
36 * SECTION: Constant definitions to be used within this file
38 #define DASD_CHANQ_MAX_SIZE 4
40 #define DASD_SLEEPON_START_TAG (void *) 1
41 #define DASD_SLEEPON_END_TAG (void *) 2
44 * SECTION: exported variables of dasd.c
46 debug_info_t *dasd_debug_area;
47 struct dasd_discipline *dasd_diag_discipline_pointer;
48 void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
50 MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
51 MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
52 " Copyright 2000 IBM Corporation");
53 MODULE_SUPPORTED_DEVICE("dasd");
54 MODULE_LICENSE("GPL");
57 * SECTION: prototypes for static functions of dasd.c
59 static int dasd_alloc_queue(struct dasd_block *);
60 static void dasd_setup_queue(struct dasd_block *);
61 static void dasd_free_queue(struct dasd_block *);
62 static void dasd_flush_request_queue(struct dasd_block *);
63 static int dasd_flush_block_queue(struct dasd_block *);
64 static void dasd_device_tasklet(struct dasd_device *);
65 static void dasd_block_tasklet(struct dasd_block *);
66 static void do_kick_device(struct work_struct *);
67 static void do_restore_device(struct work_struct *);
68 static void dasd_return_cqr_cb(struct dasd_ccw_req *, void *);
69 static void dasd_device_timeout(unsigned long);
70 static void dasd_block_timeout(unsigned long);
71 static void __dasd_process_erp(struct dasd_device *, struct dasd_ccw_req *);
74 * SECTION: Operations on the device structure.
76 static wait_queue_head_t dasd_init_waitq;
77 static wait_queue_head_t dasd_flush_wq;
78 static wait_queue_head_t generic_waitq;
81 * Allocate memory for a new device structure.
83 struct dasd_device *dasd_alloc_device(void)
85 struct dasd_device *device;
87 device = kzalloc(sizeof(struct dasd_device), GFP_ATOMIC);
89 return ERR_PTR(-ENOMEM);
91 /* Get two pages for normal block device operations. */
92 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
93 if (!device->ccw_mem) {
95 return ERR_PTR(-ENOMEM);
97 /* Get one page for error recovery. */
98 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
99 if (!device->erp_mem) {
100 free_pages((unsigned long) device->ccw_mem, 1);
102 return ERR_PTR(-ENOMEM);
105 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
106 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
107 spin_lock_init(&device->mem_lock);
108 atomic_set(&device->tasklet_scheduled, 0);
109 tasklet_init(&device->tasklet,
110 (void (*)(unsigned long)) dasd_device_tasklet,
111 (unsigned long) device);
112 INIT_LIST_HEAD(&device->ccw_queue);
113 init_timer(&device->timer);
114 device->timer.function = dasd_device_timeout;
115 device->timer.data = (unsigned long) device;
116 INIT_WORK(&device->kick_work, do_kick_device);
117 INIT_WORK(&device->restore_device, do_restore_device);
118 device->state = DASD_STATE_NEW;
119 device->target = DASD_STATE_NEW;
120 mutex_init(&device->state_mutex);
126 * Free memory of a device structure.
128 void dasd_free_device(struct dasd_device *device)
130 kfree(device->private);
131 free_page((unsigned long) device->erp_mem);
132 free_pages((unsigned long) device->ccw_mem, 1);
137 * Allocate memory for a new device structure.
139 struct dasd_block *dasd_alloc_block(void)
141 struct dasd_block *block;
143 block = kzalloc(sizeof(*block), GFP_ATOMIC);
145 return ERR_PTR(-ENOMEM);
146 /* open_count = 0 means device online but not in use */
147 atomic_set(&block->open_count, -1);
149 spin_lock_init(&block->request_queue_lock);
150 atomic_set(&block->tasklet_scheduled, 0);
151 tasklet_init(&block->tasklet,
152 (void (*)(unsigned long)) dasd_block_tasklet,
153 (unsigned long) block);
154 INIT_LIST_HEAD(&block->ccw_queue);
155 spin_lock_init(&block->queue_lock);
156 init_timer(&block->timer);
157 block->timer.function = dasd_block_timeout;
158 block->timer.data = (unsigned long) block;
164 * Free memory of a device structure.
166 void dasd_free_block(struct dasd_block *block)
172 * Make a new device known to the system.
174 static int dasd_state_new_to_known(struct dasd_device *device)
179 * As long as the device is not in state DASD_STATE_NEW we want to
180 * keep the reference count > 0.
182 dasd_get_device(device);
185 rc = dasd_alloc_queue(device->block);
187 dasd_put_device(device);
191 device->state = DASD_STATE_KNOWN;
196 * Let the system forget about a device.
198 static int dasd_state_known_to_new(struct dasd_device *device)
200 /* Disable extended error reporting for this device. */
201 dasd_eer_disable(device);
202 /* Forget the discipline information. */
203 if (device->discipline) {
204 if (device->discipline->uncheck_device)
205 device->discipline->uncheck_device(device);
206 module_put(device->discipline->owner);
208 device->discipline = NULL;
209 if (device->base_discipline)
210 module_put(device->base_discipline->owner);
211 device->base_discipline = NULL;
212 device->state = DASD_STATE_NEW;
215 dasd_free_queue(device->block);
217 /* Give up reference we took in dasd_state_new_to_known. */
218 dasd_put_device(device);
223 * Request the irq line for the device.
225 static int dasd_state_known_to_basic(struct dasd_device *device)
229 /* Allocate and register gendisk structure. */
231 rc = dasd_gendisk_alloc(device->block);
235 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
236 device->debug_area = debug_register(dev_name(&device->cdev->dev), 4, 1,
238 debug_register_view(device->debug_area, &debug_sprintf_view);
239 debug_set_level(device->debug_area, DBF_WARNING);
240 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
242 device->state = DASD_STATE_BASIC;
247 * Release the irq line for the device. Terminate any running i/o.
249 static int dasd_state_basic_to_known(struct dasd_device *device)
253 dasd_gendisk_free(device->block);
254 dasd_block_clear_timer(device->block);
256 rc = dasd_flush_device_queue(device);
259 dasd_device_clear_timer(device);
261 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
262 if (device->debug_area != NULL) {
263 debug_unregister(device->debug_area);
264 device->debug_area = NULL;
266 device->state = DASD_STATE_KNOWN;
271 * Do the initial analysis. The do_analysis function may return
272 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
273 * until the discipline decides to continue the startup sequence
274 * by calling the function dasd_change_state. The eckd disciplines
275 * uses this to start a ccw that detects the format. The completion
276 * interrupt for this detection ccw uses the kernel event daemon to
277 * trigger the call to dasd_change_state. All this is done in the
278 * discipline code, see dasd_eckd.c.
279 * After the analysis ccw is done (do_analysis returned 0) the block
281 * In case the analysis returns an error, the device setup is stopped
282 * (a fake disk was already added to allow formatting).
284 static int dasd_state_basic_to_ready(struct dasd_device *device)
287 struct dasd_block *block;
290 block = device->block;
291 /* make disk known with correct capacity */
293 if (block->base->discipline->do_analysis != NULL)
294 rc = block->base->discipline->do_analysis(block);
297 device->state = DASD_STATE_UNFMT;
300 dasd_setup_queue(block);
301 set_capacity(block->gdp,
302 block->blocks << block->s2b_shift);
303 device->state = DASD_STATE_READY;
304 rc = dasd_scan_partitions(block);
306 device->state = DASD_STATE_BASIC;
308 device->state = DASD_STATE_READY;
314 * Remove device from block device layer. Destroy dirty buffers.
315 * Forget format information. Check if the target level is basic
316 * and if it is create fake disk for formatting.
318 static int dasd_state_ready_to_basic(struct dasd_device *device)
322 device->state = DASD_STATE_BASIC;
324 struct dasd_block *block = device->block;
325 rc = dasd_flush_block_queue(block);
327 device->state = DASD_STATE_READY;
330 dasd_flush_request_queue(block);
331 dasd_destroy_partitions(block);
334 block->s2b_shift = 0;
342 static int dasd_state_unfmt_to_basic(struct dasd_device *device)
344 device->state = DASD_STATE_BASIC;
349 * Make the device online and schedule the bottom half to start
350 * the requeueing of requests from the linux request queue to the
354 dasd_state_ready_to_online(struct dasd_device * device)
357 struct gendisk *disk;
358 struct disk_part_iter piter;
359 struct hd_struct *part;
361 if (device->discipline->ready_to_online) {
362 rc = device->discipline->ready_to_online(device);
366 device->state = DASD_STATE_ONLINE;
368 dasd_schedule_block_bh(device->block);
369 disk = device->block->bdev->bd_disk;
370 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
371 while ((part = disk_part_iter_next(&piter)))
372 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
373 disk_part_iter_exit(&piter);
379 * Stop the requeueing of requests again.
381 static int dasd_state_online_to_ready(struct dasd_device *device)
384 struct gendisk *disk;
385 struct disk_part_iter piter;
386 struct hd_struct *part;
388 if (device->discipline->online_to_ready) {
389 rc = device->discipline->online_to_ready(device);
393 device->state = DASD_STATE_READY;
395 disk = device->block->bdev->bd_disk;
396 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0);
397 while ((part = disk_part_iter_next(&piter)))
398 kobject_uevent(&part_to_dev(part)->kobj, KOBJ_CHANGE);
399 disk_part_iter_exit(&piter);
405 * Device startup state changes.
407 static int dasd_increase_state(struct dasd_device *device)
412 if (device->state == DASD_STATE_NEW &&
413 device->target >= DASD_STATE_KNOWN)
414 rc = dasd_state_new_to_known(device);
417 device->state == DASD_STATE_KNOWN &&
418 device->target >= DASD_STATE_BASIC)
419 rc = dasd_state_known_to_basic(device);
422 device->state == DASD_STATE_BASIC &&
423 device->target >= DASD_STATE_READY)
424 rc = dasd_state_basic_to_ready(device);
427 device->state == DASD_STATE_UNFMT &&
428 device->target > DASD_STATE_UNFMT)
432 device->state == DASD_STATE_READY &&
433 device->target >= DASD_STATE_ONLINE)
434 rc = dasd_state_ready_to_online(device);
440 * Device shutdown state changes.
442 static int dasd_decrease_state(struct dasd_device *device)
447 if (device->state == DASD_STATE_ONLINE &&
448 device->target <= DASD_STATE_READY)
449 rc = dasd_state_online_to_ready(device);
452 device->state == DASD_STATE_READY &&
453 device->target <= DASD_STATE_BASIC)
454 rc = dasd_state_ready_to_basic(device);
457 device->state == DASD_STATE_UNFMT &&
458 device->target <= DASD_STATE_BASIC)
459 rc = dasd_state_unfmt_to_basic(device);
462 device->state == DASD_STATE_BASIC &&
463 device->target <= DASD_STATE_KNOWN)
464 rc = dasd_state_basic_to_known(device);
467 device->state == DASD_STATE_KNOWN &&
468 device->target <= DASD_STATE_NEW)
469 rc = dasd_state_known_to_new(device);
475 * This is the main startup/shutdown routine.
477 static void dasd_change_state(struct dasd_device *device)
481 if (device->state == device->target)
482 /* Already where we want to go today... */
484 if (device->state < device->target)
485 rc = dasd_increase_state(device);
487 rc = dasd_decrease_state(device);
491 device->target = device->state;
493 if (device->state == device->target)
494 wake_up(&dasd_init_waitq);
496 /* let user-space know that the device status changed */
497 kobject_uevent(&device->cdev->dev.kobj, KOBJ_CHANGE);
501 * Kick starter for devices that did not complete the startup/shutdown
502 * procedure or were sleeping because of a pending state.
503 * dasd_kick_device will schedule a call do do_kick_device to the kernel
506 static void do_kick_device(struct work_struct *work)
508 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
509 mutex_lock(&device->state_mutex);
510 dasd_change_state(device);
511 mutex_unlock(&device->state_mutex);
512 dasd_schedule_device_bh(device);
513 dasd_put_device(device);
516 void dasd_kick_device(struct dasd_device *device)
518 dasd_get_device(device);
519 /* queue call to dasd_kick_device to the kernel event daemon. */
520 schedule_work(&device->kick_work);
524 * dasd_restore_device will schedule a call do do_restore_device to the kernel
527 static void do_restore_device(struct work_struct *work)
529 struct dasd_device *device = container_of(work, struct dasd_device,
531 device->cdev->drv->restore(device->cdev);
532 dasd_put_device(device);
535 void dasd_restore_device(struct dasd_device *device)
537 dasd_get_device(device);
538 /* queue call to dasd_restore_device to the kernel event daemon. */
539 schedule_work(&device->restore_device);
543 * Set the target state for a device and starts the state change.
545 void dasd_set_target_state(struct dasd_device *device, int target)
547 dasd_get_device(device);
548 mutex_lock(&device->state_mutex);
549 /* If we are in probeonly mode stop at DASD_STATE_READY. */
550 if (dasd_probeonly && target > DASD_STATE_READY)
551 target = DASD_STATE_READY;
552 if (device->target != target) {
553 if (device->state == target)
554 wake_up(&dasd_init_waitq);
555 device->target = target;
557 if (device->state != device->target)
558 dasd_change_state(device);
559 mutex_unlock(&device->state_mutex);
560 dasd_put_device(device);
564 * Enable devices with device numbers in [from..to].
566 static inline int _wait_for_device(struct dasd_device *device)
568 return (device->state == device->target);
571 void dasd_enable_device(struct dasd_device *device)
573 dasd_set_target_state(device, DASD_STATE_ONLINE);
574 if (device->state <= DASD_STATE_KNOWN)
575 /* No discipline for device found. */
576 dasd_set_target_state(device, DASD_STATE_NEW);
577 /* Now wait for the devices to come up. */
578 wait_event(dasd_init_waitq, _wait_for_device(device));
582 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
584 #ifdef CONFIG_DASD_PROFILE
586 struct dasd_profile_info_t dasd_global_profile;
587 unsigned int dasd_profile_level = DASD_PROFILE_OFF;
590 * Increments counter in global and local profiling structures.
592 #define dasd_profile_counter(value, counter, block) \
595 for (index = 0; index < 31 && value >> (2+index); index++); \
596 dasd_global_profile.counter[index]++; \
597 block->profile.counter[index]++; \
601 * Add profiling information for cqr before execution.
603 static void dasd_profile_start(struct dasd_block *block,
604 struct dasd_ccw_req *cqr,
608 unsigned int counter;
610 if (dasd_profile_level != DASD_PROFILE_ON)
613 /* count the length of the chanq for statistics */
615 list_for_each(l, &block->ccw_queue)
618 dasd_global_profile.dasd_io_nr_req[counter]++;
619 block->profile.dasd_io_nr_req[counter]++;
623 * Add profiling information for cqr after execution.
625 static void dasd_profile_end(struct dasd_block *block,
626 struct dasd_ccw_req *cqr,
629 long strtime, irqtime, endtime, tottime; /* in microseconds */
630 long tottimeps, sectors;
632 if (dasd_profile_level != DASD_PROFILE_ON)
635 sectors = blk_rq_sectors(req);
636 if (!cqr->buildclk || !cqr->startclk ||
637 !cqr->stopclk || !cqr->endclk ||
641 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
642 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
643 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
644 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
645 tottimeps = tottime / sectors;
647 if (!dasd_global_profile.dasd_io_reqs)
648 memset(&dasd_global_profile, 0,
649 sizeof(struct dasd_profile_info_t));
650 dasd_global_profile.dasd_io_reqs++;
651 dasd_global_profile.dasd_io_sects += sectors;
653 if (!block->profile.dasd_io_reqs)
654 memset(&block->profile, 0,
655 sizeof(struct dasd_profile_info_t));
656 block->profile.dasd_io_reqs++;
657 block->profile.dasd_io_sects += sectors;
659 dasd_profile_counter(sectors, dasd_io_secs, block);
660 dasd_profile_counter(tottime, dasd_io_times, block);
661 dasd_profile_counter(tottimeps, dasd_io_timps, block);
662 dasd_profile_counter(strtime, dasd_io_time1, block);
663 dasd_profile_counter(irqtime, dasd_io_time2, block);
664 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, block);
665 dasd_profile_counter(endtime, dasd_io_time3, block);
668 #define dasd_profile_start(block, cqr, req) do {} while (0)
669 #define dasd_profile_end(block, cqr, req) do {} while (0)
670 #endif /* CONFIG_DASD_PROFILE */
673 * Allocate memory for a channel program with 'cplength' channel
674 * command words and 'datasize' additional space. There are two
675 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
676 * memory and 2) dasd_smalloc_request uses the static ccw memory
677 * that gets allocated for each device.
679 struct dasd_ccw_req *dasd_kmalloc_request(int magic, int cplength,
681 struct dasd_device *device)
683 struct dasd_ccw_req *cqr;
686 BUG_ON(datasize > PAGE_SIZE ||
687 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
689 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
691 return ERR_PTR(-ENOMEM);
694 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
695 GFP_ATOMIC | GFP_DMA);
696 if (cqr->cpaddr == NULL) {
698 return ERR_PTR(-ENOMEM);
703 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
704 if (cqr->data == NULL) {
707 return ERR_PTR(-ENOMEM);
711 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
712 dasd_get_device(device);
716 struct dasd_ccw_req *dasd_smalloc_request(int magic, int cplength,
718 struct dasd_device *device)
721 struct dasd_ccw_req *cqr;
726 BUG_ON(datasize > PAGE_SIZE ||
727 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
729 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
731 size += cplength * sizeof(struct ccw1);
734 spin_lock_irqsave(&device->mem_lock, flags);
735 cqr = (struct dasd_ccw_req *)
736 dasd_alloc_chunk(&device->ccw_chunks, size);
737 spin_unlock_irqrestore(&device->mem_lock, flags);
739 return ERR_PTR(-ENOMEM);
740 memset(cqr, 0, sizeof(struct dasd_ccw_req));
741 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
744 cqr->cpaddr = (struct ccw1 *) data;
745 data += cplength*sizeof(struct ccw1);
746 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
751 memset(cqr->data, 0, datasize);
754 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
755 dasd_get_device(device);
760 * Free memory of a channel program. This function needs to free all the
761 * idal lists that might have been created by dasd_set_cda and the
762 * struct dasd_ccw_req itself.
764 void dasd_kfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
769 /* Clear any idals used for the request. */
772 clear_normalized_cda(ccw);
773 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
778 dasd_put_device(device);
781 void dasd_sfree_request(struct dasd_ccw_req *cqr, struct dasd_device *device)
785 spin_lock_irqsave(&device->mem_lock, flags);
786 dasd_free_chunk(&device->ccw_chunks, cqr);
787 spin_unlock_irqrestore(&device->mem_lock, flags);
788 dasd_put_device(device);
792 * Check discipline magic in cqr.
794 static inline int dasd_check_cqr(struct dasd_ccw_req *cqr)
796 struct dasd_device *device;
800 device = cqr->startdev;
801 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
802 DBF_DEV_EVENT(DBF_WARNING, device,
803 " dasd_ccw_req 0x%08x magic doesn't match"
804 " discipline 0x%08x",
806 *(unsigned int *) device->discipline->name);
813 * Terminate the current i/o and set the request to clear_pending.
814 * Timer keeps device runnig.
815 * ccw_device_clear can fail if the i/o subsystem
818 int dasd_term_IO(struct dasd_ccw_req *cqr)
820 struct dasd_device *device;
822 char errorstring[ERRORLENGTH];
825 rc = dasd_check_cqr(cqr);
829 device = (struct dasd_device *) cqr->startdev;
830 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
831 rc = ccw_device_clear(device->cdev, (long) cqr);
833 case 0: /* termination successful */
835 cqr->status = DASD_CQR_CLEAR_PENDING;
836 cqr->stopclk = get_clock();
838 DBF_DEV_EVENT(DBF_DEBUG, device,
839 "terminate cqr %p successful",
843 DBF_DEV_EVENT(DBF_ERR, device, "%s",
844 "device gone, retry");
847 DBF_DEV_EVENT(DBF_ERR, device, "%s",
852 DBF_DEV_EVENT(DBF_ERR, device, "%s",
853 "device busy, retry later");
856 /* internal error 10 - unknown rc*/
857 snprintf(errorstring, ERRORLENGTH, "10 %d", rc);
858 dev_err(&device->cdev->dev, "An error occurred in the "
859 "DASD device driver, reason=%s\n", errorstring);
865 dasd_schedule_device_bh(device);
870 * Start the i/o. This start_IO can fail if the channel is really busy.
871 * In that case set up a timer to start the request later.
873 int dasd_start_IO(struct dasd_ccw_req *cqr)
875 struct dasd_device *device;
877 char errorstring[ERRORLENGTH];
880 rc = dasd_check_cqr(cqr);
885 device = (struct dasd_device *) cqr->startdev;
886 if (cqr->retries < 0) {
887 /* internal error 14 - start_IO run out of retries */
888 sprintf(errorstring, "14 %p", cqr);
889 dev_err(&device->cdev->dev, "An error occurred in the DASD "
890 "device driver, reason=%s\n", errorstring);
891 cqr->status = DASD_CQR_ERROR;
894 cqr->startclk = get_clock();
895 cqr->starttime = jiffies;
897 if (cqr->cpmode == 1) {
898 rc = ccw_device_tm_start(device->cdev, cqr->cpaddr,
899 (long) cqr, cqr->lpm);
901 rc = ccw_device_start(device->cdev, cqr->cpaddr,
902 (long) cqr, cqr->lpm, 0);
906 cqr->status = DASD_CQR_IN_IO;
909 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
910 "start_IO: device busy, retry later");
913 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
914 "start_IO: request timeout, retry later");
917 /* -EACCES indicates that the request used only a
918 * subset of the available pathes and all these
920 * Do a retry with all available pathes.
922 cqr->lpm = LPM_ANYPATH;
923 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
924 "start_IO: selected pathes gone,"
925 " retry on all pathes");
928 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
929 "start_IO: -ENODEV device gone, retry");
932 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
933 "start_IO: -EIO device gone, retry");
936 /* most likely caused in power management context */
937 DBF_DEV_EVENT(DBF_DEBUG, device, "%s",
938 "start_IO: -EINVAL device currently "
942 /* internal error 11 - unknown rc */
943 snprintf(errorstring, ERRORLENGTH, "11 %d", rc);
944 dev_err(&device->cdev->dev,
945 "An error occurred in the DASD device driver, "
946 "reason=%s\n", errorstring);
955 * Timeout function for dasd devices. This is used for different purposes
956 * 1) missing interrupt handler for normal operation
957 * 2) delayed start of request where start_IO failed with -EBUSY
958 * 3) timeout for missing state change interrupts
959 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
960 * DASD_CQR_QUEUED for 2) and 3).
962 static void dasd_device_timeout(unsigned long ptr)
965 struct dasd_device *device;
967 device = (struct dasd_device *) ptr;
968 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
969 /* re-activate request queue */
970 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
971 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
972 dasd_schedule_device_bh(device);
976 * Setup timeout for a device in jiffies.
978 void dasd_device_set_timer(struct dasd_device *device, int expires)
981 del_timer(&device->timer);
983 mod_timer(&device->timer, jiffies + expires);
987 * Clear timeout for a device.
989 void dasd_device_clear_timer(struct dasd_device *device)
991 del_timer(&device->timer);
994 static void dasd_handle_killed_request(struct ccw_device *cdev,
995 unsigned long intparm)
997 struct dasd_ccw_req *cqr;
998 struct dasd_device *device;
1002 cqr = (struct dasd_ccw_req *) intparm;
1003 if (cqr->status != DASD_CQR_IN_IO) {
1004 DBF_EVENT_DEVID(DBF_DEBUG, cdev,
1005 "invalid status in handle_killed_request: "
1006 "%02x", cqr->status);
1010 device = dasd_device_from_cdev_locked(cdev);
1011 if (IS_ERR(device)) {
1012 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1013 "unable to get device from cdev");
1017 if (!cqr->startdev ||
1018 device != cqr->startdev ||
1019 strncmp(cqr->startdev->discipline->ebcname,
1020 (char *) &cqr->magic, 4)) {
1021 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1022 "invalid device in request");
1023 dasd_put_device(device);
1027 /* Schedule request to be retried. */
1028 cqr->status = DASD_CQR_QUEUED;
1030 dasd_device_clear_timer(device);
1031 dasd_schedule_device_bh(device);
1032 dasd_put_device(device);
1035 void dasd_generic_handle_state_change(struct dasd_device *device)
1037 /* First of all start sense subsystem status request. */
1038 dasd_eer_snss(device);
1040 dasd_device_remove_stop_bits(device, DASD_STOPPED_PENDING);
1041 dasd_schedule_device_bh(device);
1043 dasd_schedule_block_bh(device->block);
1047 * Interrupt handler for "normal" ssch-io based dasd devices.
1049 void dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
1052 struct dasd_ccw_req *cqr, *next;
1053 struct dasd_device *device;
1054 unsigned long long now;
1058 switch (PTR_ERR(irb)) {
1062 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1063 "request timed out\n", __func__);
1066 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s: "
1067 "unknown error %ld\n", __func__,
1070 dasd_handle_killed_request(cdev, intparm);
1076 /* check for unsolicited interrupts */
1077 cqr = (struct dasd_ccw_req *) intparm;
1078 if (!cqr || ((scsw_cc(&irb->scsw) == 1) &&
1079 (scsw_fctl(&irb->scsw) & SCSW_FCTL_START_FUNC) &&
1080 (scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))) {
1081 if (cqr && cqr->status == DASD_CQR_IN_IO)
1082 cqr->status = DASD_CQR_QUEUED;
1083 device = dasd_device_from_cdev_locked(cdev);
1084 if (!IS_ERR(device)) {
1085 dasd_device_clear_timer(device);
1086 device->discipline->handle_unsolicited_interrupt(device,
1088 dasd_put_device(device);
1093 device = (struct dasd_device *) cqr->startdev;
1095 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
1096 DBF_EVENT_DEVID(DBF_DEBUG, cdev, "%s",
1097 "invalid device in request");
1101 /* Check for clear pending */
1102 if (cqr->status == DASD_CQR_CLEAR_PENDING &&
1103 scsw_fctl(&irb->scsw) & SCSW_FCTL_CLEAR_FUNC) {
1104 cqr->status = DASD_CQR_CLEARED;
1105 dasd_device_clear_timer(device);
1106 wake_up(&dasd_flush_wq);
1107 dasd_schedule_device_bh(device);
1111 /* check status - the request might have been killed by dyn detach */
1112 if (cqr->status != DASD_CQR_IN_IO) {
1113 DBF_DEV_EVENT(DBF_DEBUG, device, "invalid status: bus_id %s, "
1114 "status %02x", dev_name(&cdev->dev), cqr->status);
1120 if (scsw_dstat(&irb->scsw) == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1121 scsw_cstat(&irb->scsw) == 0) {
1122 /* request was completed successfully */
1123 cqr->status = DASD_CQR_SUCCESS;
1125 /* Start first request on queue if possible -> fast_io. */
1126 if (cqr->devlist.next != &device->ccw_queue) {
1127 next = list_entry(cqr->devlist.next,
1128 struct dasd_ccw_req, devlist);
1130 } else { /* error */
1131 memcpy(&cqr->irb, irb, sizeof(struct irb));
1132 /* log sense for every failed I/O to s390 debugfeature */
1133 dasd_log_sense_dbf(cqr, irb);
1134 if (device->features & DASD_FEATURE_ERPLOG) {
1135 dasd_log_sense(cqr, irb);
1139 * If we don't want complex ERP for this request, then just
1140 * reset this and retry it in the fastpath
1142 if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags) &&
1144 if (cqr->lpm == LPM_ANYPATH)
1145 DBF_DEV_EVENT(DBF_DEBUG, device,
1146 "default ERP in fastpath "
1147 "(%i retries left)",
1149 cqr->lpm = LPM_ANYPATH;
1150 cqr->status = DASD_CQR_QUEUED;
1153 cqr->status = DASD_CQR_ERROR;
1155 if (next && (next->status == DASD_CQR_QUEUED) &&
1156 (!device->stopped)) {
1157 if (device->discipline->start_IO(next) == 0)
1158 expires = next->expires;
1161 dasd_device_set_timer(device, expires);
1163 dasd_device_clear_timer(device);
1164 dasd_schedule_device_bh(device);
1168 * If we have an error on a dasd_block layer request then we cancel
1169 * and return all further requests from the same dasd_block as well.
1171 static void __dasd_device_recovery(struct dasd_device *device,
1172 struct dasd_ccw_req *ref_cqr)
1174 struct list_head *l, *n;
1175 struct dasd_ccw_req *cqr;
1178 * only requeue request that came from the dasd_block layer
1180 if (!ref_cqr->block)
1183 list_for_each_safe(l, n, &device->ccw_queue) {
1184 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1185 if (cqr->status == DASD_CQR_QUEUED &&
1186 ref_cqr->block == cqr->block) {
1187 cqr->status = DASD_CQR_CLEARED;
1193 * Remove those ccw requests from the queue that need to be returned
1194 * to the upper layer.
1196 static void __dasd_device_process_ccw_queue(struct dasd_device *device,
1197 struct list_head *final_queue)
1199 struct list_head *l, *n;
1200 struct dasd_ccw_req *cqr;
1202 /* Process request with final status. */
1203 list_for_each_safe(l, n, &device->ccw_queue) {
1204 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1206 /* Stop list processing at the first non-final request. */
1207 if (cqr->status == DASD_CQR_QUEUED ||
1208 cqr->status == DASD_CQR_IN_IO ||
1209 cqr->status == DASD_CQR_CLEAR_PENDING)
1211 if (cqr->status == DASD_CQR_ERROR) {
1212 __dasd_device_recovery(device, cqr);
1214 /* Rechain finished requests to final queue */
1215 list_move_tail(&cqr->devlist, final_queue);
1220 * the cqrs from the final queue are returned to the upper layer
1221 * by setting a dasd_block state and calling the callback function
1223 static void __dasd_device_process_final_queue(struct dasd_device *device,
1224 struct list_head *final_queue)
1226 struct list_head *l, *n;
1227 struct dasd_ccw_req *cqr;
1228 struct dasd_block *block;
1229 void (*callback)(struct dasd_ccw_req *, void *data);
1230 void *callback_data;
1231 char errorstring[ERRORLENGTH];
1233 list_for_each_safe(l, n, final_queue) {
1234 cqr = list_entry(l, struct dasd_ccw_req, devlist);
1235 list_del_init(&cqr->devlist);
1237 callback = cqr->callback;
1238 callback_data = cqr->callback_data;
1240 spin_lock_bh(&block->queue_lock);
1241 switch (cqr->status) {
1242 case DASD_CQR_SUCCESS:
1243 cqr->status = DASD_CQR_DONE;
1245 case DASD_CQR_ERROR:
1246 cqr->status = DASD_CQR_NEED_ERP;
1248 case DASD_CQR_CLEARED:
1249 cqr->status = DASD_CQR_TERMINATED;
1252 /* internal error 12 - wrong cqr status*/
1253 snprintf(errorstring, ERRORLENGTH, "12 %p %x02", cqr, cqr->status);
1254 dev_err(&device->cdev->dev,
1255 "An error occurred in the DASD device driver, "
1256 "reason=%s\n", errorstring);
1259 if (cqr->callback != NULL)
1260 (callback)(cqr, callback_data);
1262 spin_unlock_bh(&block->queue_lock);
1267 * Take a look at the first request on the ccw queue and check
1268 * if it reached its expire time. If so, terminate the IO.
1270 static void __dasd_device_check_expire(struct dasd_device *device)
1272 struct dasd_ccw_req *cqr;
1274 if (list_empty(&device->ccw_queue))
1276 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1277 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1278 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1279 if (device->discipline->term_IO(cqr) != 0) {
1280 /* Hmpf, try again in 5 sec */
1281 dev_err(&device->cdev->dev,
1282 "cqr %p timed out (%is) but cannot be "
1283 "ended, retrying in 5 s\n",
1284 cqr, (cqr->expires/HZ));
1285 cqr->expires += 5*HZ;
1286 dasd_device_set_timer(device, 5*HZ);
1288 dev_err(&device->cdev->dev,
1289 "cqr %p timed out (%is), %i retries "
1290 "remaining\n", cqr, (cqr->expires/HZ),
1297 * Take a look at the first request on the ccw queue and check
1298 * if it needs to be started.
1300 static void __dasd_device_start_head(struct dasd_device *device)
1302 struct dasd_ccw_req *cqr;
1305 if (list_empty(&device->ccw_queue))
1307 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1308 if (cqr->status != DASD_CQR_QUEUED)
1310 /* when device is stopped, return request to previous layer */
1311 if (device->stopped) {
1312 cqr->status = DASD_CQR_CLEARED;
1313 dasd_schedule_device_bh(device);
1317 rc = device->discipline->start_IO(cqr);
1319 dasd_device_set_timer(device, cqr->expires);
1320 else if (rc == -EACCES) {
1321 dasd_schedule_device_bh(device);
1323 /* Hmpf, try again in 1/2 sec */
1324 dasd_device_set_timer(device, 50);
1328 * Go through all request on the dasd_device request queue,
1329 * terminate them on the cdev if necessary, and return them to the
1330 * submitting layer via callback.
1332 * Make sure that all 'submitting layers' still exist when
1333 * this function is called!. In other words, when 'device' is a base
1334 * device then all block layer requests must have been removed before
1335 * via dasd_flush_block_queue.
1337 int dasd_flush_device_queue(struct dasd_device *device)
1339 struct dasd_ccw_req *cqr, *n;
1341 struct list_head flush_queue;
1343 INIT_LIST_HEAD(&flush_queue);
1344 spin_lock_irq(get_ccwdev_lock(device->cdev));
1346 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
1347 /* Check status and move request to flush_queue */
1348 switch (cqr->status) {
1349 case DASD_CQR_IN_IO:
1350 rc = device->discipline->term_IO(cqr);
1352 /* unable to terminate requeust */
1353 dev_err(&device->cdev->dev,
1354 "Flushing the DASD request queue "
1355 "failed for request %p\n", cqr);
1356 /* stop flush processing */
1360 case DASD_CQR_QUEUED:
1361 cqr->stopclk = get_clock();
1362 cqr->status = DASD_CQR_CLEARED;
1364 default: /* no need to modify the others */
1367 list_move_tail(&cqr->devlist, &flush_queue);
1370 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1372 * After this point all requests must be in state CLEAR_PENDING,
1373 * CLEARED, SUCCESS or ERROR. Now wait for CLEAR_PENDING to become
1374 * one of the others.
1376 list_for_each_entry_safe(cqr, n, &flush_queue, devlist)
1377 wait_event(dasd_flush_wq,
1378 (cqr->status != DASD_CQR_CLEAR_PENDING));
1380 * Now set each request back to TERMINATED, DONE or NEED_ERP
1381 * and call the callback function of flushed requests
1383 __dasd_device_process_final_queue(device, &flush_queue);
1388 * Acquire the device lock and process queues for the device.
1390 static void dasd_device_tasklet(struct dasd_device *device)
1392 struct list_head final_queue;
1394 atomic_set (&device->tasklet_scheduled, 0);
1395 INIT_LIST_HEAD(&final_queue);
1396 spin_lock_irq(get_ccwdev_lock(device->cdev));
1397 /* Check expire time of first request on the ccw queue. */
1398 __dasd_device_check_expire(device);
1399 /* find final requests on ccw queue */
1400 __dasd_device_process_ccw_queue(device, &final_queue);
1401 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1402 /* Now call the callback function of requests with final status */
1403 __dasd_device_process_final_queue(device, &final_queue);
1404 spin_lock_irq(get_ccwdev_lock(device->cdev));
1405 /* Now check if the head of the ccw queue needs to be started. */
1406 __dasd_device_start_head(device);
1407 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1408 dasd_put_device(device);
1412 * Schedules a call to dasd_tasklet over the device tasklet.
1414 void dasd_schedule_device_bh(struct dasd_device *device)
1416 /* Protect against rescheduling. */
1417 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
1419 dasd_get_device(device);
1420 tasklet_hi_schedule(&device->tasklet);
1423 void dasd_device_set_stop_bits(struct dasd_device *device, int bits)
1425 device->stopped |= bits;
1427 EXPORT_SYMBOL_GPL(dasd_device_set_stop_bits);
1429 void dasd_device_remove_stop_bits(struct dasd_device *device, int bits)
1431 device->stopped &= ~bits;
1432 if (!device->stopped)
1433 wake_up(&generic_waitq);
1435 EXPORT_SYMBOL_GPL(dasd_device_remove_stop_bits);
1438 * Queue a request to the head of the device ccw_queue.
1439 * Start the I/O if possible.
1441 void dasd_add_request_head(struct dasd_ccw_req *cqr)
1443 struct dasd_device *device;
1444 unsigned long flags;
1446 device = cqr->startdev;
1447 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1448 cqr->status = DASD_CQR_QUEUED;
1449 list_add(&cqr->devlist, &device->ccw_queue);
1450 /* let the bh start the request to keep them in order */
1451 dasd_schedule_device_bh(device);
1452 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1456 * Queue a request to the tail of the device ccw_queue.
1457 * Start the I/O if possible.
1459 void dasd_add_request_tail(struct dasd_ccw_req *cqr)
1461 struct dasd_device *device;
1462 unsigned long flags;
1464 device = cqr->startdev;
1465 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1466 cqr->status = DASD_CQR_QUEUED;
1467 list_add_tail(&cqr->devlist, &device->ccw_queue);
1468 /* let the bh start the request to keep them in order */
1469 dasd_schedule_device_bh(device);
1470 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1474 * Wakeup helper for the 'sleep_on' functions.
1476 static void dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1478 spin_lock_irq(get_ccwdev_lock(cqr->startdev->cdev));
1479 cqr->callback_data = DASD_SLEEPON_END_TAG;
1480 spin_unlock_irq(get_ccwdev_lock(cqr->startdev->cdev));
1481 wake_up(&generic_waitq);
1484 static inline int _wait_for_wakeup(struct dasd_ccw_req *cqr)
1486 struct dasd_device *device;
1489 device = cqr->startdev;
1490 spin_lock_irq(get_ccwdev_lock(device->cdev));
1491 rc = (cqr->callback_data == DASD_SLEEPON_END_TAG);
1492 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1497 * checks if error recovery is necessary, returns 1 if yes, 0 otherwise.
1499 static int __dasd_sleep_on_erp(struct dasd_ccw_req *cqr)
1501 struct dasd_device *device;
1502 dasd_erp_fn_t erp_fn;
1504 if (cqr->status == DASD_CQR_FILLED)
1506 device = cqr->startdev;
1507 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1508 if (cqr->status == DASD_CQR_TERMINATED) {
1509 device->discipline->handle_terminated_request(cqr);
1512 if (cqr->status == DASD_CQR_NEED_ERP) {
1513 erp_fn = device->discipline->erp_action(cqr);
1517 if (cqr->status == DASD_CQR_FAILED)
1518 dasd_log_sense(cqr, &cqr->irb);
1520 __dasd_process_erp(device, cqr);
1527 static int __dasd_sleep_on_loop_condition(struct dasd_ccw_req *cqr)
1529 if (test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags)) {
1530 if (cqr->refers) /* erp is not done yet */
1532 return ((cqr->status != DASD_CQR_DONE) &&
1533 (cqr->status != DASD_CQR_FAILED));
1535 return (cqr->status == DASD_CQR_FILLED);
1538 static int _dasd_sleep_on(struct dasd_ccw_req *maincqr, int interruptible)
1540 struct dasd_device *device;
1542 struct list_head ccw_queue;
1543 struct dasd_ccw_req *cqr;
1545 INIT_LIST_HEAD(&ccw_queue);
1546 maincqr->status = DASD_CQR_FILLED;
1547 device = maincqr->startdev;
1548 list_add(&maincqr->blocklist, &ccw_queue);
1549 for (cqr = maincqr; __dasd_sleep_on_loop_condition(cqr);
1550 cqr = list_first_entry(&ccw_queue,
1551 struct dasd_ccw_req, blocklist)) {
1553 if (__dasd_sleep_on_erp(cqr))
1555 if (cqr->status != DASD_CQR_FILLED) /* could be failed */
1558 /* Non-temporary stop condition will trigger fail fast */
1559 if (device->stopped & ~DASD_STOPPED_PENDING &&
1560 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1561 (!dasd_eer_enabled(device))) {
1562 cqr->status = DASD_CQR_FAILED;
1566 /* Don't try to start requests if device is stopped */
1567 if (interruptible) {
1568 rc = wait_event_interruptible(
1569 generic_waitq, !(device->stopped));
1570 if (rc == -ERESTARTSYS) {
1571 cqr->status = DASD_CQR_FAILED;
1572 maincqr->intrc = rc;
1576 wait_event(generic_waitq, !(device->stopped));
1578 cqr->callback = dasd_wakeup_cb;
1579 cqr->callback_data = DASD_SLEEPON_START_TAG;
1580 dasd_add_request_tail(cqr);
1581 if (interruptible) {
1582 rc = wait_event_interruptible(
1583 generic_waitq, _wait_for_wakeup(cqr));
1584 if (rc == -ERESTARTSYS) {
1585 dasd_cancel_req(cqr);
1586 /* wait (non-interruptible) for final status */
1587 wait_event(generic_waitq,
1588 _wait_for_wakeup(cqr));
1589 cqr->status = DASD_CQR_FAILED;
1590 maincqr->intrc = rc;
1594 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1597 maincqr->endclk = get_clock();
1598 if ((maincqr->status != DASD_CQR_DONE) &&
1599 (maincqr->intrc != -ERESTARTSYS))
1600 dasd_log_sense(maincqr, &maincqr->irb);
1601 if (maincqr->status == DASD_CQR_DONE)
1603 else if (maincqr->intrc)
1604 rc = maincqr->intrc;
1611 * Queue a request to the tail of the device ccw_queue and wait for
1614 int dasd_sleep_on(struct dasd_ccw_req *cqr)
1616 return _dasd_sleep_on(cqr, 0);
1620 * Queue a request to the tail of the device ccw_queue and wait
1621 * interruptible for it's completion.
1623 int dasd_sleep_on_interruptible(struct dasd_ccw_req *cqr)
1625 return _dasd_sleep_on(cqr, 1);
1629 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1630 * for eckd devices) the currently running request has to be terminated
1631 * and be put back to status queued, before the special request is added
1632 * to the head of the queue. Then the special request is waited on normally.
1634 static inline int _dasd_term_running_cqr(struct dasd_device *device)
1636 struct dasd_ccw_req *cqr;
1638 if (list_empty(&device->ccw_queue))
1640 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, devlist);
1641 return device->discipline->term_IO(cqr);
1644 int dasd_sleep_on_immediatly(struct dasd_ccw_req *cqr)
1646 struct dasd_device *device;
1649 device = cqr->startdev;
1650 spin_lock_irq(get_ccwdev_lock(device->cdev));
1651 rc = _dasd_term_running_cqr(device);
1653 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1657 cqr->callback = dasd_wakeup_cb;
1658 cqr->callback_data = DASD_SLEEPON_START_TAG;
1659 cqr->status = DASD_CQR_QUEUED;
1660 list_add(&cqr->devlist, &device->ccw_queue);
1662 /* let the bh start the request to keep them in order */
1663 dasd_schedule_device_bh(device);
1665 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1667 wait_event(generic_waitq, _wait_for_wakeup(cqr));
1669 if (cqr->status == DASD_CQR_DONE)
1671 else if (cqr->intrc)
1679 * Cancels a request that was started with dasd_sleep_on_req.
1680 * This is useful to timeout requests. The request will be
1681 * terminated if it is currently in i/o.
1682 * Returns 1 if the request has been terminated.
1683 * 0 if there was no need to terminate the request (not started yet)
1684 * negative error code if termination failed
1685 * Cancellation of a request is an asynchronous operation! The calling
1686 * function has to wait until the request is properly returned via callback.
1688 int dasd_cancel_req(struct dasd_ccw_req *cqr)
1690 struct dasd_device *device = cqr->startdev;
1691 unsigned long flags;
1695 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1696 switch (cqr->status) {
1697 case DASD_CQR_QUEUED:
1698 /* request was not started - just set to cleared */
1699 cqr->status = DASD_CQR_CLEARED;
1701 case DASD_CQR_IN_IO:
1702 /* request in IO - terminate IO and release again */
1703 rc = device->discipline->term_IO(cqr);
1705 dev_err(&device->cdev->dev,
1706 "Cancelling request %p failed with rc=%d\n",
1709 cqr->stopclk = get_clock();
1712 default: /* already finished or clear pending - do nothing */
1715 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1716 dasd_schedule_device_bh(device);
1722 * SECTION: Operations of the dasd_block layer.
1726 * Timeout function for dasd_block. This is used when the block layer
1727 * is waiting for something that may not come reliably, (e.g. a state
1730 static void dasd_block_timeout(unsigned long ptr)
1732 unsigned long flags;
1733 struct dasd_block *block;
1735 block = (struct dasd_block *) ptr;
1736 spin_lock_irqsave(get_ccwdev_lock(block->base->cdev), flags);
1737 /* re-activate request queue */
1738 dasd_device_remove_stop_bits(block->base, DASD_STOPPED_PENDING);
1739 spin_unlock_irqrestore(get_ccwdev_lock(block->base->cdev), flags);
1740 dasd_schedule_block_bh(block);
1744 * Setup timeout for a dasd_block in jiffies.
1746 void dasd_block_set_timer(struct dasd_block *block, int expires)
1749 del_timer(&block->timer);
1751 mod_timer(&block->timer, jiffies + expires);
1755 * Clear timeout for a dasd_block.
1757 void dasd_block_clear_timer(struct dasd_block *block)
1759 del_timer(&block->timer);
1763 * Process finished error recovery ccw.
1765 static void __dasd_process_erp(struct dasd_device *device,
1766 struct dasd_ccw_req *cqr)
1768 dasd_erp_fn_t erp_fn;
1770 if (cqr->status == DASD_CQR_DONE)
1771 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1773 dev_err(&device->cdev->dev, "ERP failed for the DASD\n");
1774 erp_fn = device->discipline->erp_postaction(cqr);
1779 * Fetch requests from the block device queue.
1781 static void __dasd_process_request_queue(struct dasd_block *block)
1783 struct request_queue *queue;
1784 struct request *req;
1785 struct dasd_ccw_req *cqr;
1786 struct dasd_device *basedev;
1787 unsigned long flags;
1788 queue = block->request_queue;
1789 basedev = block->base;
1790 /* No queue ? Then there is nothing to do. */
1795 * We requeue request from the block device queue to the ccw
1796 * queue only in two states. In state DASD_STATE_READY the
1797 * partition detection is done and we need to requeue requests
1798 * for that. State DASD_STATE_ONLINE is normal block device
1801 if (basedev->state < DASD_STATE_READY) {
1802 while ((req = blk_fetch_request(block->request_queue)))
1803 __blk_end_request_all(req, -EIO);
1806 /* Now we try to fetch requests from the request queue */
1807 while (!blk_queue_plugged(queue) && (req = blk_peek_request(queue))) {
1808 if (basedev->features & DASD_FEATURE_READONLY &&
1809 rq_data_dir(req) == WRITE) {
1810 DBF_DEV_EVENT(DBF_ERR, basedev,
1811 "Rejecting write request %p",
1813 blk_start_request(req);
1814 __blk_end_request_all(req, -EIO);
1817 cqr = basedev->discipline->build_cp(basedev, block, req);
1819 if (PTR_ERR(cqr) == -EBUSY)
1820 break; /* normal end condition */
1821 if (PTR_ERR(cqr) == -ENOMEM)
1822 break; /* terminate request queue loop */
1823 if (PTR_ERR(cqr) == -EAGAIN) {
1825 * The current request cannot be build right
1826 * now, we have to try later. If this request
1827 * is the head-of-queue we stop the device
1830 if (!list_empty(&block->ccw_queue))
1833 get_ccwdev_lock(basedev->cdev), flags);
1834 dasd_device_set_stop_bits(basedev,
1835 DASD_STOPPED_PENDING);
1836 spin_unlock_irqrestore(
1837 get_ccwdev_lock(basedev->cdev), flags);
1838 dasd_block_set_timer(block, HZ/2);
1841 DBF_DEV_EVENT(DBF_ERR, basedev,
1842 "CCW creation failed (rc=%ld) "
1845 blk_start_request(req);
1846 __blk_end_request_all(req, -EIO);
1850 * Note: callback is set to dasd_return_cqr_cb in
1851 * __dasd_block_start_head to cover erp requests as well
1853 cqr->callback_data = (void *) req;
1854 cqr->status = DASD_CQR_FILLED;
1855 blk_start_request(req);
1856 list_add_tail(&cqr->blocklist, &block->ccw_queue);
1857 dasd_profile_start(block, cqr, req);
1861 static void __dasd_cleanup_cqr(struct dasd_ccw_req *cqr)
1863 struct request *req;
1867 req = (struct request *) cqr->callback_data;
1868 dasd_profile_end(cqr->block, cqr, req);
1869 status = cqr->block->base->discipline->free_cp(cqr, req);
1871 error = status ? status : -EIO;
1872 __blk_end_request_all(req, error);
1876 * Process ccw request queue.
1878 static void __dasd_process_block_ccw_queue(struct dasd_block *block,
1879 struct list_head *final_queue)
1881 struct list_head *l, *n;
1882 struct dasd_ccw_req *cqr;
1883 dasd_erp_fn_t erp_fn;
1884 unsigned long flags;
1885 struct dasd_device *base = block->base;
1888 /* Process request with final status. */
1889 list_for_each_safe(l, n, &block->ccw_queue) {
1890 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
1891 if (cqr->status != DASD_CQR_DONE &&
1892 cqr->status != DASD_CQR_FAILED &&
1893 cqr->status != DASD_CQR_NEED_ERP &&
1894 cqr->status != DASD_CQR_TERMINATED)
1897 if (cqr->status == DASD_CQR_TERMINATED) {
1898 base->discipline->handle_terminated_request(cqr);
1902 /* Process requests that may be recovered */
1903 if (cqr->status == DASD_CQR_NEED_ERP) {
1904 erp_fn = base->discipline->erp_action(cqr);
1905 if (IS_ERR(erp_fn(cqr)))
1910 /* log sense for fatal error */
1911 if (cqr->status == DASD_CQR_FAILED) {
1912 dasd_log_sense(cqr, &cqr->irb);
1915 /* First of all call extended error reporting. */
1916 if (dasd_eer_enabled(base) &&
1917 cqr->status == DASD_CQR_FAILED) {
1918 dasd_eer_write(base, cqr, DASD_EER_FATALERROR);
1920 /* restart request */
1921 cqr->status = DASD_CQR_FILLED;
1923 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
1924 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
1925 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
1930 /* Process finished ERP request. */
1932 __dasd_process_erp(base, cqr);
1936 /* Rechain finished requests to final queue */
1937 cqr->endclk = get_clock();
1938 list_move_tail(&cqr->blocklist, final_queue);
1942 static void dasd_return_cqr_cb(struct dasd_ccw_req *cqr, void *data)
1944 dasd_schedule_block_bh(cqr->block);
1947 static void __dasd_block_start_head(struct dasd_block *block)
1949 struct dasd_ccw_req *cqr;
1951 if (list_empty(&block->ccw_queue))
1953 /* We allways begin with the first requests on the queue, as some
1954 * of previously started requests have to be enqueued on a
1955 * dasd_device again for error recovery.
1957 list_for_each_entry(cqr, &block->ccw_queue, blocklist) {
1958 if (cqr->status != DASD_CQR_FILLED)
1960 /* Non-temporary stop condition will trigger fail fast */
1961 if (block->base->stopped & ~DASD_STOPPED_PENDING &&
1962 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1963 (!dasd_eer_enabled(block->base))) {
1964 cqr->status = DASD_CQR_FAILED;
1965 dasd_schedule_block_bh(block);
1968 /* Don't try to start requests if device is stopped */
1969 if (block->base->stopped)
1972 /* just a fail safe check, should not happen */
1974 cqr->startdev = block->base;
1976 /* make sure that the requests we submit find their way back */
1977 cqr->callback = dasd_return_cqr_cb;
1979 dasd_add_request_tail(cqr);
1984 * Central dasd_block layer routine. Takes requests from the generic
1985 * block layer request queue, creates ccw requests, enqueues them on
1986 * a dasd_device and processes ccw requests that have been returned.
1988 static void dasd_block_tasklet(struct dasd_block *block)
1990 struct list_head final_queue;
1991 struct list_head *l, *n;
1992 struct dasd_ccw_req *cqr;
1994 atomic_set(&block->tasklet_scheduled, 0);
1995 INIT_LIST_HEAD(&final_queue);
1996 spin_lock(&block->queue_lock);
1997 /* Finish off requests on ccw queue */
1998 __dasd_process_block_ccw_queue(block, &final_queue);
1999 spin_unlock(&block->queue_lock);
2000 /* Now call the callback function of requests with final status */
2001 spin_lock_irq(&block->request_queue_lock);
2002 list_for_each_safe(l, n, &final_queue) {
2003 cqr = list_entry(l, struct dasd_ccw_req, blocklist);
2004 list_del_init(&cqr->blocklist);
2005 __dasd_cleanup_cqr(cqr);
2007 spin_lock(&block->queue_lock);
2008 /* Get new request from the block device request queue */
2009 __dasd_process_request_queue(block);
2010 /* Now check if the head of the ccw queue needs to be started. */
2011 __dasd_block_start_head(block);
2012 spin_unlock(&block->queue_lock);
2013 spin_unlock_irq(&block->request_queue_lock);
2014 dasd_put_device(block->base);
2017 static void _dasd_wake_block_flush_cb(struct dasd_ccw_req *cqr, void *data)
2019 wake_up(&dasd_flush_wq);
2023 * Go through all request on the dasd_block request queue, cancel them
2024 * on the respective dasd_device, and return them to the generic
2027 static int dasd_flush_block_queue(struct dasd_block *block)
2029 struct dasd_ccw_req *cqr, *n;
2031 struct list_head flush_queue;
2033 INIT_LIST_HEAD(&flush_queue);
2034 spin_lock_bh(&block->queue_lock);
2037 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
2038 /* if this request currently owned by a dasd_device cancel it */
2039 if (cqr->status >= DASD_CQR_QUEUED)
2040 rc = dasd_cancel_req(cqr);
2043 /* Rechain request (including erp chain) so it won't be
2044 * touched by the dasd_block_tasklet anymore.
2045 * Replace the callback so we notice when the request
2046 * is returned from the dasd_device layer.
2048 cqr->callback = _dasd_wake_block_flush_cb;
2049 for (i = 0; cqr != NULL; cqr = cqr->refers, i++)
2050 list_move_tail(&cqr->blocklist, &flush_queue);
2052 /* moved more than one request - need to restart */
2055 spin_unlock_bh(&block->queue_lock);
2056 /* Now call the callback function of flushed requests */
2058 list_for_each_entry_safe(cqr, n, &flush_queue, blocklist) {
2059 wait_event(dasd_flush_wq, (cqr->status < DASD_CQR_QUEUED));
2060 /* Process finished ERP request. */
2062 spin_lock_bh(&block->queue_lock);
2063 __dasd_process_erp(block->base, cqr);
2064 spin_unlock_bh(&block->queue_lock);
2065 /* restart list_for_xx loop since dasd_process_erp
2066 * might remove multiple elements */
2069 /* call the callback function */
2070 spin_lock_irq(&block->request_queue_lock);
2071 cqr->endclk = get_clock();
2072 list_del_init(&cqr->blocklist);
2073 __dasd_cleanup_cqr(cqr);
2074 spin_unlock_irq(&block->request_queue_lock);
2080 * Schedules a call to dasd_tasklet over the device tasklet.
2082 void dasd_schedule_block_bh(struct dasd_block *block)
2084 /* Protect against rescheduling. */
2085 if (atomic_cmpxchg(&block->tasklet_scheduled, 0, 1) != 0)
2087 /* life cycle of block is bound to it's base device */
2088 dasd_get_device(block->base);
2089 tasklet_hi_schedule(&block->tasklet);
2094 * SECTION: external block device operations
2095 * (request queue handling, open, release, etc.)
2099 * Dasd request queue function. Called from ll_rw_blk.c
2101 static void do_dasd_request(struct request_queue *queue)
2103 struct dasd_block *block;
2105 block = queue->queuedata;
2106 spin_lock(&block->queue_lock);
2107 /* Get new request from the block device request queue */
2108 __dasd_process_request_queue(block);
2109 /* Now check if the head of the ccw queue needs to be started. */
2110 __dasd_block_start_head(block);
2111 spin_unlock(&block->queue_lock);
2115 * Allocate and initialize request queue and default I/O scheduler.
2117 static int dasd_alloc_queue(struct dasd_block *block)
2121 block->request_queue = blk_init_queue(do_dasd_request,
2122 &block->request_queue_lock);
2123 if (block->request_queue == NULL)
2126 block->request_queue->queuedata = block;
2128 elevator_exit(block->request_queue->elevator);
2129 block->request_queue->elevator = NULL;
2130 rc = elevator_init(block->request_queue, "deadline");
2132 blk_cleanup_queue(block->request_queue);
2139 * Allocate and initialize request queue.
2141 static void dasd_setup_queue(struct dasd_block *block)
2145 blk_queue_logical_block_size(block->request_queue, block->bp_block);
2146 max = block->base->discipline->max_blocks << block->s2b_shift;
2147 blk_queue_max_hw_sectors(block->request_queue, max);
2148 blk_queue_max_segments(block->request_queue, -1L);
2149 /* with page sized segments we can translate each segement into
2152 blk_queue_max_segment_size(block->request_queue, PAGE_SIZE);
2153 blk_queue_segment_boundary(block->request_queue, PAGE_SIZE - 1);
2154 blk_queue_ordered(block->request_queue, QUEUE_ORDERED_DRAIN, NULL);
2158 * Deactivate and free request queue.
2160 static void dasd_free_queue(struct dasd_block *block)
2162 if (block->request_queue) {
2163 blk_cleanup_queue(block->request_queue);
2164 block->request_queue = NULL;
2169 * Flush request on the request queue.
2171 static void dasd_flush_request_queue(struct dasd_block *block)
2173 struct request *req;
2175 if (!block->request_queue)
2178 spin_lock_irq(&block->request_queue_lock);
2179 while ((req = blk_fetch_request(block->request_queue)))
2180 __blk_end_request_all(req, -EIO);
2181 spin_unlock_irq(&block->request_queue_lock);
2184 static int dasd_open(struct block_device *bdev, fmode_t mode)
2186 struct dasd_block *block = bdev->bd_disk->private_data;
2187 struct dasd_device *base;
2194 atomic_inc(&block->open_count);
2195 if (test_bit(DASD_FLAG_OFFLINE, &base->flags)) {
2200 if (!try_module_get(base->discipline->owner)) {
2205 if (dasd_probeonly) {
2206 dev_info(&base->cdev->dev,
2207 "Accessing the DASD failed because it is in "
2208 "probeonly mode\n");
2213 if (base->state <= DASD_STATE_BASIC) {
2214 DBF_DEV_EVENT(DBF_ERR, base, " %s",
2215 " Cannot open unrecognized device");
2220 if ((mode & FMODE_WRITE) &&
2221 (test_bit(DASD_FLAG_DEVICE_RO, &base->flags) ||
2222 (base->features & DASD_FEATURE_READONLY))) {
2230 module_put(base->discipline->owner);
2232 atomic_dec(&block->open_count);
2236 static int dasd_release(struct gendisk *disk, fmode_t mode)
2238 struct dasd_block *block = disk->private_data;
2240 atomic_dec(&block->open_count);
2241 module_put(block->base->discipline->owner);
2246 * Return disk geometry.
2248 static int dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
2250 struct dasd_block *block;
2251 struct dasd_device *base;
2253 block = bdev->bd_disk->private_data;
2258 if (!base->discipline ||
2259 !base->discipline->fill_geometry)
2262 base->discipline->fill_geometry(block, geo);
2263 geo->start = get_start_sect(bdev) >> block->s2b_shift;
2267 const struct block_device_operations
2268 dasd_device_operations = {
2269 .owner = THIS_MODULE,
2271 .release = dasd_release,
2272 .ioctl = dasd_ioctl,
2273 .compat_ioctl = dasd_ioctl,
2274 .getgeo = dasd_getgeo,
2277 /*******************************************************************************
2278 * end of block device operations
2284 #ifdef CONFIG_PROC_FS
2288 if (dasd_page_cache != NULL) {
2289 kmem_cache_destroy(dasd_page_cache);
2290 dasd_page_cache = NULL;
2292 dasd_gendisk_exit();
2294 if (dasd_debug_area != NULL) {
2295 debug_unregister(dasd_debug_area);
2296 dasd_debug_area = NULL;
2301 * SECTION: common functions for ccw_driver use
2305 * Is the device read-only?
2306 * Note that this function does not report the setting of the
2307 * readonly device attribute, but how it is configured in z/VM.
2309 int dasd_device_is_ro(struct dasd_device *device)
2311 struct ccw_dev_id dev_id;
2312 struct diag210 diag_data;
2317 ccw_device_get_id(device->cdev, &dev_id);
2318 memset(&diag_data, 0, sizeof(diag_data));
2319 diag_data.vrdcdvno = dev_id.devno;
2320 diag_data.vrdclen = sizeof(diag_data);
2321 rc = diag210(&diag_data);
2322 if (rc == 0 || rc == 2) {
2323 return diag_data.vrdcvfla & 0x80;
2325 DBF_EVENT(DBF_WARNING, "diag210 failed for dev=%04x with rc=%d",
2330 EXPORT_SYMBOL_GPL(dasd_device_is_ro);
2332 static void dasd_generic_auto_online(void *data, async_cookie_t cookie)
2334 struct ccw_device *cdev = data;
2337 ret = ccw_device_set_online(cdev);
2339 pr_warning("%s: Setting the DASD online failed with rc=%d\n",
2340 dev_name(&cdev->dev), ret);
2344 * Initial attempt at a probe function. this can be simplified once
2345 * the other detection code is gone.
2347 int dasd_generic_probe(struct ccw_device *cdev,
2348 struct dasd_discipline *discipline)
2352 ret = dasd_add_sysfs_files(cdev);
2354 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s",
2355 "dasd_generic_probe: could not add "
2359 cdev->handler = &dasd_int_handler;
2362 * Automatically online either all dasd devices (dasd_autodetect)
2363 * or all devices specified with dasd= parameters during
2366 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
2367 (dasd_autodetect && dasd_busid_known(dev_name(&cdev->dev)) != 0))
2368 async_schedule(dasd_generic_auto_online, cdev);
2373 * This will one day be called from a global not_oper handler.
2374 * It is also used by driver_unregister during module unload.
2376 void dasd_generic_remove(struct ccw_device *cdev)
2378 struct dasd_device *device;
2379 struct dasd_block *block;
2381 cdev->handler = NULL;
2383 dasd_remove_sysfs_files(cdev);
2384 device = dasd_device_from_cdev(cdev);
2387 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2388 /* Already doing offline processing */
2389 dasd_put_device(device);
2393 * This device is removed unconditionally. Set offline
2394 * flag to prevent dasd_open from opening it while it is
2395 * no quite down yet.
2397 dasd_set_target_state(device, DASD_STATE_NEW);
2398 /* dasd_delete_device destroys the device reference. */
2399 block = device->block;
2400 device->block = NULL;
2401 dasd_delete_device(device);
2403 * life cycle of block is bound to device, so delete it after
2404 * device was safely removed
2407 dasd_free_block(block);
2411 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
2412 * the device is detected for the first time and is supposed to be used
2413 * or the user has started activation through sysfs.
2415 int dasd_generic_set_online(struct ccw_device *cdev,
2416 struct dasd_discipline *base_discipline)
2418 struct dasd_discipline *discipline;
2419 struct dasd_device *device;
2422 /* first online clears initial online feature flag */
2423 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
2424 device = dasd_create_device(cdev);
2426 return PTR_ERR(device);
2428 discipline = base_discipline;
2429 if (device->features & DASD_FEATURE_USEDIAG) {
2430 if (!dasd_diag_discipline_pointer) {
2431 pr_warning("%s Setting the DASD online failed because "
2432 "of missing DIAG discipline\n",
2433 dev_name(&cdev->dev));
2434 dasd_delete_device(device);
2437 discipline = dasd_diag_discipline_pointer;
2439 if (!try_module_get(base_discipline->owner)) {
2440 dasd_delete_device(device);
2443 if (!try_module_get(discipline->owner)) {
2444 module_put(base_discipline->owner);
2445 dasd_delete_device(device);
2448 device->base_discipline = base_discipline;
2449 device->discipline = discipline;
2451 /* check_device will allocate block device if necessary */
2452 rc = discipline->check_device(device);
2454 pr_warning("%s Setting the DASD online with discipline %s "
2455 "failed with rc=%i\n",
2456 dev_name(&cdev->dev), discipline->name, rc);
2457 module_put(discipline->owner);
2458 module_put(base_discipline->owner);
2459 dasd_delete_device(device);
2463 dasd_set_target_state(device, DASD_STATE_ONLINE);
2464 if (device->state <= DASD_STATE_KNOWN) {
2465 pr_warning("%s Setting the DASD online failed because of a "
2466 "missing discipline\n", dev_name(&cdev->dev));
2468 dasd_set_target_state(device, DASD_STATE_NEW);
2470 dasd_free_block(device->block);
2471 dasd_delete_device(device);
2473 pr_debug("dasd_generic device %s found\n",
2474 dev_name(&cdev->dev));
2476 wait_event(dasd_init_waitq, _wait_for_device(device));
2478 dasd_put_device(device);
2482 int dasd_generic_set_offline(struct ccw_device *cdev)
2484 struct dasd_device *device;
2485 struct dasd_block *block;
2486 int max_count, open_count;
2488 device = dasd_device_from_cdev(cdev);
2490 return PTR_ERR(device);
2491 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2492 /* Already doing offline processing */
2493 dasd_put_device(device);
2497 * We must make sure that this device is currently not in use.
2498 * The open_count is increased for every opener, that includes
2499 * the blkdev_get in dasd_scan_partitions. We are only interested
2500 * in the other openers.
2502 if (device->block) {
2503 max_count = device->block->bdev ? 0 : -1;
2504 open_count = atomic_read(&device->block->open_count);
2505 if (open_count > max_count) {
2507 pr_warning("%s: The DASD cannot be set offline "
2508 "with open count %i\n",
2509 dev_name(&cdev->dev), open_count);
2511 pr_warning("%s: The DASD cannot be set offline "
2512 "while it is in use\n",
2513 dev_name(&cdev->dev));
2514 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2515 dasd_put_device(device);
2519 dasd_set_target_state(device, DASD_STATE_NEW);
2520 /* dasd_delete_device destroys the device reference. */
2521 block = device->block;
2522 device->block = NULL;
2523 dasd_delete_device(device);
2525 * life cycle of block is bound to device, so delete it after
2526 * device was safely removed
2529 dasd_free_block(block);
2533 int dasd_generic_notify(struct ccw_device *cdev, int event)
2535 struct dasd_device *device;
2536 struct dasd_ccw_req *cqr;
2539 device = dasd_device_from_cdev_locked(cdev);
2547 /* First of all call extended error reporting. */
2548 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2550 if (device->state < DASD_STATE_BASIC)
2552 /* Device is active. We want to keep it. */
2553 list_for_each_entry(cqr, &device->ccw_queue, devlist)
2554 if (cqr->status == DASD_CQR_IN_IO) {
2555 cqr->status = DASD_CQR_QUEUED;
2558 dasd_device_set_stop_bits(device, DASD_STOPPED_DC_WAIT);
2559 dasd_device_clear_timer(device);
2560 dasd_schedule_device_bh(device);
2564 /* FIXME: add a sanity check. */
2565 dasd_device_remove_stop_bits(device, DASD_STOPPED_DC_WAIT);
2566 if (device->stopped & DASD_UNRESUMED_PM) {
2567 dasd_device_remove_stop_bits(device, DASD_UNRESUMED_PM);
2568 dasd_restore_device(device);
2572 dasd_schedule_device_bh(device);
2574 dasd_schedule_block_bh(device->block);
2578 dasd_put_device(device);
2582 int dasd_generic_pm_freeze(struct ccw_device *cdev)
2584 struct dasd_ccw_req *cqr, *n;
2586 struct list_head freeze_queue;
2587 struct dasd_device *device = dasd_device_from_cdev(cdev);
2590 return PTR_ERR(device);
2591 /* disallow new I/O */
2592 dasd_device_set_stop_bits(device, DASD_STOPPED_PM);
2593 /* clear active requests */
2594 INIT_LIST_HEAD(&freeze_queue);
2595 spin_lock_irq(get_ccwdev_lock(cdev));
2597 list_for_each_entry_safe(cqr, n, &device->ccw_queue, devlist) {
2598 /* Check status and move request to flush_queue */
2599 if (cqr->status == DASD_CQR_IN_IO) {
2600 rc = device->discipline->term_IO(cqr);
2602 /* unable to terminate requeust */
2603 dev_err(&device->cdev->dev,
2604 "Unable to terminate request %p "
2605 "on suspend\n", cqr);
2606 spin_unlock_irq(get_ccwdev_lock(cdev));
2607 dasd_put_device(device);
2611 list_move_tail(&cqr->devlist, &freeze_queue);
2614 spin_unlock_irq(get_ccwdev_lock(cdev));
2616 list_for_each_entry_safe(cqr, n, &freeze_queue, devlist) {
2617 wait_event(dasd_flush_wq,
2618 (cqr->status != DASD_CQR_CLEAR_PENDING));
2619 if (cqr->status == DASD_CQR_CLEARED)
2620 cqr->status = DASD_CQR_QUEUED;
2622 /* move freeze_queue to start of the ccw_queue */
2623 spin_lock_irq(get_ccwdev_lock(cdev));
2624 list_splice_tail(&freeze_queue, &device->ccw_queue);
2625 spin_unlock_irq(get_ccwdev_lock(cdev));
2627 if (device->discipline->freeze)
2628 rc = device->discipline->freeze(device);
2630 dasd_put_device(device);
2633 EXPORT_SYMBOL_GPL(dasd_generic_pm_freeze);
2635 int dasd_generic_restore_device(struct ccw_device *cdev)
2637 struct dasd_device *device = dasd_device_from_cdev(cdev);
2641 return PTR_ERR(device);
2643 /* allow new IO again */
2644 dasd_device_remove_stop_bits(device,
2645 (DASD_STOPPED_PM | DASD_UNRESUMED_PM));
2647 dasd_schedule_device_bh(device);
2650 * call discipline restore function
2651 * if device is stopped do nothing e.g. for disconnected devices
2653 if (device->discipline->restore && !(device->stopped))
2654 rc = device->discipline->restore(device);
2655 if (rc || device->stopped)
2657 * if the resume failed for the DASD we put it in
2658 * an UNRESUMED stop state
2660 device->stopped |= DASD_UNRESUMED_PM;
2663 dasd_schedule_block_bh(device->block);
2665 dasd_put_device(device);
2668 EXPORT_SYMBOL_GPL(dasd_generic_restore_device);
2670 static struct dasd_ccw_req *dasd_generic_build_rdc(struct dasd_device *device,
2672 int rdc_buffer_size,
2675 struct dasd_ccw_req *cqr;
2677 unsigned long *idaw;
2679 cqr = dasd_smalloc_request(magic, 1 /* RDC */, rdc_buffer_size, device);
2682 /* internal error 13 - Allocating the RDC request failed*/
2683 dev_err(&device->cdev->dev,
2684 "An error occurred in the DASD device driver, "
2685 "reason=%s\n", "13");
2690 ccw->cmd_code = CCW_CMD_RDC;
2691 if (idal_is_needed(rdc_buffer, rdc_buffer_size)) {
2692 idaw = (unsigned long *) (cqr->data);
2693 ccw->cda = (__u32)(addr_t) idaw;
2694 ccw->flags = CCW_FLAG_IDA;
2695 idaw = idal_create_words(idaw, rdc_buffer, rdc_buffer_size);
2697 ccw->cda = (__u32)(addr_t) rdc_buffer;
2701 ccw->count = rdc_buffer_size;
2702 cqr->startdev = device;
2703 cqr->memdev = device;
2704 cqr->expires = 10*HZ;
2706 cqr->buildclk = get_clock();
2707 cqr->status = DASD_CQR_FILLED;
2712 int dasd_generic_read_dev_chars(struct dasd_device *device, int magic,
2713 void *rdc_buffer, int rdc_buffer_size)
2716 struct dasd_ccw_req *cqr;
2718 cqr = dasd_generic_build_rdc(device, rdc_buffer, rdc_buffer_size,
2721 return PTR_ERR(cqr);
2723 ret = dasd_sleep_on(cqr);
2724 dasd_sfree_request(cqr, cqr->memdev);
2727 EXPORT_SYMBOL_GPL(dasd_generic_read_dev_chars);
2730 * In command mode and transport mode we need to look for sense
2731 * data in different places. The sense data itself is allways
2732 * an array of 32 bytes, so we can unify the sense data access
2735 char *dasd_get_sense(struct irb *irb)
2737 struct tsb *tsb = NULL;
2740 if (scsw_is_tm(&irb->scsw) && (irb->scsw.tm.fcxs == 0x01)) {
2741 if (irb->scsw.tm.tcw)
2742 tsb = tcw_get_tsb((struct tcw *)(unsigned long)
2744 if (tsb && tsb->length == 64 && tsb->flags)
2745 switch (tsb->flags & 0x07) {
2746 case 1: /* tsa_iostat */
2747 sense = tsb->tsa.iostat.sense;
2749 case 2: /* tsa_ddpc */
2750 sense = tsb->tsa.ddpc.sense;
2753 /* currently we don't use interrogate data */
2756 } else if (irb->esw.esw0.erw.cons) {
2761 EXPORT_SYMBOL_GPL(dasd_get_sense);
2763 static int __init dasd_init(void)
2767 init_waitqueue_head(&dasd_init_waitq);
2768 init_waitqueue_head(&dasd_flush_wq);
2769 init_waitqueue_head(&generic_waitq);
2771 /* register 'common' DASD debug area, used for all DBF_XXX calls */
2772 dasd_debug_area = debug_register("dasd", 1, 1, 8 * sizeof(long));
2773 if (dasd_debug_area == NULL) {
2777 debug_register_view(dasd_debug_area, &debug_sprintf_view);
2778 debug_set_level(dasd_debug_area, DBF_WARNING);
2780 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2782 dasd_diag_discipline_pointer = NULL;
2784 rc = dasd_devmap_init();
2787 rc = dasd_gendisk_init();
2793 rc = dasd_eer_init();
2796 #ifdef CONFIG_PROC_FS
2797 rc = dasd_proc_init();
2804 pr_info("The DASD device driver could not be initialized\n");
2809 module_init(dasd_init);
2810 module_exit(dasd_exit);
2812 EXPORT_SYMBOL(dasd_debug_area);
2813 EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2815 EXPORT_SYMBOL(dasd_add_request_head);
2816 EXPORT_SYMBOL(dasd_add_request_tail);
2817 EXPORT_SYMBOL(dasd_cancel_req);
2818 EXPORT_SYMBOL(dasd_device_clear_timer);
2819 EXPORT_SYMBOL(dasd_block_clear_timer);
2820 EXPORT_SYMBOL(dasd_enable_device);
2821 EXPORT_SYMBOL(dasd_int_handler);
2822 EXPORT_SYMBOL(dasd_kfree_request);
2823 EXPORT_SYMBOL(dasd_kick_device);
2824 EXPORT_SYMBOL(dasd_kmalloc_request);
2825 EXPORT_SYMBOL(dasd_schedule_device_bh);
2826 EXPORT_SYMBOL(dasd_schedule_block_bh);
2827 EXPORT_SYMBOL(dasd_set_target_state);
2828 EXPORT_SYMBOL(dasd_device_set_timer);
2829 EXPORT_SYMBOL(dasd_block_set_timer);
2830 EXPORT_SYMBOL(dasd_sfree_request);
2831 EXPORT_SYMBOL(dasd_sleep_on);
2832 EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2833 EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2834 EXPORT_SYMBOL(dasd_smalloc_request);
2835 EXPORT_SYMBOL(dasd_start_IO);
2836 EXPORT_SYMBOL(dasd_term_IO);
2838 EXPORT_SYMBOL_GPL(dasd_generic_probe);
2839 EXPORT_SYMBOL_GPL(dasd_generic_remove);
2840 EXPORT_SYMBOL_GPL(dasd_generic_notify);
2841 EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2842 EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
2843 EXPORT_SYMBOL_GPL(dasd_generic_handle_state_change);
2844 EXPORT_SYMBOL_GPL(dasd_flush_device_queue);
2845 EXPORT_SYMBOL_GPL(dasd_alloc_block);
2846 EXPORT_SYMBOL_GPL(dasd_free_block);