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