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